Skip to contentSkip to conversation

More ls improvements

PATCH Superseded Glenn Washburn 2 participants 9 messages Jun 8, 2024 – Nov 27, 2024

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover More ls improvements

Currently when given a path to a file, ls will open the file to determine if its is valid and then run the appropriate print function, in contrast to directory arguments that use the directory iterator and callback on each file. One issue with this is that opening a file does not allow access to its modification time information, whereas the info object from the callback called by the directory iterator does and the longlist print function will print the modification time if present. The result is that when longlisting ls arguments, directory arguments show moditication times but file arguments do not. Patch 1 rectifies this an in the process simplifies the code path by using the directory iterator for file arguments as well.

Patches 2 and 3 aim to make the output of GRUB's ls look more like GNU's ls output. And patch 3 also fixes an issue where there are blank lines between consecutive file arguments.

v2:
* Remove first patch of v1
* Reset grub_errno in grub_ls_list_files instead

Glenn Washburn (3):
commands/ls: Allow printing mtime for file arguments commands/ls: Add directory header for dir args and print full paths
for file args
commands/ls: Proper line breaks between arguments

 grub-core/commands/ls.c | 123 ++++++++++++++++++++++++----------------
 1 file changed, 75 insertions(+), 48 deletions(-)

Range-diff against v1:

1:  c9066a31480c < -:  ------------ disk: Reset grub_errno upon entering grub_disk_read()
2:  8947f5257962 ! 1:  187a173d6e8a commands/ls: Allow printing mtime for file arguments
    @@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int al
            if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
      	  && path[grub_strlen (path) - 1] != '/')
      	{
    - 	  /* PATH might be a regular file.  */
    +-	  /* PATH might be a regular file.  */
     -	  char *p;
     -	  grub_file_t file;
     -	  struct grub_dirhook_info info;
    @@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int al
     -	    goto fail;
     -
     -	  grub_file_close (file);
    --
    ++	  /*
    ++	   * Reset errno as it is currently set, but will cause subsequent code
    ++	   * to think there is an error.
    ++	   */
    ++	  grub_errno = GRUB_ERR_NONE;
    + 
     -	  p = grub_strrchr (dirname, '/') + 1;
     -	  ctx.dirname = grub_strndup (dirname, p - dirname);
    ++	  /* PATH might be a regular file.  */
     +	  ctx.filename = grub_strrchr (dirname, '/') + 1;
     +	  ctx.dirname = grub_strndup (dirname, ctx.filename - dirname);
      	  if (ctx.dirname == NULL)

3: ce655165f508 ! 2: cd02a1fa4512 commands/ls: Add directory header for dir args and print full paths for file args
@@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int al

(fs->fs_dir) (dev, path, print_files, &ctx);
@@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int all, int human)
- && path[grub_strlen (path) - 1] != '/')
- {
+ grub_errno = GRUB_ERR_NONE;
+
/* PATH might be a regular file. */
+ ctx.print_dirhdr = 0;
ctx.filename = grub_strrchr (dirname, '/') + 1;
4: 1a14220825c2 = 3: 6988321447be commands/ls: Proper line breaks between arguments

Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw

PATCH v2 2/3 commands/ls: Add directory header for dir args and print full paths for file args

Like the GNU ls first print a line with the directory path before printing files in the directory, which will not have a directory component, but only if there is more than one argument. Also, for arguments that are paths to files, print the full path of the file.

---
 grub-core/commands/ls.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
grub-core/commands/ls.c +18 -7
index 6a45b998e30d..10939eefeab5 100644
@@ -91,6 +91,7 @@ struct grub_ls_list_files_ctx
   int all;
   int human;
   int longlist;
+  int print_dirhdr;
 };
 
 /* Helper for grub_ls_list_files.  */
