ext2fs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * (C) Copyright 2004
  3. * esd gmbh <www.esd-electronics.com>
  4. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  5. *
  6. * based on code from grub2 fs/ext2.c and fs/fshelp.c by
  7. *
  8. * GRUB -- GRand Unified Bootloader
  9. * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <common.h>
  26. #include <ext2fs.h>
  27. #include <malloc.h>
  28. #include <asm/byteorder.h>
  29. extern int ext2fs_devread (int sector, int byte_offset, int byte_len,
  30. char *buf);
  31. /* Magic value used to identify an ext2 filesystem. */
  32. #define EXT2_MAGIC 0xEF53
  33. /* Amount of indirect blocks in an inode. */
  34. #define INDIRECT_BLOCKS 12
  35. /* Maximum lenght of a pathname. */
  36. #define EXT2_PATH_MAX 4096
  37. /* Maximum nesting of symlinks, used to prevent a loop. */
  38. #define EXT2_MAX_SYMLINKCNT 8
  39. /* Filetype used in directory entry. */
  40. #define FILETYPE_UNKNOWN 0
  41. #define FILETYPE_REG 1
  42. #define FILETYPE_DIRECTORY 2
  43. #define FILETYPE_SYMLINK 7
  44. /* Filetype information as used in inodes. */
  45. #define FILETYPE_INO_MASK 0170000
  46. #define FILETYPE_INO_REG 0100000
  47. #define FILETYPE_INO_DIRECTORY 0040000
  48. #define FILETYPE_INO_SYMLINK 0120000
  49. /* Bits used as offset in sector */
  50. #define DISK_SECTOR_BITS 9
  51. /* Log2 size of ext2 block in 512 blocks. */
  52. #define LOG2_EXT2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 1)
  53. /* Log2 size of ext2 block in bytes. */
  54. #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 10)
  55. /* The size of an ext2 block in bytes. */
  56. #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
  57. /* The ext2 superblock. */
  58. struct ext2_sblock {
  59. uint32_t total_inodes;
  60. uint32_t total_blocks;
  61. uint32_t reserved_blocks;
  62. uint32_t free_blocks;
  63. uint32_t free_inodes;
  64. uint32_t first_data_block;
  65. uint32_t log2_block_size;
  66. uint32_t log2_fragment_size;
  67. uint32_t blocks_per_group;
  68. uint32_t fragments_per_group;
  69. uint32_t inodes_per_group;
  70. uint32_t mtime;
  71. uint32_t utime;
  72. uint16_t mnt_count;
  73. uint16_t max_mnt_count;
  74. uint16_t magic;
  75. uint16_t fs_state;
  76. uint16_t error_handling;
  77. uint16_t minor_revision_level;
  78. uint32_t lastcheck;
  79. uint32_t checkinterval;
  80. uint32_t creator_os;
  81. uint32_t revision_level;
  82. uint16_t uid_reserved;
  83. uint16_t gid_reserved;
  84. uint32_t first_inode;
  85. uint16_t inode_size;
  86. uint16_t block_group_number;
  87. uint32_t feature_compatibility;
  88. uint32_t feature_incompat;
  89. uint32_t feature_ro_compat;
  90. uint32_t unique_id[4];
  91. char volume_name[16];
  92. char last_mounted_on[64];
  93. uint32_t compression_info;
  94. };
  95. /* The ext2 blockgroup. */
  96. struct ext2_block_group {
  97. uint32_t block_id;
  98. uint32_t inode_id;
  99. uint32_t inode_table_id;
  100. uint16_t free_blocks;
  101. uint16_t free_inodes;
  102. uint16_t used_dir_cnt;
  103. uint32_t reserved[3];
  104. };
  105. /* The ext2 inode. */
  106. struct ext2_inode {
  107. uint16_t mode;
  108. uint16_t uid;
  109. uint32_t size;
  110. uint32_t atime;
  111. uint32_t ctime;
  112. uint32_t mtime;
  113. uint32_t dtime;
  114. uint16_t gid;
  115. uint16_t nlinks;
  116. uint32_t blockcnt; /* Blocks of 512 bytes!! */
  117. uint32_t flags;
  118. uint32_t osd1;
  119. union {
  120. struct datablocks {
  121. uint32_t dir_blocks[INDIRECT_BLOCKS];
  122. uint32_t indir_block;
  123. uint32_t double_indir_block;
  124. uint32_t tripple_indir_block;
  125. } blocks;
  126. char symlink[60];
  127. } b;
  128. uint32_t version;
  129. uint32_t acl;
  130. uint32_t dir_acl;
  131. uint32_t fragment_addr;
  132. uint32_t osd2[3];
  133. };
  134. /* The header of an ext2 directory entry. */
  135. struct ext2_dirent {
  136. uint32_t inode;
  137. uint16_t direntlen;
  138. uint8_t namelen;
  139. uint8_t filetype;
  140. };
  141. struct ext2fs_node {
  142. struct ext2_data *data;
  143. struct ext2_inode inode;
  144. int ino;
  145. int inode_read;
  146. };
  147. /* Information about a "mounted" ext2 filesystem. */
  148. struct ext2_data {
  149. struct ext2_sblock sblock;
  150. struct ext2_inode *inode;
  151. struct ext2fs_node diropen;
  152. };
  153. typedef struct ext2fs_node *ext2fs_node_t;
  154. struct ext2_data *ext2fs_root = NULL;
  155. ext2fs_node_t ext2fs_file = NULL;
  156. int symlinknest = 0;
  157. uint32_t *indir1_block = NULL;
  158. int indir1_size = 0;
  159. int indir1_blkno = -1;
  160. uint32_t *indir2_block = NULL;
  161. int indir2_size = 0;
  162. int indir2_blkno = -1;
  163. static unsigned int inode_size;
  164. static int ext2fs_blockgroup
  165. (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
  166. unsigned int blkno;
  167. unsigned int blkoff;
  168. unsigned int desc_per_blk;
  169. desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
  170. blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
  171. group / desc_per_blk;
  172. blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
  173. #ifdef DEBUG
  174. printf ("ext2fs read %d group descriptor (blkno %d blkoff %d)\n",
  175. group, blkno, blkoff);
  176. #endif
  177. return (ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE(data),
  178. blkoff, sizeof(struct ext2_block_group), (char *)blkgrp));
  179. }
  180. static int ext2fs_read_inode
  181. (struct ext2_data *data, int ino, struct ext2_inode *inode) {
  182. struct ext2_block_group blkgrp;
  183. struct ext2_sblock *sblock = &data->sblock;
  184. int inodes_per_block;
  185. int status;
  186. unsigned int blkno;
  187. unsigned int blkoff;
  188. #ifdef DEBUG
  189. printf ("ext2fs read inode %d, inode_size %d\n", ino, inode_size);
  190. #endif
  191. /* It is easier to calculate if the first inode is 0. */
  192. ino--;
  193. status = ext2fs_blockgroup (data, ino / __le32_to_cpu
  194. (sblock->inodes_per_group), &blkgrp);
  195. if (status == 0) {
  196. return (0);
  197. }
  198. inodes_per_block = EXT2_BLOCK_SIZE(data) / inode_size;
  199. blkno = __le32_to_cpu (blkgrp.inode_table_id) +
  200. (ino % __le32_to_cpu (sblock->inodes_per_group))
  201. / inodes_per_block;
  202. blkoff = (ino % inodes_per_block) * inode_size;
  203. #ifdef DEBUG
  204. printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
  205. #endif
  206. /* Read the inode. */
  207. status = ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE (data), blkoff,
  208. sizeof (struct ext2_inode), (char *) inode);
  209. if (status == 0) {
  210. return (0);
  211. }
  212. return (1);
  213. }
  214. void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) {
  215. if ((node != &ext2fs_root->diropen) && (node != currroot)) {
  216. free (node);
  217. }
  218. }
  219. static int ext2fs_read_block (ext2fs_node_t node, int fileblock) {
  220. struct ext2_data *data = node->data;
  221. struct ext2_inode *inode = &node->inode;
  222. int blknr;
  223. int blksz = EXT2_BLOCK_SIZE (data);
  224. int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
  225. int status;
  226. /* Direct blocks. */
  227. if (fileblock < INDIRECT_BLOCKS) {
  228. blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
  229. }
  230. /* Indirect. */
  231. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
  232. if (indir1_block == NULL) {
  233. indir1_block = (uint32_t *) malloc (blksz);
  234. if (indir1_block == NULL) {
  235. printf ("** ext2fs read block (indir 1) malloc failed. **\n");
  236. return (-1);
  237. }
  238. indir1_size = blksz;
  239. indir1_blkno = -1;
  240. }
  241. if (blksz != indir1_size) {
  242. free (indir1_block);
  243. indir1_block = NULL;
  244. indir1_size = 0;
  245. indir1_blkno = -1;
  246. indir1_block = (uint32_t *) malloc (blksz);
  247. if (indir1_block == NULL) {
  248. printf ("** ext2fs read block (indir 1) malloc failed. **\n");
  249. return (-1);
  250. }
  251. indir1_size = blksz;
  252. }
  253. if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
  254. log2_blksz) != indir1_blkno) {
  255. status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
  256. 0, blksz,
  257. (char *) indir1_block);
  258. if (status == 0) {
  259. printf ("** ext2fs read block (indir 1) failed. **\n");
  260. return (0);
  261. }
  262. indir1_blkno =
  263. __le32_to_cpu (inode->b.blocks.
  264. indir_block) << log2_blksz;
  265. }
  266. blknr = __le32_to_cpu (indir1_block
  267. [fileblock - INDIRECT_BLOCKS]);
  268. }
  269. /* Double indirect. */
  270. else if (fileblock <
  271. (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
  272. unsigned int perblock = blksz / 4;
  273. unsigned int rblock = fileblock - (INDIRECT_BLOCKS
  274. + blksz / 4);
  275. if (indir1_block == NULL) {
  276. indir1_block = (uint32_t *) malloc (blksz);
  277. if (indir1_block == NULL) {
  278. printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
  279. return (-1);
  280. }
  281. indir1_size = blksz;
  282. indir1_blkno = -1;
  283. }
  284. if (blksz != indir1_size) {
  285. free (indir1_block);
  286. indir1_block = NULL;
  287. indir1_size = 0;
  288. indir1_blkno = -1;
  289. indir1_block = (uint32_t *) malloc (blksz);
  290. if (indir1_block == NULL) {
  291. printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
  292. return (-1);
  293. }
  294. indir1_size = blksz;
  295. }
  296. if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
  297. log2_blksz) != indir1_blkno) {
  298. status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
  299. 0, blksz,
  300. (char *) indir1_block);
  301. if (status == 0) {
  302. printf ("** ext2fs read block (indir 2 1) failed. **\n");
  303. return (-1);
  304. }
  305. indir1_blkno =
  306. __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
  307. }
  308. if (indir2_block == NULL) {
  309. indir2_block = (uint32_t *) malloc (blksz);
  310. if (indir2_block == NULL) {
  311. printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
  312. return (-1);
  313. }
  314. indir2_size = blksz;
  315. indir2_blkno = -1;
  316. }
  317. if (blksz != indir2_size) {
  318. free (indir2_block);
  319. indir2_block = NULL;
  320. indir2_size = 0;
  321. indir2_blkno = -1;
  322. indir2_block = (uint32_t *) malloc (blksz);
  323. if (indir2_block == NULL) {
  324. printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
  325. return (-1);
  326. }
  327. indir2_size = blksz;
  328. }
  329. if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
  330. log2_blksz) != indir2_blkno) {
  331. status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
  332. 0, blksz,
  333. (char *) indir2_block);
  334. if (status == 0) {
  335. printf ("** ext2fs read block (indir 2 2) failed. **\n");
  336. return (-1);
  337. }
  338. indir2_blkno =
  339. __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
  340. }
  341. blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
  342. }
  343. /* Tripple indirect. */
  344. else {
  345. printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
  346. return (-1);
  347. }
  348. #ifdef DEBUG
  349. printf ("ext2fs_read_block %08x\n", blknr);
  350. #endif
  351. return (blknr);
  352. }
  353. int ext2fs_read_file
  354. (ext2fs_node_t node, int pos, unsigned int len, char *buf) {
  355. int i;
  356. int blockcnt;
  357. int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
  358. int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
  359. unsigned int filesize = __le32_to_cpu(node->inode.size);
  360. /* Adjust len so it we can't read past the end of the file. */
  361. if (len > filesize) {
  362. len = filesize;
  363. }
  364. blockcnt = ((len + pos) + blocksize - 1) / blocksize;
  365. for (i = pos / blocksize; i < blockcnt; i++) {
  366. int blknr;
  367. int blockoff = pos % blocksize;
  368. int blockend = blocksize;
  369. int skipfirst = 0;
  370. blknr = ext2fs_read_block (node, i);
  371. if (blknr < 0) {
  372. return (-1);
  373. }
  374. blknr = blknr << log2blocksize;
  375. /* Last block. */
  376. if (i == blockcnt - 1) {
  377. blockend = (len + pos) % blocksize;
  378. /* The last portion is exactly blocksize. */
  379. if (!blockend) {
  380. blockend = blocksize;
  381. }
  382. }
  383. /* First block. */
  384. if (i == pos / blocksize) {
  385. skipfirst = blockoff;
  386. blockend -= skipfirst;
  387. }
  388. /* If the block number is 0 this block is not stored on disk but
  389. is zero filled instead. */
  390. if (blknr) {
  391. int status;
  392. status = ext2fs_devread (blknr, skipfirst, blockend, buf);
  393. if (status == 0) {
  394. return (-1);
  395. }
  396. } else {
  397. memset (buf, 0, blocksize - skipfirst);
  398. }
  399. buf += blocksize - skipfirst;
  400. }
  401. return (len);
  402. }
  403. static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
  404. {
  405. unsigned int fpos = 0;
  406. int status;
  407. struct ext2fs_node *diro = (struct ext2fs_node *) dir;
  408. #ifdef DEBUG
  409. if (name != NULL)
  410. printf ("Iterate dir %s\n", name);
  411. #endif /* of DEBUG */
  412. if (!diro->inode_read) {
  413. status = ext2fs_read_inode (diro->data, diro->ino,
  414. &diro->inode);
  415. if (status == 0) {
  416. return (0);
  417. }
  418. }
  419. /* Search the file. */
  420. while (fpos < __le32_to_cpu (diro->inode.size)) {
  421. struct ext2_dirent dirent;
  422. status = ext2fs_read_file (diro, fpos,
  423. sizeof (struct ext2_dirent),
  424. (char *) &dirent);
  425. if (status < 1) {
  426. return (0);
  427. }
  428. if (dirent.namelen != 0) {
  429. char filename[dirent.namelen + 1];
  430. ext2fs_node_t fdiro;
  431. int type = FILETYPE_UNKNOWN;
  432. status = ext2fs_read_file (diro,
  433. fpos + sizeof (struct ext2_dirent),
  434. dirent.namelen, filename);
  435. if (status < 1) {
  436. return (0);
  437. }
  438. fdiro = malloc (sizeof (struct ext2fs_node));
  439. if (!fdiro) {
  440. return (0);
  441. }
  442. fdiro->data = diro->data;
  443. fdiro->ino = __le32_to_cpu (dirent.inode);
  444. filename[dirent.namelen] = '\0';
  445. if (dirent.filetype != FILETYPE_UNKNOWN) {
  446. fdiro->inode_read = 0;
  447. if (dirent.filetype == FILETYPE_DIRECTORY) {
  448. type = FILETYPE_DIRECTORY;
  449. } else if (dirent.filetype ==
  450. FILETYPE_SYMLINK) {
  451. type = FILETYPE_SYMLINK;
  452. } else if (dirent.filetype == FILETYPE_REG) {
  453. type = FILETYPE_REG;
  454. }
  455. } else {
  456. /* The filetype can not be read from the dirent, get it from inode */
  457. status = ext2fs_read_inode (diro->data,
  458. __le32_to_cpu(dirent.inode),
  459. &fdiro->inode);
  460. if (status == 0) {
  461. free (fdiro);
  462. return (0);
  463. }
  464. fdiro->inode_read = 1;
  465. if ((__le16_to_cpu (fdiro->inode.mode) &
  466. FILETYPE_INO_MASK) ==
  467. FILETYPE_INO_DIRECTORY) {
  468. type = FILETYPE_DIRECTORY;
  469. } else if ((__le16_to_cpu (fdiro->inode.mode)
  470. & FILETYPE_INO_MASK) ==
  471. FILETYPE_INO_SYMLINK) {
  472. type = FILETYPE_SYMLINK;
  473. } else if ((__le16_to_cpu (fdiro->inode.mode)
  474. & FILETYPE_INO_MASK) ==
  475. FILETYPE_INO_REG) {
  476. type = FILETYPE_REG;
  477. }
  478. }
  479. #ifdef DEBUG
  480. printf ("iterate >%s<\n", filename);
  481. #endif /* of DEBUG */
  482. if ((name != NULL) && (fnode != NULL)
  483. && (ftype != NULL)) {
  484. if (strcmp (filename, name) == 0) {
  485. *ftype = type;
  486. *fnode = fdiro;
  487. return (1);
  488. }
  489. } else {
  490. if (fdiro->inode_read == 0) {
  491. status = ext2fs_read_inode (diro->data,
  492. __le32_to_cpu (dirent.inode),
  493. &fdiro->inode);
  494. if (status == 0) {
  495. free (fdiro);
  496. return (0);
  497. }
  498. fdiro->inode_read = 1;
  499. }
  500. switch (type) {
  501. case FILETYPE_DIRECTORY:
  502. printf ("<DIR> ");
  503. break;
  504. case FILETYPE_SYMLINK:
  505. printf ("<SYM> ");
  506. break;
  507. case FILETYPE_REG:
  508. printf (" ");
  509. break;
  510. default:
  511. printf ("< ? > ");
  512. break;
  513. }
  514. printf ("%10d %s\n",
  515. __le32_to_cpu (fdiro->inode.size),
  516. filename);
  517. }
  518. free (fdiro);
  519. }
  520. fpos += __le16_to_cpu (dirent.direntlen);
  521. }
  522. return (0);
  523. }
  524. static char *ext2fs_read_symlink (ext2fs_node_t node) {
  525. char *symlink;
  526. struct ext2fs_node *diro = node;
  527. int status;
  528. if (!diro->inode_read) {
  529. status = ext2fs_read_inode (diro->data, diro->ino,
  530. &diro->inode);
  531. if (status == 0) {
  532. return (0);
  533. }
  534. }
  535. symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
  536. if (!symlink) {
  537. return (0);
  538. }
  539. /* If the filesize of the symlink is bigger than
  540. 60 the symlink is stored in a separate block,
  541. otherwise it is stored in the inode. */
  542. if (__le32_to_cpu (diro->inode.size) <= 60) {
  543. strncpy (symlink, diro->inode.b.symlink,
  544. __le32_to_cpu (diro->inode.size));
  545. } else {
  546. status = ext2fs_read_file (diro, 0,
  547. __le32_to_cpu (diro->inode.size),
  548. symlink);
  549. if (status == 0) {
  550. free (symlink);
  551. return (0);
  552. }
  553. }
  554. symlink[__le32_to_cpu (diro->inode.size)] = '\0';
  555. return (symlink);
  556. }
  557. int ext2fs_find_file1
  558. (const char *currpath,
  559. ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) {
  560. char fpath[strlen (currpath) + 1];
  561. char *name = fpath;
  562. char *next;
  563. int status;
  564. int type = FILETYPE_DIRECTORY;
  565. ext2fs_node_t currnode = currroot;
  566. ext2fs_node_t oldnode = currroot;
  567. strncpy (fpath, currpath, strlen (currpath) + 1);
  568. /* Remove all leading slashes. */
  569. while (*name == '/') {
  570. name++;
  571. }
  572. if (!*name) {
  573. *currfound = currnode;
  574. return (1);
  575. }
  576. for (;;) {
  577. int found;
  578. /* Extract the actual part from the pathname. */
  579. next = strchr (name, '/');
  580. if (next) {
  581. /* Remove all leading slashes. */
  582. while (*next == '/') {
  583. *(next++) = '\0';
  584. }
  585. }
  586. /* At this point it is expected that the current node is a directory, check if this is true. */
  587. if (type != FILETYPE_DIRECTORY) {
  588. ext2fs_free_node (currnode, currroot);
  589. return (0);
  590. }
  591. oldnode = currnode;
  592. /* Iterate over the directory. */
  593. found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
  594. if (found == 0) {
  595. return (0);
  596. }
  597. if (found == -1) {
  598. break;
  599. }
  600. /* Read in the symlink and follow it. */
  601. if (type == FILETYPE_SYMLINK) {
  602. char *symlink;
  603. /* Test if the symlink does not loop. */
  604. if (++symlinknest == 8) {
  605. ext2fs_free_node (currnode, currroot);
  606. ext2fs_free_node (oldnode, currroot);
  607. return (0);
  608. }
  609. symlink = ext2fs_read_symlink (currnode);
  610. ext2fs_free_node (currnode, currroot);
  611. if (!symlink) {
  612. ext2fs_free_node (oldnode, currroot);
  613. return (0);
  614. }
  615. #ifdef DEBUG
  616. printf ("Got symlink >%s<\n", symlink);
  617. #endif /* of DEBUG */
  618. /* The symlink is an absolute path, go back to the root inode. */
  619. if (symlink[0] == '/') {
  620. ext2fs_free_node (oldnode, currroot);
  621. oldnode = &ext2fs_root->diropen;
  622. }
  623. /* Lookup the node the symlink points to. */
  624. status = ext2fs_find_file1 (symlink, oldnode,
  625. &currnode, &type);
  626. free (symlink);
  627. if (status == 0) {
  628. ext2fs_free_node (oldnode, currroot);
  629. return (0);
  630. }
  631. }
  632. ext2fs_free_node (oldnode, currroot);
  633. /* Found the node! */
  634. if (!next || *next == '\0') {
  635. *currfound = currnode;
  636. *foundtype = type;
  637. return (1);
  638. }
  639. name = next;
  640. }
  641. return (-1);
  642. }
  643. int ext2fs_find_file
  644. (const char *path,
  645. ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) {
  646. int status;
  647. int foundtype = FILETYPE_DIRECTORY;
  648. symlinknest = 0;
  649. if (!path) {
  650. return (0);
  651. }
  652. status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
  653. if (status == 0) {
  654. return (0);
  655. }
  656. /* Check if the node that was found was of the expected type. */
  657. if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
  658. return (0);
  659. } else if ((expecttype == FILETYPE_DIRECTORY)
  660. && (foundtype != expecttype)) {
  661. return (0);
  662. }
  663. return (1);
  664. }
  665. int ext2fs_ls (char *dirname) {
  666. ext2fs_node_t dirnode;
  667. int status;
  668. if (ext2fs_root == NULL) {
  669. return (0);
  670. }
  671. status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
  672. FILETYPE_DIRECTORY);
  673. if (status != 1) {
  674. printf ("** Can not find directory. **\n");
  675. return (1);
  676. }
  677. ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
  678. ext2fs_free_node (dirnode, &ext2fs_root->diropen);
  679. return (0);
  680. }
  681. int ext2fs_open (char *filename) {
  682. ext2fs_node_t fdiro = NULL;
  683. int status;
  684. int len;
  685. if (ext2fs_root == NULL) {
  686. return (-1);
  687. }
  688. ext2fs_file = NULL;
  689. status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
  690. FILETYPE_REG);
  691. if (status == 0) {
  692. goto fail;
  693. }
  694. if (!fdiro->inode_read) {
  695. status = ext2fs_read_inode (fdiro->data, fdiro->ino,
  696. &fdiro->inode);
  697. if (status == 0) {
  698. goto fail;
  699. }
  700. }
  701. len = __le32_to_cpu (fdiro->inode.size);
  702. ext2fs_file = fdiro;
  703. return (len);
  704. fail:
  705. ext2fs_free_node (fdiro, &ext2fs_root->diropen);
  706. return (-1);
  707. }
  708. int ext2fs_close (void
  709. ) {
  710. if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
  711. ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
  712. ext2fs_file = NULL;
  713. }
  714. if (ext2fs_root != NULL) {
  715. free (ext2fs_root);
  716. ext2fs_root = NULL;
  717. }
  718. if (indir1_block != NULL) {
  719. free (indir1_block);
  720. indir1_block = NULL;
  721. indir1_size = 0;
  722. indir1_blkno = -1;
  723. }
  724. if (indir2_block != NULL) {
  725. free (indir2_block);
  726. indir2_block = NULL;
  727. indir2_size = 0;
  728. indir2_blkno = -1;
  729. }
  730. return (0);
  731. }
  732. int ext2fs_read (char *buf, unsigned len) {
  733. int status;
  734. if (ext2fs_root == NULL) {
  735. return (0);
  736. }
  737. if (ext2fs_file == NULL) {
  738. return (0);
  739. }
  740. status = ext2fs_read_file (ext2fs_file, 0, len, buf);
  741. return (status);
  742. }
  743. int ext2fs_mount (unsigned part_length) {
  744. struct ext2_data *data;
  745. int status;
  746. data = malloc (sizeof (struct ext2_data));
  747. if (!data) {
  748. return (0);
  749. }
  750. /* Read the superblock. */
  751. status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
  752. (char *) &data->sblock);
  753. if (status == 0) {
  754. goto fail;
  755. }
  756. /* Make sure this is an ext2 filesystem. */
  757. if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
  758. goto fail;
  759. }
  760. if (__le32_to_cpu(data->sblock.revision_level == 0)) {
  761. inode_size = 128;
  762. } else {
  763. inode_size = __le16_to_cpu(data->sblock.inode_size);
  764. }
  765. #ifdef DEBUG
  766. printf("EXT2 rev %d, inode_size %d\n",
  767. __le32_to_cpu(data->sblock.revision_level), inode_size);
  768. #endif
  769. data->diropen.data = data;
  770. data->diropen.ino = 2;
  771. data->diropen.inode_read = 1;
  772. data->inode = &data->diropen.inode;
  773. status = ext2fs_read_inode (data, 2, data->inode);
  774. if (status == 0) {
  775. goto fail;
  776. }
  777. ext2fs_root = data;
  778. return (1);
  779. fail:
  780. printf ("Failed to mount ext2 filesystem...\n");
  781. free (data);
  782. ext2fs_root = NULL;
  783. return (0);
  784. }