Skip to content

Commit 13a194d

Browse files
committed
bin/xbps-query: add --long for -f to show file perms, owner, size
1 parent d61d615 commit 13a194d

File tree

5 files changed

+109
-12
lines changed

5 files changed

+109
-12
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, bool);
43+
int show_pkg_files_from_metadir(struct xbps_handle *, const char *, bool);
44+
int repo_show_pkg_files(struct xbps_handle *, const char *, bool);
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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ usage(bool fail)
5353
" specified multiple times\n"
5454
" --regex Use Extended Regular Expressions to match\n"
5555
" --fulldeptree Full dependency tree for -x/--deps\n"
56+
" --long Show permissions, ownership, and size for -f/--files\n"
5657
" -r, --rootdir <dir> Full path to rootdir\n"
5758
" -V, --version Show XBPS version\n"
5859
" -v, --verbose Verbose messages\n"
@@ -100,6 +101,7 @@ main(int argc, char **argv)
100101
{ "version", no_argument, NULL, 'V' },
101102
{ "verbose", no_argument, NULL, 'v' },
102103
{ "files", required_argument, NULL, 'f' },
104+
{ "long", no_argument, NULL, 4 },
103105
{ "deps", required_argument, NULL, 'x' },
104106
{ "revdeps", required_argument, NULL, 'X' },
105107
{ "regex", no_argument, NULL, 0 },
@@ -112,14 +114,14 @@ main(int argc, char **argv)
112114
int c, flags, rv;
113115
bool list_pkgs, list_repos, orphans, own, list_repolock;
114116
bool list_manual, list_hold, show_prop, show_files, show_deps, show_rdeps;
115-
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree;
117+
bool show, pkg_search, regex, repo_mode, opmode, fulldeptree, long_listing;
116118

117119
rootdir = cachedir = confdir = props = pkg = catfile = NULL;
118120
flags = rv = c = 0;
119121
list_pkgs = list_repos = list_hold = orphans = pkg_search = own = false;
120122
list_manual = list_repolock = show_prop = show_files = false;
121123
regex = show = show_deps = show_rdeps = fulldeptree = false;
122-
repo_mode = opmode = false;
124+
repo_mode = opmode = long_listing = false;
123125

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