@@ -98,11 +99,18 @@ static int
 print_files (const char *filename, const struct grub_dirhook_info *info,
 	     void *data)
 {
+  char *pathname = NULL;
   struct grub_ls_list_files_ctx *ctx = data;
 
   if ((! ctx->all) && (filename[0] == '.'))
     return 0;
 
+  if (ctx->print_dirhdr)
+    {
+      grub_printf ("%s:\n", ctx->dirname);
+      ctx->print_dirhdr = 0;
+    }
+
   if (! ctx->longlist)
     {
       if (ctx->filename != NULL)
@@ -114,7 +122,6 @@ print_files (const char *filename, const struct grub_dirhook_info *info,
   if (! info->dir)
     {
       grub_file_t file;
-      char *pathname;
 
       if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/')
 	pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
@@ -140,7 +147,6 @@ print_files (const char *filename, const struct grub_dirhook_info *info,
       else
 	grub_xputs ("????????????");
 
-      grub_free (pathname);
       grub_errno = GRUB_ERR_NONE;
     }
   else
@@ -162,7 +168,10 @@ print_files (const char *filename, const struct grub_dirhook_info *info,
 		     datetime.day, datetime.hour,
 		     datetime.minute, datetime.second);
     }
-  grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
+  grub_printf ("%s%s\n", (ctx->filename) ? pathname : filename,
+			 info->dir ? "/" : "");
+
+  grub_free (pathname);
 
   return 0;
 }
@@ -181,7 +190,7 @@ print_file (const char *filename, const struct grub_dirhook_info *info,
 }
 
 static grub_err_t
-grub_ls_list_files (char *dirname, int longlist, int all, int human)
+grub_ls_list_files (char *dirname, int longlist, int all, int human, int dirhdr)
 {
   char *device_name;
   grub_fs_t fs;
@@ -229,7 +238,8 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
 	.filename = NULL,
 	.all = all,
 	.human = human,
-	.longlist = longlist
+	.longlist = longlist,
+	.print_dirhdr = dirhdr
       };
 
       (fs->fs_dir) (dev, path, print_files, &ctx);
@@ -244,6 +254,7 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
 	  grub_errno = GRUB_ERR_NONE;
 
 	  /* PATH might be a regular file.  */
+	  ctx.print_dirhdr = 0;
 	  ctx.filename = grub_strrchr (dirname, '/') + 1;
 	  ctx.dirname = grub_strndup (dirname, ctx.filename - dirname);
 	  if (ctx.dirname == NULL)
@@ -279,8 +290,8 @@ grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
     grub_ls_list_devices (state[0].set);
   else
     for (i = 0; i < argc; i++)
-      grub_ls_list_files (args[i], state[0].set, state[2].set,
-			  state[1].set);
+      grub_ls_list_files (args[i], state[0].set, state[2].set, state[1].set,
+			  argc > 1);
 
   return 0;
 }
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#5 lore raw

PATCH v2 3/3 commands/ls: Proper line breaks between arguments

In reply to Glenn Washburn

There should be a blank line before each directory argument and before each file arugment that come after a directory argument. This brings the ls command more inline with GNU's ls. Although one key difference still is that GNU's ls reorders the output of arguments so that all file arguments are grouped at the beginning of output, while GRUB's ls does no reordering and shows output for each arument in the order that it was specified on the command line. Also fixed is an issue where there was a blank line between the output of consecutive file path arguments, which needlessly wastes valuable screen space.

---
 grub-core/commands/ls.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
grub-core/commands/ls.c +25 -7
index 10939eefeab5..0a3070372821 100644
@@ -92,6 +92,8 @@ struct grub_ls_list_files_ctx
   int human;
   int longlist;
   int print_dirhdr;
+  int first_arg;
+  int prev_file;
 };
 
 /* Helper for grub_ls_list_files.  */
