summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-11-23 15:00:16 +0100
committerAnton Kling <anton@kling.gg>2024-11-23 16:31:25 +0100
commit02194a2557b50bf850ff2a26d6e1eb6e7b67bb7c (patch)
treeae6836f20fc6b06c3f82429a8fc4683cd3767e75
parent934c30d35a3ca0e0bf6cd8709d779e060e27f6d2 (diff)
vfs: Add O_APPEND support + refactoring
-rw-r--r--kernel/cpu/syscall.c37
-rw-r--r--kernel/fs/vfs.c44
-rw-r--r--kernel/fs/vfs.h2
3 files changed, 48 insertions, 35 deletions
diff --git a/kernel/cpu/syscall.c b/kernel/cpu/syscall.c
index d4db1dd..c68c4fe 100644
--- a/kernel/cpu/syscall.c
+++ b/kernel/cpu/syscall.c
@@ -185,35 +185,8 @@ int syscall_kill(int fd, int sig) {
return process_signal(fd_ptr, sig);
}
-// FIXME: These should be in a shared header file with libc
-#define SEEK_SET 0
-#define SEEK_CUR 1
-#define SEEK_END 2
-
int syscall_lseek(int fd, int offset, int whence) {
- vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
- if (!fd_ptr) {
- return -EBADF;
- }
-
- off_t ret_offset = fd_ptr->offset;
- switch (whence) {
- case SEEK_SET:
- ret_offset = offset;
- break;
- case SEEK_CUR:
- ret_offset += offset;
- break;
- case SEEK_END:
- assert(fd_ptr->inode);
- ret_offset = fd_ptr->inode->file_size + offset;
- break;
- default:
- return -EINVAL;
- break;
- }
- fd_ptr->offset = ret_offset;
- return ret_offset;
+ return vfs_lseek(fd, offset, whence);
}
int syscall_mkdir(const char *path, int mode) {
@@ -384,13 +357,7 @@ u32 syscall_uptime(void) {
}
int syscall_write(int fd, const char *buf, size_t count) {
- vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
- if (!fd_ptr) {
- return -EBADF;
- }
- int rc = vfs_pwrite(fd, (char *)buf, count, fd_ptr->offset);
- fd_ptr->offset += rc;
- return rc;
+ return vfs_write(fd, buf, count);
}
int syscall_exec(SYS_EXEC_PARAMS *args) {
diff --git a/kernel/fs/vfs.c b/kernel/fs/vfs.c
index 1f28f13..25e2423 100644
--- a/kernel/fs/vfs.c
+++ b/kernel/fs/vfs.c
@@ -382,6 +382,50 @@ int vfs_pwrite(int fd, void *buf, u64 count, u64 offset) {
return rc;
}
+// FIXME: These should be in a shared header file with libc
+#define SEEK_SET 0
+#define SEEK_CUR 1
+#define SEEK_END 2
+
+int vfs_lseek(int fd, int offset, int whence) {
+ vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
+ if (!fd_ptr) {
+ return -EBADF;
+ }
+
+ off_t ret_offset = fd_ptr->offset;
+ switch (whence) {
+ case SEEK_SET:
+ ret_offset = offset;
+ break;
+ case SEEK_CUR:
+ ret_offset += offset;
+ break;
+ case SEEK_END:
+ assert(fd_ptr->inode);
+ ret_offset = fd_ptr->inode->file_size + offset;
+ break;
+ default:
+ return -EINVAL;
+ break;
+ }
+ fd_ptr->offset = ret_offset;
+ return ret_offset;
+}
+
+int vfs_write(int fd, const char *buf, u64 count) {
+ vfs_fd_t *fd_ptr = get_vfs_fd(fd, NULL);
+ if (!fd_ptr) {
+ return -EBADF;
+ }
+ if (fd_ptr->mode & O_APPEND) {
+ vfs_lseek(fd, 0, SEEK_END);
+ }
+ int rc = vfs_pwrite(fd, (char *)buf, count, fd_ptr->offset);
+ fd_ptr->offset += rc;
+ return rc;
+}
+
vfs_vm_object_t *vfs_get_vm_object(int fd, u64 length, u64 offset) {
vfs_fd_t *vfs_fd = get_vfs_fd(fd, NULL);
if (!vfs_fd) {
diff --git a/kernel/fs/vfs.h b/kernel/fs/vfs.h
index e22897e..af07c40 100644
--- a/kernel/fs/vfs.h
+++ b/kernel/fs/vfs.h
@@ -72,6 +72,8 @@ int vfs_close_process(process_t *p, int fd);
vfs_fd_t *get_vfs_fd(int fd, process_t *p);
int vfs_open(const char *file, int flags, int mode);
void vfs_mount(char *path, vfs_inode_t *local_root);
+int vfs_lseek(int fd, int offset, int whence);
+int vfs_write(int fd, const char *buf, u64 count);
int vfs_pwrite(int fd, void *buf, u64 count, u64 offset);
int raw_vfs_pwrite(vfs_fd_t *vfs_fd, void *buf, u64 count, u64 offset);
int raw_vfs_pread(vfs_fd_t *vfs_fd, void *buf, u64 count, u64 offset);