vfs: implement seek

This commit is contained in:
Michael Shaw 2023-10-21 11:08:13 -04:00
commit 40d94feff4
3 changed files with 26 additions and 9 deletions

View file

@ -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,
};

View file

@ -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)

View file

@ -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);
};