ext2fs.c 21 KB

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