mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-07 11:39:44 +03:00
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:
parent
1b67e19bd6
commit
ceba27c3fe
Notes:
sideshowbarker
2024-07-17 12:07:36 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/ceba27c3fe Pull-request: https://github.com/SerenityOS/serenity/pull/13603 Issue: https://github.com/SerenityOS/serenity/issues/13555 Reviewed-by: https://github.com/BertalanD Reviewed-by: https://github.com/alichraghi ✅ Reviewed-by: https://github.com/awesomekling
@ -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, ¶ms);
|
||||
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;
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user