From 40d94feff41992825415fd5e9406548c2146d1f5 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 21 Oct 2023 11:08:13 -0400 Subject: [PATCH] vfs: implement seek --- fs/ext2.c | 6 ------ fs/vfs.c | 25 +++++++++++++++++++++++-- include/fs/vfs.h | 4 +++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index 4074fd1..d26f487 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -406,11 +406,6 @@ int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t offset, return ret; } -int ext2_seek_file(struct fs *vfs, struct file *file, size_t offset) -{ - return 0; -} - struct fs *ext2_init_fs(struct block_device *bdev) { struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN); @@ -455,7 +450,6 @@ struct fs *ext2_init_fs(struct block_device *bdev) .close = ext2_close_file, .read = ext2_read_file, .write = ext2_write_file, - .seek = ext2_seek_file, .readdir = ext2_readdir, }; diff --git a/fs/vfs.c b/fs/vfs.c index d928ed6..d916088 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -28,7 +28,7 @@ int read(int fd, void *buf, size_t count) struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value; - if(fdesc->file.type == VFS_FILE_DIR) + if (fdesc->file.type == VFS_FILE_DIR) return -EISDIR; if (fdesc->file.type != VFS_FILE_FILE) @@ -95,7 +95,28 @@ int close(int fd) int seek(int fd, size_t offset, int whence) { - return 0; + struct rbnode *fdnode = rbt_search(kfd, fd); + + if (!fdnode) + return -EBADF; + + struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value; + + switch (whence) { + case SEEK_SET: + fdesc->pos = offset; + break; + case SEEK_CUR: + fdesc->pos += offset; + break; + case SEEK_END: + fdesc->pos = fdesc->file.size + offset; + break; + default: + return -EINVAL; + } + + return fdesc->pos; } DIR *opendir(const char *name) diff --git a/include/fs/vfs.h b/include/fs/vfs.h index e71d554..d693da2 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -16,6 +16,9 @@ #define VFS_FILE_SOCK 0x0006 #define VFS_FILE_SYMLINK 0x0007 +#define SEEK_SET 0 +#define SEEK_CUR 1 +#define SEEK_END 2 struct inode { uint16_t mode; @@ -88,7 +91,6 @@ struct fs_ops { int (*write)(struct fs *fs, struct file *file, void *buf, size_t offset, size_t size); int (*open)(struct fs *fs, struct file *file, const char *path); int (*close)(struct fs *fs, struct file *file); - int (*seek)(struct fs *fs, struct file *file, size_t offset); int (*readdir)(struct fs *fs, uint32_t ino, struct dirent **dir); };