Use xbps_callback_array_iter_in_repolist() and removed dup code.

--HG--
extra : convert_revision : 7eb9c1b3df3fc7fde2ff61ac0cce1d635c3fef18
This commit is contained in:
Juan RP 2009-02-14 02:26:11 +01:00
parent 90fd3828e6
commit 83ed100fcf
4 changed files with 16 additions and 57 deletions

View file

@ -43,12 +43,9 @@ typedef struct repository_info {
} repo_info_t; } repo_info_t;
static bool sanitize_localpath(char *, const char *); static bool sanitize_localpath(char *, const char *);
static prop_dictionary_t getrepolist_dict(const char *);
static bool pkgindex_getinfo(prop_dictionary_t, repo_info_t *); static bool pkgindex_getinfo(prop_dictionary_t, repo_info_t *);
static void usage(void); static void usage(void);
static char *plist;
static void static void
usage(void) usage(void)
{ {
@ -101,28 +98,6 @@ pkgindex_getinfo(prop_dictionary_t dict, repo_info_t *ri)
return true; return true;
} }
static prop_dictionary_t
getrepolist_dict(const char *root)
{
prop_dictionary_t dict;
xbps_set_rootdir(root);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
if (plist == NULL)
exit(EXIT_FAILURE);
dict = prop_dictionary_internalize_from_file(plist);
if (dict == NULL) {
printf("ERROR: cannot find repository plist file (%s).\n",
strerror(errno));
free(plist);
exit(EXIT_FAILURE);
}
return dict;
}
static bool static bool
sanitize_localpath(char *buf, const char *path) sanitize_localpath(char *buf, const char *path)
{ {
@ -169,7 +144,7 @@ main(int argc, char **argv)
{ {
prop_dictionary_t dict; prop_dictionary_t dict;
repo_info_t *rinfo = NULL; repo_info_t *rinfo = NULL;
char dpkgidx[PATH_MAX], *root = NULL; char dpkgidx[PATH_MAX], *plist, *root = NULL;
int c, rv = 0; int c, rv = 0;
while ((c = getopt(argc, argv, "r:")) != -1) { while ((c = getopt(argc, argv, "r:")) != -1) {
@ -249,11 +224,8 @@ main(int argc, char **argv)
if (argc != 1) if (argc != 1)
usage(); usage();
dict = getrepolist_dict(root); (void)xbps_callback_array_iter_in_repolist(
(void)xbps_callback_array_iter_in_dict(dict, list_strings_in_array, NULL);
"repository-list", list_strings_in_array, NULL);
prop_object_release(dict);
free(plist);
} else if ((strcasecmp(argv[0], "rm") == 0) || } else if ((strcasecmp(argv[0], "rm") == 0) ||
(strcasecmp(argv[0], "remove") == 0)) { (strcasecmp(argv[0], "remove") == 0)) {
@ -279,23 +251,16 @@ main(int argc, char **argv)
if (argc != 2) if (argc != 2)
usage(); usage();
dict = getrepolist_dict(root); (void)xbps_callback_array_iter_in_repolist(
(void)xbps_callback_array_iter_in_dict(dict, search_string_in_pkgs, argv[1]);
"repository-list", search_string_in_pkgs, argv[1]);
prop_object_release(dict);
free(plist);
} else if (strcasecmp(argv[0], "show") == 0) { } else if (strcasecmp(argv[0], "show") == 0) {
/* Shows info about a binary package. */ /* Shows info about a binary package. */
if (argc != 2) if (argc != 2)
usage(); usage();
dict = getrepolist_dict(root); rv = xbps_callback_array_iter_in_repolist(
rv = xbps_callback_array_iter_in_dict(dict, "repository-list",
show_pkg_info_from_repolist, argv[1]); show_pkg_info_from_repolist, argv[1]);
prop_object_release(dict);
free(plist);
if (rv == 0 && errno == ENOENT) { if (rv == 0 && errno == ENOENT) {
printf("Unable to locate package '%s' from " printf("Unable to locate package '%s' from "
"repository pool.\n", argv[1]); "repository pool.\n", argv[1]);

View file

@ -47,8 +47,7 @@ xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *,
int (*fn)(prop_object_t, void *, bool *), int (*fn)(prop_object_t, void *, bool *),
void *); void *);
int int
xbps_callback_array_iter_in_repolist(const char *, xbps_callback_array_iter_in_repolist(int (*fn)(prop_object_t, void *, bool *),
int (*fn)(prop_object_t, void *, bool *),
void *); void *);
/* /*
* Finds a package's dictionary into the main dictionary. * Finds a package's dictionary into the main dictionary.

View file

@ -70,7 +70,6 @@ int
xbps_install_binary_pkg(const char *pkgname, const char *destdir) xbps_install_binary_pkg(const char *pkgname, const char *destdir)
{ {
struct cbargs cb; struct cbargs cb;
char *plist;
int rv = 0; int rv = 0;
assert(pkgname != NULL); assert(pkgname != NULL);
@ -80,22 +79,14 @@ xbps_install_binary_pkg(const char *pkgname, const char *destdir)
} else } else
destdir = "NOTSET"; destdir = "NOTSET";
/*
* Get the dictionary with the list of registered repositories.
*/
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
if (plist == NULL)
return EINVAL;
cb.pkgname = pkgname; cb.pkgname = pkgname;
cb.destdir = destdir; cb.destdir = destdir;
/* /*
* Iterate over the repository pool and find out if we have * Iterate over the repository pool and find out if we have
* all available binary packages. * all available binary packages.
*/ */
rv = xbps_callback_array_iter_in_repolist(plist, rv = xbps_callback_array_iter_in_repolist(install_binpkg_repo_cb,
install_binpkg_repo_cb, (void *)&cb); (void *)&cb);
free(plist);
return rv; return rv;
} }

View file

@ -66,16 +66,19 @@ xbps_add_obj_to_array(prop_array_t array, prop_object_t obj)
} }
int int
xbps_callback_array_iter_in_repolist(const char *plist, xbps_callback_array_iter_in_repolist(int (*fn)(prop_object_t, void *, bool *),
int (*fn)(prop_object_t, void *, bool *),
void *arg) void *arg)
{ {
prop_dictionary_t repolistd; prop_dictionary_t repolistd;
char *plist;
int rv = 0; int rv = 0;
assert(plist != NULL);
assert(fn != NULL); assert(fn != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
if (plist == NULL)
return EINVAL;
/* /*
* Get the dictionary with the list of registered repositories. * Get the dictionary with the list of registered repositories.
*/ */
@ -91,6 +94,7 @@ xbps_callback_array_iter_in_repolist(const char *plist,
rv = xbps_callback_array_iter_in_dict(repolistd, "repository-list", rv = xbps_callback_array_iter_in_dict(repolistd, "repository-list",
fn, arg); fn, arg);
prop_object_release(repolistd); prop_object_release(repolistd);
free(plist);
return rv; return rv;
} }