Newer
Older
char filename[dirent.namelen + 1];
struct ext2fs_node *fdiro;
int type = FILETYPE_UNKNOWN;
status = ext4fs_read_file(diro,
fpos +
sizeof(struct ext2_dirent),
dirent.namelen, filename,
&actread);
if (status < 0)
return 0;
fdiro = zalloc(sizeof(struct ext2fs_node));
if (!fdiro)
return 0;
fdiro->data = diro->data;
fdiro->ino = le32_to_cpu(dirent.inode);
filename[dirent.namelen] = '\0';
if (dirent.filetype != FILETYPE_UNKNOWN) {
fdiro->inode_read = 0;
if (dirent.filetype == FILETYPE_DIRECTORY)
type = FILETYPE_DIRECTORY;
else if (dirent.filetype == FILETYPE_SYMLINK)
type = FILETYPE_SYMLINK;
else if (dirent.filetype == FILETYPE_REG)
type = FILETYPE_REG;
} else {
status = ext4fs_read_inode(diro->data,
(dirent.inode),
&fdiro->inode);
if (status == 0) {
free(fdiro);
return 0;
}
fdiro->inode_read = 1;
if ((le16_to_cpu(fdiro->inode.mode) &
FILETYPE_INO_MASK) ==
FILETYPE_INO_DIRECTORY) {
type = FILETYPE_DIRECTORY;
} else if ((le16_to_cpu(fdiro->inode.mode)
& FILETYPE_INO_MASK) ==
FILETYPE_INO_SYMLINK) {
type = FILETYPE_SYMLINK;
} else if ((le16_to_cpu(fdiro->inode.mode)
& FILETYPE_INO_MASK) ==
FILETYPE_INO_REG) {
type = FILETYPE_REG;
}
}
#ifdef DEBUG
printf("iterate >%s<\n", filename);
#endif /* of DEBUG */
if ((name != NULL) && (fnode != NULL)
&& (ftype != NULL)) {
if (strcmp(filename, name) == 0) {
*ftype = type;
*fnode = fdiro;
return 1;
}
} else {
if (fdiro->inode_read == 0) {
status = ext4fs_read_inode(diro->data,
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
dirent.inode),
&fdiro->inode);
if (status == 0) {
free(fdiro);
return 0;
}
fdiro->inode_read = 1;
}
switch (type) {
case FILETYPE_DIRECTORY:
printf("<DIR> ");
break;
case FILETYPE_SYMLINK:
printf("<SYM> ");
break;
case FILETYPE_REG:
printf(" ");
break;
default:
printf("< ? > ");
break;
}
printf("%10u %s\n",
fpos += le16_to_cpu(dirent.direntlen);
}
return 0;
}
static char *ext4fs_read_symlink(struct ext2fs_node *node)
{
char *symlink;
struct ext2fs_node *diro = node;
int status;
if (!diro->inode_read) {
status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
if (status == 0)
symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
symlink, &actread);
if ((status < 0) || (actread == 0)) {
symlink[le32_to_cpu(diro->inode.size)] = '\0';
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
return symlink;
}
static int ext4fs_find_file1(const char *currpath,
struct ext2fs_node *currroot,
struct ext2fs_node **currfound, int *foundtype)
{
char fpath[strlen(currpath) + 1];
char *name = fpath;
char *next;
int status;
int type = FILETYPE_DIRECTORY;
struct ext2fs_node *currnode = currroot;
struct ext2fs_node *oldnode = currroot;
strncpy(fpath, currpath, strlen(currpath) + 1);
/* Remove all leading slashes. */
while (*name == '/')
name++;
if (!*name) {
*currfound = currnode;
return 1;
}
for (;;) {
int found;
/* Extract the actual part from the pathname. */
next = strchr(name, '/');
if (next) {
/* Remove all leading slashes. */
while (*next == '/')
*(next++) = '\0';
}
if (type != FILETYPE_DIRECTORY) {
ext4fs_free_node(currnode, currroot);
return 0;
}
oldnode = currnode;
/* Iterate over the directory. */
found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
if (found == 0)
return 0;
if (found == -1)
break;
/* Read in the symlink and follow it. */
if (type == FILETYPE_SYMLINK) {
char *symlink;
/* Test if the symlink does not loop. */
if (++symlinknest == 8) {
ext4fs_free_node(currnode, currroot);
ext4fs_free_node(oldnode, currroot);
return 0;
}
symlink = ext4fs_read_symlink(currnode);
ext4fs_free_node(currnode, currroot);
if (!symlink) {
ext4fs_free_node(oldnode, currroot);
return 0;
}
debug("Got symlink >%s<\n", symlink);
if (symlink[0] == '/') {
ext4fs_free_node(oldnode, currroot);
oldnode = &ext4fs_root->diropen;
}
/* Lookup the node the symlink points to. */
status = ext4fs_find_file1(symlink, oldnode,
&currnode, &type);
free(symlink);
if (status == 0) {
ext4fs_free_node(oldnode, currroot);
return 0;
}
}
ext4fs_free_node(oldnode, currroot);
/* Found the node! */
if (!next || *next == '\0') {
*currfound = currnode;
*foundtype = type;
return 1;
}
name = next;
}
return -1;
}
int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
struct ext2fs_node **foundnode, int expecttype)
{
int status;
int foundtype = FILETYPE_DIRECTORY;
symlinknest = 0;
if (!path)
return 0;
status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
if (status == 0)
return 0;
/* Check if the node that was found was of the expected type. */
if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
return 0;
else if ((expecttype == FILETYPE_DIRECTORY)
&& (foundtype != expecttype))
return 0;
return 1;
}
int ext4fs_open(const char *filename, loff_t *len)
{
struct ext2fs_node *fdiro = NULL;
int status;
if (ext4fs_root == NULL)
return -1;
ext4fs_file = NULL;
status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
FILETYPE_REG);
if (status == 0)
goto fail;
if (!fdiro->inode_read) {
status = ext4fs_read_inode(fdiro->data, fdiro->ino,
&fdiro->inode);
if (status == 0)
goto fail;
}
*len = le32_to_cpu(fdiro->inode.size);
fail:
ext4fs_free_node(fdiro, &ext4fs_root->diropen);
return -1;
}
int ext4fs_mount(unsigned part_length)
{
struct ext2_data *data;
int status;
struct ext_filesystem *fs = get_fs();
data = zalloc(SUPERBLOCK_SIZE);
if (!data)
return 0;
/* Read the superblock. */
status = ext4_read_superblock((char *)&data->sblock);
if (status == 0)
goto fail;
/* Make sure this is an ext2 filesystem. */
if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
/*
* The 64bit feature was enabled when metadata_csum was enabled
* and we do not support metadata_csum (and cannot reliably find
* files when it is set. Refuse to mount.
*/
if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
goto fail;
}
if (le32_to_cpu(data->sblock.revision_level) == 0) {
} else {
debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
__le32_to_cpu(data->sblock.feature_compatibility),
__le32_to_cpu(data->sblock.feature_incompat),
__le32_to_cpu(data->sblock.feature_ro_compat));
fs->inodesz = le16_to_cpu(data->sblock.inode_size);
fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
EXT4_FEATURE_INCOMPAT_64BIT ?
le16_to_cpu(data->sblock.descriptor_size) : 32;
}
debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
le32_to_cpu(data->sblock.revision_level),
fs->inodesz, fs->gdsize);
data->diropen.data = data;
data->diropen.ino = 2;
data->diropen.inode_read = 1;
data->inode = &data->diropen.inode;
status = ext4fs_read_inode(data, 2, data->inode);
if (status == 0)
goto fail;
ext4fs_root = data;
return 1;
fail:
printf("Failed to mount ext2 filesystem...\n");
free(data);
ext4fs_root = NULL;
return 0;
}