Replace some if conditionals with asserts where appropiate.
--HG-- extra : convert_revision : 991989e77b31d84f9aab2e27a5366c18a78407ae
This commit is contained in:
parent
d3b02d6c95
commit
5722c8aca1
6 changed files with 53 additions and 86 deletions
|
@ -42,8 +42,7 @@ bool
|
||||||
xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj,
|
xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj,
|
||||||
const char *key)
|
const char *key)
|
||||||
{
|
{
|
||||||
if (dict == NULL || obj == NULL || key == NULL)
|
assert(dict != NULL || obj != NULL || key != NULL);
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!prop_dictionary_set(dict, key, obj))
|
if (!prop_dictionary_set(dict, key, obj))
|
||||||
return false;
|
return false;
|
||||||
|
@ -55,8 +54,7 @@ xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj,
|
||||||
bool
|
bool
|
||||||
xbps_add_obj_to_array(prop_array_t array, prop_object_t obj)
|
xbps_add_obj_to_array(prop_array_t array, prop_object_t obj)
|
||||||
{
|
{
|
||||||
if (array == NULL || obj == NULL)
|
assert(array != NULL || obj != NULL);
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!prop_array_add(array, obj)) {
|
if (!prop_array_add(array, obj)) {
|
||||||
prop_object_release(array);
|
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;
|
bool run, ret, cbloop_done;
|
||||||
|
|
||||||
run = ret = cbloop_done = false;
|
run = ret = cbloop_done = false;
|
||||||
|
assert(func != NULL);
|
||||||
if (func == NULL)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
cbloop_done = false;
|
|
||||||
|
|
||||||
iter = xbps_get_array_iter_from_dict(dict, key);
|
iter = xbps_get_array_iter_from_dict(dict, key);
|
||||||
if (iter == NULL)
|
if (iter == NULL)
|
||||||
|
@ -106,8 +100,7 @@ xbps_find_pkg_in_dict(prop_dictionary_t dict, const char *pkgname)
|
||||||
prop_object_t obj;
|
prop_object_t obj;
|
||||||
const char *dpkgn;
|
const char *dpkgn;
|
||||||
|
|
||||||
if (pkgname == NULL)
|
assert(pkgname != NULL);
|
||||||
return NULL;
|
|
||||||
|
|
||||||
iter = xbps_get_array_iter_from_dict(dict, "packages");
|
iter = xbps_get_array_iter_from_dict(dict, "packages");
|
||||||
if (iter == NULL)
|
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_iterator_t iter;
|
||||||
prop_object_t obj;
|
prop_object_t obj;
|
||||||
|
|
||||||
if (array == NULL || val == NULL)
|
assert(array != NULL || val != NULL);
|
||||||
return false;
|
|
||||||
|
|
||||||
iter = prop_array_iterator(array);
|
iter = prop_array_iterator(array);
|
||||||
if (iter == NULL)
|
if (iter == NULL)
|
||||||
|
@ -154,8 +146,7 @@ xbps_get_array_iter_from_dict(prop_dictionary_t dict, const char *key)
|
||||||
{
|
{
|
||||||
prop_array_t array;
|
prop_array_t array;
|
||||||
|
|
||||||
if (dict == NULL || key == NULL)
|
assert(dict != NULL || key != NULL);
|
||||||
return NULL;
|
|
||||||
|
|
||||||
array = prop_dictionary_get(dict, key);
|
array = prop_dictionary_get(dict, key);
|
||||||
if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY)
|
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
|
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;
|
static int idx;
|
||||||
struct callback_args *cb = arg;
|
struct callback_args *cb = arg;
|
||||||
|
|
||||||
if (prop_object_type(obj) != PROP_TYPE_STRING)
|
assert(prop_object_type(obj) == PROP_TYPE_STRING);
|
||||||
return false;
|
|
||||||
|
|
||||||
if (prop_string_equals_cstring(obj, cb->string)) {
|
if (prop_string_equals_cstring(obj, cb->string)) {
|
||||||
cb->number = idx;
|
cb->number = idx;
|
||||||
|
@ -191,8 +181,7 @@ xbps_register_repository(const char *uri)
|
||||||
prop_array_t array = NULL;
|
prop_array_t array = NULL;
|
||||||
prop_object_t obj;
|
prop_object_t obj;
|
||||||
|
|
||||||
if (uri == NULL)
|
assert(uri != NULL);
|
||||||
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(XBPS_REPOLIST_PATH);
|
dict = prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH);
|
||||||
|
@ -225,9 +214,11 @@ xbps_register_repository(const char *uri)
|
||||||
} 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 || prop_object_type(array) != PROP_TYPE_ARRAY)
|
if (array == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
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;
|
||||||
|
@ -265,17 +256,18 @@ xbps_unregister_repository(const char *uri)
|
||||||
struct callback_args *cb;
|
struct callback_args *cb;
|
||||||
bool done = false;
|
bool done = false;
|
||||||
|
|
||||||
if (uri == NULL)
|
assert(uri != NULL);
|
||||||
return false;
|
|
||||||
|
|
||||||
dict = prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH);
|
dict = prop_dictionary_internalize_from_file(XBPS_REPOLIST_PATH);
|
||||||
if (dict == NULL)
|
if (dict == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
array = prop_dictionary_get(dict, "repository-list");
|
array = prop_dictionary_get(dict, "repository-list");
|
||||||
if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY)
|
if (array == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
assert(prop_object_type(array) == PROP_TYPE_ARRAY);
|
||||||
|
|
||||||
cb = malloc(sizeof(*cb));
|
cb = malloc(sizeof(*cb));
|
||||||
if (cb == NULL) {
|
if (cb == NULL) {
|
||||||
errno = ENOMEM;
|
errno = ENOMEM;
|
||||||
|
@ -286,7 +278,7 @@ xbps_unregister_repository(const char *uri)
|
||||||
cb->number = -1;
|
cb->number = -1;
|
||||||
|
|
||||||
done = xbps_callback_array_iter_in_dict(dict, "repository-list",
|
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) {
|
if (done && cb->number >= 0) {
|
||||||
/* Found, remove it. */
|
/* Found, remove it. */
|
||||||
prop_array_remove(array, cb->number);
|
prop_array_remove(array, cb->number);
|
||||||
|
@ -314,7 +306,8 @@ xbps_show_pkg_info(prop_dictionary_t dict)
|
||||||
const char *sep = NULL;
|
const char *sep = NULL;
|
||||||
bool rundeps = false;
|
bool rundeps = false;
|
||||||
|
|
||||||
if (dict == NULL || prop_dictionary_count(dict) == 0)
|
assert(dict != NULL);
|
||||||
|
if (prop_dictionary_count(dict) == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
iter = prop_dictionary_iterator(dict);
|
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;
|
const char *repofile, *repoloc;
|
||||||
char plist[PATH_MAX];
|
char plist[PATH_MAX];
|
||||||
|
|
||||||
if (prop_object_type(obj) != PROP_TYPE_STRING)
|
assert(prop_object_type(obj) == PROP_TYPE_STRING);
|
||||||
return false;
|
|
||||||
|
|
||||||
/* Get the location */
|
/* Get the location */
|
||||||
repofile = prop_string_cstring_nocopy(obj);
|
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;
|
const char *pkgname, *version, *short_desc;
|
||||||
|
|
||||||
if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
|
assert(prop_object_type(obj) == PROP_TYPE_DICTIONARY);
|
||||||
return false;
|
|
||||||
|
|
||||||
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
|
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
|
||||||
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
|
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;
|
static uint16_t count;
|
||||||
const char *sep;
|
const char *sep;
|
||||||
|
|
||||||
if (prop_object_type(obj) != PROP_TYPE_STRING)
|
assert(prop_object_type(obj) == PROP_TYPE_STRING);
|
||||||
return false;
|
|
||||||
|
|
||||||
if (arg == NULL) {
|
if (arg == NULL) {
|
||||||
sep = "\n\t";
|
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);
|
printf("%s%s", prop_string_cstring_nocopy(obj), sep);
|
||||||
count++;
|
count++;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
xbps_list_strings_in_array(prop_object_t obj, void *arg, bool *loop_done)
|
xbps_list_strings_in_array(prop_object_t obj, void *arg, bool *loop_done)
|
||||||
{
|
{
|
||||||
if (prop_object_type(obj) != PROP_TYPE_STRING)
|
assert(prop_object_type(obj) == PROP_TYPE_STRING);
|
||||||
return false;
|
|
||||||
|
|
||||||
printf("%s\n", prop_string_cstring_nocopy(obj));
|
printf("%s\n", prop_string_cstring_nocopy(obj));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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_pkgs_in_dict(prop_object_t, void *, bool *);
|
||||||
bool xbps_list_strings_in_array(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_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 *);
|
bool xbps_show_pkg_info_from_repolist(prop_object_t obj, void *, bool *);
|
||||||
|
|
||||||
#endif /* !_XBPS_PLIST_H_ */
|
#endif /* !_XBPS_PLIST_H_ */
|
||||||
|
|
|
@ -69,8 +69,7 @@ usage(void)
|
||||||
static bool
|
static bool
|
||||||
pkgindex_getinfo(prop_dictionary_t dict, repo_info_t *ri)
|
pkgindex_getinfo(prop_dictionary_t dict, repo_info_t *ri)
|
||||||
{
|
{
|
||||||
if (dict == NULL || ri == NULL)
|
assert(dict != NULL || ri != NULL);
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!prop_dictionary_get_cstring_nocopy(dict,
|
if (!prop_dictionary_get_cstring_nocopy(dict,
|
||||||
"pkgindex-version", &ri->index_version))
|
"pkgindex-version", &ri->index_version))
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "xbps_api.h"
|
||||||
|
|
||||||
static int chkchr(const char *ch)
|
static int chkchr(const char *ch)
|
||||||
{
|
{
|
||||||
|
@ -57,10 +58,8 @@ int chkpkg(const char *a0, const char *b0)
|
||||||
char *a = strrchr(a0, '-');
|
char *a = strrchr(a0, '-');
|
||||||
char *b = strrchr(b0, '-');
|
char *b = strrchr(b0, '-');
|
||||||
|
|
||||||
if (a == NULL || b== NULL) {
|
assert(a != NULL || b != NULL);
|
||||||
fprintf(stderr, "Invalid package names\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return chkver(a+1, b+1);
|
return chkver(a+1, b+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,13 +48,11 @@ make_dict_from_pkg(pkg_data_t *pkg)
|
||||||
{
|
{
|
||||||
prop_dictionary_t dict;
|
prop_dictionary_t dict;
|
||||||
|
|
||||||
if (pkg == NULL || pkg->pkgname == NULL || pkg->version == NULL ||
|
assert(pkg != NULL || pkg->pkgname != NULL);
|
||||||
pkg->short_desc == NULL)
|
assert(pkg->version != NULL || pkg->short_desc != NULL);
|
||||||
return NULL;
|
|
||||||
|
|
||||||
dict = prop_dictionary_create();
|
dict = prop_dictionary_create();
|
||||||
if (dict == NULL)
|
assert(dict != NULL);
|
||||||
return NULL;
|
|
||||||
|
|
||||||
prop_dictionary_set_cstring_nocopy(dict, "pkgname", pkg->pkgname);
|
prop_dictionary_set_cstring_nocopy(dict, "pkgname", pkg->pkgname);
|
||||||
prop_dictionary_set_cstring_nocopy(dict, "version", pkg->version);
|
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_dictionary_t pkgdict;
|
||||||
prop_array_t array;
|
prop_array_t array;
|
||||||
|
|
||||||
if (dict == NULL || pkg == NULL || dbfile == NULL) {
|
assert(dict != NULL || pkg != NULL || dbfile != NULL);
|
||||||
printf("%s: NULL dict/pkg/dbfile\n", __func__);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
pkgdict = make_dict_from_pkg(pkg);
|
pkgdict = make_dict_from_pkg(pkg);
|
||||||
if (pkgdict == NULL) {
|
assert(pkgdict != NULL);
|
||||||
printf("%s: NULL pkgdict\n", __func__);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
array = prop_dictionary_get(dict, "packages");
|
array = prop_dictionary_get(dict, "packages");
|
||||||
if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) {
|
assert(array != NULL);
|
||||||
printf("%s: NULL or incorrect array type\n", __func__);
|
assert(prop_object_type(array) == PROP_TYPE_ARRAY);
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!xbps_add_obj_to_array(array, pkgdict)) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,22 +93,12 @@ unregister_pkg(prop_dictionary_t dict, const char *pkgname, const char *dbfile)
|
||||||
int i = 0;
|
int i = 0;
|
||||||
bool found = false;
|
bool found = false;
|
||||||
|
|
||||||
if (dict == NULL || pkgname == NULL) {
|
assert(dict != NULL || pkgname != NULL);
|
||||||
printf("%s: NULL dict/pkgname\n", __func__);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
array = prop_dictionary_get(dict, "packages");
|
array = prop_dictionary_get(dict, "packages");
|
||||||
if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) {
|
assert(array != NULL);
|
||||||
printf("%s: NULL or incorrect array type\n", __func__);
|
assert(prop_object_type(array) == PROP_TYPE_ARRAY);
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
iter = prop_array_iterator(array);
|
iter = prop_array_iterator(array);
|
||||||
if (iter == NULL) {
|
assert(iter != NULL);
|
||||||
printf("%s: NULL iter\n", __func__);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Iterate over the array of dictionaries to find its index. */
|
/* Iterate over the array of dictionaries to find its index. */
|
||||||
while ((obj = prop_object_iterator_next(iter))) {
|
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
|
static void
|
||||||
write_plist_file(prop_dictionary_t dict, const char *file)
|
write_plist_file(prop_dictionary_t dict, const char *file)
|
||||||
{
|
{
|
||||||
if (dict == NULL || file == NULL) {
|
assert(dict != NULL || file != NULL);
|
||||||
printf("=> ERROR: couldn't write to database file.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!prop_dictionary_externalize_to_file(dict, file)) {
|
if (!prop_dictionary_externalize_to_file(dict, file)) {
|
||||||
prop_object_release(dict);
|
prop_object_release(dict);
|
||||||
|
@ -228,15 +204,13 @@ main(int argc, char **argv)
|
||||||
pkg.version = argv[3];
|
pkg.version = argv[3];
|
||||||
pkg.short_desc = argv[4];
|
pkg.short_desc = argv[4];
|
||||||
pkgdict = make_dict_from_pkg(&pkg);
|
pkgdict = make_dict_from_pkg(&pkg);
|
||||||
if (pkgdict == NULL) {
|
assert(pkgdict != NULL);
|
||||||
printf("=> ERROR: couldn't register pkg\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add pkg dictionary into array. */
|
/* Add pkg dictionary into array. */
|
||||||
dbarray = prop_array_create();
|
dbarray = prop_array_create();
|
||||||
if (!xbps_add_obj_to_array(dbarray, pkgdict)) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,7 +218,8 @@ main(int argc, char **argv)
|
||||||
dbdict = prop_dictionary_create();
|
dbdict = prop_dictionary_create();
|
||||||
if (!xbps_add_obj_to_dict(dbdict, dbarray,
|
if (!xbps_add_obj_to_dict(dbdict, dbarray,
|
||||||
"packages")) {
|
"packages")) {
|
||||||
printf("=> ERROR: couldn't register pkg\n");
|
printf("=> ERROR: couldn't register %s-%s\n",
|
||||||
|
pkg.pkgname, pkg.version);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +296,8 @@ main(int argc, char **argv)
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (!prop_dictionary_externalize_to_file(dbdict, argv[2])) {
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
// #define NDEBUG
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
#include <prop/proplib.h>
|
#include <prop/proplib.h>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue