From f5dd58df5d9eacbca724611d837f2fba85a1b140 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Sat, 4 Apr 2009 18:05:18 +0200 Subject: [PATCH] Simplify xbps_{un,}register_repository(). --HG-- extra : convert_revision : 95ac1ddedbc96fe3bf2a9dfe282c20385c8740ff --- bin/xbps-repo/main.c | 10 +-- include/repository.h | 8 +-- lib/repository.c | 156 +++++++++++++------------------------------ 3 files changed, 54 insertions(+), 120 deletions(-) diff --git a/bin/xbps-repo/main.c b/bin/xbps-repo/main.c index 8af3f1662b0..62fb1ab06b7 100644 --- a/bin/xbps-repo/main.c +++ b/bin/xbps-repo/main.c @@ -206,9 +206,9 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } - if (!xbps_register_repository(dpkgidx)) { + if ((rv = xbps_register_repository(dpkgidx)) != 0) { printf("ERROR: couldn't register repository (%s)\n", - strerror(errno)); + strerror(rv)); prop_object_release(dict); free(rinfo); free(plist); @@ -240,13 +240,13 @@ main(int argc, char **argv) if (!sanitize_localpath(dpkgidx, argv[1])) exit(EXIT_FAILURE); - if (!xbps_unregister_repository(dpkgidx)) { - if (errno == ENOENT) + if ((rv = xbps_unregister_repository(dpkgidx)) != 0) { + if (rv == ENOENT) printf("Repository '%s' not actually " "registered.\n", dpkgidx); else printf("ERROR: couldn't unregister " - "repository (%s)\n", strerror(errno)); + "repository (%s)\n", strerror(rv)); exit(EXIT_FAILURE); } diff --git a/include/repository.h b/include/repository.h index deb540f5b4b..5df20951934 100644 --- a/include/repository.h +++ b/include/repository.h @@ -34,11 +34,9 @@ * Arguments: * - 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 *); -bool xbps_unregister_repository(const char *); - -int xbps_remove_string_from_array(prop_object_t, void *, bool *); +int xbps_register_repository(const char *); +int xbps_unregister_repository(const char *); #endif /* !_XBPS_REPOSITORY_H_ */ diff --git a/lib/repository.c b/lib/repository.c index e0f21d1ce9b..1c5efc11d39 100644 --- a/lib/repository.c +++ b/lib/repository.c @@ -33,44 +33,20 @@ #include -struct callback_args { - const char *string; - ssize_t number; -}; - 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) { prop_dictionary_t dict; - prop_array_t array = NULL; - prop_object_t obj; + prop_array_t array; + prop_object_t obj = NULL; char *plist; + int rv = 0; assert(uri != NULL); plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST); - if (plist == NULL) { - errno = EINVAL; - return false; - } + if (plist == NULL) + return errno; /* First check if we have the repository plist file. */ dict = prop_dictionary_internalize_from_file(plist); @@ -78,135 +54,95 @@ xbps_register_repository(const char *uri) /* Looks like not, create it. */ dict = prop_dictionary_create(); if (dict == NULL) { - free(plist); - return false; + rv = errno; + goto out2; } - /* Create the array and add the repository URI on it. */ array = prop_array_create(); if (array == NULL) { - prop_object_release(dict); - free(plist); - return false; + rv = errno; + goto out; + } + 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. */ - if (!xbps_add_obj_to_dict(dict, array, "repository-list")) - goto fail; - - /* Write dictionary into plist file. */ - if (!prop_dictionary_externalize_to_file(dict, plist)) - goto fail; - - prop_object_release(dict); - + if (!xbps_add_obj_to_dict(dict, array, "repository-list")) { + rv = errno; + goto out; + } } else { /* Append into the array, the plist file exists. */ array = prop_dictionary_get(dict, "repository-list"); - if (array == NULL) - goto fail; - - assert(prop_object_type(array) == PROP_TYPE_ARRAY); - + if (array == NULL) { + rv = errno; + goto out; + } /* It seems that this object is already there */ if (xbps_find_string_in_array(array, uri)) { errno = EEXIST; - goto fail; + goto out; } obj = prop_string_create_cstring(uri); if (!xbps_add_obj_to_array(array, obj)) { prop_object_release(obj); - return false; + rv = errno; + goto out; } - - /* Write dictionary into plist file. */ - if (!prop_dictionary_externalize_to_file(dict, plist)) { - prop_object_release(obj); - return false; - } - - prop_object_release(dict); } - free(plist); - - return true; - -fail: + /* Write dictionary into plist file. */ + if (!prop_dictionary_externalize_to_file(dict, plist)) { + if (obj) + prop_object_release(obj); + rv = errno; + } +out: prop_object_release(dict); +out2: free(plist); - return false; + return rv; } -bool +int xbps_unregister_repository(const char *uri) { prop_dictionary_t dict; prop_array_t array; - struct callback_args *cb; char *plist; - bool done = false; + int rv = 0; assert(uri != NULL); plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST); - if (plist == NULL) { - errno = EINVAL; - return false; - } + if (plist == NULL) + return errno; dict = prop_dictionary_internalize_from_file(plist); if (dict == NULL) { free(plist); - return false; + return errno; } array = prop_dictionary_get(dict, "repository-list"); if (array == NULL) { - prop_object_release(dict); - free(plist); - return false; + rv = errno; + goto out; } - assert(prop_object_type(array) == PROP_TYPE_ARRAY); - - 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); - + rv = xbps_remove_string_from_array(array, uri); + if (rv == 0) { /* Update plist file. */ - if (prop_dictionary_externalize_to_file(dict, plist)) { - prop_object_release(dict); - free(cb); - free(plist); - return true; - } - } else { - /* Not found. */ - errno = ENOENT; + if (!prop_dictionary_externalize_to_file(dict, plist)) + rv = errno; } +out: prop_object_release(dict); - free(cb); free(plist); - return false; + return rv; }