From 5722c8aca1412adf1710f974bab39b0d33d644ee Mon Sep 17 00:00:00 2001 From: Juan RP Date: Sun, 21 Dec 2008 01:50:19 +0100 Subject: [PATCH] Replace some if conditionals with asserts where appropiate. --HG-- extra : convert_revision : 991989e77b31d84f9aab2e27a5366c18a78407ae --- utils/plist.c | 59 +++++++++++++++++----------------------- utils/plist.h | 2 +- utils/xbps-bin.c | 3 +-- utils/xbps-cmpver.c | 7 +++-- utils/xbps-pkgdb.c | 66 +++++++++++++++------------------------------ utils/xbps_api.h | 2 ++ 6 files changed, 53 insertions(+), 86 deletions(-) diff --git a/utils/plist.c b/utils/plist.c index 80077f7750..0f78ea8baa 100644 --- a/utils/plist.c +++ b/utils/plist.c @@ -42,8 +42,7 @@ bool xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj, const char *key) { - if (dict == NULL || obj == NULL || key == NULL) - return false; + assert(dict != NULL || obj != NULL || key != NULL); if (!prop_dictionary_set(dict, key, obj)) return false; @@ -55,8 +54,7 @@ xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj, bool xbps_add_obj_to_array(prop_array_t array, prop_object_t obj) { - if (array == NULL || obj == NULL) - return false; + assert(array != NULL || obj != NULL); if (!prop_array_add(array, obj)) { prop_object_release(array); @@ -77,11 +75,7 @@ xbps_callback_array_iter_in_dict(prop_dictionary_t dict, const char *key, bool run, ret, cbloop_done; run = ret = cbloop_done = false; - - if (func == NULL) - return false; - - cbloop_done = false; + assert(func != NULL); iter = xbps_get_array_iter_from_dict(dict, key); if (iter == NULL) @@ -106,8 +100,7 @@ xbps_find_pkg_in_dict(prop_dictionary_t dict, const char *pkgname) prop_object_t obj; const char *dpkgn; - if (pkgname == NULL) - return NULL; + assert(pkgname != NULL); iter = xbps_get_array_iter_from_dict(dict, "packages"); if (iter == NULL) @@ -129,8 +122,7 @@ xbps_find_string_in_array(prop_array_t array, const char *val) prop_object_iterator_t iter; prop_object_t obj; - if (array == NULL || val == NULL) - return false; + assert(array != NULL || val != NULL); iter = prop_array_iterator(array); if (iter == NULL) @@ -154,8 +146,7 @@ xbps_get_array_iter_from_dict(prop_dictionary_t dict, const char *key) { prop_array_t array; - if (dict == NULL || key == NULL) - return NULL; + assert(dict != NULL || key != NULL); array = prop_dictionary_get(dict, key); if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) @@ -166,13 +157,12 @@ xbps_get_array_iter_from_dict(prop_dictionary_t dict, const char *key) bool -xbps_remove_obj_from_array(prop_object_t obj, void *arg, bool *loop_done) +xbps_remove_string_from_array(prop_object_t obj, void *arg, bool *loop_done) { static int idx; struct callback_args *cb = arg; - if (prop_object_type(obj) != PROP_TYPE_STRING) - return false; + assert(prop_object_type(obj) == PROP_TYPE_STRING); if (prop_string_equals_cstring(obj, cb->string)) { cb->number = idx; @@ -191,8 +181,7 @@ xbps_register_repository(const char *uri) prop_array_t array = NULL; prop_object_t obj; - if (uri == NULL) - return false; + assert(uri != NULL); /* First check if we have the repository plist file. */ dict = prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH); @@ -225,9 +214,11 @@ xbps_register_repository(const char *uri) } else { /* Append into the array, the plist file exists. */ array = prop_dictionary_get(dict, "repository-list"); - if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) + if (array == NULL) return false; + assert(prop_object_type(array) == PROP_TYPE_ARRAY); + /* It seems that this object is already there */ if (xbps_find_string_in_array(array, uri)) { errno = EEXIST; @@ -265,17 +256,18 @@ xbps_unregister_repository(const char *uri) struct callback_args *cb; bool done = false; - if (uri == NULL) - return false; + assert(uri != NULL); dict = prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH); if (dict == NULL) return false; array = prop_dictionary_get(dict, "repository-list"); - if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) + if (array == NULL) return false; + assert(prop_object_type(array) == PROP_TYPE_ARRAY); + cb = malloc(sizeof(*cb)); if (cb == NULL) { errno = ENOMEM; @@ -286,7 +278,7 @@ xbps_unregister_repository(const char *uri) cb->number = -1; done = xbps_callback_array_iter_in_dict(dict, "repository-list", - xbps_remove_obj_from_array, cb); + xbps_remove_string_from_array, cb); if (done && cb->number >= 0) { /* Found, remove it. */ prop_array_remove(array, cb->number); @@ -314,7 +306,8 @@ xbps_show_pkg_info(prop_dictionary_t dict) const char *sep = NULL; bool rundeps = false; - if (dict == NULL || prop_dictionary_count(dict) == 0) + assert(dict != NULL); + if (prop_dictionary_count(dict) == 0) return; iter = prop_dictionary_iterator(dict); @@ -365,8 +358,7 @@ xbps_show_pkg_info_from_repolist(prop_object_t obj, void *arg, bool *loop_done) const char *repofile, *repoloc; char plist[PATH_MAX]; - if (prop_object_type(obj) != PROP_TYPE_STRING) - return false; + assert(prop_object_type(obj) == PROP_TYPE_STRING); /* Get the location */ repofile = prop_string_cstring_nocopy(obj); @@ -403,8 +395,7 @@ xbps_list_pkgs_in_dict(prop_object_t obj, void *arg, bool *loop_done) { const char *pkgname, *version, *short_desc; - if (prop_object_type(obj) != PROP_TYPE_DICTIONARY) - return false; + assert(prop_object_type(obj) == PROP_TYPE_DICTIONARY); prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); prop_dictionary_get_cstring_nocopy(obj, "version", &version); @@ -423,8 +414,7 @@ xbps_list_strings_in_array2(prop_object_t obj, void *arg, bool *loop_done) static uint16_t count; const char *sep; - if (prop_object_type(obj) != PROP_TYPE_STRING) - return false; + assert(prop_object_type(obj) == PROP_TYPE_STRING); if (arg == NULL) { sep = "\n\t"; @@ -439,15 +429,16 @@ xbps_list_strings_in_array2(prop_object_t obj, void *arg, bool *loop_done) printf("%s%s", prop_string_cstring_nocopy(obj), sep); count++; + return true; } bool xbps_list_strings_in_array(prop_object_t obj, void *arg, bool *loop_done) { - if (prop_object_type(obj) != PROP_TYPE_STRING) - return false; + assert(prop_object_type(obj) == PROP_TYPE_STRING); printf("%s\n", prop_string_cstring_nocopy(obj)); + return true; } diff --git a/utils/plist.h b/utils/plist.h index 050a8b110c..0281e6bffc 100644 --- a/utils/plist.h +++ b/utils/plist.h @@ -130,7 +130,7 @@ void xbps_show_pkg_info(prop_dictionary_t); bool xbps_list_pkgs_in_dict(prop_object_t, void *, bool *); bool xbps_list_strings_in_array(prop_object_t, void *, bool *); bool xbps_list_strings_in_array2(prop_object_t, void *, bool *); -bool xbps_remove_obj_from_array(prop_object_t, void *, bool *); +bool xbps_remove_string_from_array(prop_object_t, void *, bool *); bool xbps_show_pkg_info_from_repolist(prop_object_t obj, void *, bool *); #endif /* !_XBPS_PLIST_H_ */ diff --git a/utils/xbps-bin.c b/utils/xbps-bin.c index e838185524..b41b43c10f 100644 --- a/utils/xbps-bin.c +++ b/utils/xbps-bin.c @@ -69,8 +69,7 @@ usage(void) static bool pkgindex_getinfo(prop_dictionary_t dict, repo_info_t *ri) { - if (dict == NULL || ri == NULL) - return false; + assert(dict != NULL || ri != NULL); if (!prop_dictionary_get_cstring_nocopy(dict, "pkgindex-version", &ri->index_version)) diff --git a/utils/xbps-cmpver.c b/utils/xbps-cmpver.c index c2e28ce6f2..050d8ed1cf 100644 --- a/utils/xbps-cmpver.c +++ b/utils/xbps-cmpver.c @@ -5,6 +5,7 @@ */ #include #include +#include "xbps_api.h" static int chkchr(const char *ch) { @@ -57,10 +58,8 @@ int chkpkg(const char *a0, const char *b0) char *a = strrchr(a0, '-'); char *b = strrchr(b0, '-'); - if (a == NULL || b== NULL) { - fprintf(stderr, "Invalid package names\n"); - return 0; - } + assert(a != NULL || b != NULL); + return chkver(a+1, b+1); } diff --git a/utils/xbps-pkgdb.c b/utils/xbps-pkgdb.c index 7aab1c2ace..502d79779f 100644 --- a/utils/xbps-pkgdb.c +++ b/utils/xbps-pkgdb.c @@ -48,13 +48,11 @@ make_dict_from_pkg(pkg_data_t *pkg) { prop_dictionary_t dict; - if (pkg == NULL || pkg->pkgname == NULL || pkg->version == NULL || - pkg->short_desc == NULL) - return NULL; + assert(pkg != NULL || pkg->pkgname != NULL); + assert(pkg->version != NULL || pkg->short_desc != NULL); dict = prop_dictionary_create(); - if (dict == NULL) - return NULL; + assert(dict != NULL); prop_dictionary_set_cstring_nocopy(dict, "pkgname", pkg->pkgname); prop_dictionary_set_cstring_nocopy(dict, "version", pkg->version); @@ -69,25 +67,16 @@ register_pkg(prop_dictionary_t dict, pkg_data_t *pkg, const char *dbfile) prop_dictionary_t pkgdict; prop_array_t array; - if (dict == NULL || pkg == NULL || dbfile == NULL) { - printf("%s: NULL dict/pkg/dbfile\n", __func__); - exit(1); - } - + assert(dict != NULL || pkg != NULL || dbfile != NULL); pkgdict = make_dict_from_pkg(pkg); - if (pkgdict == NULL) { - printf("%s: NULL pkgdict\n", __func__); - exit(1); - } - + assert(pkgdict != NULL); array = prop_dictionary_get(dict, "packages"); - if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) { - printf("%s: NULL or incorrect array type\n", __func__); - exit(1); - } + assert(array != NULL); + assert(prop_object_type(array) == PROP_TYPE_ARRAY); if (!xbps_add_obj_to_array(array, pkgdict)) { - printf("ERROR: couldn't register package in database!\n"); + printf("ERROR: couldn't register '%s-%s' in database!\n", + pkg->pkgname, pkg->version); exit(1); } @@ -104,22 +93,12 @@ unregister_pkg(prop_dictionary_t dict, const char *pkgname, const char *dbfile) int i = 0; bool found = false; - if (dict == NULL || pkgname == NULL) { - printf("%s: NULL dict/pkgname\n", __func__); - exit(1); - } - + assert(dict != NULL || pkgname != NULL); array = prop_dictionary_get(dict, "packages"); - if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) { - printf("%s: NULL or incorrect array type\n", __func__); - exit(1); - } - + assert(array != NULL); + assert(prop_object_type(array) == PROP_TYPE_ARRAY); iter = prop_array_iterator(array); - if (iter == NULL) { - printf("%s: NULL iter\n", __func__); - exit(1); - } + assert(iter != NULL); /* Iterate over the array of dictionaries to find its index. */ while ((obj = prop_object_iterator_next(iter))) { @@ -149,10 +128,7 @@ unregister_pkg(prop_dictionary_t dict, const char *pkgname, const char *dbfile) static void write_plist_file(prop_dictionary_t dict, const char *file) { - if (dict == NULL || file == NULL) { - printf("=> ERROR: couldn't write to database file.\n"); - exit(1); - } + assert(dict != NULL || file != NULL); if (!prop_dictionary_externalize_to_file(dict, file)) { prop_object_release(dict); @@ -228,15 +204,13 @@ main(int argc, char **argv) pkg.version = argv[3]; pkg.short_desc = argv[4]; pkgdict = make_dict_from_pkg(&pkg); - if (pkgdict == NULL) { - printf("=> ERROR: couldn't register pkg\n"); - exit(1); - } + assert(pkgdict != NULL); /* Add pkg dictionary into array. */ dbarray = prop_array_create(); if (!xbps_add_obj_to_array(dbarray, pkgdict)) { - printf("=> ERROR: couldn't register pkg\n"); + printf("=> ERROR: couldn't register %s-%s\n", + pkg.pkgname, pkg.version); exit(1); } @@ -244,7 +218,8 @@ main(int argc, char **argv) dbdict = prop_dictionary_create(); if (!xbps_add_obj_to_dict(dbdict, dbarray, "packages")) { - printf("=> ERROR: couldn't register pkg\n"); + printf("=> ERROR: couldn't register %s-%s\n", + pkg.pkgname, pkg.version); exit(1); } @@ -321,7 +296,8 @@ main(int argc, char **argv) exit(1); } if (!prop_dictionary_externalize_to_file(dbdict, argv[2])) { - printf("=> ERROR: couldn't write new plist file\n"); + printf("=> ERROR: couldn't write new plist file " + "(%s)\n", strerror(errno)); exit(1); } diff --git a/utils/xbps_api.h b/utils/xbps_api.h index 1cd3f8014a..01e49688e1 100644 --- a/utils/xbps_api.h +++ b/utils/xbps_api.h @@ -28,6 +28,8 @@ #include #include +// #define NDEBUG +#include #include