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