@@ -213,6 +215,9 @@ main(int argc, char **argv)
213215
case 3:
214216
list_repolock = opmode = true;
215217
break;
218+
case 4:
219+
long_listing = true;
220+
break;
216221
case '?':
217222
default:
218223
usage(true);
@@ -302,9 +307,9 @@ main(int argc, char **argv)
302307
} else if (show_files) {
303308
/* show-files mode */
304309
if (repo_mode)
305-
rv = repo_show_pkg_files(&xh, pkg);
310+
rv = repo_show_pkg_files(&xh, pkg, long_listing);
306311
else
307-
rv = show_pkg_files_from_metadir(&xh, pkg);
312+
rv = show_pkg_files_from_metadir(&xh, pkg, long_listing);
308313

309314
} else if (show_deps) {
310315
/* show-deps mode */

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

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,69 @@
3232
#include <libgen.h>
3333
#include <fnmatch.h>
3434
#include <assert.h>
35+
#include <pwd.h>
36+
#include <grp.h>
3537

3638
#include <xbps.h>
3739
#include "defs.h"
3840

3941
#define _BOLD "\033[1m"
4042
#define _RESET "\033[m"
4143

44+
/*
45+
1111 111 111 111 111
46+
^~~~ ^~~ ^~~ ^~~ ^~~
47+
| | | | `- other rwx
48+
| | | `- group rwx
49+
| | `- user rwx
50+
| `- suid, sgid, sticky
51+
`- file type
52+
*/
53+
static void
54+
print_mode(const mode_t mode) {
55+
char hmode[10] = "";
56+
switch (mode & S_IFMT) {
57+
case S_IFREG: hmode[0] = '-'; break;
58+
case S_IFLNK: hmode[0] = 'l'; break;
59+
case S_IFDIR: hmode[0] = 'd'; break;
60+
default: hmode[0] = '?'; break;
61+
}
62+
hmode[1] = (mode & S_IRUSR) ? 'r' : '-';
63+
hmode[2] = (mode & S_IWUSR) ? 'w' : '-';
64+
if ((mode & S_ISUID) && (mode & S_IXUSR)) {
65+
hmode[3] = 's';
66+
} else if (mode & S_ISUID) {
67+
hmode[3] = 'S';
68+
} else if (mode & S_IXUSR) {
69+
hmode[3] = 'x';
70+
} else {
71+
hmode[3] = '-';
72+
}
73+
hmode[4] = (mode & S_IRGRP) ? 'r' : '-';
74+
hmode[5] = (mode & S_IWGRP) ? 'w' : '-';
75+
if ((mode & S_ISGID) && (mode & S_IXGRP)) {
76+
hmode[6] = 's';
77+
} else if (mode & S_ISGID) {
78+
hmode[6] = 'S';
79+
} else if (mode & S_IXGRP) {
80+
hmode[6] = 'x';
81+
} else {
82+
hmode[6] = '-';
83+
}
84+
hmode[7] = (mode & S_IROTH) ? 'r' : '-';
85+
hmode[8] = (mode & S_IWOTH) ? 'w' : '-';
86+
if ((mode & S_ISVTX) && (mode & S_IXOTH)) {
87+
hmode[9] = 't';
88+
} else if (mode & S_ISVTX) {
89+
hmode[9] = 'T';
90+
} else if (mode & S_IXOTH) {
91+
hmode[9] = 'x';
92+
} else {
93+
hmode[9] = '-';
94+
}
95+
printf("%s\t", hmode);
96+
}
97+
4298
static void
4399
print_value_obj(const char *keyname, xbps_object_t obj,
44100
const char *indent, const char *bold,
@@ -187,11 +243,17 @@ show_pkg_info(xbps_dictionary_t dict)
187243
}
188244

189245
int
190-
show_pkg_files(xbps_dictionary_t filesd)
246+
show_pkg_files(xbps_dictionary_t filesd, bool long_listing)
191247
{
192248
xbps_array_t array, allkeys;
193249
xbps_object_t obj;
194250
xbps_dictionary_keysym_t ksym;
251+
uint64_t size;
252+
uid_t uid;
253+
gid_t gid;
254+
mode_t mode;
255+
struct passwd *user;
256+
struct group *grp;
195257
const char *keyname = NULL, *file = NULL;
196258

197259
if (xbps_object_type(filesd) != XBPS_TYPE_DICTIONARY)
@@ -214,6 +276,31 @@ show_pkg_files(xbps_dictionary_t filesd)
214276
obj = xbps_array_get(array, x);
215277
if (xbps_object_type(obj) != XBPS_TYPE_DICTIONARY)
216278
continue;
279+
if (long_listing) {
280+
mode = uid = gid = size = 0;
281+
if (xbps_dictionary_get_uint32(obj, "mode", &mode)) {
282+
print_mode(mode);
283+
} else {
284+
printf("?\t");
285+
}
286+
if (xbps_dictionary_get_uint32(obj, "uid", &uid)) {
287+
user = getpwuid(uid);
288+
(user != NULL) ? printf("%s\t", user->pw_name) : printf("%u\t", uid);
289+
} else {
290+
printf("?\t");
291+
}
292+
if (xbps_dictionary_get_uint32(obj, "gid", &gid)) {
293+
grp = getgrgid(gid);
294+
(grp != NULL) ? printf("%s\t", grp->gr_name) : printf("%u\t", gid);
295+
} else {
296+
printf("?\t");
297+
}
298+
if (xbps_dictionary_get_uint64(obj, "size", &size)) {
299+
printf("%lu\t", size);
300+
} else {
301+
printf("?\t");
302+
}
303+
}
217304
xbps_dictionary_get_cstring_nocopy(obj, "file", &file);
218305
printf("%s", file);
219306
if (xbps_dictionary_get_cstring_nocopy(obj,
@@ -248,7 +335,7 @@ show_pkg_info_from_metadir(struct xbps_handle *xhp,
248335
}
249336

250337
int
251-
show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg)
338+
show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg, bool long_listing)
252339
{
253340
xbps_dictionary_t d;
254341
int rv = 0;
@@ -257,7 +344,7 @@ show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg)
257344
if (d == NULL)
258345
return ENOENT;
259346

260-
rv = show_pkg_files(d);
347+
rv = show_pkg_files(d, long_listing);
261348

262349
return rv;
263350
}
@@ -324,7 +411,7 @@ repo_cat_file(struct xbps_handle *xhp, const char *pkg, const char *file)
324411
}
325412

326413
int
327-
repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg)
414+
repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg, bool long_listing)
328415
{
329416
xbps_dictionary_t pkgd;
330417
int rv;
@@ -337,7 +424,7 @@ repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg)
337424
return errno;
338425
}
339426

340-
rv = show_pkg_files(pkgd);
427+
rv = show_pkg_files(pkgd, long_listing);
341428
xbps_object_release(pkgd);
342429
return rv;
343430
}

bin/xbps-query/xbps-query.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ modes.
129129
Prints a full dependency tree in the
130130
.Sy show dependencies
131131
mode.
132+
.It Fl -long
133+
Prints permissions, ownership, and filesize in the
134+
.Sy files
135+
mode.
132136
.It Fl r, Fl -rootdir Ar dir
133137
Specifies a full path for the target root directory.
134138
.It Fl v, Fl -verbose

data/_xbps

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

0 commit comments

Comments
 (0)