xbps-query: added support for -p/--property argument in search mode.

See the NEWS file for more information.
This commit is contained in:
Juan RP 2014-03-21 13:19:40 +01:00
parent f2868856ee
commit cb66092fbc
4 changed files with 70 additions and 26 deletions

9
NEWS
View File

@ -1,5 +1,14 @@
xbps-0.34 (???): xbps-0.34 (???):
* xbps-query(8): search mode now accepts -p/--property argument to match patterns
against the stored package object in repositories. Any object stored in the repository
index can be used to be matched against to; the posibilities are countless:
- find out what packages are mantained by a person
- find out what packages are built at a specific date
- find out what packages are linked to a specific shared library
- etc etc etc
* xbps-query(8): in local mode now prints all objects stored on its pkgdb entry * xbps-query(8): in local mode now prints all objects stored on its pkgdb entry
and its metaplist. and its metaplist.

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2009-2013 Juan Romero Pardines. * Copyright (c) 2009-2014 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -68,6 +68,6 @@ int list_pkgs_pkgdb(struct xbps_handle *);
int repo_list(struct xbps_handle *); int repo_list(struct xbps_handle *);
/* from search.c */ /* from search.c */
int repo_search(struct xbps_handle *, int, char **); int repo_search(struct xbps_handle *, int, char **, const char *);
#endif /* !_XBPS_QUERY_DEFS_H_ */ #endif /* !_XBPS_QUERY_DEFS_H_ */

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2008-2013 Juan Romero Pardines. * Copyright (c) 2008-2014 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -42,6 +42,7 @@ usage(bool fail)
" -c --cachedir <dir> Full path to cachedir\n" " -c --cachedir <dir> Full path to cachedir\n"
" -d --debug Debug mode shown to stderr\n" " -d --debug Debug mode shown to stderr\n"
" -h --help Print help usage\n" " -h --help Print help usage\n"
" -p --property PROP,... Show properties for PKGNAME\n"
" -R --repository Enable repository mode. This mode explicitly\n" " -R --repository Enable repository mode. This mode explicitly\n"
" looks for packages in repositories.\n" " looks for packages in repositories.\n"
" --repository=<url> Enable repository mode and explicitly add the\n" " --repository=<url> Enable repository mode and explicitly add the\n"
@ -60,7 +61,6 @@ usage(bool fail)
" -o --ownedby PATTERN(s) Search for packages owning PATTERN(s)\n" " -o --ownedby PATTERN(s) Search for packages owning PATTERN(s)\n"
" -s --search PATTERN(s) Search for packages matching PATTERN(s)\n" " -s --search PATTERN(s) Search for packages matching PATTERN(s)\n"
" -f --files Show files for PKGNAME\n" " -f --files Show files for PKGNAME\n"
" -p --property PROP,... Show properties for PKGNAME\n"
" -x --deps Show dependencies for PKGNAME (set it twice for a full dependency tree)\n" " -x --deps Show dependencies for PKGNAME (set it twice for a full dependency tree)\n"
" -X --revdeps Show reverse dependencies for PKGNAME\n"); " -X --revdeps Show reverse dependencies for PKGNAME\n");
@ -235,7 +235,7 @@ main(int argc, char **argv)
} else if (search) { } else if (search) {
/* search mode */ /* search mode */
rv = repo_search(&xh, argc - optind, argv + optind); rv = repo_search(&xh, argc - optind, argv + optind, props);
} else if (show || show_prop) { } else if (show || show_prop) {
/* show mode */ /* show mode */

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2008-2013 Juan Romero Pardines. * Copyright (c) 2008-2014 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -44,8 +44,9 @@
struct search_data { struct search_data {
int npatterns; int npatterns;
char **patterns;
int maxcols; int maxcols;
char **patterns;
const char *prop, *repourl;
xbps_array_t results; xbps_array_t results;
}; };
@ -100,13 +101,17 @@ search_array_cb(struct xbps_handle *xhp _unused,
void *arg, void *arg,
bool *done _unused) bool *done _unused)
{ {
xbps_object_t obj2;
struct search_data *sd = arg; struct search_data *sd = arg;
const char *pkgver, *desc; const char *pkgver, *desc, *str;
int x;
if (sd->prop == NULL) {
/* no prop set, match on pkgver/short_desc objects */
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
xbps_dictionary_get_cstring_nocopy(obj, "short_desc", &desc); xbps_dictionary_get_cstring_nocopy(obj, "short_desc", &desc);
for (int x = 0; x < sd->npatterns; x++) { for (x = 0; x < sd->npatterns; x++) {
bool vpkgfound = false; bool vpkgfound = false;
if (xbps_match_virtual_pkg_in_dict(obj, sd->patterns[x], false)) if (xbps_match_virtual_pkg_in_dict(obj, sd->patterns[x], false))
@ -121,30 +126,60 @@ search_array_cb(struct xbps_handle *xhp _unused,
} }
return 0; return 0;
} }
/* prop set, match on prop object instead */
obj2 = xbps_dictionary_get(obj, sd->prop);
if (xbps_object_type(obj2) == XBPS_TYPE_ARRAY) {
/* property is an array */
for (unsigned int i = 0; i < xbps_array_count(obj2); i++) {
xbps_array_get_cstring_nocopy(obj2, i, &str);
for (x = 0; x < sd->npatterns; x++) {
if ((strcasestr(str, sd->patterns[x])) ||
(fnmatch(sd->patterns[x], str, FNM_PERIOD)) == 0) {
xbps_dictionary_get_cstring_nocopy(obj,
"pkgver", &pkgver);
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
}
}
}
} else if (xbps_object_type(obj2) == XBPS_TYPE_BOOL) {
/* property is a bool */
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
printf("%s: true (%s)\n", pkgver, sd->repourl);
} else if (xbps_object_type(obj2) == XBPS_TYPE_STRING) {
/* property is a string */
str = xbps_string_cstring_nocopy(obj2);
for (x = 0; x < sd->npatterns; x++) {
if (strcasestr(str, sd->patterns[x])) {
xbps_dictionary_get_cstring_nocopy(obj,
"pkgver", &pkgver);
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
}
}
}
return 0;
}
static int static int
search_pkgs_cb(struct xbps_repo *repo, void *arg, bool *done _unused) search_pkgs_cb(struct xbps_repo *repo, void *arg, bool *done _unused)
{ {
xbps_array_t allkeys; xbps_array_t allkeys;
struct search_data *sd = arg; struct search_data *sd = arg;
int rv;
if (repo->idx == NULL) if (repo->idx == NULL)
return 0; return 0;
sd->repourl = repo->uri;
allkeys = xbps_dictionary_all_keys(repo->idx); allkeys = xbps_dictionary_all_keys(repo->idx);
rv = xbps_array_foreach_cb(repo->xhp, allkeys, repo->idx, search_array_cb, sd); return xbps_array_foreach_cb(repo->xhp, allkeys, repo->idx, search_array_cb, sd);
xbps_object_release(allkeys);
return rv;
} }
int int
repo_search(struct xbps_handle *xhp, int npatterns, char **patterns) repo_search(struct xbps_handle *xhp, int npatterns, char **patterns, const char *prop)
{ {
struct search_data sd; struct search_data sd;
int rv; int rv;
sd.prop = prop;
sd.npatterns = npatterns; sd.npatterns = npatterns;
sd.patterns = patterns; sd.patterns = patterns;
sd.maxcols = get_maxcols(); sd.maxcols = get_maxcols();
@ -155,7 +190,7 @@ repo_search(struct xbps_handle *xhp, int npatterns, char **patterns)
fprintf(stderr, "Failed to initialize rpool: %s\n", fprintf(stderr, "Failed to initialize rpool: %s\n",
strerror(rv)); strerror(rv));
if (xbps_array_count(sd.results)) { if (!prop && xbps_array_count(sd.results)) {
print_results(xhp, &sd); print_results(xhp, &sd);
xbps_object_release(sd.results); xbps_object_release(sd.results);
} }