From 6371724809b057b25a4efd6c022e7d95068c42f1 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 20 Sep 2021 19:30:37 +0200 Subject: add an offset parameter to read() and write() --- src/init/main.c | 8 ++++---- src/init/syscalls.c | 8 ++++---- src/init/tar.c | 14 ++++++++++---- 3 files changed, 18 insertions(+), 12 deletions(-) (limited to 'src/init') diff --git a/src/init/main.c b/src/init/main.c index 66d4b2b..1e2d16f 100644 --- a/src/init/main.c +++ b/src/init/main.c @@ -4,7 +4,7 @@ #include #define argify(str) str, sizeof(str) - 1 -#define log(str) _syscall_write(tty_fd, argify(str)) +#define log(str) _syscall_write(tty_fd, argify(str), 0) extern char _bss_start; // provided by the linker extern char _bss_end; @@ -34,15 +34,15 @@ void read_file(const char *path, size_t len) { static char buf[64]; int buf_len = 64; - _syscall_write(tty_fd, path, len); + _syscall_write(tty_fd, path, len, 0); log(": "); if (fd < 0) { log("couldn't open.\n"); return; } - buf_len = _syscall_read(fd, buf, buf_len); - _syscall_write(tty_fd, buf, buf_len); + buf_len = _syscall_read(fd, buf, buf_len, 0); + _syscall_write(tty_fd, buf, buf_len, 0); _syscall_close(fd); } diff --git a/src/init/syscalls.c b/src/init/syscalls.c index 7d0fef9..f32594e 100644 --- a/src/init/syscalls.c +++ b/src/init/syscalls.c @@ -22,12 +22,12 @@ int _syscall_mount(handle_t handle, const char __user *path, int len) { return _syscall(_SYSCALL_MOUNT, handle, (int)path, len, 0); } -int _syscall_read(handle_t handle, char __user *buf, int len) { - return _syscall(_SYSCALL_READ, handle, (int)buf, len, 0); +int _syscall_read(handle_t handle, char __user *buf, int len, int offset) { + return _syscall(_SYSCALL_READ, handle, (int)buf, len, offset); } -int _syscall_write(handle_t handle, const char __user *buf, int len) { - return _syscall(_SYSCALL_WRITE, handle, (int)buf, len, 0); +int _syscall_write(handle_t handle, const char __user *buf, int len, int offset) { + return _syscall(_SYSCALL_WRITE, handle, (int)buf, len, offset); } int _syscall_close(handle_t handle) { diff --git a/src/init/tar.c b/src/init/tar.c index 74e4c83..691768c 100644 --- a/src/init/tar.c +++ b/src/init/tar.c @@ -24,7 +24,13 @@ void tar_driver(handle_t back, void *base) { case VFSOP_READ: { void *meta = (void*)res.id; - _syscall_fs_respond(meta + 512, tar_size(meta)); + int size = tar_size(meta); + if (res.offset < 0 || res.offset > size) { + // TODO support negative offsets + _syscall_fs_respond(NULL, -1); + } else { + _syscall_fs_respond(meta + 512 + res.offset, size - res.offset); + } break; } @@ -38,14 +44,14 @@ void tar_driver(handle_t back, void *base) { while (0 == memcmp(base + 257, "ustar", 5)) { int size = oct_parse(base + 124, 12); - _syscall_write(tty_fd, base, 100); - _syscall_write(tty_fd, " ", 1); + _syscall_write(tty_fd, base, 100, 0); + _syscall_write(tty_fd, " ", 1, 0); base += 512; // skip metadata sector base += (size + 511) & ~511; // skip file (size rounded up to 512) // TODO might pagefault if the last sector was at a page boundary } - _syscall_write(tty_fd, "done.", 5); + _syscall_write(tty_fd, "done.", 5, 0); } static int tar_open(const char *path, int len, void *base, size_t base_len) { -- cgit v1.2.3