fshelp.c 12 KB

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