sfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /* sfs.c - Amiga Smart FileSystem. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2006,2007,2008,2009 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/file.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/disk.h>
  24. #include <grub/dl.h>
  25. #include <grub/types.h>
  26. #include <grub/fshelp.h>
  27. #include <grub/charset.h>
  28. #include <grub/safemath.h>
  29. GRUB_MOD_LICENSE ("GPLv3+");
  30. /* The common header for a block. */
  31. struct grub_sfs_bheader
  32. {
  33. grub_uint8_t magic[4];
  34. grub_uint32_t chksum;
  35. grub_uint32_t ipointtomyself;
  36. } GRUB_PACKED;
  37. /* The sfs rootblock. */
  38. struct grub_sfs_rblock
  39. {
  40. struct grub_sfs_bheader header;
  41. grub_uint32_t version;
  42. grub_uint32_t createtime;
  43. grub_uint8_t flags;
  44. grub_uint8_t unused1[31];
  45. grub_uint32_t blocksize;
  46. grub_uint8_t unused2[40];
  47. grub_uint8_t unused3[8];
  48. grub_uint32_t rootobject;
  49. grub_uint32_t btree;
  50. } GRUB_PACKED;
  51. enum
  52. {
  53. FLAGS_CASE_SENSITIVE = 0x80
  54. };
  55. /* A SFS object container. */
  56. struct grub_sfs_obj
  57. {
  58. grub_uint8_t unused1[4];
  59. grub_uint32_t nodeid;
  60. grub_uint8_t unused2[4];
  61. union
  62. {
  63. struct
  64. {
  65. grub_uint32_t first_block;
  66. grub_uint32_t size;
  67. } GRUB_PACKED file;
  68. struct
  69. {
  70. grub_uint32_t hashtable;
  71. grub_uint32_t dir_objc;
  72. } GRUB_PACKED dir;
  73. } file_dir;
  74. grub_uint32_t mtime;
  75. grub_uint8_t type;
  76. grub_uint8_t filename[1];
  77. grub_uint8_t comment[1];
  78. } GRUB_PACKED;
  79. #define GRUB_SFS_TYPE_DELETED 32
  80. #define GRUB_SFS_TYPE_SYMLINK 64
  81. #define GRUB_SFS_TYPE_DIR 128
  82. /* A SFS object container. */
  83. struct grub_sfs_objc
  84. {
  85. struct grub_sfs_bheader header;
  86. grub_uint32_t parent;
  87. grub_uint32_t next;
  88. grub_uint32_t prev;
  89. /* The amount of objects depends on the blocksize. */
  90. struct grub_sfs_obj objects[1];
  91. } GRUB_PACKED;
  92. struct grub_sfs_btree_node
  93. {
  94. grub_uint32_t key;
  95. grub_uint32_t data;
  96. } GRUB_PACKED;
  97. struct grub_sfs_btree_extent
  98. {
  99. grub_uint32_t key;
  100. grub_uint32_t next;
  101. grub_uint32_t prev;
  102. grub_uint16_t size;
  103. } GRUB_PACKED;
  104. struct grub_sfs_btree
  105. {
  106. struct grub_sfs_bheader header;
  107. grub_uint16_t nodes;
  108. grub_uint8_t leaf;
  109. grub_uint8_t nodesize;
  110. /* Normally this can be kind of node, but just extents are
  111. supported. */
  112. struct grub_sfs_btree_node node[1];
  113. } GRUB_PACKED;
  114. struct cache_entry
  115. {
  116. grub_uint32_t off;
  117. grub_uint32_t block;
  118. };
  119. struct grub_fshelp_node
  120. {
  121. struct grub_sfs_data *data;
  122. grub_uint32_t block;
  123. grub_uint32_t size;
  124. grub_uint32_t mtime;
  125. grub_uint32_t cache_off;
  126. grub_uint32_t next_extent;
  127. grub_size_t cache_allocated;
  128. grub_size_t cache_size;
  129. struct cache_entry *cache;
  130. };
  131. /* Information about a "mounted" sfs filesystem. */
  132. struct grub_sfs_data
  133. {
  134. struct grub_sfs_rblock rblock;
  135. struct grub_fshelp_node diropen;
  136. grub_disk_t disk;
  137. /* Log of blocksize in sectors. */
  138. int log_blocksize;
  139. int fshelp_flags;
  140. /* Label of the filesystem. */
  141. char *label;
  142. };
  143. static grub_dl_t my_mod;
  144. /* Lookup the extent starting with BLOCK in the filesystem described
  145. by DATA. Return the extent size in SIZE and the following extent
  146. in NEXTEXT. */
  147. static grub_err_t
  148. grub_sfs_read_extent (struct grub_sfs_data *data, unsigned int block,
  149. grub_uint32_t *size, grub_uint32_t *nextext)
  150. {
  151. char *treeblock;
  152. struct grub_sfs_btree *tree;
  153. int i;
  154. grub_uint32_t next;
  155. grub_size_t blocksize = GRUB_DISK_SECTOR_SIZE << data->log_blocksize;
  156. treeblock = grub_malloc (blocksize);
  157. if (!treeblock)
  158. return grub_errno;
  159. next = grub_be_to_cpu32 (data->rblock.btree);
  160. tree = (struct grub_sfs_btree *) treeblock;
  161. /* Handle this level in the btree. */
  162. do
  163. {
  164. grub_uint16_t nnodes;
  165. grub_disk_read (data->disk,
  166. ((grub_disk_addr_t) next) << data->log_blocksize,
  167. 0, blocksize, treeblock);
  168. if (grub_errno)
  169. {
  170. grub_free (treeblock);
  171. return grub_errno;
  172. }
  173. nnodes = grub_be_to_cpu16 (tree->nodes);
  174. if (nnodes * (grub_uint32_t) (tree)->nodesize > blocksize)
  175. break;
  176. for (i = (int) nnodes - 1; i >= 0; i--)
  177. {
  178. #define EXTNODE(tree, index) \
  179. ((struct grub_sfs_btree_node *) (((char *) &(tree)->node[0]) \
  180. + (index) * (tree)->nodesize))
  181. /* Follow the tree down to the leaf level. */
  182. if ((grub_be_to_cpu32 (EXTNODE(tree, i)->key) <= block)
  183. && !tree->leaf)
  184. {
  185. next = grub_be_to_cpu32 (EXTNODE (tree, i)->data);
  186. break;
  187. }
  188. /* If the leaf level is reached, just find the correct extent. */
  189. if (grub_be_to_cpu32 (EXTNODE (tree, i)->key) == block && tree->leaf)
  190. {
  191. struct grub_sfs_btree_extent *extent;
  192. extent = (struct grub_sfs_btree_extent *) EXTNODE (tree, i);
  193. /* We found a correct leaf. */
  194. *size = grub_be_to_cpu16 (extent->size);
  195. *nextext = grub_be_to_cpu32 (extent->next);
  196. grub_free (treeblock);
  197. return 0;
  198. }
  199. #undef EXTNODE
  200. }
  201. } while (!tree->leaf);
  202. grub_free (treeblock);
  203. return grub_error (GRUB_ERR_FILE_READ_ERROR, "SFS extent not found");
  204. }
  205. static grub_disk_addr_t
  206. grub_sfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
  207. {
  208. grub_uint32_t blk;
  209. grub_uint32_t size = 0;
  210. grub_uint32_t next = 0;
  211. grub_disk_addr_t off;
  212. struct grub_sfs_data *data = node->data;
  213. /* In case of the first block we don't have to lookup the
  214. extent, the minimum size is always 1. */
  215. if (fileblock == 0)
  216. return node->block;
  217. if (!node->cache)
  218. {
  219. grub_size_t cache_size;
  220. /* Assume half-max extents (32768 sectors). */
  221. cache_size = ((node->size >> (data->log_blocksize + GRUB_DISK_SECTOR_BITS
  222. + 15))
  223. + 3);
  224. if (cache_size < 8)
  225. cache_size = 8;
  226. node->cache_off = 0;
  227. node->next_extent = node->block;
  228. node->cache_size = 0;
  229. node->cache = grub_calloc (cache_size, sizeof (node->cache[0]));
  230. if (!node->cache)
  231. {
  232. grub_errno = 0;
  233. node->cache_allocated = 0;
  234. }
  235. else
  236. {
  237. node->cache_allocated = cache_size;
  238. node->cache[0].off = 0;
  239. node->cache[0].block = node->block;
  240. }
  241. }
  242. if (fileblock < node->cache_off)
  243. {
  244. unsigned int i = 0;
  245. int j, lg;
  246. for (lg = 0; node->cache_size >> lg; lg++);
  247. for (j = lg - 1; j >= 0; j--)
  248. if ((i | (1 << j)) < node->cache_size
  249. && node->cache[(i | (1 << j))].off <= fileblock)
  250. i |= (1 << j);
  251. return node->cache[i].block + fileblock - node->cache[i].off;
  252. }
  253. off = node->cache_off;
  254. blk = node->next_extent;
  255. while (blk)
  256. {
  257. grub_err_t err;
  258. err = grub_sfs_read_extent (node->data, blk, &size, &next);
  259. if (err)
  260. return 0;
  261. if (node->cache && node->cache_size >= node->cache_allocated)
  262. {
  263. struct cache_entry *e = node->cache;
  264. grub_size_t sz;
  265. if (grub_mul (node->cache_allocated, 2 * sizeof (e[0]), &sz))
  266. goto fail;
  267. e = grub_realloc (node->cache, sz);
  268. if (!e)
  269. {
  270. fail:
  271. grub_errno = 0;
  272. grub_free (node->cache);
  273. node->cache = 0;
  274. }
  275. else
  276. {
  277. node->cache_allocated *= 2;
  278. node->cache = e;
  279. }
  280. }
  281. if (node->cache)
  282. {
  283. node->cache_off = off + size;
  284. node->next_extent = next;
  285. node->cache[node->cache_size].off = off;
  286. node->cache[node->cache_size].block = blk;
  287. node->cache_size++;
  288. }
  289. if (fileblock - off < size)
  290. return fileblock - off + blk;
  291. off += size;
  292. blk = next;
  293. }
  294. grub_error (GRUB_ERR_FILE_READ_ERROR,
  295. "reading a SFS block outside the extent");
  296. return 0;
  297. }
  298. /* Read LEN bytes from the file described by DATA starting with byte
  299. POS. Return the amount of read bytes in READ. */
  300. static grub_ssize_t
  301. grub_sfs_read_file (grub_fshelp_node_t node,
  302. grub_disk_read_hook_t read_hook, void *read_hook_data,
  303. grub_off_t pos, grub_size_t len, char *buf)
  304. {
  305. return grub_fshelp_read_file (node->data->disk, node,
  306. read_hook, read_hook_data,
  307. pos, len, buf, grub_sfs_read_block,
  308. node->size, node->data->log_blocksize, 0);
  309. }
  310. static struct grub_sfs_data *
  311. grub_sfs_mount (grub_disk_t disk)
  312. {
  313. struct grub_sfs_data *data;
  314. struct grub_sfs_objc *rootobjc;
  315. char *rootobjc_data = 0;
  316. grub_uint32_t blk;
  317. unsigned int max_len;
  318. data = grub_malloc (sizeof (*data));
  319. if (!data)
  320. return 0;
  321. /* Read the rootblock. */
  322. grub_disk_read (disk, 0, 0, sizeof (struct grub_sfs_rblock),
  323. &data->rblock);
  324. if (grub_errno)
  325. goto fail;
  326. /* Make sure this is a sfs filesystem. */
  327. if (grub_strncmp ((char *) (data->rblock.header.magic), "SFS", 4)
  328. || data->rblock.blocksize == 0
  329. || (data->rblock.blocksize & (data->rblock.blocksize - 1)) != 0
  330. || (data->rblock.blocksize & grub_cpu_to_be32_compile_time (0xf00001ff)))
  331. {
  332. grub_error (GRUB_ERR_BAD_FS, "not a SFS filesystem");
  333. goto fail;
  334. }
  335. for (data->log_blocksize = 9;
  336. (1U << data->log_blocksize) < grub_be_to_cpu32 (data->rblock.blocksize);
  337. data->log_blocksize++);
  338. data->log_blocksize -= GRUB_DISK_SECTOR_BITS;
  339. if (data->rblock.flags & FLAGS_CASE_SENSITIVE)
  340. data->fshelp_flags = 0;
  341. else
  342. data->fshelp_flags = GRUB_FSHELP_CASE_INSENSITIVE;
  343. rootobjc_data = grub_malloc (GRUB_DISK_SECTOR_SIZE << data->log_blocksize);
  344. if (! rootobjc_data)
  345. goto fail;
  346. /* Read the root object container. */
  347. grub_disk_read (disk, ((grub_disk_addr_t) grub_be_to_cpu32 (data->rblock.rootobject))
  348. << data->log_blocksize, 0,
  349. GRUB_DISK_SECTOR_SIZE << data->log_blocksize, rootobjc_data);
  350. if (grub_errno)
  351. goto fail;
  352. rootobjc = (struct grub_sfs_objc *) rootobjc_data;
  353. blk = grub_be_to_cpu32 (rootobjc->objects[0].file_dir.dir.dir_objc);
  354. data->diropen.size = 0;
  355. data->diropen.block = blk;
  356. data->diropen.data = data;
  357. data->diropen.cache = 0;
  358. data->disk = disk;
  359. /* We only read 1 block of data, so truncate the name if needed. */
  360. max_len = ((GRUB_DISK_SECTOR_SIZE << data->log_blocksize)
  361. - 24 /* offsetof (struct grub_sfs_objc, objects) */
  362. - 25); /* offsetof (struct grub_sfs_obj, filename) */
  363. data->label = grub_zalloc (max_len + 1);
  364. grub_strncpy (data->label, (char *) rootobjc->objects[0].filename, max_len);
  365. grub_free (rootobjc_data);
  366. return data;
  367. fail:
  368. if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
  369. grub_error (GRUB_ERR_BAD_FS, "not an SFS filesystem");
  370. grub_free (data);
  371. grub_free (rootobjc_data);
  372. return 0;
  373. }
  374. static char *
  375. grub_sfs_read_symlink (grub_fshelp_node_t node)
  376. {
  377. struct grub_sfs_data *data = node->data;
  378. char *symlink;
  379. char *block;
  380. block = grub_malloc (GRUB_DISK_SECTOR_SIZE << data->log_blocksize);
  381. if (!block)
  382. return 0;
  383. grub_disk_read (data->disk, ((grub_disk_addr_t) node->block)
  384. << data->log_blocksize,
  385. 0, GRUB_DISK_SECTOR_SIZE << data->log_blocksize, block);
  386. if (grub_errno)
  387. {
  388. grub_free (block);
  389. return 0;
  390. }
  391. /* This is just a wild guess, but it always worked for me. How the
  392. SLNK block looks like is not documented in the SFS docs. */
  393. symlink = grub_malloc (((GRUB_DISK_SECTOR_SIZE << data->log_blocksize)
  394. - 24) * GRUB_MAX_UTF8_PER_LATIN1 + 1);
  395. if (!symlink)
  396. {
  397. grub_free (block);
  398. return 0;
  399. }
  400. *grub_latin1_to_utf8 ((grub_uint8_t *) symlink, (grub_uint8_t *) &block[24],
  401. (GRUB_DISK_SECTOR_SIZE << data->log_blocksize) - 24) = '\0';
  402. grub_free (block);
  403. return symlink;
  404. }
  405. /* Helper for grub_sfs_iterate_dir. */
  406. static int
  407. grub_sfs_create_node (struct grub_fshelp_node **node,
  408. struct grub_sfs_data *data,
  409. const char *name,
  410. grub_uint32_t block, grub_uint32_t size, int type,
  411. grub_uint32_t mtime,
  412. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  413. {
  414. grub_size_t len = grub_strlen (name);
  415. grub_uint8_t *name_u8;
  416. int ret;
  417. grub_size_t sz;
  418. if (grub_mul (len, GRUB_MAX_UTF8_PER_LATIN1, &sz) ||
  419. grub_add (sz, 1, &sz))
  420. return 1;
  421. *node = grub_malloc (sizeof (**node));
  422. if (!*node)
  423. return 1;
  424. name_u8 = grub_malloc (sz);
  425. if (!name_u8)
  426. {
  427. grub_free (*node);
  428. return 1;
  429. }
  430. (*node)->data = data;
  431. (*node)->size = size;
  432. (*node)->block = block;
  433. (*node)->mtime = mtime;
  434. (*node)->cache = 0;
  435. (*node)->cache_off = 0;
  436. (*node)->next_extent = block;
  437. (*node)->cache_size = 0;
  438. (*node)->cache_allocated = 0;
  439. *grub_latin1_to_utf8 (name_u8, (const grub_uint8_t *) name, len) = '\0';
  440. ret = hook ((char *) name_u8, type | data->fshelp_flags, *node, hook_data);
  441. grub_free (name_u8);
  442. return ret;
  443. }
  444. static int
  445. grub_sfs_iterate_dir (grub_fshelp_node_t dir,
  446. grub_fshelp_iterate_dir_hook_t hook, void *hook_data)
  447. {
  448. struct grub_fshelp_node *node = 0;
  449. struct grub_sfs_data *data = dir->data;
  450. char *objc_data;
  451. struct grub_sfs_objc *objc;
  452. unsigned int next = dir->block;
  453. grub_uint32_t pos;
  454. objc_data = grub_malloc (GRUB_DISK_SECTOR_SIZE << data->log_blocksize);
  455. if (!objc_data)
  456. goto fail;
  457. /* The Object container can consist of multiple blocks, iterate over
  458. every block. */
  459. while (next)
  460. {
  461. grub_disk_read (data->disk, ((grub_disk_addr_t) next)
  462. << data->log_blocksize, 0,
  463. GRUB_DISK_SECTOR_SIZE << data->log_blocksize, objc_data);
  464. if (grub_errno)
  465. goto fail;
  466. objc = (struct grub_sfs_objc *) objc_data;
  467. pos = (char *) &objc->objects[0] - (char *) objc;
  468. /* Iterate over all entries in this block. */
  469. while (pos + sizeof (struct grub_sfs_obj)
  470. < (1U << (GRUB_DISK_SECTOR_BITS + data->log_blocksize)))
  471. {
  472. struct grub_sfs_obj *obj;
  473. obj = (struct grub_sfs_obj *) ((char *) objc + pos);
  474. const char *filename = (const char *) obj->filename;
  475. grub_size_t len;
  476. enum grub_fshelp_filetype type;
  477. grub_uint32_t block;
  478. /* The filename and comment dynamically increase the size of
  479. the object. */
  480. len = grub_strlen (filename);
  481. len += grub_strlen (filename + len + 1);
  482. pos += sizeof (*obj) + len;
  483. /* Round up to a multiple of two bytes. */
  484. pos = ((pos + 1) >> 1) << 1;
  485. if (filename[0] == 0)
  486. continue;
  487. /* First check if the file was not deleted. */
  488. if (obj->type & GRUB_SFS_TYPE_DELETED)
  489. continue;
  490. else if (obj->type & GRUB_SFS_TYPE_SYMLINK)
  491. type = GRUB_FSHELP_SYMLINK;
  492. else if (obj->type & GRUB_SFS_TYPE_DIR)
  493. type = GRUB_FSHELP_DIR;
  494. else
  495. type = GRUB_FSHELP_REG;
  496. if (type == GRUB_FSHELP_DIR)
  497. block = grub_be_to_cpu32 (obj->file_dir.dir.dir_objc);
  498. else
  499. block = grub_be_to_cpu32 (obj->file_dir.file.first_block);
  500. if (grub_sfs_create_node (&node, data, filename, block,
  501. grub_be_to_cpu32 (obj->file_dir.file.size),
  502. type, grub_be_to_cpu32 (obj->mtime),
  503. hook, hook_data))
  504. {
  505. grub_free (objc_data);
  506. return 1;
  507. }
  508. }
  509. next = grub_be_to_cpu32 (objc->next);
  510. }
  511. fail:
  512. grub_free (objc_data);
  513. return 0;
  514. }
  515. /* Open a file named NAME and initialize FILE. */
  516. static grub_err_t
  517. grub_sfs_open (struct grub_file *file, const char *name)
  518. {
  519. struct grub_sfs_data *data;
  520. struct grub_fshelp_node *fdiro = 0;
  521. grub_dl_ref (my_mod);
  522. data = grub_sfs_mount (file->device->disk);
  523. if (!data)
  524. goto fail;
  525. grub_fshelp_find_file (name, &data->diropen, &fdiro, grub_sfs_iterate_dir,
  526. grub_sfs_read_symlink, GRUB_FSHELP_REG);
  527. if (grub_errno)
  528. goto fail;
  529. file->size = fdiro->size;
  530. data->diropen = *fdiro;
  531. grub_free (fdiro);
  532. file->data = data;
  533. file->offset = 0;
  534. return 0;
  535. fail:
  536. if (data && fdiro != &data->diropen)
  537. grub_free (fdiro);
  538. if (data)
  539. grub_free (data->label);
  540. grub_free (data);
  541. grub_dl_unref (my_mod);
  542. return grub_errno;
  543. }
  544. static grub_err_t
  545. grub_sfs_close (grub_file_t file)
  546. {
  547. struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
  548. grub_free (data->diropen.cache);
  549. grub_free (data->label);
  550. grub_free (data);
  551. grub_dl_unref (my_mod);
  552. return GRUB_ERR_NONE;
  553. }
  554. /* Read LEN bytes data from FILE into BUF. */
  555. static grub_ssize_t
  556. grub_sfs_read (grub_file_t file, char *buf, grub_size_t len)
  557. {
  558. struct grub_sfs_data *data = (struct grub_sfs_data *) file->data;
  559. return grub_sfs_read_file (&data->diropen,
  560. file->read_hook, file->read_hook_data,
  561. file->offset, len, buf);
  562. }
  563. /* Context for grub_sfs_dir. */
  564. struct grub_sfs_dir_ctx
  565. {
  566. grub_fs_dir_hook_t hook;
  567. void *hook_data;
  568. };
  569. /* Helper for grub_sfs_dir. */
  570. static int
  571. grub_sfs_dir_iter (const char *filename, enum grub_fshelp_filetype filetype,
  572. grub_fshelp_node_t node, void *data)
  573. {
  574. struct grub_sfs_dir_ctx *ctx = data;
  575. struct grub_dirhook_info info;
  576. grub_memset (&info, 0, sizeof (info));
  577. info.dir = ((filetype & GRUB_FSHELP_TYPE_MASK) == GRUB_FSHELP_DIR);
  578. info.mtime = node->mtime + 8 * 365 * 86400 + 86400 * 2;
  579. info.mtimeset = 1;
  580. grub_free (node->cache);
  581. grub_free (node);
  582. return ctx->hook (filename, &info, ctx->hook_data);
  583. }
  584. static grub_err_t
  585. grub_sfs_dir (grub_device_t device, const char *path,
  586. grub_fs_dir_hook_t hook, void *hook_data)
  587. {
  588. struct grub_sfs_dir_ctx ctx = { hook, hook_data };
  589. struct grub_sfs_data *data = 0;
  590. struct grub_fshelp_node *fdiro = 0;
  591. grub_dl_ref (my_mod);
  592. data = grub_sfs_mount (device->disk);
  593. if (!data)
  594. goto fail;
  595. grub_fshelp_find_file (path, &data->diropen, &fdiro, grub_sfs_iterate_dir,
  596. grub_sfs_read_symlink, GRUB_FSHELP_DIR);
  597. if (grub_errno)
  598. goto fail;
  599. grub_sfs_iterate_dir (fdiro, grub_sfs_dir_iter, &ctx);
  600. fail:
  601. if (data && fdiro != &data->diropen)
  602. grub_free (fdiro);
  603. if (data)
  604. grub_free (data->label);
  605. grub_free (data);
  606. grub_dl_unref (my_mod);
  607. return grub_errno;
  608. }
  609. static grub_err_t
  610. grub_sfs_label (grub_device_t device, char **label)
  611. {
  612. struct grub_sfs_data *data;
  613. grub_disk_t disk = device->disk;
  614. data = grub_sfs_mount (disk);
  615. if (data)
  616. {
  617. grub_size_t sz, len = grub_strlen (data->label);
  618. if (grub_mul (len, GRUB_MAX_UTF8_PER_LATIN1, &sz) ||
  619. grub_add (sz, 1, &sz))
  620. return GRUB_ERR_OUT_OF_RANGE;
  621. *label = grub_malloc (sz);
  622. if (*label)
  623. *grub_latin1_to_utf8 ((grub_uint8_t *) *label,
  624. (const grub_uint8_t *) data->label,
  625. len) = '\0';
  626. grub_free (data->label);
  627. }
  628. grub_free (data);
  629. return grub_errno;
  630. }
  631. static struct grub_fs grub_sfs_fs =
  632. {
  633. .name = "sfs",
  634. .fs_dir = grub_sfs_dir,
  635. .fs_open = grub_sfs_open,
  636. .fs_read = grub_sfs_read,
  637. .fs_close = grub_sfs_close,
  638. .fs_label = grub_sfs_label,
  639. #ifdef GRUB_UTIL
  640. .reserved_first_sector = 0,
  641. .blocklist_install = 1,
  642. #endif
  643. .next = 0
  644. };
  645. GRUB_MOD_INIT(sfs)
  646. {
  647. grub_fs_register (&grub_sfs_fs);
  648. my_mod = mod;
  649. }
  650. GRUB_MOD_FINI(sfs)
  651. {
  652. grub_fs_unregister (&grub_sfs_fs);
  653. }