diff --git a/utils/plist.c b/utils/plist.c index ac198f7a22..f10da6b5e0 100644 --- a/utils/plist.c +++ b/utils/plist.c @@ -33,7 +33,7 @@ #include "xbps_api.h" -static bool xbps_list_strings_in_array2(prop_object_t); +static bool xbps_list_strings_in_array2(prop_object_t, void *); bool xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj, @@ -66,7 +66,8 @@ xbps_add_obj_to_array(prop_array_t array, prop_object_t obj) bool xbps_callback_array_iter_in_dict(prop_dictionary_t dict, const char *key, - bool (*func)(prop_object_t)) + bool (*func)(prop_object_t, void *), + void *arg) { prop_object_iterator_t iter; prop_object_t obj; @@ -79,7 +80,7 @@ xbps_callback_array_iter_in_dict(prop_dictionary_t dict, const char *key, return false; while ((obj = prop_object_iterator_next(iter))) { - if (!(*func)(obj)) { + if (!(*func)(obj, arg)) { prop_object_iterator_release(iter); return false; } @@ -257,7 +258,7 @@ xbps_show_pkg_info(prop_dictionary_t dict) printf("\n\t"); if (!xbps_callback_array_iter_in_dict(dict, prop_dictionary_keysym_cstring_nocopy(obj), - xbps_list_strings_in_array2)) + xbps_list_strings_in_array2, NULL)) return; printf("\n"); } @@ -267,7 +268,38 @@ xbps_show_pkg_info(prop_dictionary_t dict) } bool -xbps_list_pkgs_in_dict(prop_object_t obj) +xbps_show_pkg_info_from_repolist(prop_object_t obj, void *arg) +{ + prop_dictionary_t dict, pkgdict; + prop_string_t oloc; + const char *repofile, *repoloc; + + if (prop_object_type(obj) != PROP_TYPE_STRING) + return false; + + repofile = prop_string_cstring_nocopy(obj); + dict = prop_dictionary_internalize_from_file(repofile); + pkgdict = xbps_find_pkg_in_dict(dict, arg); + if (pkgdict == NULL) + return false; + + oloc = prop_dictionary_get(dict, "location-remote"); + if (oloc == NULL) + oloc = prop_dictionary_get(dict, "location-local"); + + if (oloc && prop_object_type(oloc) == PROP_TYPE_STRING) + repoloc = prop_string_cstring_nocopy(oloc); + else + return false; + + printf("Repository: %s\n", repoloc); + xbps_show_pkg_info(pkgdict); + + return true; +} + +bool +xbps_list_pkgs_in_dict(prop_object_t obj, void *arg) { const char *pkgname, *version, *short_desc; @@ -286,7 +318,7 @@ xbps_list_pkgs_in_dict(prop_object_t obj) } static bool -xbps_list_strings_in_array2(prop_object_t obj) +xbps_list_strings_in_array2(prop_object_t obj, void *arg) { static uint16_t count; @@ -304,7 +336,7 @@ xbps_list_strings_in_array2(prop_object_t obj) } bool -xbps_list_strings_in_array(prop_object_t obj) +xbps_list_strings_in_array(prop_object_t obj, void *arg) { if (prop_object_type(obj) != PROP_TYPE_STRING) return false; diff --git a/utils/plist.h b/utils/plist.h index c8a00de5ba..208d6d4554 100644 --- a/utils/plist.h +++ b/utils/plist.h @@ -58,13 +58,15 @@ xbps_add_obj_to_array(prop_array_t, prop_object_t); * Arguments: * - prop_dictionary_t: dictionary to search on. * - const char *: key of the array. - * - (*func)(prop_object_t): callback associated. + * - (*func)(prop_object_t, void *): callback associated. + * - void *: argument passed to the callback. * * Returns true on success, false otherwise and the loop is terminated. */ bool xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *, - bool (*func)(prop_object_t)); + bool (*func)(prop_object_t, void *), + void *); /* * Finds a package's dictionary into the main dictionary. @@ -108,22 +110,24 @@ xbps_get_array_iter_from_dict(prop_dictionary_t, const char *); * * Arguments: * - prop_object_t: the object to be processed. + * - void *: argument passed. * * Returns true on success, false otherwise. */ bool -xbps_list_pkgs_in_dict(prop_object_t); +xbps_list_pkgs_in_dict(prop_object_t, void *); /* * Lists all string values in an array object in a dictionary. * * Arguments: * - prop_object_t: the object to be processed. + * - void *: argument passed. * * Returns true on success, false otherwise. */ bool -xbps_list_strings_in_array(prop_object_t); +xbps_list_strings_in_array(prop_object_t, void *); /* * Registers a repository specified by an URI into the pool. @@ -146,4 +150,18 @@ xbps_register_repository(const char *); void xbps_show_pkg_info(prop_dictionary_t); +/* + * Shows information of a package by searching in all repositories + * registered in the pool. It will show information from the + * first repository that has the package. + * + * Arguments: + * - prop_object_t: the object to be processed. + * - const char *: passed argument (pkgname string). + * + * Returns true on success, false otherwise. + */ +bool +xbps_show_pkg_info_from_repolist(prop_object_t obj, void *arg); + #endif /* !_XBPS_PLIST_H_ */ diff --git a/utils/xbps-bin.c b/utils/xbps-bin.c index 897c19a47b..bea6aca375 100644 --- a/utils/xbps-bin.c +++ b/utils/xbps-bin.c @@ -39,6 +39,7 @@ typedef struct repository_info { size_t total_pkgs; } repo_info_t; +static prop_dictionary_t getrepolist_dict(void); static bool pkgindex_getinfo(prop_dictionary_t, repo_info_t *); static void usage(void); @@ -90,6 +91,21 @@ pkgindex_getinfo(prop_dictionary_t dict, repo_info_t *ri) return true; } +static prop_dictionary_t +getrepolist_dict(void) +{ + prop_dictionary_t dict; + + dict = prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH); + if (dict == NULL) { + printf("cannot find repository list file: %s\n", + strerror(errno)); + exit(EINVAL); + } + + return dict; +} + int main(int argc, char **argv) { @@ -149,22 +165,20 @@ main(int argc, char **argv) if (argc != 2) usage(); - dict = - prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH); - if (dict == NULL) { - printf("cannot find repository list file: %s\n", - strerror(errno)); - exit(EINVAL); - } - - xbps_callback_array_iter_in_dict(dict, "repository-list", - xbps_list_strings_in_array); + xbps_callback_array_iter_in_dict(getrepolist_dict(), + "repository-list", xbps_list_strings_in_array, NULL); } else if (strcmp(argv[1], "show") == 0) { /* Shows info about a binary package. */ if (argc != 3) usage(); + if (!xbps_callback_array_iter_in_dict(getrepolist_dict(), + "repository-list", xbps_show_pkg_info_from_repolist, + argv[2])) { + printf("Package %s doesn't exist on any repository.\n", + argv[2]); + } } else { usage(); } diff --git a/utils/xbps-pkgdb.c b/utils/xbps-pkgdb.c index 46d74dd7f7..202b98056e 100644 --- a/utils/xbps-pkgdb.c +++ b/utils/xbps-pkgdb.c @@ -292,7 +292,7 @@ main(int argc, char **argv) dbdict = prop_dictionary_internalize_from_file(dbfile); if (!xbps_callback_array_iter_in_dict(dbdict, - "packages", xbps_list_pkgs_in_dict)) + "packages", xbps_list_pkgs_in_dict, NULL)) exit(EINVAL); } else if (strcmp(argv[1], "version") == 0) {