@@ -105,6 +107,19 @@ print_files (const char *filename, const struct grub_dirhook_info *info,
   if ((! ctx->all) && (filename[0] == '.'))
     return 0;
 
+  /* Never print a leading newline if this is the first arg */
+  if (! ctx->first_arg)
+    /*
+     * Print leading newline(s) if (this is a file argument and the previous
+     * was not) or this is the first file from a directory argument.
+     */
+    if (((ctx->filename != NULL) && (! ctx->prev_file)) || ctx->print_dirhdr)
+    {
+      grub_xputs ("\n");
+      if (! ctx->longlist)
+	grub_xputs ("\n");
+    }
+
   if (ctx->print_dirhdr)
     {
       grub_printf ("%s:\n", ctx->dirname);
@@ -190,7 +205,8 @@ print_file (const char *filename, const struct grub_dirhook_info *info,
 }
 
 static grub_err_t
-grub_ls_list_files (char *dirname, int longlist, int all, int human, int dirhdr)
+grub_ls_list_files (char *dirname, int longlist, int all, int human,
+		    int dirhdr, int first_arg, int *prev_file)
 {
   char *device_name;
   grub_fs_t fs;
@@ -239,7 +255,9 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human, int dirhdr)
 	.all = all,
 	.human = human,
 	.longlist = longlist,
-	.print_dirhdr = dirhdr
+	.print_dirhdr = dirhdr,
+	.first_arg = first_arg,
+	.prev_file = *prev_file
       };
 
       (fs->fs_dir) (dev, path, print_files, &ctx);
@@ -262,11 +280,11 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human, int dirhdr)
 
 	  (fs->fs_dir) (dev, ctx.dirname, print_file, &ctx);
 
+	  *prev_file = 1;
 	  grub_free (ctx.dirname);
 	}
-
-      if (grub_errno == GRUB_ERR_NONE)
-	grub_xputs ("\n");
+      else
+	*prev_file = 0;
 
       grub_refresh ();
     }
@@ -284,14 +302,14 @@ static grub_err_t
 grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char **args)
 {
   struct grub_arg_list *state = ctxt->state;
-  int i;
+  int i, prev_file = 0;
 
   if (argc == 0)
     grub_ls_list_devices (state[0].set);
   else
     for (i = 0; i < argc; i++)
       grub_ls_list_files (args[i], state[0].set, state[2].set, state[1].set,
-			  argc > 1);
+			  argc > 1, i == 0, &prev_file);
 
   return 0;
 }
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v2 1/3 commands/ls: Allow printing mtime for file arguments

In reply to Glenn Washburn

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.

---
 grub-core/commands/ls.c | 76 ++++++++++++++++++++---------------------
 1 file changed, 37 insertions(+), 39 deletions(-)
grub-core/commands/ls.c +37 -39
index 6a1c7f5d3626..6a45b998e30d 100644
@@ -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,30 @@ 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);
+	  /*
+	   * Reset errno as it is currently set, but will cause subsequent code
+	   * to think there is an error.
+	   */
+	  grub_errno = GRUB_ERR_NONE;
 
-	  p = grub_strrchr (dirname, '/') + 1;
-	  ctx.dirname = grub_strndup (dirname, p - dirname);
+	  /* PATH might be a regular file.  */
+	  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 +266,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
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#7 lore raw
In reply to Glenn Washburn

Ping. Wondering if this is still in the queue to be looked at.

Glenn

Quoted 60 lines · On Sat, 8 Jun 2024 16:58:30 -0500 Glenn Washburn…

Currently when given a path to a file, ls will open the file to determine if its is valid and then run the appropriate print function, in contrast to directory arguments that use the directory iterator and callback on each file. One issue with this is that opening a file does not allow access to its modification time information, whereas the info object from the callback called by the directory iterator does and the longlist print function will print the modification time if present. The result is that when longlisting ls arguments, directory arguments show moditication times but file arguments do not. Patch 1 rectifies this an in the process simplifies the code path by using the directory iterator for file arguments as well.

Patches 2 and 3 aim to make the output of GRUB's ls look more like GNU's ls output. And patch 3 also fixes an issue where there are blank lines between consecutive file arguments.

