| 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 | |
| 26 | #include <qi.h> |
| 27 | |
| 28 | #include <ext2.h> |
| 29 | #include <malloc.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | extern int ext2fs_devread(int sector, int log2blksize, int byte_offset, int byte_len, |
| 33 | char *buf); |
| 34 | |
| 35 | /* Magic value used to identify an ext2 filesystem. */ |
| 36 | #define EXT2_MAGIC 0xEF53 |
| 37 | /* Amount of indirect blocks in an inode. */ |
| 38 | #define INDIRECT_BLOCKS 12 |
| 39 | /* Maximum lenght of a pathname. */ |
| 40 | #define EXT2_PATH_MAX 4096 |
| 41 | /* Maximum nesting of symlinks, used to prevent a loop. */ |
| 42 | #define EXT2_MAX_SYMLINKCNT 8 |
| 43 | |
| 44 | /* Filetype used in directory entry. */ |
| 45 | #define FILETYPE_UNKNOWN 0 |
| 46 | #define FILETYPE_REG 1 |
| 47 | #define FILETYPE_DIRECTORY 2 |
| 48 | #define FILETYPE_SYMLINK 7 |
| 49 | |
| 50 | /* Filetype information as used in inodes. */ |
| 51 | #define FILETYPE_INO_MASK 0170000 |
| 52 | #define FILETYPE_INO_REG 0100000 |
| 53 | #define FILETYPE_INO_DIRECTORY 0040000 |
| 54 | #define FILETYPE_INO_SYMLINK 0120000 |
| 55 | |
| 56 | /* Bits used as offset in sector */ |
| 57 | #define DISK_SECTOR_BITS 9 |
| 58 | |
| 59 | /* Log2 size of ext2 block in 512 blocks. */ |
| 60 | #define LOG2_EXT2_BLOCK_SIZE(data)(__le32_to_cpu(data->sblock.log2_block_size) + 1) |
| 61 | |
| 62 | /* Log2 size of ext2 block in bytes. */ |
| 63 | #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu(data->sblock.log2_block_size) + 10) |
| 64 | |
| 65 | /* The size of an ext2 block in bytes. */ |
| 66 | #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data)) |
| 67 | |
| 68 | #define EXT2_GOOD_OLD_REV 0 /* The good old (original) format */ |
| 69 | #define EXT2_DYNAMIC_REV 1 /* V2 format w/ dynamic inode sizes */ |
| 70 | |
| 71 | #define EXT2_GOOD_OLD_INODE_SIZE 128 |
| 72 | uint32_t ext2_inode_size = EXT2_GOOD_OLD_INODE_SIZE; |
| 73 | |
| 74 | /* The ext2 superblock. */ |
| 75 | struct ext2_sblock { |
| 76 | uint32_t total_inodes; |
| 77 | uint32_t total_blocks; |
| 78 | uint32_t reserved_blocks; |
| 79 | uint32_t free_blocks; |
| 80 | uint32_t free_inodes; |
| 81 | uint32_t first_data_block; |
| 82 | uint32_t log2_block_size; |
| 83 | uint32_t log2_fragment_size; |
| 84 | uint32_t blocks_per_group; |
| 85 | uint32_t fragments_per_group; |
| 86 | uint32_t inodes_per_group; |
| 87 | uint32_t mtime; |
| 88 | uint32_t utime; |
| 89 | uint16_t mnt_count; |
| 90 | uint16_t max_mnt_count; |
| 91 | uint16_t magic; |
| 92 | uint16_t fs_state; |
| 93 | uint16_t error_handling; |
| 94 | uint16_t minor_revision_level; |
| 95 | uint32_t lastcheck; |
| 96 | uint32_t checkinterval; |
| 97 | uint32_t creator_os; |
| 98 | uint32_t revision_level; |
| 99 | uint16_t uid_reserved; |
| 100 | uint16_t gid_reserved; |
| 101 | uint32_t first_inode; |
| 102 | uint16_t inode_size; |
| 103 | uint16_t block_group_number; |
| 104 | uint32_t feature_compatibility; |
| 105 | uint32_t feature_incompat; |
| 106 | uint32_t feature_ro_compat; |
| 107 | uint32_t unique_id[4]; |
| 108 | char volume_name[16]; |
| 109 | char last_mounted_on[64]; |
| 110 | uint32_t compression_info; |
| 111 | }; |
| 112 | |
| 113 | /* The ext2 blockgroup. */ |
| 114 | struct ext2_block_group { |
| 115 | uint32_t block_id; |
| 116 | uint32_t inode_id; |
| 117 | uint32_t inode_table_id; |
| 118 | uint16_t free_blocks; |
| 119 | uint16_t free_inodes; |
| 120 | uint16_t pad; |
| 121 | uint32_t reserved[3]; |
| 122 | }; |
| 123 | |
| 124 | /* The ext2 inode. */ |
| 125 | struct ext2_inode { |
| 126 | uint16_t mode; |
| 127 | uint16_t uid; |
| 128 | uint32_t size; |
| 129 | uint32_t atime; |
| 130 | uint32_t ctime; |
| 131 | uint32_t mtime; |
| 132 | uint32_t dtime; |
| 133 | uint16_t gid; |
| 134 | uint16_t nlinks; |
| 135 | uint32_t blockcnt; /* Blocks of 512 bytes!! */ |
| 136 | uint32_t flags; |
| 137 | uint32_t osd1; |
| 138 | union { |
| 139 | struct datablocks { |
| 140 | uint32_t dir_blocks[INDIRECT_BLOCKS]; |
| 141 | uint32_t indir_block; |
| 142 | uint32_t double_indir_block; |
| 143 | uint32_t tripple_indir_block; |
| 144 | } blocks; |
| 145 | char symlink[60]; |
| 146 | } b; |
| 147 | uint32_t version; |
| 148 | uint32_t acl; |
| 149 | uint32_t dir_acl; |
| 150 | uint32_t fragment_addr; |
| 151 | uint32_t osd2[3]; |
| 152 | }; |
| 153 | |
| 154 | /* The header of an ext2 directory entry. */ |
| 155 | struct ext2_dirent { |
| 156 | uint32_t inode; |
| 157 | uint16_t direntlen; |
| 158 | uint8_t namelen; |
| 159 | uint8_t filetype; |
| 160 | }; |
| 161 | |
| 162 | struct ext2fs_node { |
| 163 | struct ext2_data *data; |
| 164 | struct ext2_inode inode; |
| 165 | int ino; |
| 166 | int inode_read; |
| 167 | }; |
| 168 | |
| 169 | /* Information about a "mounted" ext2 filesystem. */ |
| 170 | struct ext2_data { |
| 171 | struct ext2_sblock sblock; |
| 172 | struct ext2_inode *inode; |
| 173 | struct ext2fs_node diropen; |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | typedef struct ext2fs_node *ext2fs_node_t; |
| 178 | |
| 179 | struct ext2_data *ext2fs_root = NULL; |
| 180 | ext2fs_node_t ext2fs_file = NULL; |
| 181 | int symlinknest = 0; |
| 182 | uint32_t *indir1_block = NULL; |
| 183 | int indir1_size = 0; |
| 184 | int indir1_blkno = -1; |
| 185 | uint32_t *indir2_block = NULL; |
| 186 | int indir2_size = 0; |
| 187 | int indir2_blkno = -1; |
| 188 | |
| 189 | |
| 190 | static int ext2fs_blockgroup |
| 191 | (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) { |
| 192 | return ext2fs_devread |
| 193 | ((__le32_to_cpu(data->sblock.first_data_block) + |
| 194 | 1), LOG2_EXT2_BLOCK_SIZE(data), |
| 195 | group * sizeof(struct ext2_block_group), |
| 196 | sizeof(struct ext2_block_group),(char *) blkgrp); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | static int ext2fs_read_inode |
| 201 | (struct ext2_data *data, int ino, struct ext2_inode *inode) { |
| 202 | struct ext2_block_group blkgrp; |
| 203 | struct ext2_sblock *sblock = &data->sblock; |
| 204 | int inodes_per_block; |
| 205 | int status; |
| 206 | |
| 207 | unsigned int blkno; |
| 208 | unsigned int blkoff; |
| 209 | |
| 210 | /* It is easier to calculate if the first inode is 0. */ |
| 211 | ino--; |
| 212 | status = ext2fs_blockgroup(data, |
| 213 | ino / |
| 214 | __le32_to_cpu(sblock->inodes_per_group), |
| 215 | &blkgrp); |
| 216 | if (status == 0) |
| 217 | return 0; |
| 218 | |
| 219 | inodes_per_block = EXT2_BLOCK_SIZE(data) / ext2_inode_size; |
| 220 | blkno =(ino % __le32_to_cpu(sblock->inodes_per_group)) / |
| 221 | inodes_per_block; |
| 222 | blkoff =(ino % __le32_to_cpu(sblock->inodes_per_group)) % |
| 223 | inodes_per_block; |
| 224 | #ifdef DEBUG |
| 225 | puts("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff); |
| 226 | #endif |
| 227 | /* Read the inode. */ |
| 228 | |
| 229 | status = ext2fs_devread(__le32_to_cpu(blkgrp.inode_table_id) + blkno, |
| 230 | LOG2_EXT2_BLOCK_SIZE(data), |
| 231 | ext2_inode_size * blkoff, |
| 232 | sizeof(struct ext2_inode), (char *)inode); |
| 233 | |
| 234 | return !!status; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | void ext2fs_free_node(ext2fs_node_t node, ext2fs_node_t currroot) { |
| 239 | if ((node != &ext2fs_root->diropen) &&(node != currroot)) { |
| 240 | free(node); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | static int ext2fs_read_block(ext2fs_node_t node, int fileblock) { |
| 246 | struct ext2_data *data = node->data; |
| 247 | struct ext2_inode *inode = &node->inode; |
| 248 | int blknr; |
| 249 | int blksz = EXT2_BLOCK_SIZE(data); |
| 250 | int log2_blksz = LOG2_EXT2_BLOCK_SIZE(data); |
| 251 | int status; |
| 252 | |
| 253 | /* Direct blocks. */ |
| 254 | if (fileblock < INDIRECT_BLOCKS) { |
| 255 | blknr = __le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]); |
| 256 | } |
| 257 | /* Indirect. */ |
| 258 | else if (fileblock <(INDIRECT_BLOCKS +(blksz / 4))) { |
| 259 | if (indir1_block == NULL) { |
| 260 | indir1_block =(uint32_t *) malloc(blksz); |
| 261 | if (indir1_block == NULL) { |
| 262 | puts("** ext2fs read block(indir 1) malloc failed. **\n"); |
| 263 | return -1; |
| 264 | } |
| 265 | indir1_size = blksz; |
| 266 | indir1_blkno = -1; |
| 267 | } |
| 268 | if (blksz != indir1_size) { |
| 269 | free(indir1_block); |
| 270 | indir1_block = NULL; |
| 271 | indir1_size = 0; |
| 272 | indir1_blkno = -1; |
| 273 | indir1_block =(uint32_t *) malloc(blksz); |
| 274 | if (indir1_block == NULL) { |
| 275 | puts("** ext2fs read block(indir 1) malloc failed. **\n"); |
| 276 | return -1; |
| 277 | } |
| 278 | indir1_size = blksz; |
| 279 | } |
| 280 | if ((__le32_to_cpu(inode->b.blocks.indir_block) << |
| 281 | log2_blksz) != indir1_blkno) { |
| 282 | status = ext2fs_devread(__le32_to_cpu(inode->b.blocks.indir_block), log2_blksz, |
| 283 | 0, blksz, |
| 284 | (char *) indir1_block); |
| 285 | if (status == 0) { |
| 286 | puts("** ext2fs read block(indir 1) failed. **\n"); |
| 287 | return 0; |
| 288 | } |
| 289 | indir1_blkno = |
| 290 | __le32_to_cpu(inode->b.blocks. |
| 291 | indir_block) << log2_blksz; |
| 292 | } |
| 293 | blknr = __le32_to_cpu(indir1_block |
| 294 | [fileblock - INDIRECT_BLOCKS]); |
| 295 | } |
| 296 | /* Double indirect. */ |
| 297 | else if (fileblock < |
| 298 | (INDIRECT_BLOCKS +(blksz / 4 *(blksz / 4 + 1)))) { |
| 299 | unsigned int perblock = blksz / 4; |
| 300 | unsigned int rblock = fileblock -(INDIRECT_BLOCKS |
| 301 | + blksz / 4); |
| 302 | |
| 303 | if (indir1_block == NULL) { |
| 304 | indir1_block =(uint32_t *) malloc(blksz); |
| 305 | if (indir1_block == NULL) { |
| 306 | puts("** ext2fs read block(indir 2 1) malloc failed. **\n"); |
| 307 | return -1; |
| 308 | } |
| 309 | indir1_size = blksz; |
| 310 | indir1_blkno = -1; |
| 311 | } |
| 312 | if (blksz != indir1_size) { |
| 313 | free(indir1_block); |
| 314 | indir1_block = NULL; |
| 315 | indir1_size = 0; |
| 316 | indir1_blkno = -1; |
| 317 | indir1_block =(uint32_t *) malloc(blksz); |
| 318 | if (indir1_block == NULL) { |
| 319 | puts("** ext2fs read block(indir 2 1) malloc failed. **\n"); |
| 320 | return -1; |
| 321 | } |
| 322 | indir1_size = blksz; |
| 323 | } |
| 324 | if ((__le32_to_cpu(inode->b.blocks.double_indir_block) << |
| 325 | log2_blksz) != indir1_blkno) { |
| 326 | status = ext2fs_devread(__le32_to_cpu(inode->b.blocks.double_indir_block), log2_blksz, |
| 327 | 0, blksz, |
| 328 | (char *) indir1_block); |
| 329 | if (status == 0) { |
| 330 | puts("** ext2fs read block(indir 2 1) failed. **\n"); |
| 331 | return -1; |
| 332 | } |
| 333 | indir1_blkno = |
| 334 | __le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz; |
| 335 | } |
| 336 | |
| 337 | if (indir2_block == NULL) { |
| 338 | indir2_block =(uint32_t *) malloc(blksz); |
| 339 | if (indir2_block == NULL) { |
| 340 | puts("** ext2fs read block(indir 2 2) malloc failed. **\n"); |
| 341 | return -1; |
| 342 | } |
| 343 | indir2_size = blksz; |
| 344 | indir2_blkno = -1; |
| 345 | } |
| 346 | if (blksz != indir2_size) { |
| 347 | free(indir2_block); |
| 348 | indir2_block = NULL; |
| 349 | indir2_size = 0; |
| 350 | indir2_blkno = -1; |
| 351 | indir2_block =(uint32_t *) malloc(blksz); |
| 352 | if (indir2_block == NULL) { |
| 353 | puts("** ext2fs read block(indir 2 2) malloc failed. **\n"); |
| 354 | return -1; |
| 355 | } |
| 356 | indir2_size = blksz; |
| 357 | } |
| 358 | if ((__le32_to_cpu(indir1_block[rblock / perblock]) << |
| 359 | log2_blksz) != indir2_blkno) { |
| 360 | status = ext2fs_devread(__le32_to_cpu(indir1_block[rblock / perblock]), log2_blksz, |
| 361 | 0, blksz, |
| 362 | (char *) indir2_block); |
| 363 | if (status == 0) { |
| 364 | puts("** ext2fs read block(indir 2 2) failed. **\n"); |
| 365 | return -1; |
| 366 | } |
| 367 | indir2_blkno = |
| 368 | __le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz; |
| 369 | } |
| 370 | blknr = __le32_to_cpu(indir2_block[rblock % perblock]); |
| 371 | } |
| 372 | /* Triple indirect. */ |
| 373 | else { |
| 374 | puts("** ext2fs doesn't support triple indirect blocks. **\n"); |
| 375 | return -1; |
| 376 | } |
| 377 | #ifdef DEBUG |
| 378 | printf("ext2fs_read_block %08x\n", blknr); |
| 379 | #endif |
| 380 | return blknr; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | int ext2fs_read_file(ext2fs_node_t node, int pos, unsigned int len, char *buf) { |
| 385 | int i; |
| 386 | int blockcnt; |
| 387 | int log2blocksize = LOG2_EXT2_BLOCK_SIZE(node->data); |
| 388 | int blocksize = 1 <<(log2blocksize + DISK_SECTOR_BITS); |
| 389 | unsigned int filesize = __le32_to_cpu(node->inode.size); |
| 390 | int previous_block_number = -1; |
| 391 | int delayed_start = 0; |
| 392 | int delayed_extent = 0; |
| 393 | int delayed_skipfirst = 0; |
| 394 | int delayed_next = 0; |
| 395 | char * delayed_buf = NULL; |
| 396 | int status; |
| 397 | |
| 398 | /* Adjust len so it we can't read past the end of the file. */ |
| 399 | if (len > filesize) { |
| 400 | len = filesize; |
| 401 | } |
| 402 | blockcnt = ((len + pos) + blocksize - 1) / blocksize; |
| 403 | |
| 404 | for(i = pos / blocksize; i < blockcnt; i++) { |
| 405 | int blknr; |
| 406 | int blockoff = pos % blocksize; |
| 407 | int blockend = blocksize; |
| 408 | |
| 409 | int skipfirst = 0; |
| 410 | |
| 411 | blknr = ext2fs_read_block(node, i); |
| 412 | if (blknr < 0) |
| 413 | return -1; |
| 414 | |
| 415 | blknr = blknr << log2blocksize; |
| 416 | |
| 417 | /* Last block. */ |
| 418 | if (i == blockcnt - 1) { |
| 419 | blockend =(len + pos) % blocksize; |
| 420 | |
| 421 | /* The last portion is exactly blocksize. */ |
| 422 | if (!blockend) { |
| 423 | blockend = blocksize; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /* First block. */ |
| 428 | if (i == pos / blocksize) { |
| 429 | skipfirst = blockoff; |
| 430 | blockend -= skipfirst; |
| 431 | } |
| 432 | |
| 433 | /* If the block number is 0 this block is not stored on disk but |
| 434 | is zero filled instead. */ |
| 435 | if (blknr) { |
| 436 | int status; |
| 437 | |
| 438 | if (previous_block_number != -1) { |
| 439 | if (delayed_next == blknr) { |
| 440 | delayed_extent += blockend; |
| 441 | delayed_next += blockend >> SECTOR_BITS; |
| 442 | } else { /* spill */ |
| 443 | status = ext2fs_devread(delayed_start, |
| 444 | 0, delayed_skipfirst, |
| 445 | delayed_extent, delayed_buf); |
| 446 | if (status == 0) |
| 447 | return -1; |
| 448 | previous_block_number = blknr; |
| 449 | delayed_start = blknr; |
| 450 | delayed_extent = blockend; |
| 451 | delayed_skipfirst = skipfirst; |
| 452 | delayed_buf = buf; |
| 453 | delayed_next = blknr + (blockend >> SECTOR_BITS); |
| 454 | } |
| 455 | } else { |
| 456 | previous_block_number = blknr; |
| 457 | delayed_start = blknr; |
| 458 | delayed_extent = blockend; |
| 459 | delayed_skipfirst = skipfirst; |
| 460 | delayed_buf = buf; |
| 461 | delayed_next = blknr + (blockend >> SECTOR_BITS); |
| 462 | } |
| 463 | |
| 464 | } else { |
| 465 | if (previous_block_number != -1) { |
| 466 | /* spill */ |
| 467 | status = ext2fs_devread(delayed_start, |
| 468 | 0, delayed_skipfirst, |
| 469 | delayed_extent, delayed_buf); |
| 470 | if (status == 0) |
| 471 | return -1; |
| 472 | previous_block_number = -1; |
| 473 | } |
| 474 | memset(buf, 0, blocksize - skipfirst); |
| 475 | } |
| 476 | buf += blocksize - skipfirst; |
| 477 | } |
| 478 | |
| 479 | if (previous_block_number != -1) { |
| 480 | /* spill */ |
| 481 | status = ext2fs_devread(delayed_start, |
| 482 | 0, delayed_skipfirst, |
| 483 | delayed_extent, delayed_buf); |
| 484 | if (status == 0) |
| 485 | return -1; |
| 486 | previous_block_number = -1; |
| 487 | } |
| 488 | |
| 489 | return(len); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | static int ext2fs_iterate_dir(ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype) |
| 494 | { |
| 495 | unsigned int fpos = 0; |
| 496 | int status; |
| 497 | struct ext2fs_node *diro =(struct ext2fs_node *) dir; |
| 498 | |
| 499 | #ifdef DEBUG |
| 500 | if (name != NULL) |
| 501 | printf("Iterate dir %s\n", name); |
| 502 | #endif /* of DEBUG */ |
| 503 | if (!diro->inode_read) { |
| 504 | status = ext2fs_read_inode(diro->data, diro->ino, |
| 505 | &diro->inode); |
| 506 | if (status == 0) { |
| 507 | printdec(diro->ino); |
| 508 | puts("failed to read inode\n"); |
| 509 | return(0); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* Search the file. */ |
| 514 | while (fpos < __le32_to_cpu(diro->inode.size)) { |
| 515 | struct ext2_dirent dirent; |
| 516 | |
| 517 | status = ext2fs_read_file(diro, fpos, |
| 518 | sizeof(struct ext2_dirent), |
| 519 | (char *) &dirent); |
| 520 | if (status < 1) { |
| 521 | puts("ext2fs_read_file ret < 1\n"); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | if (dirent.namelen != 0) { |
| 526 | char filename[256]; |
| 527 | ext2fs_node_t fdiro; |
| 528 | int type = FILETYPE_UNKNOWN; |
| 529 | |
| 530 | status = ext2fs_read_file(diro, |
| 531 | fpos + sizeof(struct ext2_dirent), |
| 532 | dirent.namelen, filename); |
| 533 | if (status < 1) { |
| 534 | puts("ext2fs_read_file fail 2\n"); |
| 535 | return(0); |
| 536 | } |
| 537 | |
| 538 | fdiro = malloc(sizeof(struct ext2fs_node)); |
| 539 | if (!fdiro) { |
| 540 | puts("malloc fail\n"); |
| 541 | return(0); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | fdiro->data = diro->data; |
| 546 | fdiro->ino = __le32_to_cpu(dirent.inode); |
| 547 | |
| 548 | filename[dirent.namelen] = '\0'; |
| 549 | |
| 550 | if (dirent.filetype != FILETYPE_UNKNOWN) { |
| 551 | fdiro->inode_read = 0; |
| 552 | |
| 553 | if (dirent.filetype == FILETYPE_DIRECTORY) { |
| 554 | type = FILETYPE_DIRECTORY; |
| 555 | } else if (dirent.filetype == |
| 556 | FILETYPE_SYMLINK) { |
| 557 | type = FILETYPE_SYMLINK; |
| 558 | } else if (dirent.filetype == FILETYPE_REG) { |
| 559 | type = FILETYPE_REG; |
| 560 | } |
| 561 | } else { |
| 562 | /* The filetype can not be read from the dirent, get it from inode */ |
| 563 | |
| 564 | status = ext2fs_read_inode(diro->data, |
| 565 | __le32_to_cpu(dirent.inode), |
| 566 | &fdiro->inode); |
| 567 | if (status == 0) { |
| 568 | puts("inner ext2fs_read_inode fail\n"); |
| 569 | free(fdiro); |
| 570 | return(0); |
| 571 | } |
| 572 | fdiro->inode_read = 1; |
| 573 | |
| 574 | if ((__le16_to_cpu(fdiro->inode.mode) & |
| 575 | FILETYPE_INO_MASK) == |
| 576 | FILETYPE_INO_DIRECTORY) { |
| 577 | type = FILETYPE_DIRECTORY; |
| 578 | } else if ((__le16_to_cpu(fdiro->inode.mode) |
| 579 | & FILETYPE_INO_MASK) == |
| 580 | FILETYPE_INO_SYMLINK) { |
| 581 | type = FILETYPE_SYMLINK; |
| 582 | } else if ((__le16_to_cpu(fdiro->inode.mode) |
| 583 | & FILETYPE_INO_MASK) == |
| 584 | FILETYPE_INO_REG) { |
| 585 | type = FILETYPE_REG; |
| 586 | } |
| 587 | } |
| 588 | #ifdef DEBUG |
| 589 | printf("iterate >%s<\n", filename); |
| 590 | #endif /* of DEBUG */ |
| 591 | if ((name != NULL) &&(fnode != NULL) |
| 592 | &&(ftype != NULL)) { |
| 593 | if (strcmp(filename, name) == 0) { |
| 594 | *ftype = type; |
| 595 | *fnode = fdiro; |
| 596 | return 1; |
| 597 | } |
| 598 | } else { |
| 599 | if (fdiro->inode_read == 0) { |
| 600 | status = ext2fs_read_inode(diro->data, |
| 601 | __le32_to_cpu(dirent.inode), |
| 602 | &fdiro->inode); |
| 603 | if (status == 0) { |
| 604 | puts("ext2fs_read_inode 3 fail\n"); |
| 605 | free(fdiro); |
| 606 | return(0); |
| 607 | } |
| 608 | fdiro->inode_read = 1; |
| 609 | } |
| 610 | switch(type) { |
| 611 | case FILETYPE_DIRECTORY: |
| 612 | puts("<DIR> "); |
| 613 | break; |
| 614 | case FILETYPE_SYMLINK: |
| 615 | puts("<SYM> "); |
| 616 | break; |
| 617 | case FILETYPE_REG: |
| 618 | puts(" "); |
| 619 | break; |
| 620 | default: |
| 621 | puts("< ? > "); |
| 622 | break; |
| 623 | } |
| 624 | printdec(__le32_to_cpu(fdiro->inode.size)); |
| 625 | puts(" "); |
| 626 | puts(filename); |
| 627 | puts("\n"); |
| 628 | } |
| 629 | free(fdiro); |
| 630 | } |
| 631 | fpos += __le16_to_cpu(dirent.direntlen); |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | static char *ext2fs_read_symlink(ext2fs_node_t node) { |
| 638 | char *symlink; |
| 639 | struct ext2fs_node *diro = node; |
| 640 | int status; |
| 641 | |
| 642 | if (!diro->inode_read) { |
| 643 | status = ext2fs_read_inode(diro->data, diro->ino, |
| 644 | &diro->inode); |
| 645 | if (status == 0) { |
| 646 | return(0); |
| 647 | } |
| 648 | } |
| 649 | symlink = malloc(__le32_to_cpu(diro->inode.size) + 1); |
| 650 | if (!symlink) |
| 651 | return(0); |
| 652 | |
| 653 | /* If the filesize of the symlink is bigger than |
| 654 | 60 the symlink is stored in a separate block, |
| 655 | otherwise it is stored in the inode. */ |
| 656 | if (__le32_to_cpu(diro->inode.size) < 60) { |
| 657 | strncpy(symlink, diro->inode.b.symlink, |
| 658 | __le32_to_cpu(diro->inode.size)); |
| 659 | } else { |
| 660 | status = ext2fs_read_file(diro, 0, |
| 661 | __le32_to_cpu(diro->inode.size), |
| 662 | symlink); |
| 663 | if (status == 0) { |
| 664 | free(symlink); |
| 665 | return(0); |
| 666 | } |
| 667 | } |
| 668 | symlink[__le32_to_cpu(diro->inode.size)] = '\0'; |
| 669 | return(symlink); |
| 670 | } |
| 671 | |
| 672 | |
| 673 | int ext2fs_find_file1 |
| 674 | (const char *currpath, |
| 675 | ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) { |
| 676 | char fpath[strlen(currpath) + 1]; |
| 677 | char *name = fpath; |
| 678 | char *next; |
| 679 | int status; |
| 680 | int type = FILETYPE_DIRECTORY; |
| 681 | ext2fs_node_t currnode = currroot; |
| 682 | ext2fs_node_t oldnode = currroot; |
| 683 | |
| 684 | strncpy(fpath, currpath, strlen(currpath) + 1); |
| 685 | |
| 686 | /* Remove all leading slashes. */ |
| 687 | while (*name == '/') |
| 688 | name++; |
| 689 | |
| 690 | if (!*name) { |
| 691 | *currfound = currnode; |
| 692 | return 1; |
| 693 | } |
| 694 | |
| 695 | for(;;) { |
| 696 | int found; |
| 697 | |
| 698 | /* Extract the actual part from the pathname. */ |
| 699 | next = strchr(name, '/'); |
| 700 | if (next) { |
| 701 | /* Remove all leading slashes. */ |
| 702 | while (*next == '/') { |
| 703 | *(next++) = '\0'; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /* At this point it is expected that the current node is a directory, check if this is true. */ |
| 708 | if (type != FILETYPE_DIRECTORY) { |
| 709 | ext2fs_free_node(currnode, currroot); |
| 710 | return(0); |
| 711 | } |
| 712 | |
| 713 | oldnode = currnode; |
| 714 | |
| 715 | /* Iterate over the directory. */ |
| 716 | found = ext2fs_iterate_dir(currnode, name, &currnode, &type); |
| 717 | if (found == 0) |
| 718 | return(0); |
| 719 | |
| 720 | if (found == -1) |
| 721 | break; |
| 722 | |
| 723 | /* Read in the symlink and follow it. */ |
| 724 | if (type == FILETYPE_SYMLINK) { |
| 725 | char *symlink; |
| 726 | |
| 727 | /* Test if the symlink does not loop. */ |
| 728 | if (++symlinknest == 8) { |
| 729 | ext2fs_free_node(currnode, currroot); |
| 730 | ext2fs_free_node(oldnode, currroot); |
| 731 | return(0); |
| 732 | } |
| 733 | |
| 734 | symlink = ext2fs_read_symlink(currnode); |
| 735 | ext2fs_free_node(currnode, currroot); |
| 736 | |
| 737 | if (!symlink) { |
| 738 | ext2fs_free_node(oldnode, currroot); |
| 739 | return(0); |
| 740 | } |
| 741 | #ifdef DEBUG |
| 742 | printf("Got symlink >%s<\n", symlink); |
| 743 | #endif /* of DEBUG */ |
| 744 | /* The symlink is an absolute path, go back to the root inode. */ |
| 745 | if (symlink[0] == '/') { |
| 746 | ext2fs_free_node(oldnode, currroot); |
| 747 | oldnode = &ext2fs_root->diropen; |
| 748 | } |
| 749 | |
| 750 | /* Lookup the node the symlink points to. */ |
| 751 | status = ext2fs_find_file1(symlink, oldnode, |
| 752 | &currnode, &type); |
| 753 | |
| 754 | free(symlink); |
| 755 | |
| 756 | if (status == 0) { |
| 757 | ext2fs_free_node(oldnode, currroot); |
| 758 | return(0); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | ext2fs_free_node(oldnode, currroot); |
| 763 | |
| 764 | /* Found the node! */ |
| 765 | if (!next || *next == '\0') { |
| 766 | *currfound = currnode; |
| 767 | *foundtype = type; |
| 768 | return(1); |
| 769 | } |
| 770 | name = next; |
| 771 | } |
| 772 | return -1; |
| 773 | } |
| 774 | |
| 775 | |
| 776 | int ext2fs_find_file |
| 777 | (const char *path, |
| 778 | ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) { |
| 779 | int status; |
| 780 | int foundtype = FILETYPE_DIRECTORY; |
| 781 | |
| 782 | |
| 783 | symlinknest = 0; |
| 784 | if (!path) |
| 785 | return 0; |
| 786 | |
| 787 | status = ext2fs_find_file1(path, rootnode, foundnode, &foundtype); |
| 788 | if (status == 0) |
| 789 | return 0; |
| 790 | |
| 791 | /* Check if the node that was found was of the expected type. */ |
| 792 | if ((expecttype == FILETYPE_REG) &&(foundtype != expecttype)) { |
| 793 | return 0; |
| 794 | } else if ((expecttype == FILETYPE_DIRECTORY) |
| 795 | &&(foundtype != expecttype)) { |
| 796 | return 0; |
| 797 | } |
| 798 | return 1; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | int ext2fs_ls(char *dirname) { |
| 803 | ext2fs_node_t dirnode; |
| 804 | int status; |
| 805 | |
| 806 | if (ext2fs_root == NULL) |
| 807 | return 0; |
| 808 | |
| 809 | status = ext2fs_find_file(dirname, &ext2fs_root->diropen, &dirnode, |
| 810 | FILETYPE_DIRECTORY); |
| 811 | if (status != 1) { |
| 812 | puts("** Can not find directory. **\n"); |
| 813 | return 1; |
| 814 | } |
| 815 | ext2fs_iterate_dir(dirnode, NULL, NULL, NULL); |
| 816 | ext2fs_free_node(dirnode, &ext2fs_root->diropen); |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | int ext2fs_open(const char *filename) { |
| 822 | ext2fs_node_t fdiro = NULL; |
| 823 | int status; |
| 824 | int len; |
| 825 | int ret = -1; |
| 826 | |
| 827 | if (ext2fs_root == NULL) |
| 828 | goto fail; |
| 829 | |
| 830 | ext2fs_file = NULL; |
| 831 | status = ext2fs_find_file(filename, &ext2fs_root->diropen, &fdiro, |
| 832 | FILETYPE_REG); |
| 833 | if (status == 0) { |
| 834 | ret = -2; |
| 835 | goto fail; |
| 836 | } |
| 837 | |
| 838 | if (!fdiro->inode_read) { |
| 839 | status = ext2fs_read_inode(fdiro->data, fdiro->ino, |
| 840 | &fdiro->inode); |
| 841 | if (status == 0) { |
| 842 | ret = -3; |
| 843 | goto fail; |
| 844 | } |
| 845 | } |
| 846 | len = __le32_to_cpu(fdiro->inode.size); |
| 847 | ext2fs_file = fdiro; |
| 848 | |
| 849 | return(len); |
| 850 | |
| 851 | fail: |
| 852 | ext2fs_free_node(fdiro, &ext2fs_root->diropen); |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | |
| 857 | int ext2fs_close(void |
| 858 | ) { |
| 859 | if ((ext2fs_file != NULL) &&(ext2fs_root != NULL)) { |
| 860 | ext2fs_free_node(ext2fs_file, &ext2fs_root->diropen); |
| 861 | ext2fs_file = NULL; |
| 862 | } |
| 863 | if (ext2fs_root != NULL) { |
| 864 | free(ext2fs_root); |
| 865 | ext2fs_root = NULL; |
| 866 | } |
| 867 | if (indir1_block != NULL) { |
| 868 | free(indir1_block); |
| 869 | indir1_block = NULL; |
| 870 | indir1_size = 0; |
| 871 | indir1_blkno = -1; |
| 872 | } |
| 873 | if (indir2_block != NULL) { |
| 874 | free(indir2_block); |
| 875 | indir2_block = NULL; |
| 876 | indir2_size = 0; |
| 877 | indir2_blkno = -1; |
| 878 | } |
| 879 | return(0); |
| 880 | } |
| 881 | |
| 882 | |
| 883 | int ext2fs_read(char *buf, unsigned len) { |
| 884 | int status; |
| 885 | |
| 886 | if (ext2fs_root == NULL) |
| 887 | return 0; |
| 888 | |
| 889 | if (ext2fs_file == NULL) |
| 890 | return 0; |
| 891 | |
| 892 | status = ext2fs_read_file(ext2fs_file, 0, len, buf); |
| 893 | return status; |
| 894 | } |
| 895 | |
| 896 | |
| 897 | int ext2fs_mount(void) { |
| 898 | struct ext2_data *data; |
| 899 | int status; |
| 900 | |
| 901 | data = malloc(sizeof(struct ext2_data)); |
| 902 | if (!data) |
| 903 | return 0; |
| 904 | |
| 905 | /* Read the superblock. */ |
| 906 | status = ext2fs_devread(1 * 2, 0, 0, sizeof(struct ext2_sblock), |
| 907 | (char *) &data->sblock); |
| 908 | if (!status) |
| 909 | goto fail; |
| 910 | |
| 911 | /* Make sure this is an ext2 filesystem. */ |
| 912 | if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC) |
| 913 | goto fail; |
| 914 | |
| 915 | if (__le32_to_cpu(data->sblock.revision_level) == EXT2_GOOD_OLD_REV) |
| 916 | ext2_inode_size = EXT2_GOOD_OLD_INODE_SIZE; |
| 917 | else |
| 918 | ext2_inode_size = __le16_to_cpu (data->sblock.inode_size); |
| 919 | |
| 920 | data->diropen.data = data; |
| 921 | data->diropen.ino = 2; |
| 922 | data->diropen.inode_read = 1; |
| 923 | data->inode = &data->diropen.inode; |
| 924 | |
| 925 | status = ext2fs_read_inode(data, 2, data->inode); |
| 926 | if (status == 0) |
| 927 | goto fail; |
| 928 | |
| 929 | ext2fs_root = data; |
| 930 | |
| 931 | return 1; |
| 932 | |
| 933 | fail: |
| 934 | puts("Failed to mount ext2 filesystem...\n"); |
| 935 | free(data); |
| 936 | ext2fs_root = NULL; |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | |