Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1MO1Xd-0002Zj-0u for mharc-grub-devel@gnu.org; Mon, 06 Jul 2009 23:40:25 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MO1XZ-0002XR-QE for grub-devel@gnu.org; Mon, 06 Jul 2009 23:40:21 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MO1XV-0002XF-Q7 for grub-devel@gnu.org; Mon, 06 Jul 2009 23:40:21 -0400 Received: from [199.232.76.173] (port=33997 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MO1XV-0002XC-O2 for grub-devel@gnu.org; Mon, 06 Jul 2009 23:40:17 -0400 Received: from c60.cesmail.net ([216.154.195.49]:22641) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.60) (envelope-from ) id 1MO1XV-0000pY-4B for grub-devel@gnu.org; Mon, 06 Jul 2009 23:40:17 -0400 Received: from unknown (HELO smtprelay2.cesmail.net) ([192.168.1.112]) by c60.cesmail.net with ESMTP; 06 Jul 2009 23:40:15 -0400 Received: from mj.roinet.com (static-72-92-88-10.phlapa.fios.verizon.net [72.92.88.10]) by smtprelay2.cesmail.net (Postfix) with ESMTPSA id 1E2C234C6A for ; Mon, 6 Jul 2009 23:48:31 -0400 (EDT) From: Pavel Roskin To: grub-devel@gnu.org Date: Mon, 06 Jul 2009 23:40:14 -0400 Message-ID: <20090707034014.11168.26357.stgit@mj.roinet.com> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. Subject: [PATCH] Refactor commands/search.c - second take X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Jul 2009 03:40:22 -0000 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");