Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
status = ext4fs_read_inode(diro->data,
__le32_to_cpu(
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",
__le32_to_cpu(fdiro->inode.size),
filename);
}
free(fdiro);
}
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)
return 0;
}
symlink = zalloc(__le32_to_cpu(diro->inode.size) + 1);
if (!symlink)
return 0;
if (__le32_to_cpu(diro->inode.size) <= 60) {
strncpy(symlink, diro->inode.b.symlink,
__le32_to_cpu(diro->inode.size));
} else {
status = ext4fs_read_file(diro, 0,
__le32_to_cpu(diro->inode.size),
symlink, &actread);
if ((status < 0) || (actread == 0)) {
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
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
free(symlink);
return 0;
}
}
symlink[__le32_to_cpu(diro->inode.size)] = '\0';
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);
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
2259
2260
2261
2262
2263
2264
2265
2266
2267
if (status == 0)
goto fail;
/* Make sure this is an ext2 filesystem. */
if (__le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
goto fail;
if (__le32_to_cpu(data->sblock.revision_level == 0))
fs->inodesz = 128;
else
fs->inodesz = __le16_to_cpu(data->sblock.inode_size);
debug("EXT2 rev %d, inode_size %d\n",
__le32_to_cpu(data->sblock.revision_level), fs->inodesz);
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;
}