From 3f7c353356ad45164692231da997a500a600ea44 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Mon, 22 Dec 2008 05:00:15 +0100 Subject: [PATCH] Fix some issues in repo handling. --HG-- extra : convert_revision : 5ec7488c8967f50b7e2dce93276accd829c541b3 --- bin/xbps-bin.c | 26 +++++++++++++++++--------- include/plist.h | 2 +- lib/plist.c | 31 ++++++++++++++++++++++--------- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/bin/xbps-bin.c b/bin/xbps-bin.c index 92b412ec3e5..898ac416425 100644 --- a/bin/xbps-bin.c +++ b/bin/xbps-bin.c @@ -114,9 +114,9 @@ getrepolist_dict(void) static const char * sanitize_localpath(const char *path) { - const char *res; - char strtmp[PATH_MAX]; + static char strtmp[PATH_MAX]; char *dirnp, *basenp, *dir, *base; + const char *res; dir = strdup(path); if (dir == NULL) @@ -124,15 +124,15 @@ sanitize_localpath(const char *path) base = strdup(path); if (base == NULL) - goto fail; + goto fail2; dirnp = dirname(dir); if (strcmp(dirnp, ".") == 0) - goto fail2; + goto fail; basenp = basename(base); if (strcmp(basenp, base) == 0) - goto fail2; + goto fail; strncpy(strtmp, dirnp, sizeof(strtmp) - 1); strtmp[sizeof(strtmp) - 1] = '\0'; @@ -143,11 +143,12 @@ sanitize_localpath(const char *path) free(dir); free(base); res = strtmp; + return res; fail: - free(dir); -fail2: free(base); +fail2: + free(dir); return NULL; } @@ -156,7 +157,8 @@ main(int argc, char **argv) { prop_dictionary_t dict; repo_info_t *rinfo = NULL; - const char *dpkgidx, *repolist; + const char *dpkgidx; + char *repolist; if (argc < 2) usage(); @@ -179,19 +181,25 @@ main(int argc, char **argv) if (dict == NULL) { printf("Directory %s does not contain any " "xbps pkgindex file.\n", dpkgidx); + free(repolist); exit(EINVAL); } rinfo = malloc(sizeof(*rinfo)); - if (rinfo == NULL) + if (rinfo == NULL) { + free(repolist); exit(ENOMEM); + } if (!pkgindex_getinfo(dict, rinfo)) { printf("'%s' is incomplete.\n", repolist); free(rinfo); + free(repolist); exit(EINVAL); } + free(repolist); + if (!xbps_register_repository(dpkgidx)) { printf("ERROR: couldn't register repository (%s)\n", strerror(errno)); diff --git a/include/plist.h b/include/plist.h index c38465965a0..c509f7c731f 100644 --- a/include/plist.h +++ b/include/plist.h @@ -147,6 +147,6 @@ 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_namedesc(prop_object_t, void *, bool *); bool xbps_search_string_in_pkgs(prop_object_t, void *, bool *); -const char *xbps_get_pkgidx_string(const char *); +char *xbps_get_pkgidx_string(const char *); #endif /* !_XBPS_PLIST_H_ */ diff --git a/lib/plist.c b/lib/plist.c index dde67f8e5da..67a6066b2a3 100644 --- a/lib/plist.c +++ b/lib/plist.c @@ -155,11 +155,10 @@ xbps_get_array_iter_from_dict(prop_dictionary_t dict, const char *key) return prop_array_iterator(array); } -const char * +char * xbps_get_pkgidx_string(const char *repofile) { - const char *res; - char plist[PATH_MAX], *len; + char plist[PATH_MAX], *len, *result; assert(repofile != NULL); @@ -171,9 +170,13 @@ xbps_get_pkgidx_string(const char *repofile) plist[sizeof(plist) - 1] = '\0'; strncat(plist, "/", sizeof(plist) - strlen(plist) - 1); strncat(plist, XBPS_PKGINDEX, sizeof(plist) - strlen(plist) - 1); - res = plist; - return res; + result = malloc(strlen(plist)); + if (result == NULL) + return NULL; + + strncpy(result, plist, strlen(plist)); + return result; } bool @@ -459,7 +462,8 @@ bool xbps_search_string_in_pkgs(prop_object_t obj, void *arg, bool *loop_done) { prop_dictionary_t dict; - const char *repofile, *plist; + const char *repofile; + char *plist; assert(prop_object_type(obj) == PROP_TYPE_STRING); @@ -473,13 +477,17 @@ xbps_search_string_in_pkgs(prop_object_t obj, void *arg, bool *loop_done) return false; dict = prop_dictionary_internalize_from_file(plist); - if (dict == NULL || prop_dictionary_count(dict) == 0) + if (dict == NULL || prop_dictionary_count(dict) == 0) { + free(plist); return false; + } printf("From %s repository ...\n", repofile); xbps_callback_array_iter_in_dict(dict, "packages", xbps_show_pkg_namedesc, arg); + free(plist); + return true; } @@ -488,7 +496,8 @@ xbps_show_pkg_info_from_repolist(prop_object_t obj, void *arg, bool *loop_done) { prop_dictionary_t dict, pkgdict; prop_string_t oloc; - const char *repofile, *repoloc, *plist; + const char *repofile, *repoloc; + char *plist; assert(prop_object_type(obj) == PROP_TYPE_STRING); @@ -501,8 +510,12 @@ xbps_show_pkg_info_from_repolist(prop_object_t obj, void *arg, bool *loop_done) return false; dict = prop_dictionary_internalize_from_file(plist); - if (dict == NULL || prop_dictionary_count(dict) == 0) + if (dict == NULL || prop_dictionary_count(dict) == 0) { + free(plist); return false; + } + + free(plist); pkgdict = xbps_find_pkg_in_dict(dict, arg); if (pkgdict == NULL) return false;