📄 ext2fs.c
字号:
buf += blocksize - skipfirst; } return (len);}static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype){ unsigned int fpos = 0; int status; struct ext2fs_node *diro = (struct ext2fs_node *) dir;#ifdef DEBUG if (name != NULL) printf ("Iterate dir %s\n", name);#endif /* of DEBUG */ if (!diro->inode_read) { status = ext2fs_read_inode (diro->data, diro->ino, &diro->inode); if (status == 0) { return (0); } } /* Search the file. */ while (fpos < __le32_to_cpu (diro->inode.size)) { struct ext2_dirent dirent; status = ext2fs_read_file (diro, fpos, sizeof (struct ext2_dirent), (char *) &dirent); if (status < 1) { return (0); } if (dirent.namelen != 0) { char filename[dirent.namelen + 1]; ext2fs_node_t fdiro; int type = FILETYPE_UNKNOWN; status = ext2fs_read_file (diro, fpos + sizeof (struct ext2_dirent), dirent.namelen, filename); if (status < 1) { return (0); } fdiro = malloc (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 { /* The filetype can not be read from the dirent, get it from inode */ status = ext2fs_read_inode (diro->data, __le32_to_cpu(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 = ext2fs_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 ("%10d %s\n", __le32_to_cpu (fdiro->inode.size), filename); } free (fdiro); } fpos += __le16_to_cpu (dirent.direntlen); } return (0);}static char *ext2fs_read_symlink (ext2fs_node_t node) { char *symlink; struct ext2fs_node *diro = node; int status; if (!diro->inode_read) { status = ext2fs_read_inode (diro->data, diro->ino, &diro->inode); if (status == 0) { return (0); } } symlink = malloc (__le32_to_cpu (diro->inode.size) + 1); if (!symlink) { return (0); } /* If the filesize of the symlink is bigger than 60 the symlink is stored in a separate block, otherwise it is stored in the inode. */ if (__le32_to_cpu (diro->inode.size) <= 60) { strncpy (symlink, diro->inode.b.symlink, __le32_to_cpu (diro->inode.size)); } else { status = ext2fs_read_file (diro, 0, __le32_to_cpu (diro->inode.size), symlink); if (status == 0) { free (symlink); return (0); } } symlink[__le32_to_cpu (diro->inode.size)] = '\0'; return (symlink);}int ext2fs_find_file1 (const char *currpath, ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) { char fpath[strlen (currpath) + 1]; char *name = fpath; char *next; int status; int type = FILETYPE_DIRECTORY; ext2fs_node_t currnode = currroot; ext2fs_node_t 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'; } } /* At this point it is expected that the current node is a directory, check if this is true. */ if (type != FILETYPE_DIRECTORY) { ext2fs_free_node (currnode, currroot); return (0); } oldnode = currnode; /* Iterate over the directory. */ found = ext2fs_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) { ext2fs_free_node (currnode, currroot); ext2fs_free_node (oldnode, currroot); return (0); } symlink = ext2fs_read_symlink (currnode); ext2fs_free_node (currnode, currroot); if (!symlink) { ext2fs_free_node (oldnode, currroot); return (0); }#ifdef DEBUG printf ("Got symlink >%s<\n", symlink);#endif /* of DEBUG */ /* The symlink is an absolute path, go back to the root inode. */ if (symlink[0] == '/') { ext2fs_free_node (oldnode, currroot); oldnode = &ext2fs_root->diropen; } /* Lookup the node the symlink points to. */ status = ext2fs_find_file1 (symlink, oldnode, &currnode, &type); free (symlink); if (status == 0) { ext2fs_free_node (oldnode, currroot); return (0); } } ext2fs_free_node (oldnode, currroot); /* Found the node! */ if (!next || *next == '\0') { *currfound = currnode; *foundtype = type; return (1); } name = next; } return (-1);}int ext2fs_find_file (const char *path, ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) { int status; int foundtype = FILETYPE_DIRECTORY; symlinknest = 0; if (!path || path[0] != '/') { return (0); } status = ext2fs_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 ext2fs_ls (char *dirname) { ext2fs_node_t dirnode; int status; if (ext2fs_root == NULL) { return (0); } status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode, FILETYPE_DIRECTORY); if (status != 1) { printf ("** Can not find directory. **\n"); return (1); } ext2fs_iterate_dir (dirnode, NULL, NULL, NULL); ext2fs_free_node (dirnode, &ext2fs_root->diropen); return (0);}int ext2fs_open (char *filename) { ext2fs_node_t fdiro = NULL; int status; int len; if (ext2fs_root == NULL) { return (0); } ext2fs_file = NULL; status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro, FILETYPE_REG); if (status == 0) { goto fail; } if (!fdiro->inode_read) { status = ext2fs_read_inode (fdiro->data, fdiro->ino, &fdiro->inode); if (status == 0) { goto fail; } } len = __le32_to_cpu (fdiro->inode.size); ext2fs_file = fdiro; return (len); fail: ext2fs_free_node (fdiro, &ext2fs_root->diropen); return (0);}int ext2fs_close (void ) { if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) { ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen); ext2fs_file = NULL; } if (ext2fs_root != NULL) { free (ext2fs_root); ext2fs_root = NULL; } if (indir1_block != NULL) { free (indir1_block); indir1_block = NULL; indir1_size = 0; indir1_blkno = -1; } if (indir2_block != NULL) { free (indir2_block); indir2_block = NULL; indir2_size = 0; indir2_blkno = -1; } return (0);}int ext2fs_read (char *buf, unsigned len) { int status; if (ext2fs_root == NULL) { return (0); } if (ext2fs_file == NULL) { return (0); } status = ext2fs_read_file (ext2fs_file, 0, len, buf); return (status);}int ext2fs_mount (unsigned part_length) { struct ext2_data *data; int status; data = malloc (sizeof (struct ext2_data)); if (!data) { return (0); } /* Read the superblock. */ status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock), (char *) &data->sblock); if (status == 0) { goto fail; } /* Make sure this is an ext2 filesystem. */ if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) { goto fail; } data->diropen.data = data; data->diropen.ino = 2; data->diropen.inode_read = 1; data->inode = &data->diropen.inode; status = ext2fs_read_inode (data, 2, data->inode); if (status == 0) { goto fail; } ext2fs_root = data; return (1);fail: printf ("Failed to mount ext2 filesystem...\n"); free (data); ext2fs_root = NULL; return (0);}#endif /* CFG_CMD_EXT2FS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -