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+
4298static void
4399print_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
189245int
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
250337int
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
326413int
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}
0 commit comments