From ad2de927e2ac401cb12052ffb0ffe0049c8e99fe Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sat, 21 Oct 2023 00:45:38 -0400 Subject: [PATCH] ext2/vfs: implement `read` fs call also implement the cat command! --- dev/console.c | 18 ++++++++++++++++++ fs/ext2.c | 43 ++++++++++++++++++++++++++++++++++++++++--- fs/vfs.c | 25 ++++++++++++++++++++++--- include/dev/console.h | 3 +++ include/errno.h | 1 + include/fs/vfs.h | 4 ++-- init/cmd.c | 42 +++++++++++++++++++++++++++++++++++++++++- lib/errno.c | 1 + 8 files changed, 128 insertions(+), 9 deletions(-) diff --git a/dev/console.c b/dev/console.c index e379ac9..7417cc1 100644 --- a/dev/console.c +++ b/dev/console.c @@ -37,6 +37,16 @@ bool console_ready() return (console.resizemode == 0); } +void console_clear() +{ + if (console.resizemode) + return; + + kprintf("\e[2J\e[H"); + console.cursorpos = 0; + return; +} + void console_resize() { console.resizemode = 1; @@ -67,6 +77,14 @@ void console_input(char c) kexec(console.line); memset(console.line, 0, sizeof(console.line)); console.l_line = 0; + } else if (c == 0x7F || c == '\b') { + if (console.l_line > 0) { + console_write('\b'); + console_write(' '); + console_write('\b'); + console.l_line--; + console.line[console.l_line] = 0; + } } } else { /* TODO: replace with a proper state machine */ diff --git a/fs/ext2.c b/fs/ext2.c index b5d0256..4074fd1 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -359,14 +359,51 @@ int ext2_close_file(struct fs *vfs, struct file *file) return 0; } -int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t count) +int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count) { return 0; } -int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t count) +int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count) { - return 0; + struct ext2fs *fs = vfs->fs; + + if ((file->inode.mode & 0xF000) != EXT2_INO_REG) + return -EISDIR; + + size_t block = offset / fs->block_size; + offset %= fs->block_size; + + if (offset > file->size) + return -EINVAL; + + if (offset + count > file->size) + count = (file->size - offset); + + size_t ret = count; + + void *block_buf = kmalloc(fs->block_size, ALLOC_KERN); + + if (!block_buf) + return -ENOMEM; + + size_t bufoffset = 0; + + while (count > 0) { + int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&file->inode, block_buf, block); + if (retval < 0) { + kfree(block_buf); + return retval; + } + + memcpy(buf + bufoffset, block_buf + offset, MIN(count, fs->block_size - offset)); + bufoffset += fs->block_size; + offset = 0; + + count -= MIN(fs->block_size, count); + } + + return ret; } int ext2_seek_file(struct fs *vfs, struct file *file, size_t offset) diff --git a/fs/vfs.c b/fs/vfs.c index ce53cfb..d58d499 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -21,7 +21,27 @@ int write(int fd, const void *buf, size_t count) int read(int fd, void *buf, size_t count) { - return 0; + struct rbnode *fdnode = rbt_search(kfd, fd); + + if (!fdnode) + return -EBADF; + + struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value; + + if(fdesc->file.type == VFS_FILE_DIR) + return -EISDIR; + + if (fdesc->file.type != VFS_FILE_FILE) + return -EBADF; + + int ret = fdesc->fs->ops.read(fdesc->fs, &fdesc->file, buf, fdesc->pos, count); + + if (ret < 0) + return ret; + + fdesc->pos = MIN(fdesc->pos + ret, fdesc->file.size); + + return ret; } int open(const char *pathname, int flags) @@ -107,7 +127,7 @@ DIR *opendir(const char *name) struct dirent *readdir(DIR *dir) { - if(!dir) + if (!dir) return NULL; struct fs *fs = dir->fs; @@ -171,4 +191,3 @@ void vfs_init(const char *rootdev_name) /* init file descriptor slab */ fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0); } - diff --git a/include/dev/console.h b/include/dev/console.h index e5e4482..b9868dc 100644 --- a/include/dev/console.h +++ b/include/dev/console.h @@ -3,9 +3,12 @@ #ifndef __ASM__ +#include + void console_init(); void console_resize(); void console_input(char c); +void console_clear(); bool console_ready(); void console_write(char c); diff --git a/include/errno.h b/include/errno.h index 68325c0..186faa1 100644 --- a/include/errno.h +++ b/include/errno.h @@ -7,6 +7,7 @@ #define EINVAL 3 #define EBADF 4 #define ENOTDIR 5 +#define EISDIR 6 const char *strerror(int errnum); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 8a40db5..e71d554 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -84,8 +84,8 @@ struct file_descriptor { }; struct fs_ops { - int (*read)(struct fs *fs, struct file *file, void *buf, size_t size); - int (*write)(struct fs *fs, struct file *file, void *buf, size_t size); + int (*read)(struct fs *fs, struct file *file, void *buf, size_t offset, size_t size); + 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); diff --git a/init/cmd.c b/init/cmd.c index dc98376..38fb380 100644 --- a/init/cmd.c +++ b/init/cmd.c @@ -10,6 +10,8 @@ int kcmd_help(int argc, char **argv); int kcmd_ls(int argc, char **argv); +int kcmd_clear(int argc, char **argv); +int kcmd_cat(int argc, char **argv); #define KCMD_DECL(name) \ { \ @@ -21,7 +23,45 @@ struct kcmd { int (*func)(int argc, char **argv); }; -struct kcmd cmd_list[] = { KCMD_DECL(help), KCMD_DECL(ls) }; +struct kcmd cmd_list[] = { KCMD_DECL(cat), KCMD_DECL(clear), KCMD_DECL(help), KCMD_DECL(ls) }; + +int kcmd_cat(int argc, char **argv) +{ + if (!argv[1] || strempty(argv[1])) { + kprintf("Usage: cat \n"); + return 1; + } + + int fd = open(argv[1], 0); + if (fd < 0) { + kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd)); + return 1; + } + + char buf[1024]; + + while (true) { + int n = read(fd, buf, sizeof(buf)); + if (n < 0) { + kprintf("Failed to read %s: %s\n", argv[1], strerror(-n)); + return 1; + } else if (n == 0) { + break; + } + + for (int i = 0; i < n; i++) { + console_write(buf[i]); + } + } + + return 0; +} + +int kcmd_clear(int argc, char **argv) +{ + console_clear(); + return 0; +} int kcmd_help(int argc, char **argv) { diff --git a/lib/errno.c b/lib/errno.c index 595b895..2e6df83 100644 --- a/lib/errno.c +++ b/lib/errno.c @@ -9,6 +9,7 @@ static const char *errno_str[] = { [EINVAL] = "Invalid argument", [EBADF] = "Bad file descriptor", [ENOTDIR] = "Not a directory", + [EISDIR] = "Is a directory", }; inline const char *strerror(int errnum)