grub-devel
[Top][All Lists]
Advanced

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

[RFC][PATCH v1 4/4] commands/ls: support --set for files/directories.


From: Denis 'GNUtoo' Carikli
Subject: [RFC][PATCH v1 4/4] commands/ls: support --set for files/directories.
Date: Sun, 30 Jun 2024 18:25:19 +0200

The ls command has no way to get the name of the files or directories
being listed inside an environment variable.

This enables to programmatically, inside the grub.cfg be able to look
for files and react if they are found or not found.

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
---
 grub-core/commands/ls.c | 192 ++++++++++++++++++++++++++++++++++------
 1 file changed, 166 insertions(+), 26 deletions(-)

diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c
index 113235781..8a6528caa 100644
--- a/grub-core/commands/ls.c
+++ b/grub-core/commands/ls.c
@@ -127,6 +127,7 @@ struct grub_ls_list_files_ctx
   char *dirname;
   int all;
   int human;
+  char *varname;
 };
 
 /* Helper for grub_ls_list_files.  */
@@ -137,7 +138,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 ? "/" : "");
+  {
+    if (ctx->varname)
+      {
+       grub_env_append (ctx->varname, filename);
+       grub_env_append (ctx->varname, info->dir ? "/" : "");
+      }
+    else
+      {
+       grub_printf ("%s%s ", filename, info->dir ? "/" : "");
+      }
+  }
 
   return 0;
 }
@@ -158,9 +169,26 @@ print_files_long (const char *filename, const struct 
grub_dirhook_info *info,
       char *pathname;
 
       if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/')
-       pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
+       if (ctx->varname)
+         {
+           grub_env_append (ctx->varname, ctx->dirname);
+           grub_env_append (ctx->varname, filename);
+         }
+       else
+         {
+           pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
+         }
       else
-       pathname = grub_xasprintf ("%s/%s", ctx->dirname, filename);
+       if (ctx->varname)
+         {
+           grub_env_append (ctx->varname, ctx->dirname);
+           grub_env_append (ctx->varname, "/");
+           grub_env_append (ctx->varname, filename);
+         }
+       else
+         {
+           pathname = grub_xasprintf ("%s/%s", ctx->dirname, filename);
+         }
 
       if (!pathname)
        return 1;
@@ -172,38 +200,144 @@ print_files_long (const char *filename, const struct 
grub_dirhook_info *info,
       if (file)
        {
          if (! ctx->human)
-           grub_printf ("%-12llu", (unsigned long long) file->size);
+           if (ctx->varname)
+             {
+               char *str = grub_xasprintf("%-12llu",
+                                          (unsigned long long) file->size);
+               if (str)
+                 {
+                   grub_env_append (ctx->varname, str);
+                   grub_free (str);
+                 }
+               else
+                 {
+                   grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                               "print_files_long :grub_xasprintf failed.");
+                   return 0;
+                 }
+             }
+           else
+             {
+               grub_printf ("%-12llu", (unsigned long long) file->size);
+             }
          else
-           grub_printf ("%-12s", grub_get_human_size (file->size,
-                                                  GRUB_HUMAN_SIZE_SHORT));
+           if (ctx->varname)
+             {
+               char *str = grub_xasprintf("%-12s",
+                                          grub_get_human_size (file->size,
+                                                               
GRUB_HUMAN_SIZE_SHORT));
+               if (str)
+                 {
+                   grub_env_append (ctx->varname, str);
+                   grub_free (str);
+                 }
+               else
+                 {
+                   grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                               "print_files_long: grub_xasprintf failed.");
+                   return 0;
+                 }
+             }
+           else
+             {
+               grub_printf ("%-12s", grub_get_human_size (file->size,
+                                                          
GRUB_HUMAN_SIZE_SHORT));
+             }
          grub_file_close (file);
        }
       else
-       grub_xputs ("????????????");
+       if (ctx->varname)
+         grub_env_append (ctx->varname, "????????????");
+       else
+         grub_xputs ("????????????");
 
       grub_free (pathname);
       grub_errno = GRUB_ERR_NONE;
     }
   else
