Skip to content

Commit 46c7ac5

Browse files
committed
bin/xbps-query: add --long for -f to show file perms, owner, size
1 parent 9569f2e commit 46c7ac5

File tree

5 files changed

+98
-22
lines changed

5 files changed

+98
-22
lines changed

bin/xbps-query/defs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ void show_pkg_info(xbps_dictionary_t);
3939
void show_pkg_info_one(xbps_dictionary_t, const char *);
4040
int show_pkg_info_from_metadir(struct xbps_handle *, const char *,
4141
const char *);
42-
int show_pkg_files(xbps_dictionary_t);
43-
int show_pkg_files_from_metadir(struct xbps_handle *, const char *);
44-
int repo_show_pkg_files(struct xbps_handle *, const char *);
42+
int show_pkg_files(xbps_dictionary_t, const struct xbps_fmt *);
43+
int show_pkg_files_from_metadir(struct xbps_handle *, const char *, const struct xbps_fmt *);
44+
int repo_show_pkg_files(struct xbps_handle *, const char *, const struct xbps_fmt *);
4545
int cat_file(struct xbps_handle *, const char *, const char *);
4646
int repo_cat_file(struct xbps_handle *, const char *, const char *);
4747
int repo_show_pkg_info(struct xbps_handle *, const char *, const char *);

bin/xbps-query/main.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ usage(bool fail)
5656
" specified multiple times\n"
5757
" --regex Use Extended Regular Expressions to match\n"
5858
" --fulldeptree Full dependency tree for -x/--deps\n"
59+
" --long Show permissions, ownership, and size for -f/--files\n"
5960
" -r, --rootdir <dir> Full path to rootdir\n"
6061
" -V, --version Show XBPS version\n"
6162
" -v, --verbose Verbose messages\n"
@@ -124,6 +125,7 @@ main(int argc, char **argv)
124125
{ "verbose", no_argument, NULL, 'v' },
125126
{ "files", required_argument, NULL, 'f' },
126127
{ "format", required_argument, NULL, 'F' },
128+
{ "long", no_argument, NULL, 4 },
127129
{ "deps", required_argument, NULL, 'x' },
128130
{ "revdeps", required_argument, NULL, 'X' },
129131
{ "regex", no_argument, NULL, 0 },
@@ -136,14 +138,14 @@ main(int argc, char **argv)
136138
int c, flags, rv;
137139
bool list_pkgs, list_repos, orphans, own, list_repolock;
138140
bool list_manual, list_hold, show_prop, show_files, show_deps, show_rdeps;
139-
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree;
141+
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree, long_listing;
140142

141143
rootdir = cachedir = confdir = props = pkg = catfile = format = NULL;
142144
flags = rv = c = 0;
143145
list_pkgs = list_repos = list_hold = orphans = pkg_search = own = false;
144146
list_manual = list_repolock = show_prop = show_files = false;
145147
regex = show = show_deps = show_rdeps = fulldeptree = false;
146-
repo_mode = opmode = false;
148+
repo_mode = opmode = long_listing = false;
147149

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

@@ -240,6 +242,9 @@ main(int argc, char **argv)
240242
case 3:
241243
list_repolock = opmode = true;
242244
break;
245+
case 4:
246+
long_listing = true;
247+
break;
243248
case '?':
244249
default:
245250
usage(true);
@@ -328,10 +333,17 @@ main(int argc, char **argv)
328333

329334
} else if (show_files) {
330335
/* show-files mode */
336+
struct xbps_fmt *fmt = xbps_fmt_parse(
337+
format ? format :
338+
(long_listing ?
339+
"{mode?0!strmode} {user?\"root\":<8} {group?\"root\":<8} "
340+
"{size?0!humanize .8Bi:>8} {file-target}\n"
341+
: "{file-target}\n")
342+
);
331343
if (repo_mode)
332-
rv = repo_show_pkg_files(&xh, pkg);
344+
rv = repo_show_pkg_files(&xh, pkg, fmt);
333345
else
334-
rv = show_pkg_files_from_metadir(&xh, pkg);
346+
rv = show_pkg_files_from_metadir(&xh, pkg, fmt);
335347

336348
} else if (show_deps) {
337349
/* show-deps mode */

bin/xbps-query/show-info-files.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,43 @@ show_pkg_info(xbps_dictionary_t dict)
186186
xbps_object_release(all_keys);
187187
}
188188

