be nearer to original
[grub2/phcoder.git] / commands / search.c
blob4ac7429ebc2a5f4f34cdf41dffa5eb83763e71dd
1 /* search.c - search devices based on a file or a filesystem label */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.h>
25 #include <grub/device.h>
26 #include <grub/file.h>
27 #include <grub/env.h>
28 #include <grub/extcmd.h>
30 static const struct grub_arg_option options[] =
32 {"file", 'f', 0, "search devices by a file (default)", 0, 0},
33 {"label", 'l', 0, "search devices by a filesystem label", 0, 0},
34 {"fs-uuid", 'u', 0, "search devices by a filesystem UUID", 0, 0},
35 {"set", 's', GRUB_ARG_OPTION_OPTIONAL, "set a variable to the first device found", "VAR", ARG_TYPE_STRING},
36 {"no-floppy", 'n', 0, "do not probe any floppy drive", 0, 0},
37 {0, 0, 0, 0, 0, 0}
40 enum options
42 SEARCH_FILE,
43 SEARCH_LABEL,
44 SEARCH_FS_UUID,
45 SEARCH_SET,
46 SEARCH_NO_FLOPPY,
49 static void
50 search_fs (const char *key, const char *var, int no_floppy, enum options type)
52 int count = 0;
53 char *buf = NULL;
55 auto int iterate_device (const char *name);
56 int iterate_device (const char *name)
58 int found = 0;
60 /* Skip floppy drives when requested. */
61 if (no_floppy &&
62 name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
63 return 0;
65 if (type == SEARCH_FILE)
67 grub_size_t len;
68 char *p;
69 grub_file_t file;
71 len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
72 p = grub_realloc (buf, len);
73 if (! p)
74 return 1;
76 buf = p;
77 grub_sprintf (buf, "(%s)%s", name, key);
79 file = grub_file_open (buf);
80 if (file)
82 found = 1;
83 grub_file_close (file);
86 else
88 /* type is SEARCH_FS_UUID or SEARCH_LABEL */
89 grub_device_t dev;
90 grub_fs_t fs;
91 int (*compare_fn) (const char *, const char *);
92 char *quid;
94 dev = grub_device_open (name);
95 if (dev)
97 fs = grub_fs_probe (dev);
98 compare_fn =
99 (type == SEARCH_FS_UUID) ? grub_strcasecmp : grub_strcmp;
101 if (fs && ((type == SEARCH_FS_UUID) ? fs->uuid : fs->label))
103 if (type == SEARCH_FS_UUID)
104 fs->uuid (dev, &quid);
105 else
106 fs->label (dev, &quid);
108 if (grub_errno == GRUB_ERR_NONE && quid)
110 if (compare_fn (quid, key) == 0)
111 found = 1;
113 grub_free (quid);
117 grub_device_close (dev);
121 if (found)
123 count++;
124 if (var)
125 grub_env_set (var, name);
126 else
127 grub_printf (" %s", name);
130 grub_errno = GRUB_ERR_NONE;
131 return (found && var);
134 grub_device_iterate (iterate_device);
136 grub_free (buf);
138 if (grub_errno == GRUB_ERR_NONE && count == 0)
139 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
142 static grub_err_t
143 grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
145 struct grub_arg_list *state = cmd->state;
146 const char *var = 0;
148 if (argc == 0)
149 return grub_error (GRUB_ERR_INVALID_COMMAND, "no argument specified");
151 if (state[SEARCH_SET].set)
152 var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
154 if (state[SEARCH_LABEL].set)
155 search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_LABEL);
156 else if (state[SEARCH_FS_UUID].set)
157 search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FS_UUID);
158 else if (state[SEARCH_FILE].set)
159 search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FILE);
160 else
161 return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
163 return grub_errno;
166 static grub_extcmd_t cmd;
168 GRUB_MOD_INIT(search)
170 cmd =
171 grub_register_extcmd ("search", grub_cmd_search,
172 GRUB_COMMAND_FLAG_BOTH,
173 "search [-f|-l|-u|-s|-n] NAME",
174 "Search devices by file, filesystem label or filesystem UUID."
175 " If --set is specified, the first device found is"
176 " set to a variable. If no variable name is"
177 " specified, \"root\" is used.",
178 options);
181 GRUB_MOD_FINI(search)
183 grub_unregister_extcmd (cmd);