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