189+
struct file_print_cb {
190+
xbps_dictionary_t dict;
191+
bool islnk;
192+
};
193+
194+
static int
195+
file_print_cb(FILE *fp, const struct xbps_fmt *fmt, void *data)
196+
{
197+
struct file_print_cb *ctx = data;
198+
xbps_object_t obj;
199+
if (ctx->islnk && strcmp(fmt->var, "mode") == 0) {
200+
// symbolic links don't store mode in the metadata, so it would normally display as
201+
// unknown (?---------). be a bit more like ls -l and print 'l---------' without
202+
// having to include this data in the plist
203+
return xbps_fmt_print_number(fmt, 0120000, fp);
204+
} else if (strcmp(fmt->var, "file-target") == 0) {
205+
const char *buf, *target;
206+
int len;
207+
xbps_dictionary_get_cstring_nocopy(ctx->dict, "file", &buf);
208+
if (xbps_dictionary_get_cstring_nocopy(ctx->dict, "target", &target)) {
209+
buf = xbps_xasprintf("%s -> %s", buf, target);
210+
}
211+
len = strlen(buf);
212+
return xbps_fmt_print_string(fmt, buf, len, fp);
213+
}
214+
obj = xbps_dictionary_get(ctx->dict, fmt->var);
215+
return xbps_fmt_print_object(fmt, obj, fp);
216+
}
217+
189218
int
190-
show_pkg_files(xbps_dictionary_t filesd)
219+
show_pkg_files(xbps_dictionary_t filesd, const struct xbps_fmt *fmt)
191220
{
192221
xbps_array_t array, allkeys;
193222
xbps_object_t obj;
194223
xbps_dictionary_keysym_t ksym;
195-
const char *keyname = NULL, *file = NULL;
224+
struct file_print_cb ctx = {0};
225+
const char *keyname = NULL;
196226

197227
if (xbps_object_type(filesd) != XBPS_TYPE_DICTIONARY)
198228
return EINVAL;
@@ -206,6 +236,8 @@ show_pkg_files(xbps_dictionary_t filesd)
206236
(strcmp(keyname, "links")))))
207237
continue;
208238

239+
ctx.islnk = strcmp(keyname, "links") == 0;
240+
209241
array = xbps_dictionary_get(filesd, keyname);
210242
if (array == NULL || xbps_array_count(array) == 0)
211243
continue;
@@ -214,13 +246,9 @@ show_pkg_files(xbps_dictionary_t filesd)
214246
obj = xbps_array_get(array, x);
215247
if (xbps_object_type(obj) != XBPS_TYPE_DICTIONARY)
216248
continue;
217-
xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
218-
printf("%s", file);
219-
if (xbps_dictionary_get_cstring_nocopy(obj,
220-
"target", &file))
221-
printf(" -> %s", file);
222249

223-
printf("\n");
250+
ctx.dict = obj;
251+
xbps_fmt(fmt, &file_print_cb, &ctx, stdout);
224252
}
225253
}
226254
xbps_object_release(allkeys);
@@ -248,7 +276,7 @@ show_pkg_info_from_metadir(struct xbps_handle *xhp,
248276
}
249277

250278
int
251-
show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg)
279+
show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg, const struct xbps_fmt *fmt)
252280
{
253281
xbps_dictionary_t d;
254282
int rv = 0;
@@ -257,7 +285,7 @@ show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg)
257285
if (d == NULL)
258286
return ENOENT;
259287

