xbps_requiredby_pkg_{add,remove}: simplify and make them aware of virtual pkgs.

This commit is contained in:
Juan RP 2011-01-29 12:29:30 +01:00
parent 241751f1b7
commit d7bafff252

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2009-2010 Juan Romero Pardines. * Copyright (c) 2009-2011 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -33,33 +33,36 @@
#include "xbps_api_impl.h" #include "xbps_api_impl.h"
static int static int
add_pkg_into_reqby(prop_dictionary_t pkgd, const char *reqname) add_pkg_into_reqby(prop_dictionary_t pkgd, const char *pkgver)
{ {
prop_array_t array; prop_array_t reqby;
prop_string_t reqstr; prop_string_t reqstr;
bool alloc = false; bool alloc = false;
array = prop_dictionary_get(pkgd, "requiredby"); if ((reqby = prop_dictionary_get(pkgd, "requiredby")) == NULL) {
if (array == NULL) {
alloc = true; alloc = true;
array = prop_array_create(); if ((reqby = prop_array_create()) == NULL)
if (array == NULL)
return ENOMEM; return ENOMEM;
} }
if (xbps_find_string_in_array(array, reqname)) if (xbps_find_string_in_array(reqby, pkgver)) {
return EEXIST; if (alloc)
prop_object_release(reqby);
reqstr = prop_string_create_cstring(reqname); return EEXIST;
}
reqstr = prop_string_create_cstring(pkgver);
if (reqstr == NULL) { if (reqstr == NULL) {
if (alloc) if (alloc)
prop_object_release(array); prop_object_release(reqby);
return ENOMEM; return ENOMEM;
} }
if (!xbps_add_obj_to_array(array, reqstr)) { if (!xbps_add_obj_to_array(reqby, reqstr)) {
if (alloc) if (alloc)
prop_object_release(array); prop_object_release(reqby);
prop_object_release(reqstr); prop_object_release(reqstr);
return EINVAL; return EINVAL;
@ -68,9 +71,9 @@ add_pkg_into_reqby(prop_dictionary_t pkgd, const char *reqname)
if (!alloc) if (!alloc)
return 0; return 0;
if (!xbps_add_obj_to_dict(pkgd, array, "requiredby")) { if (!xbps_add_obj_to_dict(pkgd, reqby, "requiredby")) {
if (alloc) if (alloc)
prop_object_release(array); prop_object_release(reqby);
return EINVAL; return EINVAL;
} }
@ -81,43 +84,18 @@ add_pkg_into_reqby(prop_dictionary_t pkgd, const char *reqname)
static int static int
remove_pkg_from_reqby(prop_object_t obj, void *arg, bool *loop_done) remove_pkg_from_reqby(prop_object_t obj, void *arg, bool *loop_done)
{ {
prop_array_t array; prop_array_t reqby;
prop_object_t obj2;
prop_object_iterator_t iter;
const char *pkgname = arg; const char *pkgname = arg;
char *curpkgname;
unsigned int idx = 0;
bool found = false;
(void)loop_done; (void)loop_done;
array = prop_dictionary_get(obj, "requiredby"); reqby = prop_dictionary_get(obj, "requiredby");
if (array == NULL || prop_array_count(array) == 0) if (reqby == NULL || prop_array_count(reqby) == 0)
return 0; return 0;
iter = prop_array_iterator(array); if (xbps_find_pkgname_in_array(reqby, pkgname))
if (iter == NULL) if (!xbps_remove_pkgname_from_array(reqby, pkgname))
return ENOMEM;
while ((obj2 = prop_object_iterator_next(iter)) != NULL) {
curpkgname =
xbps_get_pkg_name(prop_string_cstring_nocopy(obj2));
if (curpkgname == NULL) {
prop_object_iterator_release(iter);
return EINVAL; return EINVAL;
}
if (strcmp(curpkgname, pkgname) == 0) {
free(curpkgname);
found = true;
break;
}
free(curpkgname);
idx++;
}
prop_object_iterator_release(iter);
if (found)
prop_array_remove(array, idx);
return 0; return 0;
} }
@ -162,24 +140,23 @@ out:
} }
int HIDDEN int HIDDEN
xbps_requiredby_pkg_add(prop_array_t regar, prop_dictionary_t pkg) xbps_requiredby_pkg_add(prop_array_t pkgs_array, prop_dictionary_t pkgd)
{ {
prop_array_t rdeps; prop_array_t pkg_rdeps;
prop_object_t obj, obj2; prop_object_t obj, pkgd_regpkgdb;
prop_object_iterator_t iter, iter2; prop_object_iterator_t iter;
const char *reqname, *pkgver, *str; const char *pkgver, *str;
char *rdepname;
int rv = 0; int rv = 0;
assert(regar != NULL); assert(pkgs_array != NULL);
assert(pkg != NULL); assert(pkgd != NULL);
prop_dictionary_get_cstring_nocopy(pkg, "pkgver", &pkgver); prop_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);
rdeps = prop_dictionary_get(pkg, "run_depends"); pkg_rdeps = prop_dictionary_get(pkgd, "run_depends");
if (rdeps == NULL || prop_array_count(rdeps) == 0) if (pkg_rdeps == NULL || prop_array_count(pkg_rdeps) == 0)
return EINVAL; return EINVAL;
iter = prop_array_iterator(rdeps); iter = prop_array_iterator(pkg_rdeps);
if (iter == NULL) if (iter == NULL)
return ENOMEM; return ENOMEM;
@ -187,45 +164,17 @@ xbps_requiredby_pkg_add(prop_array_t regar, prop_dictionary_t pkg)
str = prop_string_cstring_nocopy(obj); str = prop_string_cstring_nocopy(obj);
if (str == NULL) { if (str == NULL) {
rv = EINVAL; rv = EINVAL;
goto out;
}
rdepname = xbps_get_pkgpattern_name(str);
if (rdepname == NULL) {
rv = EINVAL;
goto out;
}
iter2 = prop_array_iterator(regar);
if (iter2 == NULL) {
rv = ENOMEM;
free(rdepname);
goto out;
}
/*
* Iterate over the array to find the dictionary for the
* current run dependency.
*/
while ((obj2 = prop_object_iterator_next(iter2)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj2,
"pkgname", &reqname);
if (strcmp(rdepname, reqname) == 0) {
rv = add_pkg_into_reqby(obj2, pkgver);
if (rv == EEXIST)
continue;
else if (rv != 0) {
free(rdepname);
prop_object_iterator_release(iter2);
goto out;
}
break; break;
} }
} pkgd_regpkgdb =
prop_object_iterator_release(iter2); xbps_find_pkg_in_array_by_pattern(pkgs_array, str);
free(rdepname); if (pkgd_regpkgdb == NULL)
} return EINVAL;
out: rv = add_pkg_into_reqby(pkgd_regpkgdb, pkgver);
if (rv != 0 && rv != EEXIST)
break;
}
prop_object_iterator_release(iter); prop_object_iterator_release(iter);
return rv; return rv;