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

[Xen-devel] [PATCH 1/4] Add an option to exclude devices from search results.



* grub-core/commands/search.c (struct search_ctx): Add excludes and
nexcludes.
(iterate_device): Ignore devices listed in ctx->excludes.
(FUNC_NAME): Accept excludes and nexcludes parameters.
(grub_cmd_do_search): Pass empty excludes.
* grub-core/commands/search_wrap.c (options): Add --exclude option.
(grub_cmd_search): Handle --exclude.
* include/grub/search.h (grub_search_fs_file): Update prototype.
(grub_search_fs_uuid): Likewise.
(grub_search_label): Likewise.
* docs/grub.texi (search): Document --exclude.
---
 ChangeLog                        | 16 ++++++++++++++++
 docs/grub.texi                   |  7 ++++++-
 grub-core/commands/search.c      | 15 +++++++++++++--
 grub-core/commands/search_wrap.c | 27 ++++++++++++++++++++++-----
 include/grub/search.h            |  9 ++++++---
 5 files changed, 63 insertions(+), 11 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9cec63f..766fe4b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2013-12-12  Colin Watson  <cjwatson@xxxxxxxxxx>
+
+       Add an option to exclude devices from search results.
+
+       * grub-core/commands/search.c (struct search_ctx): Add excludes and
+       nexcludes.
+       (iterate_device): Ignore devices listed in ctx->excludes.
+       (FUNC_NAME): Accept excludes and nexcludes parameters.
+       (grub_cmd_do_search): Pass empty excludes.
+       * grub-core/commands/search_wrap.c (options): Add --exclude option.
+       (grub_cmd_search): Handle --exclude.
+       * include/grub/search.h (grub_search_fs_file): Update prototype.
+       (grub_search_fs_uuid): Likewise.
+       (grub_search_label): Likewise.
+       * docs/grub.texi (search): Document --exclude.
+
 2013-12-11  Vladimir Serbinenko  <phcoder@xxxxxxxxx>
 
        * grub-core/normal/charset.c: Fix premature line wrap and crash.
diff --git a/docs/grub.texi b/docs/grub.texi
index 91fa1de..4c53665 100644
--- a/docs/grub.texi
+++ b/docs/grub.texi
@@ -4731,7 +4731,8 @@ unbootable. @xref{Using digital signatures}, for more 
information.
 
 @deffn Command search @
  [@option{--file}|@option{--label}|@option{--fs-uuid}] @
- [@option{--set} [var]] [@option{--no-floppy}] name
+ [@option{--set} [var]] [@option{--no-floppy}] @
+ [@option{--exclude} @var{name} ...] name
 Search devices by file (@option{-f}, @option{--file}), filesystem label
 (@option{-l}, @option{--label}), or filesystem UUID (@option{-u},
 @option{--fs-uuid}).
@@ -4743,6 +4744,10 @@ value of environment variable @var{var}.  The default 
variable is
 The @option{--no-floppy} option prevents searching floppy devices, which can
 be slow.
 
+The @option{--exclude} option excludes any matches of the given GRUB device
+name.  For example, this may be used to search for any file named
+@file{/boot/grub/grub.cfg} other than the one in a memdisk.
+
 The @samp{search.file}, @samp{search.fs_label}, and @samp{search.fs_uuid}
 commands are aliases for @samp{search --file}, @samp{search --label}, and
 @samp{search --fs-uuid} respectively.
diff --git a/grub-core/commands/search.c b/grub-core/commands/search.c
index 16143a3..03cfea1 100644
--- a/grub-core/commands/search.c
+++ b/grub-core/commands/search.c
@@ -50,6 +50,8 @@ struct search_ctx
   int no_floppy;
   char **hints;
   unsigned nhints;
+  char **excludes;
+  unsigned nexcludes;
   int count;
   int is_cache;
 };
@@ -59,8 +61,15 @@ static int
 iterate_device (const char *name, void *data)
 {
   struct search_ctx *ctx = data;
+  unsigned i;
   int found = 0;
 
+  for (i = 0; i < ctx->nexcludes; i++)
+    {
+      if (grub_strcmp (name, ctx->excludes[i]) == 0)
+       return 0;
+    }
+
   /* Skip floppy drives when requested.  */
   if (ctx->no_floppy &&
       name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
@@ -262,7 +271,7 @@ try (struct search_ctx *ctx)
 
 void
 FUNC_NAME (const char *key, const char *var, int no_floppy,
-          char **hints, unsigned nhints)
+          char **hints, unsigned nhints, char **excludes, unsigned nexcludes)
 {
   struct search_ctx ctx = {
     .key = key,
@@ -270,6 +279,8 @@ FUNC_NAME (const char *key, const char *var, int no_floppy,
     .no_floppy = no_floppy,
     .hints = hints,
     .nhints = nhints,
+    .excludes = excludes,
+    .nexcludes = nexcludes,
     .count = 0,
     .is_cache = 0
   };
@@ -304,7 +315,7 @@ grub_cmd_do_search (grub_command_t cmd __attribute__ 
((unused)), int argc,
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
 
   FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
-            argc > 2 ? argc - 2 : 0);
+            argc > 2 ? argc - 2 : 0, NULL, 0);
 
   return grub_errno;
 }
diff --git a/grub-core/commands/search_wrap.c b/grub-core/commands/search_wrap.c
index 3f75fec..7493df4 100644
--- a/grub-core/commands/search_wrap.c
+++ b/grub-core/commands/search_wrap.c
@@ -63,6 +63,9 @@ static const struct grub_arg_option options[] =
      N_("First try the device HINT if currently running on ARC."
        " If HINT ends in comma, also try subpartitions"),
      N_("HINT"), ARG_TYPE_STRING},
+    {"exclude",                0, GRUB_ARG_OPTION_REPEATABLE,
+     N_("Exclude the device whose GRUB device name is NAME."), N_("NAME"),
+     ARG_TYPE_STRING},
     {0, 0, 0, 0, 0, 0}
   };
 
@@ -79,6 +82,7 @@ enum options
     SEARCH_HINT_BAREMETAL,
     SEARCH_HINT_EFI,
     SEARCH_HINT_ARC,
+    SEARCH_EXCLUDE,
  };
 
 static grub_err_t
@@ -87,8 +91,8 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, char 
**args)
   struct grub_arg_list *state = ctxt->state;
   const char *var = 0;
   const char *id = 0;
-  int i = 0, j = 0, nhints = 0;
-  char **hints = NULL;
+  int i = 0, j = 0, nhints = 0, nexcludes = 0;
+  char **hints = NULL, **excludes = NULL;
 
   if (state[SEARCH_HINT].set)
     for (i = 0; state[SEARCH_HINT].args[i]; i++)
@@ -164,6 +168,19 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, 
char **args)
     if (grub_memcmp (args[j], "--hint-", sizeof ("--hint-") - 1) != 0)
       break;
 
+  if (state[SEARCH_EXCLUDE].set)
+    {
+      for (i = 0; state[SEARCH_EXCLUDE].args[i]; i++)
+       nexcludes++;
+
+      excludes = grub_malloc (sizeof (excludes[0]) * nexcludes);
+      if (!excludes)
+       return grub_errno;
+
+      for (i = 0; state[SEARCH_EXCLUDE].args[i]; i++)
+       excludes[i] = state[SEARCH_EXCLUDE].args[i];
+    }
+
   if (state[SEARCH_SET].set)
     var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
 
@@ -179,13 +196,13 @@ grub_cmd_search (grub_extcmd_context_t ctxt, int argc, 
char **args)
 
   if (state[SEARCH_LABEL].set)
     grub_search_label (id, var, state[SEARCH_NO_FLOPPY].set, 
-                      hints, nhints);
+                      hints, nhints, excludes, nexcludes);
   else if (state[SEARCH_FS_UUID].set)
     grub_search_fs_uuid (id, var, state[SEARCH_NO_FLOPPY].set,
-                        hints, nhints);
+                        hints, nhints, excludes, nexcludes);
   else if (state[SEARCH_FILE].set)
     grub_search_fs_file (id, var, state[SEARCH_NO_FLOPPY].set, 
-                        hints, nhints);
+                        hints, nhints, excludes, nexcludes);
   else
     return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
 
diff --git a/include/grub/search.h b/include/grub/search.h
index d80347d..e7bcd4b 100644
--- a/include/grub/search.h
+++ b/include/grub/search.h
@@ -20,10 +20,13 @@
 #define GRUB_SEARCH_HEADER 1
 
 void grub_search_fs_file (const char *key, const char *var, int no_floppy,
-                         char **hints, unsigned nhints);
+                         char **hints, unsigned nhints,
+                         char **excludes, unsigned nexcludes);
 void grub_search_fs_uuid (const char *key, const char *var, int no_floppy,
-                         char **hints, unsigned nhints);
+                         char **hints, unsigned nhints,
+                         char **excludes, unsigned nexcludes);
 void grub_search_label (const char *key, const char *var, int no_floppy,
-                       char **hints, unsigned nhints);
+                       char **hints, unsigned nhints,
+                       char **excludes, unsigned nexcludes);
 
 #endif
-- 
1.8.4.4

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.