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