From faad0a65978514972099d9a00d2225e245bd4ca5 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Thu, 27 Jan 2011 18:22:57 +0100 Subject: [PATCH] Make xbps_remove_pkg_from_* use shared code, and add another variant. The functions have been renamed to really match what they do. --- bin/xbps-repo/index.c | 3 +- bin/xbps-uhelper/main.c | 2 +- include/xbps_api.h | 26 ++++++++---- lib/package_register.c | 2 +- lib/plist.c | 88 ++++++++++++++++++----------------------- 5 files changed, 62 insertions(+), 59 deletions(-) diff --git a/bin/xbps-repo/index.c b/bin/xbps-repo/index.c index f8424e99..c3250e16 100644 --- a/bin/xbps-repo/index.c +++ b/bin/xbps-repo/index.c @@ -163,7 +163,8 @@ xbps_repo_addpkg_index(prop_dictionary_t idxdict, const char *filedir, rv = errno; goto out; } - if (!xbps_remove_pkg_from_dict(idxdict, "packages", pkgname)) { + if (!xbps_remove_pkg_from_dict_by_name(idxdict, + "packages", pkgname)) { fprintf(stderr, "E: couldn't remove %s dict from " "index (%s)\n", pkgname, strerror(errno)); prop_object_release(newpkgd); diff --git a/bin/xbps-uhelper/main.c b/bin/xbps-uhelper/main.c index 29a521f1..28babeb9 100644 --- a/bin/xbps-uhelper/main.c +++ b/bin/xbps-uhelper/main.c @@ -195,7 +195,7 @@ main(int argc, char **argv) if (argc != 3) usage(); - if (!xbps_remove_pkg_dict_from_file(argv[1], plist)) { + if (!xbps_remove_pkg_dict_from_plist_by_name(argv[1], plist)) { if (errno == ENOENT) fprintf(stderr, "%s=> ERROR: %s not registered " "in database.%s\n", MSG_WARN, argv[1], MSG_RESET); diff --git a/include/xbps_api.h b/include/xbps_api.h index ead189f1..b0564c4c 100644 --- a/include/xbps_api.h +++ b/include/xbps_api.h @@ -53,7 +53,7 @@ * @def XBPS_RELVER * Current library release date. */ -#define XBPS_RELVER "20110126" +#define XBPS_RELVER "20110127" /** * @def XBPS_META_PATH @@ -520,12 +520,24 @@ prop_dictionary_t xbps_get_pkg_dict_from_metadata_plist(const char *pkgn, * Removes the package's proplib dictionary matching \a pkgname * in a plist file. * - * @param[in] pkgname Package name to look for. + * @param[in] name Package name to match in plist's dictionary. * @param[in] plist Path to a plist file. * * @return true on success, false otherwise and errno is set appropiately. */ -bool xbps_remove_pkg_dict_from_file(const char *pkgname, const char *plist); +bool xbps_remove_pkg_dict_from_plist_by_name(const char *name, + const char *plist); + +/** + * Removes the package's proplib dictionary matching \a pkgname + * in a proplib array. + * + * @param[in] array Proplib array where to look for. + * @param[in] name Package name to match in the array. + * + * @return true on success, false otherwise and errno is set appropiately. + */ +bool xbps_remove_pkg_from_array_by_name(prop_array_t array, const char *name); /** * Removes the package's proplib dictionary matching \a pkgname, @@ -533,13 +545,13 @@ bool xbps_remove_pkg_dict_from_file(const char *pkgname, const char *plist); * * @param[in] dict Proplib dictionary storing the proplib array. * @param[in] key Key associated with the proplib array. - * @param[in] pkgname Package name to look for. + * @param[in] name Package name to look for. * * @return true on success, false otherwise and errno is set appropiately. */ -bool xbps_remove_pkg_from_dict(prop_dictionary_t dict, - const char *key, - const char *pkgname); +bool xbps_remove_pkg_from_dict_by_name(prop_dictionary_t dict, + const char *key, + const char *name); /** * Removes a string from a proplib's array. diff --git a/lib/package_register.c b/lib/package_register.c index 1fabd13c..2d2d8e5b 100644 --- a/lib/package_register.c +++ b/lib/package_register.c @@ -151,7 +151,7 @@ xbps_unregister_pkg(const char *pkgname) if (plist == NULL) return ENOMEM; - if (!xbps_remove_pkg_dict_from_file(pkgname, plist)) + if (!xbps_remove_pkg_dict_from_plist_by_name(pkgname, plist)) rv = errno; free(plist); diff --git a/lib/plist.c b/lib/plist.c index b9fb8047..692e5c3c 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -436,25 +436,34 @@ xbps_get_pkg_dict_from_metadata_plist(const char *pkgn, const char *plist) return plistd; } -bool -xbps_remove_string_from_array(prop_array_t array, const char *str) +static bool +remove_string_from_array(prop_array_t array, const char *str, bool byname) { - prop_object_t obj; prop_object_iterator_t iter; - unsigned int idx = 0; + prop_object_t obj; + const char *curname; + size_t idx = 0; bool found = false; - assert(array != NULL); - assert(str != NULL); - iter = prop_array_iterator(array); if (iter == NULL) return false; - while ((obj = prop_object_iterator_next(iter)) != NULL) { - if (prop_string_equals_cstring(obj, str)) { - found = true; - break; + while ((obj = prop_object_iterator_next(iter))) { + if (byname) { + /* match by name */ + prop_dictionary_get_cstring_nocopy(obj, + "pkgname", &curname); + if (strcmp(curname, str) == 0) { + found = true; + break; + } + } else { + /* exact match */ + if (prop_string_equals_cstring(obj, str)) { + found = true; + break; + } } idx++; } @@ -465,60 +474,41 @@ xbps_remove_string_from_array(prop_array_t array, const char *str) } prop_array_remove(array, idx); - return true; } bool -xbps_remove_pkg_from_dict(prop_dictionary_t dict, - const char *key, - const char *pkgname) +xbps_remove_string_from_array(prop_array_t array, const char *str) +{ + return remove_string_from_array(array, str, false); +} + +bool +xbps_remove_pkg_from_array_by_name(prop_array_t array, const char *name) +{ + return remove_string_from_array(array, name, true); +} + +bool +xbps_remove_pkg_from_dict_by_name(prop_dictionary_t dict, + const char *key, + const char *pkgname) { prop_array_t array; - prop_object_t obj; - prop_object_iterator_t iter; - const char *curpkgname; - unsigned int i = 0; - bool found = false; assert(dict != NULL); assert(key != NULL); assert(pkgname != NULL); array = prop_dictionary_get(dict, key); - if (prop_object_type(array) != PROP_TYPE_ARRAY) { - errno = EINVAL; - xbps_dbg_printf("invalid array type for key: %s pkgname: %s\n", - key, pkgname); - return false; - } - - iter = prop_array_iterator(array); - if (iter == NULL) + if (array == NULL) return false; - /* Iterate over the array of dictionaries to find its index. */ - while ((obj = prop_object_iterator_next(iter))) { - prop_dictionary_get_cstring_nocopy(obj, "pkgname", &curpkgname); - if ((curpkgname && (strcmp(curpkgname, pkgname) == 0))) { - found = true; - break; - } - i++; - } - prop_object_iterator_release(iter); - if (found) { - prop_array_remove(array, i); - return true; - } - - errno = ENOENT; - - return false; + return xbps_remove_pkg_from_array_by_name(array, pkgname); } bool -xbps_remove_pkg_dict_from_file(const char *pkg, const char *plist) +xbps_remove_pkg_dict_from_plist_by_name(const char *pkg, const char *plist) { prop_dictionary_t pdict; @@ -532,7 +522,7 @@ xbps_remove_pkg_dict_from_file(const char *pkg, const char *plist) return false; } - if (!xbps_remove_pkg_from_dict(pdict, "packages", pkg)) { + if (!xbps_remove_pkg_from_dict_by_name(pdict, "packages", pkg)) { prop_object_release(pdict); return false; }