From a3c6635f3ea114095a921d97b0410c843c4cab3b Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 27 Oct 2022 07:43:55 +0200 Subject: [PATCH] Kernel, libc: Add O_APPEND and stub out O_CREAT and O_TRUNC --- kernel/src/sys/stdio.cpp | 22 ++++++++++++++++++++++ libs/libc/include/fcntl.h | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/kernel/src/sys/stdio.cpp b/kernel/src/sys/stdio.cpp index ec806a4b..b4ed6d44 100644 --- a/kernel/src/sys/stdio.cpp +++ b/kernel/src/sys/stdio.cpp @@ -17,6 +17,9 @@ #define OPEN_NONBLOCK 4 #define OPEN_CLOEXEC 8 #define OPEN_DIRECTORY 16 +#define OPEN_TRUNCATED 32 +#define OPEN_CREATE 64 +#define OPEN_APPEND 128 #define SEEK_SET 0 #define SEEK_CUR 1 @@ -154,6 +157,8 @@ void sys_open(Context* context, const char* filename, int flags) VFS::Node* node = VFS::resolve_path(kfilename); if (!node) { + bool create = (flags & OPEN_CREATE) > 0; + if (create) kwarnln("FIXME: open(O_CREAT) is not implemented"); kfree(kfilename); context->rax = -ENOENT; return; @@ -173,6 +178,17 @@ void sys_open(Context* context, const char* filename, int flags) bool only_directory = (flags & OPEN_DIRECTORY) > 0; + bool truncate = (flags & OPEN_TRUNCATED) > 0; + if (truncate) + { + kfree(kfilename); + kerrorln("FIXME: open(O_TRUNC) is not implemented"); + context->rax = -ENOTSUP; + return; + } + + bool append = (flags & OPEN_APPEND) > 0; + if (only_directory && node->type != VFS_DIRECTORY) { kfree(kfilename); @@ -188,6 +204,12 @@ void sys_open(Context* context, const char* filename, int flags) kfree(kfilename); current_task->files[fd].open(node, can_read, can_write, able_to_block, close_on_exec); + + if (append && current_task->files[fd].node()->type != VFS_DEVICE) + { + current_task->files[fd].seek((long)current_task->files[fd].length()); + } + context->rax = fd; return; } diff --git a/libs/libc/include/fcntl.h b/libs/libc/include/fcntl.h index fa306abe..6e801bd6 100644 --- a/libs/libc/include/fcntl.h +++ b/libs/libc/include/fcntl.h @@ -13,6 +13,12 @@ #define O_CLOEXEC 8 /* Refuse to open the file if it is not a directory. */ #define O_DIRECTORY 16 +/* Truncate the file on open. */ +#define O_TRUNC 32 +/* Create the file if it doesn't exist. */ +#define O_CREAT 64 +/* Open the file for appending. */ +#define O_APPEND 128 /* Duplicate a file descriptor. */ #define F_DUPFD 0