grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/4] commands/ls: Allow printing mtime for file arguments


From: Glenn Washburn
Subject: [PATCH 2/4] commands/ls: Allow printing mtime for file arguments
Date: Mon, 14 Aug 2023 13:57:08 -0500

File arguments were processed differently than files listed from directory
arguments. A side effect of this was that mtime was not shown for file
arguments when long listing was enabled. Refactor to have the same code
path for printing files that are arguments and ones that are contained in
directories. This also has the added benefit of simplifying the code path.

Signed-off-by: Glenn Washburn <development@efficientek.com>
---
 grub-core/commands/ls.c | 70 ++++++++++++++++++-----------------------
 1 file changed, 31 insertions(+), 39 deletions(-)

diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c
index 6a1c7f5d3626..bc84c66b3060 100644
--- a/grub-core/commands/ls.c
+++ b/grub-core/commands/ls.c
@@ -87,8 +87,10 @@ grub_ls_list_devices (int longlist)
 struct grub_ls_list_files_ctx
 {
   char *dirname;
+  char *filename;
   int all;
   int human;
+  int longlist;
 };
 
 /* Helper for grub_ls_list_files.  */
@@ -98,22 +100,17 @@ print_files (const char *filename, const struct 
grub_dirhook_info *info,
 {
   struct grub_ls_list_files_ctx *ctx = data;
 
-  if (ctx->all || filename[0] != '.')
-    grub_printf ("%s%s ", filename, info->dir ? "/" : "");
-
-  return 0;
-}
-
-/* Helper for grub_ls_list_files.  */
-static int
-print_files_long (const char *filename, const struct grub_dirhook_info *info,
-                 void *data)
-{
-  struct grub_ls_list_files_ctx *ctx = data;
-
   if ((! ctx->all) && (filename[0] == '.'))
     return 0;
 
+  if (! ctx->longlist)
+    {
+      if (ctx->filename != NULL)
+       grub_xputs (ctx->dirname);
+      grub_printf ("%s%s ", filename, info->dir ? "/" : "");
+      return 0;
+    }
+
   if (! info->dir)
     {
       grub_file_t file;
@@ -170,6 +167,19 @@ print_files_long (const char *filename, const struct 
grub_dirhook_info *info,
   return 0;
 }
 
+/* Helper for grub_ls_list_files.  */
+static int
+print_file (const char *filename, const struct grub_dirhook_info *info,
+           void *data)
+{
+  struct grub_ls_list_files_ctx *ctx = data;
+
+  if (grub_strcmp (filename, ctx->filename) != 0)
+    return 0;
+
+  return print_files (filename, info, data);
+}
+
 static grub_err_t
 grub_ls_list_files (char *dirname, int longlist, int all, int human)
 {
@@ -216,42 +226,24 @@ 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
+       .human = human,
+       .longlist = longlist
       };
 
-      if (longlist)
-       (fs->fs_dir) (dev, path, print_files_long, &ctx);
-      else
-       (fs->fs_dir) (dev, path, print_files, &ctx);
+      (fs->fs_dir) (dev, path, print_files, &ctx);
 
       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;
-
-         grub_file_close (file);
-
-         p = grub_strrchr (dirname, '/') + 1;
-         ctx.dirname = grub_strndup (dirname, p - dirname);
+         ctx.filename = grub_strrchr (dirname, '/') + 1;
+         ctx.dirname = grub_strndup (dirname, ctx.filename - dirname);
          if (ctx.dirname == NULL)
            goto fail;
 
-         all = 1;
-         grub_memset (&info, 0, sizeof (info));
-         if (longlist)
-           print_files_long (p, &info, &ctx);
-         else
-           print_files (p, &info, &ctx);
+         (fs->fs_dir) (dev, ctx.dirname, print_file, &ctx);
 
          grub_free (ctx.dirname);
        }
@@ -268,7 +260,7 @@ grub_ls_list_files (char *dirname, int longlist, int all, 
int human)
 
   grub_free (device_name);
 
-  return 0;
+  return GRUB_ERR_NONE;
 }
 
 static grub_err_t
-- 
2.34.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]