Simplify xbps_{un,}register_repository().

--HG--
extra : convert_revision : 95ac1ddedbc96fe3bf2a9dfe282c20385c8740ff
This commit is contained in:
Juan RP 2009-04-04 18:05:18 +02:00
parent ba56172222
commit f5dd58df5d
3 changed files with 54 additions and 120 deletions

View file

@ -206,9 +206,9 @@ main(int argc, char **argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!xbps_register_repository(dpkgidx)) { if ((rv = xbps_register_repository(dpkgidx)) != 0) {
printf("ERROR: couldn't register repository (%s)\n", printf("ERROR: couldn't register repository (%s)\n",
strerror(errno)); strerror(rv));
prop_object_release(dict); prop_object_release(dict);
free(rinfo); free(rinfo);
free(plist); free(plist);
@ -240,13 +240,13 @@ main(int argc, char **argv)
if (!sanitize_localpath(dpkgidx, argv[1])) if (!sanitize_localpath(dpkgidx, argv[1]))
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (!xbps_unregister_repository(dpkgidx)) { if ((rv = xbps_unregister_repository(dpkgidx)) != 0) {
if (errno == ENOENT) if (rv == ENOENT)
printf("Repository '%s' not actually " printf("Repository '%s' not actually "
"registered.\n", dpkgidx); "registered.\n", dpkgidx);
else else
printf("ERROR: couldn't unregister " printf("ERROR: couldn't unregister "
"repository (%s)\n", strerror(errno)); "repository (%s)\n", strerror(rv));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View file

@ -34,11 +34,9 @@
* Arguments: * Arguments:
* - const char *: URI to (un)register. * - const char *: URI to (un)register.
* *
* Returns true on success, or false and an appropiate errno value otherwise. * Returns 0 on success, or an appropiate value otherwise.
*/ */
bool xbps_register_repository(const char *); int xbps_register_repository(const char *);
bool xbps_unregister_repository(const char *); int xbps_unregister_repository(const char *);
int xbps_remove_string_from_array(prop_object_t, void *, bool *);
#endif /* !_XBPS_REPOSITORY_H_ */ #endif /* !_XBPS_REPOSITORY_H_ */

View file

@ -33,44 +33,20 @@
#include <xbps_api.h> #include <xbps_api.h>
struct callback_args {
const char *string;
ssize_t number;
};
int int
xbps_remove_string_from_array(prop_object_t obj, void *arg, bool *loop_done)
{
static ssize_t idx;
struct callback_args *cb = arg;
assert(prop_object_type(obj) == PROP_TYPE_STRING);
if (prop_string_equals_cstring(obj, cb->string)) {
cb->number = idx;
*loop_done = true;
return 0;
}
idx++;
return 0;
}
bool
xbps_register_repository(const char *uri) xbps_register_repository(const char *uri)
{ {
prop_dictionary_t dict; prop_dictionary_t dict;
prop_array_t array = NULL; prop_array_t array;
prop_object_t obj; prop_object_t obj = NULL;
char *plist; char *plist;
int rv = 0;
assert(uri != NULL); assert(uri != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST); plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
if (plist == NULL) { if (plist == NULL)
errno = EINVAL; return errno;
return false;
}
/* First check if we have the repository plist file. */ /* First check if we have the repository plist file. */
dict = prop_dictionary_internalize_from_file(plist); dict = prop_dictionary_internalize_from_file(plist);
@ -78,135 +54,95 @@ xbps_register_repository(const char *uri)
/* Looks like not, create it. */ /* Looks like not, create it. */
dict = prop_dictionary_create(); dict = prop_dictionary_create();
if (dict == NULL) { if (dict == NULL) {
free(plist); rv = errno;
return false; goto out2;
} }
/* Create the array and add the repository URI on it. */ /* Create the array and add the repository URI on it. */
array = prop_array_create(); array = prop_array_create();
if (array == NULL) { if (array == NULL) {
prop_object_release(dict); rv = errno;
free(plist); goto out;
return false; }
if (!prop_array_set_cstring_nocopy(array, 0, uri)) {
rv = errno;
goto out;
} }
if (!prop_array_set_cstring_nocopy(array, 0, uri))
goto fail;
/* Add the array obj into the main dictionary. */ /* Add the array obj into the main dictionary. */
if (!xbps_add_obj_to_dict(dict, array, "repository-list")) if (!xbps_add_obj_to_dict(dict, array, "repository-list")) {
goto fail; rv = errno;
goto out;
/* Write dictionary into plist file. */ }
if (!prop_dictionary_externalize_to_file(dict, plist))
goto fail;
prop_object_release(dict);
} else { } else {
/* Append into the array, the plist file exists. */ /* Append into the array, the plist file exists. */
array = prop_dictionary_get(dict, "repository-list"); array = prop_dictionary_get(dict, "repository-list");
if (array == NULL) if (array == NULL) {
goto fail; rv = errno;
goto out;
assert(prop_object_type(array) == PROP_TYPE_ARRAY); }
/* It seems that this object is already there */ /* It seems that this object is already there */
if (xbps_find_string_in_array(array, uri)) { if (xbps_find_string_in_array(array, uri)) {
errno = EEXIST; errno = EEXIST;
goto fail; goto out;
} }
obj = prop_string_create_cstring(uri); obj = prop_string_create_cstring(uri);
if (!xbps_add_obj_to_array(array, obj)) { if (!xbps_add_obj_to_array(array, obj)) {
prop_object_release(obj); prop_object_release(obj);
return false; rv = errno;
goto out;
}
} }
/* Write dictionary into plist file. */ /* Write dictionary into plist file. */
if (!prop_dictionary_externalize_to_file(dict, plist)) { if (!prop_dictionary_externalize_to_file(dict, plist)) {
if (obj)
prop_object_release(obj); prop_object_release(obj);
return false; rv = errno;
} }
out:
prop_object_release(dict); prop_object_release(dict);
} out2:
free(plist); free(plist);
return true; return rv;
fail:
prop_object_release(dict);
free(plist);
return false;
} }
bool int
xbps_unregister_repository(const char *uri) xbps_unregister_repository(const char *uri)
{ {
prop_dictionary_t dict; prop_dictionary_t dict;
prop_array_t array; prop_array_t array;
struct callback_args *cb;
char *plist; char *plist;
bool done = false; int rv = 0;
assert(uri != NULL); assert(uri != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST); plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
if (plist == NULL) { if (plist == NULL)
errno = EINVAL; return errno;
return false;
}
dict = prop_dictionary_internalize_from_file(plist); dict = prop_dictionary_internalize_from_file(plist);
if (dict == NULL) { if (dict == NULL) {
free(plist); free(plist);
return false; return errno;
} }
array = prop_dictionary_get(dict, "repository-list"); array = prop_dictionary_get(dict, "repository-list");
if (array == NULL) { if (array == NULL) {
prop_object_release(dict); rv = errno;
free(plist); goto out;
return false;
} }
assert(prop_object_type(array) == PROP_TYPE_ARRAY); rv = xbps_remove_string_from_array(array, uri);
if (rv == 0) {
cb = malloc(sizeof(*cb));
if (cb == NULL) {
prop_object_release(dict);
free(plist);
errno = ENOMEM;
return false;
}
cb->string = uri;
cb->number = -1;
done = xbps_callback_array_iter_in_dict(dict, "repository-list",
xbps_remove_string_from_array, cb);
if (done == 0 && cb->number >= 0) {
/* Found, remove it. */
prop_array_remove(array, cb->number);
/* Update plist file. */ /* Update plist file. */
if (prop_dictionary_externalize_to_file(dict, plist)) { if (!prop_dictionary_externalize_to_file(dict, plist))
prop_object_release(dict); rv = errno;
free(cb);
free(plist);
return true;
}
} else {
/* Not found. */
errno = ENOENT;
} }
out:
prop_object_release(dict); prop_object_release(dict);
free(cb);
free(plist); free(plist);
return false; return rv;
} }