From fb34b20ce0920edf1b34d9f23ecf768d1a66645f Mon Sep 17 00:00:00 2001 From: Francesco Lavra Date: Sun, 24 May 2026 19:46:30 +0200 Subject: [PATCH 1/2] file_open(): Refactor to avoid multiple table lookups The is_dir() call is somewhat expensive, as is does a table lookup, and file_open() contains a couple of these function calls; then, there is a file_type_from_tuple() call which can potentially re-do the same lookups again. To avoid multiple table lookups, move the file_type_from_tuple() call at the top of the function, so that the `type` variable can be compared against known values in the rest of the function. No functional changes. --- src/unix/syscall.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/unix/syscall.c b/src/unix/syscall.c index 0bf93c1fe..038f3a6f0 100755 --- a/src/unix/syscall.c +++ b/src/unix/syscall.c @@ -842,6 +842,7 @@ int file_open(filesystem fs, tuple n, tuple parent, int flags, fsfile fsf) { thread t = current; process p = t->p; + int type = file_type_from_tuple(n); if (flags & O_TMPFILE) flags |= O_DIRECTORY; @@ -860,26 +861,23 @@ int file_open(filesystem fs, tuple n, tuple parent, int flags, fsfile fsf) if (!(file_meta_perms(p, n) & ACCESS_PERM_WRITE)) { return -EACCES; } - if (is_dir(n) && !(flags & O_TMPFILE)) { + if ((type == FDESC_TYPE_DIRECTORY) && !(flags & O_TMPFILE)) { return -EISDIR; } break; default: return -EINVAL; } - if ((flags & (O_CREAT|O_DIRECTORY)) == O_DIRECTORY && !is_dir(n)) { + if ((flags & (O_CREAT|O_DIRECTORY)) == O_DIRECTORY && (type != FDESC_TYPE_DIRECTORY)) { return -ENOTDIR; } - int type; - if (flags & O_TMPFILE) { int fss = filesystem_creat_unnamed(fs, &fsf); if (fss != 0) return fss; type = FDESC_TYPE_REGULAR; } else { - type = file_type_from_tuple(n); if (type == FDESC_TYPE_REGULAR) { assert(fsf); if (flags & O_TRUNC) From e6579d21c64e0678874e7ef314de867ec1715d8f Mon Sep 17 00:00:00 2001 From: Francesco Lavra Date: Sun, 24 May 2026 19:52:36 +0200 Subject: [PATCH 2/2] file_open: Do not return -EROFS when opening non-regular files open() with O_WRONLY or O_RDWR on device files like /dev/null returns EROFS when the `readonly_rootfs` manifest option is enabled. Writes to non-regular files do not modify the filesystem, so these files should be exempt from the read-only check. --- src/unix/syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unix/syscall.c b/src/unix/syscall.c index 038f3a6f0..68a100601 100755 --- a/src/unix/syscall.c +++ b/src/unix/syscall.c @@ -855,7 +855,7 @@ int file_open(filesystem fs, tuple n, tuple parent, int flags, fsfile fsf) break; case O_WRONLY: case O_RDWR: - if (filesystem_is_readonly(fs)) { + if ((type == FDESC_TYPE_REGULAR) && filesystem_is_readonly(fs)) { return -EROFS; } if (!(file_meta_perms(p, n) & ACCESS_PERM_WRITE)) {