Try and get a lock on the semaphore, if not then just move on as some housekeeping...
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / search.c
blob16143a34c83345d9e0b278f49f957d3a697a2b40
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/command.h>
29 #include <grub/search.h>
30 #include <grub/i18n.h>
31 #include <grub/disk.h>
32 #include <grub/partition.h>
34 GRUB_MOD_LICENSE ("GPLv3+");
36 struct cache_entry
38 struct cache_entry *next;
39 char *key;
40 char *value;
43 static struct cache_entry *cache;
45 /* Context for FUNC_NAME. */
46 struct search_ctx
48 const char *key;
49 const char *var;
50 int no_floppy;
51 char **hints;
52 unsigned nhints;
53 int count;
54 int is_cache;
57 /* Helper for FUNC_NAME. */
58 static int
59 iterate_device (const char *name, void *data)
61 struct search_ctx *ctx = data;
62 int found = 0;
64 /* Skip floppy drives when requested. */
65 if (ctx->no_floppy &&
66 name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
67 return 0;
69 #ifdef DO_SEARCH_FS_UUID
70 #define compare_fn grub_strcasecmp
71 #else
72 #define compare_fn grub_strcmp
73 #endif
75 #ifdef DO_SEARCH_FILE
77 char *buf;
78 grub_file_t file;
80 buf = grub_xasprintf ("(%s)%s", name, ctx->key);
81 if (! buf)
82 return 1;
84 grub_file_filter_disable_compression ();
85 file = grub_file_open (buf);
86 if (file)
88 found = 1;
89 grub_file_close (file);
91 grub_free (buf);
93 #else
95 /* SEARCH_FS_UUID or SEARCH_LABEL */
96 grub_device_t dev;
97 grub_fs_t fs;
98 char *quid;
100 dev = grub_device_open (name);
101 if (dev)
103 fs = grub_fs_probe (dev);
105 #ifdef DO_SEARCH_FS_UUID
106 #define read_fn uuid
107 #else
108 #define read_fn label
109 #endif
111 if (fs && fs->read_fn)
113 fs->read_fn (dev, &quid);
115 if (grub_errno == GRUB_ERR_NONE && quid)
117 if (compare_fn (quid, ctx->key) == 0)
118 found = 1;
120 grub_free (quid);
124 grub_device_close (dev);
127 #endif
129 if (!ctx->is_cache && found && ctx->count == 0)
131 struct cache_entry *cache_ent;
132 cache_ent = grub_malloc (sizeof (*cache_ent));
133 if (cache_ent)
135 cache_ent->key = grub_strdup (ctx->key);
136 cache_ent->value = grub_strdup (name);
137 if (cache_ent->value && cache_ent->key)
139 cache_ent->next = cache;
140 cache = cache_ent;
142 else
144 grub_free (cache_ent->value);
145 grub_free (cache_ent->key);
146 grub_free (cache_ent);
147 grub_errno = GRUB_ERR_NONE;
150 else
151 grub_errno = GRUB_ERR_NONE;
154 if (found)
156 ctx->count++;
157 if (ctx->var)
158 grub_env_set (ctx->var, name);
159 else
160 grub_printf (" %s", name);
163 grub_errno = GRUB_ERR_NONE;
164 return (found && ctx->var);
167 /* Helper for FUNC_NAME. */
168 static int
169 part_hook (grub_disk_t disk, const grub_partition_t partition, void *data)
171 struct search_ctx *ctx = data;
172 char *partition_name, *devname;
173 int ret;
175 partition_name = grub_partition_get_name (partition);
176 if (! partition_name)
177 return 1;
179 devname = grub_xasprintf ("%s,%s", disk->name, partition_name);
180 grub_free (partition_name);
181 if (!devname)
182 return 1;
183 ret = iterate_device (devname, ctx);
184 grub_free (devname);
186 return ret;
189 /* Helper for FUNC_NAME. */
190 static void
191 try (struct search_ctx *ctx)
193 unsigned i;
194 struct cache_entry **prev;
195 struct cache_entry *cache_ent;
197 for (prev = &cache, cache_ent = *prev; cache_ent;
198 prev = &cache_ent->next, cache_ent = *prev)
199 if (compare_fn (cache_ent->key, ctx->key) == 0)
200 break;
201 if (cache_ent)
203 ctx->is_cache = 1;
204 if (iterate_device (cache_ent->value, ctx))
206 ctx->is_cache = 0;
207 return;
209 ctx->is_cache = 0;
210 /* Cache entry was outdated. Remove it. */
211 if (!ctx->count)
213 grub_free (cache_ent->key);
214 grub_free (cache_ent->value);
215 grub_free (cache_ent);
216 *prev = cache_ent->next;
220 for (i = 0; i < ctx->nhints; i++)
222 char *end;
223 if (!ctx->hints[i][0])
224 continue;
225 end = ctx->hints[i] + grub_strlen (ctx->hints[i]) - 1;
226 if (*end == ',')
227 *end = 0;
228 if (iterate_device (ctx->hints[i], ctx))
230 if (!*end)
231 *end = ',';
232 return;
234 if (!*end)
236 grub_device_t dev;
237 int ret;
238 dev = grub_device_open (ctx->hints[i]);
239 if (!dev)
241 if (!*end)
242 *end = ',';
243 continue;
245 if (!dev->disk)
247 grub_device_close (dev);
248 if (!*end)
249 *end = ',';
250 continue;
252 ret = grub_partition_iterate (dev->disk, part_hook, ctx);
253 if (!*end)
254 *end = ',';
255 grub_device_close (dev);
256 if (ret)
257 return;
260 grub_device_iterate (iterate_device, ctx);
263 void
264 FUNC_NAME (const char *key, const char *var, int no_floppy,
265 char **hints, unsigned nhints)
267 struct search_ctx ctx = {
268 .key = key,
269 .var = var,
270 .no_floppy = no_floppy,
271 .hints = hints,
272 .nhints = nhints,
273 .count = 0,
274 .is_cache = 0
276 grub_fs_autoload_hook_t saved_autoload;
278 /* First try without autoloading if we're setting variable. */
279 if (var)
281 saved_autoload = grub_fs_autoload_hook;
282 grub_fs_autoload_hook = 0;
283 try (&ctx);
285 /* Restore autoload hook. */
286 grub_fs_autoload_hook = saved_autoload;
288 /* Retry with autoload if nothing found. */
289 if (grub_errno == GRUB_ERR_NONE && ctx.count == 0)
290 try (&ctx);
292 else
293 try (&ctx);
295 if (grub_errno == GRUB_ERR_NONE && ctx.count == 0)
296 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
299 static grub_err_t
300 grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
301 char **args)
303 if (argc == 0)
304 return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
306 FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0, (args + 2),
307 argc > 2 ? argc - 2 : 0);
309 return grub_errno;
312 static grub_command_t cmd;
314 #ifdef DO_SEARCH_FILE
315 GRUB_MOD_INIT(search_fs_file)
316 #elif defined (DO_SEARCH_FS_UUID)
317 GRUB_MOD_INIT(search_fs_uuid)
318 #else
319 GRUB_MOD_INIT(search_label)
320 #endif
322 cmd =
323 grub_register_command (COMMAND_NAME, grub_cmd_do_search,
324 N_("NAME [VARIABLE] [HINTS]"),
325 HELP_MESSAGE);
328 #ifdef DO_SEARCH_FILE
329 GRUB_MOD_FINI(search_fs_file)
330 #elif defined (DO_SEARCH_FS_UUID)
331 GRUB_MOD_FINI(search_fs_uuid)
332 #else
333 GRUB_MOD_FINI(search_label)
334 #endif
336 grub_unregister_command (cmd);