ext2fs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 pad;
  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 int ext2fs_blockgroup
  164. (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
  165. #ifdef DEBUG
  166. printf ("ext2fs read blockgroup\n");
  167. #endif
  168. return (ext2fs_devread
  169. (((__le32_to_cpu (data->sblock.first_data_block) +
  170. 1) << LOG2_EXT2_BLOCK_SIZE (data)),
  171. group * sizeof (struct ext2_block_group),
  172. sizeof (struct ext2_block_group), (char *) blkgrp));
  173. }
  174. static int ext2fs_read_inode
  175. (struct ext2_data *data, int ino, struct ext2_inode *inode) {
  176. struct ext2_block_group blkgrp;
  177. struct ext2_sblock *sblock = &data->sblock;
  178. int inodes_per_block;
  179. int status;
  180. unsigned int blkno;
  181. unsigned int blkoff;
  182. /* It is easier to calculate if the first inode is 0. */
  183. ino--;
  184. #ifdef DEBUG
  185. printf ("ext2fs read inode %d\n", ino);
  186. #endif
  187. status = ext2fs_blockgroup (data,
  188. ino /
  189. __le32_to_cpu (sblock->inodes_per_group),
  190. &blkgrp);
  191. if (status == 0) {
  192. return (0);
  193. }
  194. inodes_per_block = EXT2_BLOCK_SIZE (data) / 128;
  195. blkno = (ino % __le32_to_cpu (sblock->inodes_per_group)) /
  196. inodes_per_block;
  197. blkoff = (ino % __le32_to_cpu (sblock->inodes_per_group)) %
  198. inodes_per_block;
  199. #ifdef DEBUG
  200. printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
  201. #endif
  202. /* Read the inode. */
  203. status = ext2fs_devread (((__le32_to_cpu (blkgrp.inode_table_id) +
  204. blkno) << LOG2_EXT2_BLOCK_SIZE (data)),
  205. sizeof (struct ext2_inode) * blkoff,
  206. sizeof (struct ext2_inode), (char *) inode);
  207. if (status == 0) {
  208. return (0);
  209. }
  210. return (1);
  211. }
  212. void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) {
  213. if ((node != &ext2fs_root->diropen) && (node != currroot)) {
  214. free (node);
  215. }
  216. }
  217. static int ext2fs_read_block (ext2fs_node_t node, int fileblock) {
  218. struct ext2_data *data = node->data;
  219. struct ext2_inode *inode = &node->inode;
  220. int blknr;
  221. int blksz = EXT2_BLOCK_SIZE (data);
  222. int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
  223. int status;
  224. /* Direct blocks. */
  225. if (fileblock < INDIRECT_BLOCKS) {
  226. blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
  227. }
  228. /* Indirect. */
  229. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
  230. if (indir1_block == NULL) {
  231. indir1_block = (uint32_t *) malloc (blksz);
  232. if (indir1_block == NULL) {
  233. printf ("** ext2fs read block (indir 1) malloc failed. **\n");
  234. return (-1);
  235. }
  236. indir1_size = blksz;
  237. indir1_blkno = -1;
  238. }
  239. if (blksz != indir1_size) {
  240. free (indir1_block);
  241. indir1_block = NULL;
  242. indir1_size = 0;
  243. indir1_blkno = -1;
  244. indir1_block = (uint32_t *) malloc (blksz);
  245. if (indir1_block == NULL) {
  246. printf ("** ext2fs read block (indir 1) malloc failed. **\n");
  247. return (-1);
  248. }
  249. indir1_size = blksz;
  250. }
  251. if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
  252. log2_blksz) != indir1_blkno) {
  253. status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
  254. 0, blksz,
  255. (char *) indir1_block);
  256. if (status == 0) {
  257. printf ("** ext2fs read block (indir 1) failed. **\n");
  258. return (0);
  259. }
  260. indir1_blkno =
  261. __le32_to_cpu (inode->b.blocks.
  262. indir_block) << log2_blksz;
  263. }
  264. blknr = __le32_to_cpu (indir1_block
  265. [fileblock - INDIRECT_BLOCKS]);
  266. }
  267. /* Double indirect. */
  268. else if (fileblock <
  269. (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
  270. unsigned int perblock = blksz / 4;
  271. unsigned int rblock = fileblock - (INDIRECT_BLOCKS
  272. + blksz / 4);
  273. if (indir1_block == NULL) {
  274. indir1_block = (uint32_t *) malloc (blksz);
  275. if (indir1_block == NULL) {
  276. printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
  277. return (-1);
  278. }
  279. indir1_size = blksz;
  280. indir1_blkno = -1;
  281. }
  282. if (blksz != indir1_size) {
  283. free (indir1_block);
  284. indir1_block = NULL;
  285. indir1_size = 0;
  286. indir1_blkno = -1;
  287. indir1_block = (uint32_t *) malloc (blksz);
  288. if (indir1_block == NULL) {
  289. printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
  290. return (-1);
  291. }
  292. indir1_size = blksz;
  293. }
  294. if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
  295. log2_blksz) != indir1_blkno) {
  296. status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
  297. 0, blksz,
  298. (char *) indir1_block);
  299. if (status == 0) {
  300. printf ("** ext2fs read block (indir 2 1) failed. **\n");
  301. return (-1);
  302. }
  303. indir1_blkno =
  304. __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
  305. }
  306. if (indir2_block == NULL) {
  307. indir2_block = (uint32_t *) malloc (blksz);
  308. if (indir2_block == NULL) {
  309. printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
  310. return (-1);
  311. }
  312. indir2_size = blksz;
  313. indir2_blkno = -1;
  314. }
  315. if (blksz != indir2_size) {
  316. free (indir2_block);
  317. indir2_block = NULL;
  318. indir2_size = 0;
  319. indir2_blkno = -1;
  320. indir2_block = (uint32_t *) malloc (blksz);
  321. if (indir2_block == NULL) {
  322. printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
  323. return (-1);
  324. }
  325. indir2_size = blksz;
  326. }
  327. if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
  328. log2_blksz) != indir1_blkno) {
  329. status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
  330. 0, blksz,
  331. (char *) indir2_block);
  332. if (status == 0) {
  333. printf ("** ext2fs read block (indir 2 2) failed. **\n");
  334. return (-1);
  335. }
  336. indir2_blkno =
  337. __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
  338. }
  339. blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
  340. }
  341. /* Tripple indirect. */
  342. else {
  343. printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
  344. return (-1);
  345. }
  346. #ifdef DEBUG
  347. printf ("ext2fs_read_block %08x\n", blknr);
  348. #endif
  349. return (blknr);
  350. }
  351. int ext2fs_read_file
  352. (ext2fs_node_t node, int pos, unsigned int len, char *buf) {
  353. int i;
  354. int blockcnt;
  355. int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
  356. int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
  357. unsigned int filesize = __le32_to_cpu(node->inode.size);
  358. /* Adjust len so it we can't read past the end of the file. */
  359. if (len > filesize) {
  360. len = filesize;
  361. }
  362. blockcnt = ((len + pos) + blocksize - 1) / blocksize;
  363. for (i = pos / blocksize; i < blockcnt; i++) {
  364. int blknr;
  365. int blockoff = pos % blocksize;
  366. int blockend = blocksize;
  367. int skipfirst = 0;
  368. blknr = ext2fs_read_block (node, i);
  369. if (blknr < 0) {
  370. return (-1);
  371. }
  372. blknr = blknr << log2blocksize;
  373. /* Last block. */
  374. if (i == blockcnt - 1) {
  375. blockend = (len + pos) % blocksize;
  376. /* The last portion is exactly blocksize. */
  377. if (!blockend) {
  378. blockend = blocksize;
  379. }
  380. }
  381. /* First block. */
  382. if (i == pos / blocksize) {
  383. skipfirst = blockoff;
  384. blockend -= skipfirst;
  385. }
  386. /* If the block number is 0 this block is not stored on disk but
  387. is zero filled instead. */
  388. if (blknr) {
  389. int status;
  390. status = ext2fs_devread (blknr, skipfirst, blockend, buf);
  391. if (status == 0) {
  392. return (-1);
  393. }
  394. } else {
  395. memset (buf, 0, blocksize - skipfirst);
  396. }
  397. buf += blocksize - skipfirst;
  398. }
  399. return (len);
  400. }
  401. static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
  402. {
  403. unsigned int fpos = 0;
  404. int status;
  405. struct ext2fs_node *diro = (struct ext2fs_node *) dir;
  406. #ifdef DEBUG
  407. if (name != NULL)
  408. printf ("Iterate dir %s\n", name);
  409. #endif /* of DEBUG */
  410. if (!diro->inode_read) {
  411. status = ext2fs_read_inode (diro->data, diro->ino,
  412. &diro->inode);
  413. if (status == 0) {
  414. return (0);
  415. }
  416. }
  417. /* Search the file. */
  418. while (fpos < __le32_to_cpu (diro->inode.size)) {
  419. struct ext2_dirent dirent;
  420. status = ext2fs_read_file (diro, fpos,
  421. sizeof (struct ext2_dirent),
  422. (char *) &dirent);
  423. if (status < 1) {
  424. return (0);
  425. }
  426. if (dirent.namelen != 0) {
  427. char filename[dirent.namelen + 1];
  428. ext2fs_node_t fdiro;
  429. int type = FILETYPE_UNKNOWN;
  430. status = ext2fs_read_file (diro,
  431. fpos + sizeof (struct ext2_dirent),
  432. dirent.namelen, filename);
  433. if (status < 1) {
  434. return (0);
  435. }
  436. fdiro = malloc (sizeof (struct ext2fs_node));
  437. if (!fdiro) {
  438. return (0);
  439. }
  440. fdiro->data = diro->data;
  441. fdiro->ino = __le32_to_cpu (dirent.inode);
  442. filename[dirent.namelen] = '\0';
  443. if (dirent.filetype != FILETYPE_UNKNOWN) {
  444. fdiro->inode_read = 0;
  445. if (dirent.filetype == FILETYPE_DIRECTORY) {
  446. type = FILETYPE_DIRECTORY;
  447. } else if (dirent.filetype ==
  448. FILETYPE_SYMLINK) {
  449. type = FILETYPE_SYMLINK;
  450. } else if (dirent.filetype == FILETYPE_REG) {
  451. type = FILETYPE_REG;
  452. }
  453. } else {
  454. /* The filetype can not be read from the dirent, get it from inode */
  455. status = ext2fs_read_inode (diro->data,
  456. __le32_to_cpu(dirent.inode),
  457. &fdiro->inode);
  458. if (status == 0) {
  459. free (fdiro);
  460. return (0);
  461. }
  462. fdiro->inode_read = 1;
  463. if ((__le16_to_cpu (fdiro->inode.mode) &
  464. FILETYPE_INO_MASK) ==
  465. FILETYPE_INO_DIRECTORY) {
  466. type = FILETYPE_DIRECTORY;
  467. } else if ((__le16_to_cpu (fdiro->inode.mode)
  468. & FILETYPE_INO_MASK) ==
  469. FILETYPE_INO_SYMLINK) {
  470. type = FILETYPE_SYMLINK;
  471. } else if ((__le16_to_cpu (fdiro->inode.mode)
  472. & FILETYPE_INO_MASK) ==
  473. FILETYPE_INO_REG) {
  474. type = FILETYPE_REG;
  475. }
  476. }
  477. #ifdef DEBUG
  478. printf ("iterate >%s<\n", filename);
  479. #endif /* of DEBUG */
  480. if ((name != NULL) && (fnode != NULL)
  481. && (ftype != NULL)) {
  482. if (strcmp (filename, name) == 0) {
  483. *ftype = type;
  484. *fnode = fdiro;
  485. return (1);
  486. }
  487. } else {
  488. if (fdiro->inode_read == 0) {
  489. status = ext2fs_read_inode (diro->data,
  490. __le32_to_cpu (dirent.inode),
  491. &fdiro->inode);
  492. if (status == 0) {
  493. free (fdiro);
  494. return (0);
  495. }
  496. fdiro->inode_read = 1;
  497. }
  498. switch (type) {
  499. case FILETYPE_DIRECTORY:
  500. printf ("<DIR> ");
  501. break;
  502. case FILETYPE_SYMLINK:
  503. printf ("<SYM> ");
  504. break;
  505. case FILETYPE_REG:
  506. printf (" ");
  507. break;
  508. default:
  509. printf ("< ? > ");
  510. break;
  511. }
  512. printf ("%10d %s\n",
  513. __le32_to_cpu (fdiro->inode.size),
  514. filename);
  515. }
  516. free (fdiro);
  517. }
  518. fpos += __le16_to_cpu (dirent.direntlen);
  519. }
  520. return (0);
  521. }
  522. static char *ext2fs_read_symlink (ext2fs_node_t node) {
  523. char *symlink;
  524. struct ext2fs_node *diro = node;
  525. int status;
  526. if (!diro->inode_read) {
  527. status = ext2fs_read_inode (diro->data, diro->ino,
  528. &diro->inode);
  529. if (status == 0) {
  530. return (0);
  531. }
  532. }
  533. symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
  534. if (!symlink) {
  535. return (0);
  536. }
  537. /* If the filesize of the symlink is bigger than
  538. 60 the symlink is stored in a separate block,
  539. otherwise it is stored in the inode. */
  540. if (__le32_to_cpu (diro->inode.size) <= 60) {
  541. strncpy (symlink, diro->inode.b.symlink,
  542. __le32_to_cpu (diro->inode.size));
  543. } else {
  544. status = ext2fs_read_file (diro, 0,
  545. __le32_to_cpu (diro->inode.size),
  546. symlink);
  547. if (status == 0) {
  548. free (symlink);
  549. return (0);
  550. }
  551. }
  552. symlink[__le32_to_cpu (diro->inode.size)] = '\0';
  553. return (symlink);
  554. }
  555. int ext2fs_find_file1
  556. (const char *currpath,
  557. ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) {
  558. char fpath[strlen (currpath) + 1];
  559. char *name = fpath;
  560. char *next;
  561. int status;
  562. int type = FILETYPE_DIRECTORY;
  563. ext2fs_node_t currnode = currroot;
  564. ext2fs_node_t oldnode = currroot;
  565. strncpy (fpath, currpath, strlen (currpath) + 1);
  566. /* Remove all leading slashes. */
  567. while (*name == '/') {
  568. name++;
  569. }
  570. if (!*name) {
  571. *currfound = currnode;
  572. return (1);
  573. }
  574. for (;;) {
  575. int found;
  576. /* Extract the actual part from the pathname. */
  577. next = strchr (name, '/');
  578. if (next) {
  579. /* Remove all leading slashes. */
  580. while (*next == '/') {
  581. *(next++) = '\0';
  582. }
  583. }
  584. /* At this point it is expected that the current node is a directory, check if this is true. */
  585. if (type != FILETYPE_DIRECTORY) {
  586. ext2fs_free_node (currnode, currroot);
  587. return (0);
  588. }
  589. oldnode = currnode;
  590. /* Iterate over the directory. */
  591. found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
  592. if (found == 0) {
  593. return (0);
  594. }
  595. if (found == -1) {
  596. break;
  597. }
  598. /* Read in the symlink and follow it. */
  599. if (type == FILETYPE_SYMLINK) {
  600. char *symlink;
  601. /* Test if the symlink does not loop. */
  602. if (++symlinknest == 8) {
  603. ext2fs_free_node (currnode, currroot);
  604. ext2fs_free_node (oldnode, currroot);
  605. return (0);
  606. }
  607. symlink = ext2fs_read_symlink (currnode);
  608. ext2fs_free_node (currnode, currroot);
  609. if (!symlink) {
  610. ext2fs_free_node (oldnode, currroot);
  611. return (0);
  612. }
  613. #ifdef DEBUG
  614. printf ("Got symlink >%s<\n", symlink);
  615. #endif /* of DEBUG */
  616. /* The symlink is an absolute path, go back to the root inode. */
  617. if (symlink[0] == '/') {
  618. ext2fs_free_node (oldnode, currroot);
  619. oldnode = &ext2fs_root->diropen;
  620. }
  621. /* Lookup the node the symlink points to. */
  622. status = ext2fs_find_file1 (symlink, oldnode,
  623. &currnode, &type);
  624. free (symlink);
  625. if (status == 0) {
  626. ext2fs_free_node (oldnode, currroot);
  627. return (0);
  628. }
  629. }
  630. ext2fs_free_node (oldnode, currroot);
  631. /* Found the node! */
  632. if (!next || *next == '\0') {
  633. *currfound = currnode;
  634. *foundtype = type;
  635. return (1);
  636. }
  637. name = next;
  638. }
  639. return (-1);
  640. }
  641. int ext2fs_find_file
  642. (const char *path,
  643. ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) {
  644. int status;
  645. int foundtype = FILETYPE_DIRECTORY;
  646. symlinknest = 0;
  647. if (!path) {
  648. return (0);
  649. }
  650. status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
  651. if (status == 0) {
  652. return (0);
  653. }
  654. /* Check if the node that was found was of the expected type. */
  655. if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
  656. return (0);
  657. } else if ((expecttype == FILETYPE_DIRECTORY)
  658. && (foundtype != expecttype)) {
  659. return (0);
  660. }
  661. return (1);
  662. }
  663. int ext2fs_ls (char *dirname) {
  664. ext2fs_node_t dirnode;
  665. int status;
  666. if (ext2fs_root == NULL) {
  667. return (0);
  668. }
  669. status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
  670. FILETYPE_DIRECTORY);
  671. if (status != 1) {
  672. printf ("** Can not find directory. **\n");
  673. return (1);
  674. }
  675. ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
  676. ext2fs_free_node (dirnode, &ext2fs_root->diropen);
  677. return (0);
  678. }
  679. int ext2fs_open (char *filename) {
  680. ext2fs_node_t fdiro = NULL;
  681. int status;
  682. int len;
  683. if (ext2fs_root == NULL) {
  684. return (-1);
  685. }
  686. ext2fs_file = NULL;
  687. status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
  688. FILETYPE_REG);
  689. if (status == 0) {
  690. goto fail;
  691. }
  692. if (!fdiro->inode_read) {
  693. status = ext2fs_read_inode (fdiro->data, fdiro->ino,
  694. &fdiro->inode);
  695. if (status == 0) {
  696. goto fail;
  697. }
  698. }
  699. len = __le32_to_cpu (fdiro->inode.size);
  700. ext2fs_file = fdiro;
  701. return (len);
  702. fail:
  703. ext2fs_free_node (fdiro, &ext2fs_root->diropen);
  704. return (-1);
  705. }
  706. int ext2fs_close (void
  707. ) {
  708. if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
  709. ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
  710. ext2fs_file = NULL;
  711. }
  712. if (ext2fs_root != NULL) {
  713. free (ext2fs_root);
  714. ext2fs_root = NULL;
  715. }
  716. if (indir1_block != NULL) {
  717. free (indir1_block);
  718. indir1_block = NULL;
  719. indir1_size = 0;
  720. indir1_blkno = -1;
  721. }
  722. if (indir2_block != NULL) {
  723. free (indir2_block);
  724. indir2_block = NULL;
  725. indir2_size = 0;
  726. indir2_blkno = -1;
  727. }
  728. return (0);
  729. }
  730. int ext2fs_read (char *buf, unsigned len) {
  731. int status;
  732. if (ext2fs_root == NULL) {
  733. return (0);
  734. }
  735. if (ext2fs_file == NULL) {
  736. return (0);
  737. }
  738. status = ext2fs_read_file (ext2fs_file, 0, len, buf);
  739. return (status);
  740. }
  741. int ext2fs_mount (unsigned part_length) {
  742. struct ext2_data *data;
  743. int status;
  744. data = malloc (sizeof (struct ext2_data));
  745. if (!data) {
  746. return (0);
  747. }
  748. /* Read the superblock. */
  749. status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
  750. (char *) &data->sblock);
  751. if (status == 0) {
  752. goto fail;
  753. }
  754. /* Make sure this is an ext2 filesystem. */
  755. if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
  756. goto fail;
  757. }
  758. data->diropen.data = data;
  759. data->diropen.ino = 2;
  760. data->diropen.inode_read = 1;
  761. data->inode = &data->diropen.inode;
  762. status = ext2fs_read_inode (data, 2, data->inode);
  763. if (status == 0) {
  764. goto fail;
  765. }
  766. ext2fs_root = data;
  767. return (1);
  768. fail:
  769. printf ("Failed to mount ext2 filesystem...\n");
  770. free (data);
  771. ext2fs_root = NULL;
  772. return (0);
  773. }