-    grub_printf ("%-12s", _("DIR"));
+    if (ctx->varname)
+      {
+       char *str = grub_xasprintf("%-12s", _("DIR"));
+       if (str)
+         {
+           grub_env_append (ctx->varname, str);
+           grub_free (str);
+         }
+       else
+         {
+           grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                       "print_files_long: grub_xasprintf failed.");
+           return 0;
+         }
+      }
+    else
+      {
+       grub_printf ("%-12s", _("DIR"));
+      }
 
   if (info->mtimeset)
     {
       struct grub_datetime datetime;
       grub_unixtime2datetime (info->mtime, &datetime);
       if (ctx->human)
-       grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
-                    datetime.year, datetime.month, datetime.day,
-                    datetime.hour, datetime.minute,
-                    datetime.second,
-                    grub_get_weekday_name (&datetime));
+       if (ctx->varname) {
+         char *str = grub_xasprintf(" %d-%02d-%02d %02d:%02d:%02d %-11s ",
+                                    datetime.year, datetime.month,
+                                    datetime.day,
+                                    datetime.hour, datetime.minute,
+                                    datetime.second,
+                                    grub_get_weekday_name (&datetime));
+         if (str) {
+           grub_env_append (ctx->varname, str);
+           grub_free (str);
+         } else {
+           grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                       "print_files_long: grub_xasprintf failed.");
+           return 0;
+         }
+       } else {
+         grub_printf (" %d-%02d-%02d %02d:%02d:%02d %-11s ",
+                      datetime.year, datetime.month, datetime.day,
+                      datetime.hour, datetime.minute,
+                      datetime.second,
+                      grub_get_weekday_name (&datetime));
+       }
       else
-       grub_printf (" %04d%02d%02d%02d%02d%02d ",
-                    datetime.year, datetime.month,
-                    datetime.day, datetime.hour,
-                    datetime.minute, datetime.second);
+       if (ctx->varname)
+         {
+           char *str = grub_xasprintf(" %04d%02d%02d%02d%02d%02d ",
+                                      datetime.year, datetime.month,
+                                      datetime.day, datetime.hour,
+                                      datetime.minute, datetime.second);
+           if (str)
+             {
+               grub_env_append (ctx->varname, str);
+               grub_free (str);
+             }
+           else
+             {
+               grub_error (GRUB_ERR_OUT_OF_MEMORY,
+                           "print_files_long: grub_xasprintf failed.");
+               return 0;
+             }
+         }
+       else
+         {
+           grub_printf (" %04d%02d%02d%02d%02d%02d ",
+                        datetime.year, datetime.month,
+                        datetime.day, datetime.hour,
+                        datetime.minute, datetime.second);
+         }
+    }
+  if (ctx->varname)
+    {
+      grub_env_append (ctx->varname, filename);
+      grub_env_append (ctx->varname, info->dir ? "/" : "");
+    }
+  else
+    {
+      grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
     }
-  grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
 
   return 0;
 }
@@ -217,12 +351,6 @@ grub_ls_list_files (char *dirname, int longlist, int all, 
int human,
   const char *path;
   grub_device_t dev;
 
-  if (varname)
-    {
-      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "set and list files");
-      return GRUB_ERR_NOT_IMPLEMENTED_YET;
-    }
-
   device_name = grub_file_get_device_name (dirname);
   dev = grub_device_open (device_name);
   if (! dev)
@@ -243,6 +371,11 @@ grub_ls_list_files (char *dirname, int longlist, int all, 
int human,
 
   if (! *path && device_name)
     {
+      if (varname) {
+       grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, "set and device infos");
+        goto fail;
+      }
+
       if (grub_errno == GRUB_ERR_UNKNOWN_FS)
        grub_errno = GRUB_ERR_NONE;
 
@@ -262,9 +395,13 @@ grub_ls_list_files (char *dirname, int longlist, int all, 
int human,
       struct grub_ls_list_files_ctx ctx = {
        .dirname = dirname,
        .all = all,
-       .human = human
+       .human = human,
+       .varname = varname
       };
 
+      if (varname)
+             grub_env_unset(varname);
+
       if (longlist)
        (fs->fs_dir) (dev, path, print_files_long, &ctx);
       else
@@ -302,7 +439,10 @@ grub_ls_list_files (char *dirname, int longlist, int all, 
int human,
        }
 
       if (grub_errno == GRUB_ERR_NONE)
-       grub_xputs ("\n");
+       if (varname)
+         grub_env_append (varname, "\n");
+       else
+         grub_xputs ("\n");
 
       grub_refresh ();
     }
-- 
2.45.1




reply via email to

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