@@ -186,13 +186,50 @@ 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+
189218int
190- show_pkg_files (xbps_dictionary_t filesd )
219+ show_pkg_files (xbps_dictionary_t filesd , const char * fmts )
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 ;
226+
227+ struct xbps_fmt * fmt = xbps_fmt_parse (fmts );
228+ if (!fmt ) {
229+ int rv = - errno ;
230+ xbps_error_printf ("failed to parse format: %s\n" , strerror (- rv ));
231+ return rv ;
232+ }
196233
197234 if (xbps_object_type (filesd ) != XBPS_TYPE_DICTIONARY )
198235 return EINVAL ;
@@ -206,6 +243,8 @@ show_pkg_files(xbps_dictionary_t filesd)
206243 (strcmp (keyname , "links" )))))
207244 continue ;
208245
246+ ctx .islnk = strcmp (keyname , "links" ) == 0 ;
247+
209248 array = xbps_dictionary_get (filesd , keyname );
210249 if (array == NULL || xbps_array_count (array ) == 0 )
211250 continue ;
@@ -214,17 +253,13 @@ show_pkg_files(xbps_dictionary_t filesd)
214253 obj = xbps_array_get (array , x );
215254 if (xbps_object_type (obj ) != XBPS_TYPE_DICTIONARY )
216255 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 );
222256
223- printf ("\n" );
257+ ctx .dict = obj ;
258+ xbps_fmt (fmt , & file_print_cb , & ctx , stdout );
224259 }
225260 }
226261 xbps_object_release (allkeys );
227-
262+ xbps_fmt_free ( fmt );
228263 return 0 ;
229264}
230265
@@ -248,7 +283,7 @@ show_pkg_info_from_metadir(struct xbps_handle *xhp,
248283}
249284
250285int
251- show_pkg_files_from_metadir (struct xbps_handle * xhp , const char * pkg )
286+ show_pkg_files_from_metadir (struct xbps_handle * xhp , const char * pkg , const char * fmts )
252287{
253288 xbps_dictionary_t d ;
254289 int rv = 0 ;
@@ -257,7 +292,7 @@ show_pkg_files_from_metadir(struct xbps_handle *xhp, const char *pkg)
257292 if (d == NULL )
258293 return ENOENT ;
259294
260- rv = show_pkg_files (d );
295+ rv = show_pkg_files (d , fmts );
261296
262297 return rv ;
263298}
@@ -324,7 +359,7 @@ repo_cat_file(struct xbps_handle *xhp, const char *pkg, const char *file)
324359}
325360
326361int
327- repo_show_pkg_files (struct xbps_handle * xhp , const char * pkg )
362+ repo_show_pkg_files (struct xbps_handle * xhp , const char * pkg , const char * fmts )
328363{
329364 xbps_dictionary_t pkgd ;
330365 int rv ;
@@ -337,7 +372,7 @@ repo_show_pkg_files(struct xbps_handle *xhp, const char *pkg)
337372 return errno ;
338373 }
339374
340- rv = show_pkg_files (pkgd );
375+ rv = show_pkg_files (pkgd , fmts );
341376 xbps_object_release (pkgd );
342377 return rv ;
343378}
0 commit comments