grub-devel
[Top][All Lists]
Advanced

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

[PATCH] Refactor commands/search.c - second take


From: Pavel Roskin
Subject: [PATCH] Refactor commands/search.c - second take
Date: Mon, 06 Jul 2009 23:40:14 -0400
User-agent: StGIT/0.14.3

It's rebased on top of the current code.  The difference from the
previous patch is that we don't dereference fs before it's checked to be
not NULL (my mistake, fixed in Subversion now).  Also, the code would
still print the key in the error message, so that error messages from
scripts can be identified.

The refactoring would allow us to add more options to exclude specific
disks.  It will be easier to keep error handling sync.

ChangeLog:

        * commands/search.c (search_file): Merge into ...
        (search_fs): ... this.  Accept search type as argument.
        (grub_cmd_search): Pass search type to search_fs().
---

 commands/search.c |  193 ++++++++++++++++++++++-------------------------------
 1 files changed, 81 insertions(+), 112 deletions(-)

diff --git a/commands/search.c b/commands/search.c
index 68ec8cc..9b39bd1 100644
--- a/commands/search.c
+++ b/commands/search.c
@@ -47,126 +47,95 @@ enum options
  };
 
 static void
-search_fs (const char *key, const char *var, int no_floppy, int is_uuid)
+search_fs (const char *key, const char *var, int no_floppy, enum options type)
 {
   int count = 0;
-  auto int iterate_device (const char *name);
-
-  int iterate_device (const char *name)
-    {
-      grub_device_t dev;
-      int abort = 0;
-
-      /* Skip floppy drives when requested.  */
-      if (no_floppy &&
-         name[0] == 'f' && name[1] == 'd' &&
-         name[2] >= '0' && name[2] <= '9')
-       return 0;
-
-      dev = grub_device_open (name);
-      if (dev)
-       {
-         grub_fs_t fs;
-         int (*compare_fn) (const char *, const char *);
-
-         fs = grub_fs_probe (dev);
-         compare_fn = is_uuid ? grub_strcasecmp : grub_strcmp;
-
-         if (fs && (is_uuid ? fs->uuid : fs->label))
-           {
-             char *quid;
-
-             if (is_uuid)
-               fs->uuid (dev, &quid);
-             else
-               fs->label (dev, &quid);
-
-             if (grub_errno == GRUB_ERR_NONE && quid)
-               {
-                 if (compare_fn (quid, key) == 0)
-                   {
-                     /* Found!  */
-                     count++;
-                     if (var)
-                       {
-                         grub_env_set (var, name);
-                         abort = 1;
-                       }
-                     else
-                         grub_printf (" %s", name);
-                   }
-
-                 grub_free (quid);
-               }
-           }
-
-         grub_device_close (dev);
-       }
-
-      grub_errno = GRUB_ERR_NONE;
-      return abort;
-    }
-
-  grub_device_iterate (iterate_device);
-
-  if (count == 0)
-    grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
-}
+  char *buf = NULL;
 
-static void
-search_file (const char *key, const char *var, int no_floppy)
-{
-  int count = 0;
-  char *buf = 0;
   auto int iterate_device (const char *name);
-
   int iterate_device (const char *name)
-    {
-      grub_size_t len;
-      char *p;
-      grub_file_t file;
-      int abort = 0;
-
-      /* Skip floppy drives when requested.  */
-      if (no_floppy &&
-         name[0] == 'f' && name[1] == 'd' &&
-         name[2] >= '0' && name[2] <= '9')
-       return 0;
-
-      len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
-      p = grub_realloc (buf, len);
-      if (! p)
-       return 1;
-
-      buf = p;
-      grub_sprintf (buf, "(%s)%s", name, key);
-
-      file = grub_file_open (buf);
-      if (file)
-       {
-         /* Found!  */
-         count++;
-         if (var)
-           {
-             grub_env_set (var, name);
-             abort = 1;
-           }
-         else
-           grub_printf (" %s", name);
-
-         grub_file_close (file);
-       }
-
-      grub_errno = GRUB_ERR_NONE;
-      return abort;
-    }
+  {
+    int found = 0;
+
+    /* Skip floppy drives when requested.  */
+    if (no_floppy &&
+       name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
+      return 0;
+
+    if (type == SEARCH_FILE)
+      {
+       grub_size_t len;
+       char *p;
+       grub_file_t file;
+
+       len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
+       p = grub_realloc (buf, len);
+       if (! p)
+         return 1;
+
+       buf = p;
+       grub_sprintf (buf, "(%s)%s", name, key);
+
+       file = grub_file_open (buf);
+       if (file)
+         {
+           found = 1;
+           grub_file_close (file);
+         }
+      }
+    else
+      {
+       grub_device_t dev;
+       grub_fs_t fs;
+       int (*compare_fn) (const char *, const char *);
+       char *quid;
+
+       dev = grub_device_open (name);
+       if (dev)
+         {
+           fs = grub_fs_probe (dev);
+           compare_fn =
+             (type == SEARCH_FS_UUID) ? grub_strcasecmp : grub_strcmp;
+
+           if (fs && ((type == SEARCH_FS_UUID) ? fs->uuid : fs->label))
+             {
+               if (type == SEARCH_FS_UUID)
+                 fs->uuid (dev, &quid);
+               else
+                 fs->label (dev, &quid);
+
+               if (grub_errno == GRUB_ERR_NONE && quid)
+                 {
+                   if (compare_fn (quid, key) == 0)
+                     found = 1;
+
+                   grub_free (quid);
+                 }
+             }
+
+           grub_device_close (dev);
+         }
+      }
+
+    if (found)
+      {
+       count++;
+       if (var)
+         grub_env_set (var, name);
+       else
+         grub_printf (" %s", name);
+      }
+
+    grub_errno = GRUB_ERR_NONE;
+    return (found && var);
+  }
 
   grub_device_iterate (iterate_device);
 
   grub_free (buf);
 
   if (grub_errno == GRUB_ERR_NONE && count == 0)
-    grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such file: %s", key);
+    grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
 }
 
 static grub_err_t
@@ -182,11 +151,11 @@ grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
     var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
 
   if (state[SEARCH_LABEL].set)
-    search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, 0);
+    search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_LABEL);
   else if (state[SEARCH_FS_UUID].set)
-    search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, 1);
+    search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FS_UUID);
   else if (state[SEARCH_FILE].set)
-    search_file (args[0], var, state[SEARCH_NO_FLOPPY].set);
+    search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FILE);
   else
     return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
 




reply via email to

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