Date: Tue, 2 Nov 2010 03:25:50 GMT From: David Forsythe <dforsyth@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 185318 for review Message-ID: <201011020325.oA23Po8u002982@skunkworks.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@185318?ac=10 Change 185318 by dforsyth@skunk on 2010/11/02 03:25:39 Use FTS in the directorydb _all function. Affected files ... .. //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.c#9 edit Differences ... ==== //depot/projects/soc2010/dforsyth_libpkg/libpkg/freebsd_database_directorydb.c#9 (text+ko) ==== @@ -12,6 +12,7 @@ #include <sys/tree.h> #include <sys/types.h> #include <sys/stat.h> +#include <fts.h> #include <limits.h> #include <unistd.h> @@ -85,7 +86,6 @@ static char *read_file(const char *); static int _read_plist_cmp(struct _read_plist *, struct _read_plist *); -static int dselect(const struct dirent *); static void fbsd_directorydb_pkg_setup(struct pkg_db *, struct pkg *, const char *); @@ -135,12 +135,6 @@ return (strcmp(a->key, b->key)); } -static int -dselect(const struct dirent *ent) -{ - return (ent->d_name[0] != '.' && ent->d_type == DT_DIR); -} - /* Close a "connection" to a directorydb. */ int fbsd_directorydb_close(struct pkg_db *db) @@ -245,35 +239,52 @@ pkg->add_file = fbsd_directorydb_add_file; } +static int +ftsentcmp(const FTSENT *const *ent1, const FTSENT *const *ent2) +{ + const FTSENT *e1; + const FTSENT *e2; + + e1 = *ent1; + e2 = *ent2; + + return (strcmp(e1->fts_name, e2->fts_name)); +} + struct pkg_list * fbsd_directorydb_all(struct pkg_db *db) { - int c; struct _directorydb *d; - struct dirent **ents; + FTS *ftsp; + FTSENT *ftsent; + char *paths[2]; struct pkg *pkg; struct pkg_list *list; - + d = db->internal; - /* XXX: Since I'm only doing this in _add, go back to fts for the speed. - * */ - c = scandir(d->path, &ents, dselect, alphasort); - if (c < 0) { - return (NULL); - } - + /* XXX: Not seeing the kind of speed up I would have liked from this + * loop... */ + + paths[0] = strdup(d->path); + paths[1] = NULL; + + ftsp = fts_open(paths, FTS_LOGICAL | FTS_NOCHDIR | FTS_NOSTAT, + ftsentcmp); + assert(ftsp != NULL); + list = malloc(sizeof(*list)); TAILQ_INIT(list); - for (int i = 0; i < c; ++i) { + while ((ftsent = fts_read(ftsp)) != NULL) { + if (ftsent->fts_info != FTS_D || ftsent->fts_level != 1) + continue; + fts_set(ftsp, ftsent, FTS_SKIP); + pkg = pkg_alloc(); - /* XXX: I should just do a get here. */ - fbsd_directorydb_pkg_setup(db, pkg, ents[i]->d_name); - + fbsd_directorydb_pkg_setup(db, pkg, ftsent->fts_name); TAILQ_INSERT_TAIL(list, pkg, next); - free(ents[i]); } - free(ents); + fts_close(ftsp); return (list); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201011020325.oA23Po8u002982>