Make xbps_remove_pkg_from_* use shared code, and add another variant.

The functions have been renamed to really match what they do.
This commit is contained in:
Juan RP 2011-01-27 18:22:57 +01:00
parent 93e1a0a22c
commit faad0a6597
5 changed files with 62 additions and 59 deletions

View File

@ -163,7 +163,8 @@ xbps_repo_addpkg_index(prop_dictionary_t idxdict, const char *filedir,
rv = errno; rv = errno;
goto out; 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 " fprintf(stderr, "E: couldn't remove %s dict from "
"index (%s)\n", pkgname, strerror(errno)); "index (%s)\n", pkgname, strerror(errno));
prop_object_release(newpkgd); prop_object_release(newpkgd);

View File

@ -195,7 +195,7 @@ main(int argc, char **argv)
if (argc != 3) if (argc != 3)
usage(); 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) if (errno == ENOENT)
fprintf(stderr, "%s=> ERROR: %s not registered " fprintf(stderr, "%s=> ERROR: %s not registered "
"in database.%s\n", MSG_WARN, argv[1], MSG_RESET); "in database.%s\n", MSG_WARN, argv[1], MSG_RESET);

View File

@ -53,7 +53,7 @@
* @def XBPS_RELVER * @def XBPS_RELVER
* Current library release date. * Current library release date.
*/ */
#define XBPS_RELVER "20110126" #define XBPS_RELVER "20110127"
/** /**
* @def XBPS_META_PATH * @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 * Removes the package's proplib dictionary matching \a pkgname
* in a plist file. * 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. * @param[in] plist Path to a plist file.
* *
* @return true on success, false otherwise and errno is set appropiately. * @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, * 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] dict Proplib dictionary storing the proplib array.
* @param[in] key Key associated with 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. * @return true on success, false otherwise and errno is set appropiately.
*/ */
bool xbps_remove_pkg_from_dict(prop_dictionary_t dict, bool xbps_remove_pkg_from_dict_by_name(prop_dictionary_t dict,
const char *key, const char *key,
const char *pkgname); const char *name);
/** /**
* Removes a string from a proplib's array. * Removes a string from a proplib's array.

View File

@ -151,7 +151,7 @@ xbps_unregister_pkg(const char *pkgname)
if (plist == NULL) if (plist == NULL)
return ENOMEM; return ENOMEM;
if (!xbps_remove_pkg_dict_from_file(pkgname, plist)) if (!xbps_remove_pkg_dict_from_plist_by_name(pkgname, plist))
rv = errno; rv = errno;
free(plist); free(plist);

View File

@ -436,25 +436,34 @@ xbps_get_pkg_dict_from_metadata_plist(const char *pkgn, const char *plist)
return plistd; return plistd;
} }
bool static bool
xbps_remove_string_from_array(prop_array_t array, const char *str) remove_string_from_array(prop_array_t array, const char *str, bool byname)
{ {
prop_object_t obj;
prop_object_iterator_t iter; prop_object_iterator_t iter;
unsigned int idx = 0; prop_object_t obj;
const char *curname;
size_t idx = 0;
bool found = false; bool found = false;
assert(array != NULL);
assert(str != NULL);
iter = prop_array_iterator(array); iter = prop_array_iterator(array);
if (iter == NULL) if (iter == NULL)
return false; return false;
while ((obj = prop_object_iterator_next(iter)) != NULL) { while ((obj = prop_object_iterator_next(iter))) {
if (prop_string_equals_cstring(obj, str)) { if (byname) {
found = true; /* match by name */
break; 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++; idx++;
} }
@ -465,60 +474,41 @@ xbps_remove_string_from_array(prop_array_t array, const char *str)
} }
prop_array_remove(array, idx); prop_array_remove(array, idx);
return true; return true;
} }
bool bool
xbps_remove_pkg_from_dict(prop_dictionary_t dict, xbps_remove_string_from_array(prop_array_t array, const char *str)
const char *key, {
const char *pkgname) 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_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(dict != NULL);
assert(key != NULL); assert(key != NULL);
assert(pkgname != NULL); assert(pkgname != NULL);
array = prop_dictionary_get(dict, key); array = prop_dictionary_get(dict, key);
if (prop_object_type(array) != PROP_TYPE_ARRAY) { if (array == NULL)
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)
return false; return false;
/* Iterate over the array of dictionaries to find its index. */ return xbps_remove_pkg_from_array_by_name(array, pkgname);
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;
} }
bool 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; prop_dictionary_t pdict;
@ -532,7 +522,7 @@ xbps_remove_pkg_dict_from_file(const char *pkg, const char *plist)
return false; 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); prop_object_release(pdict);
return false; return false;
} }