LibCore: Add openat() syscall wrapper and improve open's implementation

We don't need va_args in open(), we can just use a default parameter.
This commit is contained in:
kleines Filmröllchen 2022-04-10 18:25:22 +02:00 committed by Andreas Kling
parent 1b67e19bd6
commit ceba27c3fe
Notes: sideshowbarker 2024-07-17 12:07:36 +09:00
2 changed files with 11 additions and 8 deletions

View File

@ -328,22 +328,23 @@ ErrorOr<int> anon_create([[maybe_unused]] size_t size, [[maybe_unused]] int opti
return fd;
}
ErrorOr<int> open(StringView path, int options, ...)
ErrorOr<int> open(StringView path, int options, mode_t mode)
{
return openat(AT_FDCWD, path, options, mode);
}
ErrorOr<int> openat(int fd, StringView path, int options, mode_t mode)
{
if (!path.characters_without_null_termination())
return Error::from_syscall("open"sv, -EFAULT);
va_list ap;
va_start(ap, options);
auto mode = (mode_t)va_arg(ap, unsigned);
va_end(ap);
#ifdef __serenity__
Syscall::SC_open_params params { AT_FDCWD, { path.characters_without_null_termination(), path.length() }, options, mode };
Syscall::SC_open_params params { fd, { path.characters_without_null_termination(), path.length() }, options, mode };
int rc = syscall(SC_open, &params);
HANDLE_SYSCALL_RETURN_VALUE("open"sv, rc, rc);
#else
// NOTE: We have to ensure that the path is null-terminated.
String path_string = path;
int rc = ::open(path_string.characters(), options, mode);
int rc = ::openat(fd, path_string.characters(), options, mode);
if (rc < 0)
return Error::from_syscall("open"sv, -errno);
return rc;

View File

@ -10,6 +10,7 @@
#include <AK/Error.h>
#include <AK/StringView.h>
#include <dirent.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
@ -68,7 +69,8 @@ ErrorOr<int> fcntl(int fd, int command, ...);
ErrorOr<void*> mmap(void* address, size_t, int protection, int flags, int fd, off_t, size_t alignment = 0, StringView name = {});
ErrorOr<void> munmap(void* address, size_t);
ErrorOr<int> anon_create(size_t size, int options);
ErrorOr<int> open(StringView path, int options, ...);
ErrorOr<int> open(StringView path, int options, mode_t mode = 0);
ErrorOr<int> openat(int fd, StringView path, int options, mode_t mode = 0);
ErrorOr<void> close(int fd);
ErrorOr<void> ftruncate(int fd, off_t length);
ErrorOr<struct stat> stat(StringView path);