Skip to content

Commit

Permalink
bin/xbps-query: add --long for -f to show file perms, owner, size
Browse files Browse the repository at this point in the history
  • Loading branch information
classabbyamp committed Mar 15, 2023
1 parent 84e06b1 commit 74dab9b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
6 changes: 3 additions & 3 deletions bin/xbps-query/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void show_pkg_info(xbps_dictionary_t);
void show_pkg_info_one(xbps_dictionary_t, const char *);
int show_pkg_info_from_metadir(struct xbps_handle *, const char *,
const char *);
int show_pkg_files(xbps_dictionary_t);
int show_pkg_files_from_metadir(struct xbps_handle *, const char *);
int repo_show_pkg_files(struct xbps_handle *, const char *);
int show_pkg_files(xbps_dictionary_t, const struct xbps_fmt *);
int show_pkg_files_from_metadir(struct xbps_handle *, const char *, const struct xbps_fmt *);
int repo_show_pkg_files(struct xbps_handle *, const char *, const struct xbps_fmt *);
int cat_file(struct xbps_handle *, const char *, const char *);
int repo_cat_file(struct xbps_handle *, const char *, const char *);
int repo_show_pkg_info(struct xbps_handle *, const char *, const char *);
Expand Down
20 changes: 16 additions & 4 deletions bin/xbps-query/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ usage(bool fail)
" specified multiple times\n"
" --regex Use Extended Regular Expressions to match\n"
" --fulldeptree Full dependency tree for -x/--deps\n"
" --long Show permissions, ownership, and size for -f/--files\n"
" -r, --rootdir <dir> Full path to rootdir\n"
" -V, --version Show XBPS version\n"
" -v, --verbose Verbose messages\n"
Expand Down Expand Up @@ -124,6 +125,7 @@ main(int argc, char **argv)
{ "verbose", no_argument, NULL, 'v' },
{ "files", required_argument, NULL, 'f' },
{ "format", required_argument, NULL, 'F' },
{ "long", no_argument, NULL, 4 },
{ "deps", required_argument, NULL, 'x' },
{ "revdeps", required_argument, NULL, 'X' },
{ "regex", no_argument, NULL, 0 },
Expand All @@ -136,14 +138,14 @@ main(int argc, char **argv)
int c, flags, rv;
bool list_pkgs, list_repos, orphans, own, list_repolock;
bool list_manual, list_hold, show_prop, show_files, show_deps, show_rdeps;
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree;
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree, long_listing;

rootdir = cachedir = confdir = props = pkg = catfile = format = NULL;
flags = rv = c = 0;
list_pkgs = list_repos = list_hold = orphans = pkg_search = own = false;
list_manual = list_repolock = show_prop = show_files = false;
regex = show = show_deps = show_rdeps = fulldeptree = false;
repo_mode = opmode = false;
repo_mode = opmode = long_listing = false;

memset(&xh, 0, sizeof(xh));

Expand Down Expand Up @@ -240,6 +242,9 @@ main(int argc, char **argv)
case 3:
list_repolock = opmode = true;
break;
case 4:
long_listing = true;
break;
case '?':
default:
usage(true);
Expand Down Expand Up @@ -328,10 +333,17 @@ main(int argc, char **argv)

} else if (show_files) {
/* show-files mode */
struct xbps_fmt *fmt = xbps_fmt_parse(
format ? format :
(long_listing ?
"{mode?0!strmode} {user?\"root\":<8} {group?\"root\":<8} "
"{size?0!humanize .8Bi:>8} {file-target}\n"
: "{file-target}\n")
);
if (repo_mode)
rv = repo_show_pkg_files(&xh, pkg);
rv = repo_show_pkg_files(&xh, pkg, fmt);
else
rv = show_pkg_files_from_metadir(&xh, pkg);
rv = show_pkg_files_from_metadir(&xh, pkg, fmt);

} else if (show_deps) {
/* show-deps mode */
Expand Down
52 changes: 40 additions & 12 deletions bin/xbps-query/show-info-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,43 @@ show_pkg_info(xbps_dictionary_t dict)
xbps_object_release(all_keys);
}

struct file_print_cb {
xbps_dictionary_t dict;
bool islnk;
};

static int
file_print_cb(FILE *fp, const struct xbps_fmt *fmt, void *data)
{
struct file_print_cb *ctx = data;
xbps_object_t obj;
if (ctx->islnk && strcmp(fmt->var, "mode") == 0) {
// symbolic links don't store mode in the metadata, so it would normally display as
// unknown (?---------). be a bit more like ls -l and print 'l---------' without
// having to include this data in the plist
return xbps_fmt_print_number(fmt, 0120000, fp);
} else if (strcmp(fmt->var, "file-target") == 0) {
const char *buf, *target;
int len;
xbps_dictionary_get_cstring_nocopy(ctx->dict, "file", &buf);
if (xbps_dictionary_get_cstring_nocopy(ctx->dict, "target", &target)) {
buf = xbps_xasprintf("%s -> %s", buf, target);
}
len = strlen(buf);
return xbps_fmt_print_string(fmt, buf, len, fp);
}
obj = xbps_dictionary_get(ctx->dict, fmt->var);
return xbps_fmt_print_object(fmt, obj, fp);
}

int
show_pkg_files(xbps_dictionary_t filesd)
show_pkg_files(xbps_dictionary_t filesd, const struct xbps_fmt *fmt)
{
xbps_array_t array, allkeys;
xbps_object_t obj;
xbps_dictionary_keysym_t ksym;
const char *keyname = NULL, *file = NULL;
struct file_print_cb ctx = {0};
const char *keyname = NULL;

if (xbps_object_type(filesd) != XBPS_TYPE_DICTIONARY)
return EINVAL;
Expand All @@ -206,6 +236,8 @@ show_pkg_files(xbps_dictionary_t filesd)
(strcmp(keyname, "links")))))
continue;

ctx.islnk = strcmp(keyname, "links") == 0;

array = xbps_dictionary_get(filesd, keyname);
if (array == NULL || xbps_array_count(array) == 0)
continue;
Expand All @@ -214,13 +246,9 @@ show_pkg_files(xbps_dictionary_t filesd)
obj = xbps_array_get(array, x);
if (xbps_object_type(obj) != XBPS_TYPE_DICTIONARY)
continue;
xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
printf("%s", file);
if (xbps_dictionary_get_cstring_nocopy(obj,
"target", &file))
printf(" -> %s", file);

printf("\n");
ctx.dict = obj;
xbps_fmt(fmt, &file_print_cb, &ctx, stdout);
}
}
xbps_object_release(allkeys);
Expand Down Expand Up @@ -248,7 +276,7 @@ show_pkg_info_from_metadir(struct xbps_handle *xhp,
}

int
show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg)
show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg, const struct xbps_fmt *fmt)
{
xbps_dictionary_t d;
int rv = 0;
Expand All @@ -257,7 +285,7 @@ show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg)
if (d == NULL)
return ENOENT;

rv = show_pkg_files(d);
rv = show_pkg_files(d, fmt);

return rv;
}
Expand Down Expand Up @@ -324,7 +352,7 @@ repo_cat_file(struct xbps_handle *xhp, const char *pkg, const char *file)
}

int
repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg)
repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg, const struct xbps_fmt *fmt)
{
xbps_dictionary_t pkgd;
int rv;
Expand All @@ -337,7 +365,7 @@ repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg)
return errno;
}

rv = show_pkg_files(pkgd);
rv = show_pkg_files(pkgd, fmt);
xbps_object_release(pkgd);
return rv;
}
6 changes: 6 additions & 0 deletions bin/xbps-query/xbps-query.1
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ modes.
Prints a full dependency tree in the
.Sy show dependencies
mode.
.It Fl -long
Prints permissions, ownership, and filesize in the
.Sy files
mode.
Equivalent to
.Fl -format Ar '{mode?0!strmode} {user?"root":<8} {group?"root":<8} {size?0!humanize .8Bi:>8} {file-target}\(rsn' .
.It Fl r, Fl -rootdir Ar dir
Specifies a full path for the target root directory.
.It Fl v, Fl -verbose
Expand Down
1 change: 1 addition & 0 deletions data/_xbps
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ _xbps_query() {
{-p,--property=-}'[Show properties]:property:($_xbps_properties)' \
--regex'[Use Extended Regular Expressions to match]' \
--fulldeptree'[Full dependency tree for -x/--deps]' \
--long'[Show permissions, ownership, and size for -f/--files]' \
{-R,--repository}'[Enable repository mode]' \
'*'--repository=-'[Add repository to the top of the list]:repository url:_files -/' \
- '(mode)' \
Expand Down

0 comments on commit 74dab9b

Please sign in to comment.