diff options
author | pav <pav@FreeBSD.org> | 2008-04-11 08:26:06 +0000 |
---|---|---|
committer | pav <pav@FreeBSD.org> | 2008-04-11 08:26:06 +0000 |
commit | df43e8c984cf5b4b1ddd9908916a7ec8a2669a31 (patch) | |
tree | 10d3af807888e075cd1a6aec7853f38e50370ba8 /usr.sbin/pkg_install/lib | |
parent | 65e9886a96e340738e55f0fec0827590e3d4715c (diff) | |
download | FreeBSD-src-df43e8c984cf5b4b1ddd9908916a7ec8a2669a31.zip FreeBSD-src-df43e8c984cf5b4b1ddd9908916a7ec8a2669a31.tar.gz |
Optimize package registration/deregistration. Previously, when looking up the
package name for the origin of a dependency, all entries in /var/db/pkg were
traversed for each dependency of added/removed package. Now, gather all the
origins first, then do the lookup in a single pass over /var/db/pkg.
This should provide a major speedup for packages with hundreds of dependencies.
Submitted by: rdivacky (earlier version)
MFC after: 1 month
Diffstat (limited to 'usr.sbin/pkg_install/lib')
-rw-r--r-- | usr.sbin/pkg_install/lib/lib.h | 1 | ||||
-rw-r--r-- | usr.sbin/pkg_install/lib/match.c | 72 |
2 files changed, 56 insertions, 17 deletions
diff --git a/usr.sbin/pkg_install/lib/lib.h b/usr.sbin/pkg_install/lib/lib.h index cfa95ca..3e0ce7e 100644 --- a/usr.sbin/pkg_install/lib/lib.h +++ b/usr.sbin/pkg_install/lib/lib.h @@ -225,6 +225,7 @@ int real_main(int, char **); /* Query installed packages */ char **matchinstalled(match_t, char **, int *); char **matchbyorigin(const char *, int *); +char **matchallbyorigin(const char **, int *); int isinstalledpkg(const char *name); int pattern_match(match_t MatchType, char *pattern, const char *pkgname); diff --git a/usr.sbin/pkg_install/lib/match.c b/usr.sbin/pkg_install/lib/match.c index 1dba77c..885533f 100644 --- a/usr.sbin/pkg_install/lib/match.c +++ b/usr.sbin/pkg_install/lib/match.c @@ -238,18 +238,10 @@ pattern_match(match_t MatchType, char *pattern, const char *pkgname) * as a key for matching packages. */ char ** -matchbyorigin(const char *origin, int *retval) +matchallbyorigin(const char **origins, int *retval) { - char **installed; - int i; - static struct store *store = NULL; - - store = storecreate(store); - if (store == NULL) { - if (retval != NULL) - *retval = 1; - return NULL; - } + char **installed, **allorigins = NULL, **matches = NULL; + int i, j; if (retval != NULL) *retval = 0; @@ -258,11 +250,15 @@ matchbyorigin(const char *origin, int *retval) if (installed == NULL) return NULL; + /* Gather origins for all installed packages */ for (i = 0; installed[i] != NULL; i++) { FILE *fp; - char *cp, tmp[PATH_MAX]; + char *buf, *cp, tmp[PATH_MAX]; int cmd; + allorigins = realloc(allorigins, (i + 1) * sizeof(*allorigins)); + allorigins[i] = NULL; + snprintf(tmp, PATH_MAX, "%s/%s", LOG_DIR, installed[i]); /* * SPECIAL CASE: ignore empty dirs, since we can can see them @@ -290,8 +286,8 @@ matchbyorigin(const char *origin, int *retval) continue; cmd = plist_cmd(tmp + 1, &cp); if (cmd == PLIST_ORIGIN) { - if (csh_match(origin, cp, FNM_PATHNAME) == 0) - storeappend(store, installed[i]); + asprintf(&buf, "%s", cp); + allorigins[i] = buf; break; } } @@ -300,10 +296,52 @@ matchbyorigin(const char *origin, int *retval) fclose(fp); } - if (store->used == 0) + /* Resolve origins into package names, retaining the sequence */ + for (i = 0; origins[i] != NULL; i++) { + matches = realloc(matches, (i + 1) * sizeof(*matches)); + matches[i] = NULL; + + for (j = 0; installed[j] != NULL; j++) { + if (allorigins[j]) { + if (csh_match(origins[i], allorigins[j], FNM_PATHNAME) == 0) { + matches[i] = installed[j]; + break; + } + } + } + } + + if (allorigins) { + for (i = 0; installed[i] != NULL; i++) + if (allorigins[i]) + free(allorigins[i]); + free(allorigins); + } + + return matches; +} + +/* + * Synopsis is similar to matchinstalled(), but use origin + * as a key for matching packages. + */ +char ** +matchbyorigin(const char *origin, int *retval) +{ + const char *origins[2]; + char **tmp; + + origins[0] = origin; + origins[1] = NULL; + + tmp = matchallbyorigin(origins, retval); + if (tmp && tmp[0]) { + tmp = realloc(tmp, 2 * sizeof(*tmp)); + tmp[1] = NULL; + return tmp; + } else { return NULL; - else - return store->store; + } } /* |