v2:
* Remove first patch of v1
* Reset grub_errno in grub_ls_list_files instead

Glenn Washburn (3):
commands/ls: Allow printing mtime for file arguments commands/ls: Add directory header for dir args and print full paths
for file args
commands/ls: Proper line breaks between arguments

 grub-core/commands/ls.c | 123 ++++++++++++++++++++++++----------------
 1 file changed, 75 insertions(+), 48 deletions(-)

Range-diff against v1:

1:  c9066a31480c < -:  ------------ disk: Reset grub_errno upon entering grub_disk_read()
2:  8947f5257962 ! 1:  187a173d6e8a commands/ls: Allow printing mtime for file arguments
    @@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int al
            if (grub_errno == GRUB_ERR_BAD_FILE_TYPE
      	  && path[grub_strlen (path) - 1] != '/')
      	{
    - 	  /* PATH might be a regular file.  */
    +-	  /* PATH might be a regular file.  */
     -	  char *p;
     -	  grub_file_t file;
     -	  struct grub_dirhook_info info;
    @@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int al
     -	    goto fail;
     -
     -	  grub_file_close (file);
    --
    ++	  /*
    ++	   * Reset errno as it is currently set, but will cause subsequent code
    ++	   * to think there is an error.
    ++	   */
    ++	  grub_errno = GRUB_ERR_NONE;
    + 
     -	  p = grub_strrchr (dirname, '/') + 1;
     -	  ctx.dirname = grub_strndup (dirname, p - dirname);
    ++	  /* PATH might be a regular file.  */
     +	  ctx.filename = grub_strrchr (dirname, '/') + 1;
     +	  ctx.dirname = grub_strndup (dirname, ctx.filename - dirname);
      	  if (ctx.dirname == NULL)

3: ce655165f508 ! 2: cd02a1fa4512 commands/ls: Add directory header for dir args and print full paths for file args
@@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int al

Quoted 10 lines

(fs->fs_dir) (dev, path, print_files, &ctx);
@@ grub-core/commands/ls.c: grub_ls_list_files (char *dirname, int longlist, int all, int human)
- && path[grub_strlen (path) - 1] != '/')
- {
+ grub_errno = GRUB_ERR_NONE;
+
/* PATH might be a regular file. */
+ ctx.print_dirhdr = 0;
ctx.filename = grub_strrchr (dirname, '/') + 1;
4: 1a14220825c2 = 3: 6988321447be commands/ls: Proper line breaks between arguments

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw

Re: [PATCH v2 1/3] commands/ls: Allow printing mtime for file arguments

In reply to Glenn Washburn
Quoted 5 lines · Glenn Washburn

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.

I think you are doing at least three different things in this patch. First you change how print_files_long() works. Than you replace print_files() and print_files_long() with print_files(). And finally you change how the ls works in general. So, I can see three patches here...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw

Re: [PATCH v2 2/3] commands/ls: Add directory header for dir args and print full paths for file args

In reply to Glenn Washburn
Quoted 4 lines · Glenn Washburn

Like the GNU ls first print a line with the directory path before printing files in the directory, which will not have a directory component, but only if there is more than one argument. Also, for arguments that are paths to files, print the full path of the file.

The commit message suggests two patches here...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw

Re: [PATCH v2 3/3] commands/ls: Proper line breaks between arguments

In reply to Glenn Washburn
Quoted 9 lines · Glenn Washburn

There should be a blank line before each directory argument and before each file arugment that come after a directory argument. This brings the ls command more inline with GNU's ls. Although one key difference still is that GNU's ls reorders the output of arguments so that all file arguments are grouped at the beginning of output, while GRUB's ls does no reordering and shows output for each arument in the order that it was specified on the command line. Also fixed is an issue where there was a blank line between the output of consecutive file path arguments, which needlessly wastes valuable screen space.

Again, two separate patches?

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel