]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/fs/fshelp.c
Add Occitan language (#212)
[Ventoy.git] / GRUB2 / MOD_SRC / grub-2.04 / grub-core / fs / fshelp.c
1 /* fshelp.c -- Filesystem helper functions */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2004,2005,2006,2007,2008 Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <grub/err.h>
21 #include <grub/mm.h>
22 #include <grub/misc.h>
23 #include <grub/disk.h>
24 #include <grub/fshelp.h>
25 #include <grub/dl.h>
26 #include <grub/i18n.h>
27
28 GRUB_MOD_LICENSE ("GPLv3+");
29
30 typedef int (*iterate_dir_func) (grub_fshelp_node_t dir,
31 grub_fshelp_iterate_dir_hook_t hook,
32 void *data);
33 typedef grub_err_t (*lookup_file_func) (grub_fshelp_node_t dir,
34 const char *name,
35 grub_fshelp_node_t *foundnode,
36 enum grub_fshelp_filetype *foundtype);
37 typedef char *(*read_symlink_func) (grub_fshelp_node_t node);
38
39 struct stack_element {
40 struct stack_element *parent;
41 grub_fshelp_node_t node;
42 enum grub_fshelp_filetype type;
43 };
44
45 /* Context for grub_fshelp_find_file. */
46 struct grub_fshelp_find_file_ctx
47 {
48 /* Inputs. */
49 const char *path;
50 grub_fshelp_node_t rootnode;
51
52 /* Global options. */
53 int symlinknest;
54
55 /* Current file being traversed and its parents. */
56 struct stack_element *currnode;
57 };
58
59 /* Helper for find_file_iter. */
60 static void
61 free_node (grub_fshelp_node_t node, struct grub_fshelp_find_file_ctx *ctx)
62 {
63 if (node != ctx->rootnode)
64 grub_free (node);
65 }
66
67 static void
68 pop_element (struct grub_fshelp_find_file_ctx *ctx)
69 {
70 struct stack_element *el;
71 el = ctx->currnode;
72 ctx->currnode = el->parent;
73 free_node (el->node, ctx);
74 grub_free (el);
75 }
76
77 static void
78 free_stack (struct grub_fshelp_find_file_ctx *ctx)
79 {
80 while (ctx->currnode)
81 pop_element (ctx);
82 }
83
84 static void
85 go_up_a_level (struct grub_fshelp_find_file_ctx *ctx)
86 {
87 if (!ctx->currnode->parent)
88 return;
89 pop_element (ctx);
90 }
91
92 static grub_err_t
93 push_node (struct grub_fshelp_find_file_ctx *ctx, grub_fshelp_node_t node, enum grub_fshelp_filetype filetype)
94 {
95 struct stack_element *nst;
96 nst = grub_malloc (sizeof (*nst));
97 if (!nst)
98 return grub_errno;
99 nst->node = node;
100 nst->type = filetype & ~GRUB_FSHELP_CASE_INSENSITIVE;
101 nst->parent = ctx->currnode;
102 ctx->currnode = nst;
103 return GRUB_ERR_NONE;
104 }
105
106 static grub_err_t
107 go_to_root (struct grub_fshelp_find_file_ctx *ctx)
108 {
109 free_stack (ctx);
110 return push_node (ctx, ctx->rootnode, GRUB_FSHELP_DIR);
111 }
112
113 struct grub_fshelp_find_file_iter_ctx
114 {
115 const char *name;
116 grub_fshelp_node_t *foundnode;
117 enum grub_fshelp_filetype *foundtype;
118 };
119
120 /* Helper for grub_fshelp_find_file. */
121 static int
122 find_file_iter (const char *filename, enum grub_fshelp_filetype filetype,
123 grub_fshelp_node_t node, void *data)
124 {
125 struct grub_fshelp_find_file_iter_ctx *ctx = data;
126
127 if (filetype == GRUB_FSHELP_UNKNOWN ||
128 ((filetype & GRUB_FSHELP_CASE_INSENSITIVE)
129 ? grub_strcasecmp (ctx->name, filename)
130 : grub_strcmp (ctx->name, filename)))
131 {
132 grub_free (node);
133 return 0;
134 }
135
136 /* The node is found, stop iterating over the nodes. */
137 *ctx->foundnode = node;
138 *ctx->foundtype = filetype;
139 return 1;
140 }
141
142 static grub_err_t
143 directory_find_file (grub_fshelp_node_t node, const char *name, grub_fshelp_node_t *foundnode,
144 enum grub_fshelp_filetype *foundtype, iterate_dir_func iterate_dir)
145 {
146 int found;
147 struct grub_fshelp_find_file_iter_ctx ctx = {
148 .foundnode = foundnode,
149 .foundtype = foundtype,
150 .name = name
151 };
152 found = iterate_dir (node, find_file_iter, &ctx);
153 if (! found)
154 {
155 if (grub_errno)
156 return grub_errno;
157 }
158 return GRUB_ERR_NONE;
159 }
160
161 static grub_err_t
162 find_file (char *currpath,
163 iterate_dir_func iterate_dir, lookup_file_func lookup_file,
164 read_symlink_func read_symlink,
165 struct grub_fshelp_find_file_ctx *ctx)
166 {
167 char *name, *next;
168 grub_err_t err;
169 for (name = currpath; ; name = next)
170 {
171 char c;
172 grub_fshelp_node_t foundnode = NULL;
173 enum grub_fshelp_filetype foundtype = 0;
174
175 /* Remove all leading slashes. */
176 while (*name == '/')
177 name++;
178
179 /* Found the node! */
180 if (! *name)
181 return 0;
182
183 /* Extract the actual part from the pathname. */
184 for (next = name; *next && *next != '/'; next++);
185
186 /* At this point it is expected that the current node is a
187 directory, check if this is true. */
188 if (ctx->currnode->type != GRUB_FSHELP_DIR)
189 return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
190
191 /* Don't rely on fs providing actual . in the listing. */
192 if (next - name == 1 && name[0] == '.')
193 continue;
194
195 /* Don't rely on fs providing actual .. in the listing. */
196 if (next - name == 2 && name[0] == '.' && name[1] == '.')
197 {
198 go_up_a_level (ctx);
199 continue;
200 }
201
202 /* Iterate over the directory. */
203 c = *next;
204 *next = '\0';
205 if (lookup_file)
206 err = lookup_file (ctx->currnode->node, name, &foundnode, &foundtype);
207 else
208 err = directory_find_file (ctx->currnode->node, name, &foundnode, &foundtype, iterate_dir);
209 *next = c;
210
211 if (err)
212 return err;
213
214 if (!foundnode)
215 break;
216
217 push_node (ctx, foundnode, foundtype);
218
219 /* Read in the symlink and follow it. */
220 if (ctx->currnode->type == GRUB_FSHELP_SYMLINK)
221 {
222 char *symlink;
223
224 /* Test if the symlink does not loop. */
225 if (++ctx->symlinknest == 8)
226 return grub_error (GRUB_ERR_SYMLINK_LOOP,
227 N_("too deep nesting of symlinks"));
228
229 symlink = read_symlink (ctx->currnode->node);
230
231 if (!symlink)
232 return grub_errno;
233
234 /* The symlink is an absolute path, go back to the root inode. */
235 if (symlink[0] == '/')
236 {
237 err = go_to_root (ctx);
238 if (err)
239 return err;
240 }
241 else
242 {
243 /* Get from symlink to containing directory. */
244 go_up_a_level (ctx);
245 }
246
247
248 /* Lookup the node the symlink points to. */
249 find_file (symlink, iterate_dir, lookup_file, read_symlink, ctx);
250 grub_free (symlink);
251
252 if (grub_errno)
253 return grub_errno;
254 }
255 }
256
257 return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("file `%s' not found"),
258 ctx->path);
259 }
260
261 static grub_err_t
262 grub_fshelp_find_file_real (const char *path, grub_fshelp_node_t rootnode,
263 grub_fshelp_node_t *foundnode,
264 iterate_dir_func iterate_dir,
265 lookup_file_func lookup_file,
266 read_symlink_func read_symlink,
267 enum grub_fshelp_filetype expecttype)
268 {
269 struct grub_fshelp_find_file_ctx ctx = {
270 .path = path,
271 .rootnode = rootnode,
272 .symlinknest = 0,
273 .currnode = 0
274 };
275 grub_err_t err;
276 enum grub_fshelp_filetype foundtype;
277 char *duppath;
278
279 if (!path || path[0] != '/')
280 {
281 return grub_error (GRUB_ERR_BAD_FILENAME, N_("invalid file name `%s'"), path);
282 }
283
284 err = go_to_root (&ctx);
285 if (err)
286 return err;
287
288 duppath = grub_strdup (path);
289 if (!duppath)
290 return grub_errno;
291 err = find_file (duppath, iterate_dir, lookup_file, read_symlink, &ctx);
292 grub_free (duppath);
293 if (err)
294 {
295 free_stack (&ctx);
296 return err;
297 }
298
299 *foundnode = ctx.currnode->node;
300 foundtype = ctx.currnode->type;
301 /* Avoid the node being freed. */
302 ctx.currnode->node = 0;
303 free_stack (&ctx);
304
305 /* Check if the node that was found was of the expected type. */
306 if (expecttype == GRUB_FSHELP_REG && foundtype != expecttype)
307 return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a regular file"));
308 else if (expecttype == GRUB_FSHELP_DIR && foundtype != expecttype)
309 return grub_error (GRUB_ERR_BAD_FILE_TYPE, N_("not a directory"));
310
311 return 0;
312 }
313
314 /* Lookup the node PATH. The node ROOTNODE describes the root of the
315 directory tree. The node found is returned in FOUNDNODE, which is
316 either a ROOTNODE or a new malloc'ed node. ITERATE_DIR is used to
317 iterate over all directory entries in the current node.
318 READ_SYMLINK is used to read the symlink if a node is a symlink.
319 EXPECTTYPE is the type node that is expected by the called, an
320 error is generated if the node is not of the expected type. */
321 grub_err_t
322 grub_fshelp_find_file (const char *path, grub_fshelp_node_t rootnode,
323 grub_fshelp_node_t *foundnode,
324 iterate_dir_func iterate_dir,
325 read_symlink_func read_symlink,
326 enum grub_fshelp_filetype expecttype)
327 {
328 return grub_fshelp_find_file_real (path, rootnode, foundnode,
329 iterate_dir, NULL,
330 read_symlink, expecttype);
331
332 }
333
334 grub_err_t
335 grub_fshelp_find_file_lookup (const char *path, grub_fshelp_node_t rootnode,
336 grub_fshelp_node_t *foundnode,
337 lookup_file_func lookup_file,
338 read_symlink_func read_symlink,
339 enum grub_fshelp_filetype expecttype)
340 {
341 return grub_fshelp_find_file_real (path, rootnode, foundnode,
342 NULL, lookup_file,
343 read_symlink, expecttype);
344
345 }
346
347 /* Read LEN bytes from the file NODE on disk DISK into the buffer BUF,
348 beginning with the block POS. READ_HOOK should be set before
349 reading a block from the file. READ_HOOK_DATA is passed through as
350 the DATA argument to READ_HOOK. GET_BLOCK is used to translate
351 file blocks to disk blocks. The file is FILESIZE bytes big and the
352 blocks have a size of LOG2BLOCKSIZE (in log2). */
353 grub_ssize_t
354 grub_fshelp_read_file (grub_disk_t disk, grub_fshelp_node_t node,
355 grub_disk_read_hook_t read_hook, void *read_hook_data,
356 grub_off_t pos, grub_size_t len, char *buf,
357 grub_disk_addr_t (*get_block) (grub_fshelp_node_t node,
358 grub_disk_addr_t block),
359 grub_off_t filesize, int log2blocksize,
360 grub_disk_addr_t blocks_start)
361 {
362 grub_disk_addr_t i, blockcnt;
363 int blocksize = 1 << (log2blocksize + GRUB_DISK_SECTOR_BITS);
364
365 if (pos > filesize)
366 {
367 grub_error (GRUB_ERR_OUT_OF_RANGE,
368 N_("attempt to read past the end of file"));
369 return -1;
370 }
371
372 /* Adjust LEN so it we can't read past the end of the file. */
373 if (pos + len > filesize)
374 len = filesize - pos;
375
376 blockcnt = ((len + pos) + blocksize - 1) >> (log2blocksize + GRUB_DISK_SECTOR_BITS);
377
378 for (i = pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS); i < blockcnt; i++)
379 {
380 grub_disk_addr_t blknr;
381 int blockoff = pos & (blocksize - 1);
382 int blockend = blocksize;
383
384 int skipfirst = 0;
385
386 blknr = get_block (node, i);
387 if (grub_errno)
388 return -1;
389
390 blknr = blknr << log2blocksize;
391
392 /* Last block. */
393 if (i == blockcnt - 1)
394 {
395 blockend = (len + pos) & (blocksize - 1);
396
397 /* The last portion is exactly blocksize. */
398 if (! blockend)
399 blockend = blocksize;
400 }
401
402 /* First block. */
403 if (i == (pos >> (log2blocksize + GRUB_DISK_SECTOR_BITS)))
404 {
405 skipfirst = blockoff;
406 blockend -= skipfirst;
407 }
408
409 /* If the block number is 0 this block is not stored on disk but
410 is zero filled instead. */
411 if (blknr)
412 {
413 disk->read_hook = read_hook;
414 disk->read_hook_data = read_hook_data;
415
416 grub_disk_read (disk, blknr + blocks_start, skipfirst,
417 blockend, buf);
418 disk->read_hook = 0;
419 if (grub_errno)
420 return -1;
421 }
422 else if (read_hook != (grub_disk_read_hook_t)grub_disk_blocklist_read)
423 grub_memset (buf, 0, blockend);
424
425 buf += blocksize - skipfirst;
426 }
427
428 return len;
429 }