xbps-query(8): performance improvement to the ownedby mode with --regex.

Only compile the ERE once, rather than on any file. Found and suggested
by Christian Neukirchen (@chneukirchen).
This commit is contained in:
Juan RP 2015-01-08 10:32:08 +01:00
parent 5d7a5a646d
commit cdf7fc81af
2 changed files with 17 additions and 9 deletions

3
NEWS
View File

@ -1,5 +1,8 @@
xbps-0.44 (???): xbps-0.44 (???):
* xbps-query(8): only compile the ERE (Extended Regular Expression) once in
the ownedby mode. A performance improvement suggested by Christian Neukirchen.
xbps-0.43.1 (2015-01-05): xbps-0.43.1 (2015-01-05):
* Fixed a regression in virtual packages defined via xbps.d(5) introduced in 0.43. * Fixed a regression in virtual packages defined via xbps.d(5) introduced in 0.43.

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2010-2014 Juan Romero Pardines. * Copyright (c) 2010-2015 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -37,8 +37,9 @@
#include "defs.h" #include "defs.h"
struct ffdata { struct ffdata {
bool regex; bool rematch;
const char *pat, *repouri; const char *pat, *repouri;
regex_t regex;
xbps_array_t allkeys; xbps_array_t allkeys;
xbps_dictionary_t filesd; xbps_dictionary_t filesd;
}; };
@ -52,7 +53,6 @@ match_files_by_pattern(xbps_dictionary_t pkg_filesd,
xbps_array_t array; xbps_array_t array;
xbps_object_t obj; xbps_object_t obj;
const char *keyname, *filestr, *typestr; const char *keyname, *filestr, *typestr;
regex_t regex;
keyname = xbps_dictionary_keysym_cstring_nocopy(key); keyname = xbps_dictionary_keysym_cstring_nocopy(key);
@ -72,13 +72,10 @@ match_files_by_pattern(xbps_dictionary_t pkg_filesd,
xbps_dictionary_get_cstring_nocopy(obj, "file", &filestr); xbps_dictionary_get_cstring_nocopy(obj, "file", &filestr);
if (filestr == NULL) if (filestr == NULL)
continue; continue;
if (ffd->regex) { if (ffd->rematch) {
if (regcomp(&regex, ffd->pat, REG_EXTENDED|REG_NOSUB) != 0) if (regexec(&ffd->regex, filestr, 0, 0, 0) == 0) {
return;
if (regexec(&regex, filestr, 0, 0, 0) == 0) {
printf("%s: %s (%s)\n", pkgver, filestr, typestr); printf("%s: %s (%s)\n", pkgver, filestr, typestr);
} }
regfree(&regex);
} else { } else {
if ((fnmatch(ffd->pat, filestr, FNM_PERIOD)) == 0) if ((fnmatch(ffd->pat, filestr, FNM_PERIOD)) == 0)
printf("%s: %s (%s)\n", pkgver, filestr, typestr); printf("%s: %s (%s)\n", pkgver, filestr, typestr);
@ -176,16 +173,24 @@ ownedby(struct xbps_handle *xhp, const char *pat, bool repo, bool regex)
char *rfile; char *rfile;
int rv; int rv;
ffd.regex = regex; ffd.rematch = false;
ffd.pat = pat; ffd.pat = pat;
if ((rfile = realpath(pat, NULL)) != NULL) if ((rfile = realpath(pat, NULL)) != NULL)
ffd.pat = rfile; ffd.pat = rfile;
if (regex) {
ffd.rematch = true;
if (regcomp(&ffd.regex, ffd.pat, REG_EXTENDED|REG_NOSUB) != 0)
return EINVAL;
}
if (repo) if (repo)
rv = xbps_rpool_foreach(xhp, repo_ownedby_cb, &ffd); rv = xbps_rpool_foreach(xhp, repo_ownedby_cb, &ffd);
else else
rv = xbps_pkgdb_foreach_cb(xhp, ownedby_pkgdb_cb, &ffd); rv = xbps_pkgdb_foreach_cb(xhp, ownedby_pkgdb_cb, &ffd);
if (regex)
regfree(&ffd.regex);
return rv; return rv;
} }