260-
rv = show_pkg_files(d);
288+
rv = show_pkg_files(d, fmt);
261289

262290
return rv;
263291
}
@@ -324,7 +352,7 @@ repo_cat_file(struct xbps_handle *xhp, const char *pkg, const char *file)
324352
}
325353

326354
int
327-
repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg)
355+
repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg, const struct xbps_fmt *fmt)
328356
{
329357
xbps_dictionary_t pkgd;
330358
int rv;
@@ -337,7 +365,7 @@ repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg)
337365
return errno;
338366
}
339367

340-
rv = show_pkg_files(pkgd);
368+
rv = show_pkg_files(pkgd, fmt);
341369
xbps_object_release(pkgd);
342370
return rv;
343371
}

bin/xbps-query/xbps-query.1

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ modes.
134134
Prints a full dependency tree in the
135135
.Sy show dependencies
136136
mode.
137+
.It Fl -long
138+
Prints permissions, ownership, and filesize in the
139+
.Sy files
140+
mode.
141+
Equivalent to
142+
.Fl -format Ar '{mode?0!strmode} {user?"root":<8} {group?"root":<8} {size?0!humanize .8Bi:>8} {file-target}\(rsn' .
137143
.It Fl r, Fl -rootdir Ar dir
138144
Specifies a full path for the target root directory.
139145
.It Fl v, Fl -verbose
@@ -284,9 +290,11 @@ This mode only works with repositories.
284290
.El
285291
.Sh FORMAT STRINGS
286292
Variables are package properties if not otherwise documented.
287-
See
293+
See the
288294
.Sx PROPERTIES
289-
section for a list of available properties.
295+
and
296+
.Sx FILE PROPERTIES
297+
sections for a list of available properties.
290298
.Pp
291299
As example a format string like:
292300
.Bd -offset indent -literal
@@ -350,7 +358,7 @@ Format strings are parsed by the following EBNF:
350358
| "X" -- Hexadecimal with uppercase letters.
351359
.Ed
352360
.Sh PROPERTIES
353-
This is the list of a packages properties.
361+
This is the list of a package's properties.
354362
Note that not all properties are available for all packages.
355363
.Pp
356364
.Bl -tag -compact -width 17m
@@ -425,6 +433,33 @@ installation state of the package.
425433
.It Ic tags
426434
list of categories the package is associated with.
427435
.El
436+
.Sh FILE PROPERTIES
437+
This is the list of a package's files' properties.
438+
Note that not all properties are available for all files in all packages.
439+
.Pp
440+
.Bl -tag -compact -width 17m
441+
.It Ic file
442+
absolute path of the file.
443+
.It Ic file-target
444+
alias for
445+
.Ar file -> target
446+
if target exists, otherwise
447+
.Ar file .
448+
.It Ic group
449+
group that owns the file (root if unspecified).
450+
.It Ic mode
451+
file type and permissions as an integer.
452+
See also:
453+
.Xr inode 7 .
454+
.It Ic mtime
455+
modification time of the file (deprecated).
456+
.It Ic sha256
457+
sha256sum of the file.
458+
.It Ic target
459+
target of the file, if it is a symlink.
460+
.It Ic user
461+
user that owns the file (root if unspecified).
462+
.El
428463
.Sh ENVIRONMENT
429464
.Bl -tag -width XBPS_TARGET_ARCH
430465
.It Sy XBPS_ARCH

data/_xbps

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ _xbps_query() {
156156
{-p,--property=-}'[Show properties]:property:($_xbps_properties)' \
157157
--regex'[Use Extended Regular Expressions to match]' \
158158
--fulldeptree'[Full dependency tree for -x/--deps]' \
159+
--long'[Show permissions, ownership, and size for -f/--files]' \
159160
{-R,--repository}'[Enable repository mode]' \
160161
'*'--repository=-'[Add repository to the top of the list]:repository url:_files -/' \
161162
- '(mode)' \

0 commit comments

Comments
 (0)