fs: implement readdir function
this also fixes flawed file parsing in ext2_open_file
This commit is contained in:
parent
4c040f280f
commit
0eb461410c
7 changed files with 292 additions and 67 deletions
192
fs/ext2.c
192
fs/ext2.c
|
|
@ -5,6 +5,15 @@
|
||||||
#include <fs/ext2.h>
|
#include <fs/ext2.h>
|
||||||
#include <fs/vfs.h>
|
#include <fs/vfs.h>
|
||||||
|
|
||||||
|
#define ITYPE_DECL(_ino, _de) [(_ino >> 12)] = (_de)
|
||||||
|
static uint8_t inode_to_ftype[] = {
|
||||||
|
ITYPE_DECL(EXT2_INO_FIFO, VFS_FILE_FIFO), ITYPE_DECL(EXT2_INO_CHARDEV, VFS_FILE_CHARDEV),
|
||||||
|
ITYPE_DECL(EXT2_INO_DIR, VFS_FILE_DIR), ITYPE_DECL(EXT2_INO_BLKDEV, VFS_FILE_BLKDEV),
|
||||||
|
ITYPE_DECL(EXT2_INO_REG, VFS_FILE_FILE), ITYPE_DECL(EXT2_INO_SYMLINK, VFS_FILE_SYMLINK),
|
||||||
|
ITYPE_DECL(EXT2_INO_SOCK, VFS_FILE_SOCK),
|
||||||
|
};
|
||||||
|
#undef ITYPE_DECL
|
||||||
|
|
||||||
static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb_struct)
|
static int ext2_read_super(struct block_device *bdev, struct ext2_superblock *sb_struct)
|
||||||
{
|
{
|
||||||
char *buf = kmalloc(1024, ALLOC_DMA);
|
char *buf = kmalloc(1024, ALLOC_DMA);
|
||||||
|
|
@ -200,10 +209,10 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ext2_open_file(void *extfs, struct file *file, const char *path)
|
int ext2_open_file(struct fs *vfs, struct file *file, const char *path)
|
||||||
{
|
{
|
||||||
/* find the inode of the file */
|
/* find the inode of the file */
|
||||||
struct ext2fs *fs = (struct ext2fs *)extfs;
|
struct ext2fs *fs = (struct ext2fs *)vfs->fs;
|
||||||
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||||
if (!inode)
|
if (!inode)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
@ -240,31 +249,19 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
||||||
|
|
||||||
ext2_inode_read_block(fs, inode, entry, i);
|
ext2_inode_read_block(fs, inode, entry, i);
|
||||||
|
|
||||||
while (entry->inode) {
|
uint32_t rec_offset = 0;
|
||||||
char *name = kmalloc(entry->name_len + 1, ALLOC_KERN);
|
while (entry->inode && rec_offset < fs->block_size) {
|
||||||
if (!name) {
|
|
||||||
kfree(inode);
|
|
||||||
kfree(path_copy);
|
|
||||||
kfree(entry);
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(name, entry->name, entry->name_len);
|
|
||||||
name[entry->name_len] = '\0';
|
|
||||||
|
|
||||||
if (!strcmp(name, token)) {
|
|
||||||
kfree(name);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
kfree(name);
|
|
||||||
|
|
||||||
inode_no = entry->inode;
|
inode_no = entry->inode;
|
||||||
|
if (!strncmp(entry->name, token, MIN(entry->name_len, strlen(token))))
|
||||||
|
goto found;
|
||||||
|
|
||||||
entry = (void *)entry + entry->rec_len;
|
entry = (void *)entry + entry->rec_len;
|
||||||
|
rec_offset += entry->rec_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!entry->inode) {
|
found:
|
||||||
|
if (!entry || !entry->inode) {
|
||||||
kfree(inode);
|
kfree(inode);
|
||||||
kfree(path_copy);
|
kfree(path_copy);
|
||||||
kfree(entry);
|
kfree(entry);
|
||||||
|
|
@ -280,6 +277,7 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
||||||
|
|
||||||
memcpy(&file->inode, inode, sizeof(struct inode));
|
memcpy(&file->inode, inode, sizeof(struct inode));
|
||||||
file->size = inode->size;
|
file->size = inode->size;
|
||||||
|
file->type = inode_to_ftype[inode->mode >> 12];
|
||||||
file->inode_num = inode_no;
|
file->inode_num = inode_no;
|
||||||
|
|
||||||
/* check for long filesize */
|
/* check for long filesize */
|
||||||
|
|
@ -289,26 +287,167 @@ int ext2_open_file(void *extfs, struct file *file, const char *path)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ext2_close_file(void *extfs, struct file *file)
|
int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out)
|
||||||
|
{
|
||||||
|
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||||
|
if (!inode)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
int ret = ext2_read_inode(fs->fs, inode, ino_num);
|
||||||
|
if (ret < 0) {
|
||||||
|
kfree(inode);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((inode->mode & 0xF000) != EXT2_INO_DIR) {
|
||||||
|
kfree(inode);
|
||||||
|
return -ENOTDIR;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ext2fs *extfs = (struct ext2fs *)fs->fs;
|
||||||
|
struct ext2_dir_entry *entry = kmalloc(extfs->block_size, ALLOC_DMA);
|
||||||
|
void *oentry = entry;
|
||||||
|
if (!entry) {
|
||||||
|
kfree(inode);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* populate the dirent array with all the entries in the inode */
|
||||||
|
int num_entries = 0;
|
||||||
|
size_t dir_arr_size = inode->blocks * extfs->block_size;
|
||||||
|
dir_arr_size /= sizeof(struct dirent);
|
||||||
|
|
||||||
|
struct dirent *dir_arr = kmalloc(dir_arr_size * sizeof(struct dirent), ALLOC_KERN);
|
||||||
|
if (!dir_arr) {
|
||||||
|
kfree(inode);
|
||||||
|
kfree(entry);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < inode->blocks; i++) {
|
||||||
|
ext2_inode_read_block(extfs, inode, entry, i);
|
||||||
|
|
||||||
|
uint32_t rec_offset = 0;
|
||||||
|
while (entry->inode && rec_offset < extfs->block_size) {
|
||||||
|
struct dirent *dirent = &dir_arr[num_entries];
|
||||||
|
|
||||||
|
dirent->inode = entry->inode;
|
||||||
|
dirent->type = inode_to_ftype[entry->file_type];
|
||||||
|
dirent->reclen = entry->rec_len;
|
||||||
|
|
||||||
|
memcpy(dirent->name, entry->name, MIN(255, entry->name_len));
|
||||||
|
dirent->name[MIN(255, entry->name_len)] = '\0';
|
||||||
|
|
||||||
|
num_entries++;
|
||||||
|
|
||||||
|
entry = (void *)entry + entry->rec_len;
|
||||||
|
rec_offset += entry->rec_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kfree(inode);
|
||||||
|
kfree(oentry);
|
||||||
|
|
||||||
|
*out = dir_arr;
|
||||||
|
return num_entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ext2_close_file(struct fs *vfs, struct file *file)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ext2_write_file(void *extfs, struct file *file, void *buf, size_t count)
|
int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t count)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ext2_read_file(void *extfs, struct file *file, void *buf, size_t count)
|
int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t count)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ext2_seek_file(void *extfs, struct file *file, size_t offset)
|
int ext2_seek_file(struct fs *vfs, struct file *file, size_t offset)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef KDEBUG
|
||||||
|
static void ext2_print_dir(struct ext2fs *fs, struct ext2_inode *inode, int indent)
|
||||||
|
{
|
||||||
|
static const char *types[] = { "UNKNOWN", "FILE", "DIR", "CHARDEV", "BLOCKDEV", "FIFO", "SOCKET", "SYMLINK" };
|
||||||
|
/* print the names of files and directories in the root directory */
|
||||||
|
|
||||||
|
struct ext2_dir_entry *entry = kmalloc(fs->block_size, ALLOC_DMA);
|
||||||
|
void *oentry = entry;
|
||||||
|
if (!entry)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ext2_read_block(fs, entry, inode->block[0]);
|
||||||
|
|
||||||
|
while (entry->inode) {
|
||||||
|
char *name = kmalloc(entry->name_len + 1, ALLOC_KERN);
|
||||||
|
if (!name)
|
||||||
|
break;
|
||||||
|
|
||||||
|
memcpy(name, entry->name, entry->name_len);
|
||||||
|
name[entry->name_len] = '\0';
|
||||||
|
|
||||||
|
kprintf(LOG_DEBUG);
|
||||||
|
for (int i = 0; i < indent; i++)
|
||||||
|
kprintf(" ");
|
||||||
|
kprintf("[%s] %s\n", types[MIN(7, entry->file_type)], name);
|
||||||
|
|
||||||
|
if (entry->file_type == EXT2_DE_DIR) {
|
||||||
|
if (!strcmp(name, ".") || !strcmp(name, ".."))
|
||||||
|
goto next;
|
||||||
|
|
||||||
|
struct ext2_inode *in = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||||
|
if (!in)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ext2_read_inode(fs, in, entry->inode);
|
||||||
|
ext2_print_dir(fs, in, indent + 1);
|
||||||
|
kfree(in);
|
||||||
|
} else if (entry->file_type == EXT2_DE_FILE) {
|
||||||
|
/* print the contents of the file */
|
||||||
|
struct ext2_inode *in = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
|
||||||
|
if (!in)
|
||||||
|
break;
|
||||||
|
|
||||||
|
ext2_read_inode(fs, in, entry->inode);
|
||||||
|
|
||||||
|
char *buf = kmalloc(in->size, ALLOC_KERN);
|
||||||
|
|
||||||
|
if (!buf) {
|
||||||
|
kfree(in);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ext2_read_block(fs, buf, in->block[0]);
|
||||||
|
|
||||||
|
kprintf(LOG_DEBUG);
|
||||||
|
for (int i = 0; i < indent + 1; i++)
|
||||||
|
kprintf(" ");
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < in->size; i++) {
|
||||||
|
if (buf[i] == '\n')
|
||||||
|
kprintf("\n");
|
||||||
|
else
|
||||||
|
console_write(buf[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next:
|
||||||
|
|
||||||
|
kfree(name);
|
||||||
|
|
||||||
|
entry = (void *)entry + entry->rec_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
kfree(oentry);
|
||||||
|
}
|
||||||
|
#endif /* KDEBUG */
|
||||||
|
|
||||||
struct fs *ext2_init_fs(struct block_device *bdev)
|
struct fs *ext2_init_fs(struct block_device *bdev)
|
||||||
{
|
{
|
||||||
struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN);
|
struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN);
|
||||||
|
|
@ -354,6 +493,7 @@ struct fs *ext2_init_fs(struct block_device *bdev)
|
||||||
.read = ext2_read_file,
|
.read = ext2_read_file,
|
||||||
.write = ext2_write_file,
|
.write = ext2_write_file,
|
||||||
.seek = ext2_seek_file,
|
.seek = ext2_seek_file,
|
||||||
|
.readdir = ext2_readdir,
|
||||||
};
|
};
|
||||||
|
|
||||||
ret->ops = ops;
|
ret->ops = ops;
|
||||||
|
|
|
||||||
172
fs/vfs.c
172
fs/vfs.c
|
|
@ -14,6 +14,116 @@ static int fd_counter = 0;
|
||||||
|
|
||||||
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
||||||
|
|
||||||
|
int write(int fd, const void *buf, size_t count)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int read(int fd, void *buf, size_t count)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int open(const char *pathname, int flags)
|
||||||
|
{
|
||||||
|
/* allocate a file descriptor */
|
||||||
|
struct file_descriptor *fd = slab_alloc(fd_slab);
|
||||||
|
if (!fd) {
|
||||||
|
kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* find the file */
|
||||||
|
int result = rootfs->ops.open(rootfs, &fd->file, pathname);
|
||||||
|
|
||||||
|
if (result < 0) {
|
||||||
|
kprintf(LOG_ERROR "Failed to open %s\n", pathname);
|
||||||
|
return result;
|
||||||
|
} else if (result == VFSE_IS_BDEV) {
|
||||||
|
/* TODO: continue search into mounted filesystems */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* add the file descriptor to the tree */
|
||||||
|
fd->fd = fd_counter;
|
||||||
|
fd->fs = rootfs;
|
||||||
|
fd_counter += 1;
|
||||||
|
|
||||||
|
struct rbnode *fdnode = rbt_insert(kfd, fd->fd);
|
||||||
|
fdnode->value = (uint64_t)fd;
|
||||||
|
|
||||||
|
return fd->fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
int close(int fd)
|
||||||
|
{
|
||||||
|
struct rbnode *fdnode = rbt_search(kfd, fd);
|
||||||
|
|
||||||
|
if (!fdnode)
|
||||||
|
return -EBADF;
|
||||||
|
|
||||||
|
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
||||||
|
fdesc->fs->ops.close(fdesc->fs, &fdesc->file);
|
||||||
|
|
||||||
|
slab_free(fd_slab, fdesc);
|
||||||
|
rbt_delete(kfd, fdnode);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int seek(int fd, size_t offset, int whence)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
DIR *opendir(const char *name)
|
||||||
|
{
|
||||||
|
int fd = open(name, 0);
|
||||||
|
if (fd < 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
struct file_descriptor *fdesc = (struct file_descriptor *)rbt_search(kfd, fd)->value;
|
||||||
|
|
||||||
|
if (!fdesc)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
struct fs *fs = fdesc->fs;
|
||||||
|
struct file *file = &fdesc->file;
|
||||||
|
|
||||||
|
if (file->type != VFS_FILE_DIR) {
|
||||||
|
close(fd);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN);
|
||||||
|
dir->fd = fd;
|
||||||
|
dir->fs = fs;
|
||||||
|
dir->file = file;
|
||||||
|
dir->pos = 0;
|
||||||
|
dir->dirents = NULL;
|
||||||
|
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dirent *readdir(DIR *dir)
|
||||||
|
{
|
||||||
|
struct fs *fs = dir->fs;
|
||||||
|
struct file *file = dir->file;
|
||||||
|
|
||||||
|
if (file->type != VFS_FILE_DIR)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (dir->dirents == NULL)
|
||||||
|
dir->num_dirents = fs->ops.readdir(fs, file->inode_num, &dir->dirents);
|
||||||
|
|
||||||
|
if (dir->dirents == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (dir->pos >= dir->num_dirents)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return &dir->dirents[dir->pos++];
|
||||||
|
}
|
||||||
|
|
||||||
void vfs_init(const char *rootdev_name)
|
void vfs_init(const char *rootdev_name)
|
||||||
{
|
{
|
||||||
kfd = kzalloc(sizeof(struct rbtree), ALLOC_DMA);
|
kfd = kzalloc(sizeof(struct rbtree), ALLOC_DMA);
|
||||||
|
|
@ -46,65 +156,3 @@ void vfs_init(const char *rootdev_name)
|
||||||
fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0);
|
fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int write(int fd, const void *buf, size_t count)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int read(int fd, void *buf, size_t count)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int open(const char *pathname, int flags)
|
|
||||||
{
|
|
||||||
/* allocate a file descriptor */
|
|
||||||
struct file_descriptor *fd = slab_alloc(fd_slab);
|
|
||||||
if (!fd) {
|
|
||||||
kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* find the file */
|
|
||||||
struct file file;
|
|
||||||
int result = rootfs->ops.open(rootfs, &file, pathname);
|
|
||||||
|
|
||||||
if (result < 0) {
|
|
||||||
kprintf(LOG_ERROR "Failed to open %s\n", pathname);
|
|
||||||
return result;
|
|
||||||
} else if (result == VFSE_IS_BDEV) {
|
|
||||||
/* TODO: continue search into mounted filesystems */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add the file descriptor to the tree */
|
|
||||||
fd->fd = fd_counter;
|
|
||||||
fd->fs = rootfs;
|
|
||||||
fd->file = file;
|
|
||||||
fd_counter += 1;
|
|
||||||
|
|
||||||
struct rbnode *fdnode = rbt_insert(kfd, fd->fd);
|
|
||||||
fdnode->value = (uint64_t)fd;
|
|
||||||
|
|
||||||
return fd->fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
int close(int fd)
|
|
||||||
{
|
|
||||||
struct rbnode *fdnode = rbt_search(kfd, fd);
|
|
||||||
|
|
||||||
if (!fdnode)
|
|
||||||
return -EBADF;
|
|
||||||
|
|
||||||
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
|
|
||||||
fdesc->fs->ops.close(fdesc->fs, &fdesc->file);
|
|
||||||
|
|
||||||
slab_free(fd_slab, fdesc);
|
|
||||||
rbt_delete(kfd, fdnode);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int seek(int fd, size_t offset, int whence)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
#define ENOENT 2
|
#define ENOENT 2
|
||||||
#define EINVAL 3
|
#define EINVAL 3
|
||||||
#define EBADF 4
|
#define EBADF 4
|
||||||
|
#define ENOTDIR 5
|
||||||
|
|
||||||
const char *strerror(int errnum);
|
const char *strerror(int errnum);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,14 @@
|
||||||
#define EXT2_DE_SOCK 6
|
#define EXT2_DE_SOCK 6
|
||||||
#define EXT2_DE_SYMLINK 7
|
#define EXT2_DE_SYMLINK 7
|
||||||
|
|
||||||
|
#define EXT2_INO_FIFO 0x1000
|
||||||
|
#define EXT2_INO_CHARDEV 0x2000
|
||||||
|
#define EXT2_INO_DIR 0x4000
|
||||||
|
#define EXT2_INO_BLKDEV 0x6000
|
||||||
|
#define EXT2_INO_REG 0x8000
|
||||||
|
#define EXT2_INO_SYMLINK 0xA000
|
||||||
|
#define EXT2_INO_SOCK 0xC000
|
||||||
|
|
||||||
#define EXT2_FLAG_LONG_FILESIZE 0x0002
|
#define EXT2_FLAG_LONG_FILESIZE 0x0002
|
||||||
|
|
||||||
struct ext2_superblock {
|
struct ext2_superblock {
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,15 @@
|
||||||
|
|
||||||
#define VFS_TYPE_EXT2 0x0001
|
#define VFS_TYPE_EXT2 0x0001
|
||||||
|
|
||||||
|
#define VFS_FILE_FILE 0x0001
|
||||||
|
#define VFS_FILE_DIR 0x0002
|
||||||
|
#define VFS_FILE_CHARDEV 0x0003
|
||||||
|
#define VFS_FILE_BLKDEV 0x0004
|
||||||
|
#define VFS_FILE_FIFO 0x0005
|
||||||
|
#define VFS_FILE_SOCK 0x0006
|
||||||
|
#define VFS_FILE_SYMLINK 0x0007
|
||||||
|
|
||||||
|
|
||||||
struct inode {
|
struct inode {
|
||||||
uint16_t mode;
|
uint16_t mode;
|
||||||
uint16_t uid;
|
uint16_t uid;
|
||||||
|
|
@ -37,16 +46,25 @@ struct inode {
|
||||||
|
|
||||||
struct dirent {
|
struct dirent {
|
||||||
uint64_t inode;
|
uint64_t inode;
|
||||||
uint64_t offset;
|
|
||||||
uint16_t reclen;
|
uint16_t reclen;
|
||||||
uint8_t type;
|
uint8_t type;
|
||||||
char name[256];
|
char name[256];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef struct _DIR {
|
||||||
|
struct fs *fs;
|
||||||
|
struct dirent *dirents;
|
||||||
|
size_t num_dirents;
|
||||||
|
struct file *file;
|
||||||
|
int fd;
|
||||||
|
uint64_t pos;
|
||||||
|
} DIR;
|
||||||
|
|
||||||
struct file {
|
struct file {
|
||||||
struct inode inode;
|
struct inode inode;
|
||||||
|
|
||||||
uint32_t inode_num; /* inode number */
|
uint32_t inode_num; /* inode number */
|
||||||
|
uint32_t type; /* type of the file */
|
||||||
uint64_t size; /* size of the file */
|
uint64_t size; /* size of the file */
|
||||||
|
|
||||||
char *path; /* path to the file */
|
char *path; /* path to the file */
|
||||||
|
|
@ -66,11 +84,12 @@ struct file_descriptor {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fs_ops {
|
struct fs_ops {
|
||||||
int (*read)(void *fs, struct file *file, void *buf, size_t size);
|
int (*read)(struct fs *fs, struct file *file, void *buf, size_t size);
|
||||||
int (*write)(void *fs, struct file *file, void *buf, size_t size);
|
int (*write)(struct fs *fs, struct file *file, void *buf, size_t size);
|
||||||
int (*open)(void *fs, struct file *file, const char *path);
|
int (*open)(struct fs *fs, struct file *file, const char *path);
|
||||||
int (*close)(void *fs, struct file *file);
|
int (*close)(struct fs *fs, struct file *file);
|
||||||
int (*seek)(void *fs, struct file *file, size_t offset);
|
int (*seek)(struct fs *fs, struct file *file, size_t offset);
|
||||||
|
int (*readdir)(struct fs *fs, uint32_t ino, struct dirent **dir);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fs {
|
struct fs {
|
||||||
|
|
@ -82,5 +101,10 @@ struct fs {
|
||||||
};
|
};
|
||||||
|
|
||||||
void vfs_init(const char *rootfs);
|
void vfs_init(const char *rootfs);
|
||||||
|
int write(int fd, const void *buf, size_t count);
|
||||||
|
int read(int fd, void *buf, size_t count);
|
||||||
|
int open(const char *pathname, int flags);
|
||||||
|
int close(int fd);
|
||||||
|
int seek(int fd, size_t offset, int whence);
|
||||||
|
|
||||||
#endif /* _VFS_H_ */
|
#endif /* _VFS_H_ */
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ static const char *errno_str[] = {
|
||||||
[ENOENT] = "No such file or directory",
|
[ENOENT] = "No such file or directory",
|
||||||
[EINVAL] = "Invalid argument",
|
[EINVAL] = "Invalid argument",
|
||||||
[EBADF] = "Bad file descriptor",
|
[EBADF] = "Bad file descriptor",
|
||||||
|
[ENOTDIR] = "Not a directory",
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const char *strerror(int errnum)
|
inline const char *strerror(int errnum)
|
||||||
|
|
|
||||||
|
|
@ -138,6 +138,9 @@ char *strtok(char *str, const char *delim, char **llast)
|
||||||
(*llast)++;
|
(*llast)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!(*ret))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue