From: Glenn Washburn Date: Sun, 2 Mar 2025 05:15:34 +0000 (-0600) Subject: commands/ls: Show modification time for file paths X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6337d84afa247ada63dda65ca33a015ec9f800f6;p=thirdparty%2Fgrub.git commands/ls: Show modification time for file paths The modification time for paths to files was not being printed because the grub_dirhook_info, which contains the mtime, was initialized to NULL. Instead of calling print_file() directly, use fs->fs_dir() to call print_file() with a properly filled in grub_dirhook_info. This has the added benefit of reducing code complexity. Signed-off-by: Glenn Washburn Reviewed-by: Daniel Kiper --- diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c index 2eaefaf90..1b447acf7 100644 --- a/grub-core/commands/ls.c +++ b/grub-core/commands/ls.c @@ -87,6 +87,7 @@ grub_ls_list_devices (int longlist) struct grub_ls_list_files_ctx { char *dirname; + char *filename; int all; int human; int longlist; @@ -102,6 +103,9 @@ print_file (const char *filename, const struct grub_dirhook_info *info, if ((! ctx->all) && (filename[0] == '.')) return 0; + if ((ctx->filename != NULL) && (grub_strcmp (filename, ctx->filename) != 0)) + return 0; + if (! ctx->longlist) { grub_printf ("%s%s ", filename, info->dir ? "/" : ""); @@ -210,6 +214,7 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human) { struct grub_ls_list_files_ctx ctx = { .dirname = dirname, + .filename = NULL, .all = all, .human = human, .longlist = longlist @@ -220,30 +225,23 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human) if (grub_errno == GRUB_ERR_BAD_FILE_TYPE && path[grub_strlen (path) - 1] != '/') { - /* PATH might be a regular file. */ - char *p; - grub_file_t file; - struct grub_dirhook_info info; - grub_errno = 0; - - file = grub_file_open (dirname, GRUB_FILE_TYPE_GET_SIZE - | GRUB_FILE_TYPE_NO_DECOMPRESS); - if (! file) - goto fail; + /* + * Reset errno as it is currently set, but will cause subsequent code + * to think there is an error. + */ + grub_errno = GRUB_ERR_NONE; - grub_file_close (file); - - p = grub_strrchr (dirname, '/'); - if (p == NULL) + /* PATH might be a regular file. */ + ctx.filename = grub_strrchr (dirname, '/'); + if (ctx.filename == NULL) goto fail; - ++p; + ++(ctx.filename); - ctx.dirname = grub_strndup (dirname, p - dirname); + ctx.dirname = grub_strndup (dirname, ctx.filename - dirname); if (ctx.dirname == NULL) goto fail; - grub_memset (&info, 0, sizeof (info)); - print_file (p, &info, &ctx); + (fs->fs_dir) (dev, ctx.dirname + (path - dirname), print_file, &ctx); grub_free (ctx.dirname); }