ext2/vfs: implement read fs call
also implement the cat command!
This commit is contained in:
parent
0029621ada
commit
ad2de927e2
8 changed files with 128 additions and 9 deletions
|
|
@ -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 */
|
||||
|
|
|
|||
43
fs/ext2.c
43
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)
|
||||
|
|
|
|||
25
fs/vfs.c
25
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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,9 +3,12 @@
|
|||
|
||||
#ifndef __ASM__
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
void console_init();
|
||||
void console_resize();
|
||||
void console_input(char c);
|
||||
void console_clear();
|
||||
bool console_ready();
|
||||
void console_write(char c);
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#define EINVAL 3
|
||||
#define EBADF 4
|
||||
#define ENOTDIR 5
|
||||
#define EISDIR 6
|
||||
|
||||
const char *strerror(int errnum);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
42
init/cmd.c
42
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 <file>\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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue