From bbe6dcc0886d21dcd3aa4f75fff22b1009ec5a79 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 27 Aug 2016 09:48:53 -0500 Subject: [PATCH] pkg: make pkgconf_scan_all() API more flexible to allow it to scan until it finds a qualifying pkgconf_pkg_t --- libpkgconf/libpkgconf.h | 4 ++-- libpkgconf/pkg.c | 26 +++++++++++++++++++------- main.c | 24 ++++++++++++++++-------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/libpkgconf/libpkgconf.h b/libpkgconf/libpkgconf.h index 7f740a46..3d11b607 100644 --- a/libpkgconf/libpkgconf.h +++ b/libpkgconf/libpkgconf.h @@ -126,7 +126,7 @@ struct pkgconf_pkg_ { #define PKGCONF_PKG_ERRF_PACKAGE_CONFLICT 0x4 #define PKGCONF_PKG_ERRF_DEPGRAPH_BREAK 0x8 -typedef void (*pkgconf_pkg_iteration_func_t)(const pkgconf_pkg_t *pkg); +typedef bool (*pkgconf_pkg_iteration_func_t)(const pkgconf_pkg_t *pkg, void *data); typedef void (*pkgconf_pkg_traverse_func_t)(pkgconf_pkg_t *pkg, void *data, unsigned int flags); typedef bool (*pkgconf_queue_apply_func_t)(pkgconf_pkg_t *world, void *data, int maxdepth, unsigned int flags); typedef bool (*pkgconf_error_handler_func_t)(const char *msg); @@ -160,7 +160,7 @@ pkgconf_pkg_comparator_t pkgconf_pkg_comparator_lookup_by_name(const char *name) pkgconf_pkg_t *pkgconf_builtin_pkg_get(const char *name); int pkgconf_compare_version(const char *a, const char *b); -void pkgconf_scan_all(pkgconf_pkg_iteration_func_t func); +pkgconf_pkg_t *pkgconf_scan_all(void *ptr, pkgconf_pkg_iteration_func_t func); /* parse.c */ pkgconf_pkg_t *pkgconf_pkg_new_from_file(const char *path, FILE *f, unsigned int flags); diff --git a/libpkgconf/pkg.c b/libpkgconf/pkg.c index 2ec009b1..6ec65eee 100644 --- a/libpkgconf/pkg.c +++ b/libpkgconf/pkg.c @@ -417,15 +417,16 @@ pkgconf_pkg_try_specific_path(const char *path, const char *name, unsigned int f return pkg; } -static void -pkgconf_pkg_scan_dir(const char *path, pkgconf_pkg_iteration_func_t func) +static pkgconf_pkg_t * +pkgconf_pkg_scan_dir(const char *path, void *data, pkgconf_pkg_iteration_func_t func) { DIR *dir; struct dirent *dirent; + pkgconf_pkg_t *outpkg = NULL; dir = opendir(path); if (dir == NULL) - return; + return NULL; for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) { @@ -449,18 +450,26 @@ pkgconf_pkg_scan_dir(const char *path, pkgconf_pkg_iteration_func_t func) pkg = pkgconf_pkg_new_from_file(filebuf, f, 0); if (pkg != NULL) { - func(pkg); + if (func(pkg, data)) + { + outpkg = pkg; + goto out; + } + pkgconf_pkg_unref(pkg); } } +out: closedir(dir); + return outpkg; } -void -pkgconf_scan_all(pkgconf_pkg_iteration_func_t func) +pkgconf_pkg_t * +pkgconf_scan_all(void *data, pkgconf_pkg_iteration_func_t func) { pkgconf_node_t *n; + pkgconf_pkg_t *pkg; pkgconf_pkg_dir_list_build(0); @@ -468,8 +477,11 @@ pkgconf_scan_all(pkgconf_pkg_iteration_func_t func) { pkg_path_t *pkg_path = n->data; - pkgconf_pkg_scan_dir(pkg_path->path, func); + if ((pkg = pkgconf_pkg_scan_dir(pkg_path->path, data, func)) != NULL) + return pkg; } + + return NULL; } #ifdef _WIN32 diff --git a/main.c b/main.c index 8b27a96c..85fa0d26 100644 --- a/main.c +++ b/main.c @@ -125,22 +125,30 @@ print_fragment(pkgconf_fragment_t *frag) printf("%s ", frag->data); } -static void -print_list_entry(const pkgconf_pkg_t *entry) +static bool +print_list_entry(const pkgconf_pkg_t *entry, void *data) { + (void) data; + if (entry->flags & PKGCONF_PKG_PROPF_UNINSTALLED) - return; + return false; printf("%-30s %s - %s\n", entry->id, entry->realname, entry->description); + + return false; } -static void -print_package_entry(const pkgconf_pkg_t *entry) +static bool +print_package_entry(const pkgconf_pkg_t *entry, void *data) { + (void) data; + if (entry->flags & PKGCONF_PKG_PROPF_UNINSTALLED) - return; + return false; printf("%s\n", entry->id); + + return false; } static void @@ -818,13 +826,13 @@ main(int argc, char *argv[]) if ((want_flags & PKG_LIST) == PKG_LIST) { - pkgconf_scan_all(print_list_entry); + pkgconf_scan_all(NULL, print_list_entry); return EXIT_SUCCESS; } if ((want_flags & PKG_LIST_PACKAGE_NAMES) == PKG_LIST_PACKAGE_NAMES) { - pkgconf_scan_all(print_package_entry); + pkgconf_scan_all(NULL, print_package_entry); return EXIT_SUCCESS; }