From da8da79e6206920a6edc1240aaa8d7d11b23fe3d Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 17 Mar 2023 15:23:24 +0000 Subject: LibCore: Add wrapper for fstatat() `Core::System::fstatat()` is similar to our standard `Core::System` wrappers. `Core::Directory::stat(relative_path, flags)` is a convenience method if you already have a Directory, to stat a file relative to it. --- Userland/Libraries/LibCore/Directory.cpp | 5 +++++ Userland/Libraries/LibCore/Directory.h | 1 + Userland/Libraries/LibCore/System.cpp | 18 +++++++++++++++++- Userland/Libraries/LibCore/System.h | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) (limited to 'Userland/Libraries/LibCore') diff --git a/Userland/Libraries/LibCore/Directory.cpp b/Userland/Libraries/LibCore/Directory.cpp index cbfd048fc7..5509ee5947 100644 --- a/Userland/Libraries/LibCore/Directory.cpp +++ b/Userland/Libraries/LibCore/Directory.cpp @@ -88,6 +88,11 @@ ErrorOr> Directory::open(StringView filename, File::OpenMode return File::adopt_fd(fd, mode); } +ErrorOr Directory::stat(StringView filename, int flags) const +{ + return System::fstatat(m_directory_fd, filename, flags); +} + ErrorOr Directory::stat() const { return System::fstat(m_directory_fd); diff --git a/Userland/Libraries/LibCore/Directory.h b/Userland/Libraries/LibCore/Directory.h index ec04cc714a..44b1048d19 100644 --- a/Userland/Libraries/LibCore/Directory.h +++ b/Userland/Libraries/LibCore/Directory.h @@ -41,6 +41,7 @@ public: static ErrorOr adopt_fd(int fd, LexicalPath path); ErrorOr> open(StringView filename, File::OpenMode mode) const; + ErrorOr stat(StringView filename, int flags) const; ErrorOr stat() const; int fd() const { return m_directory_fd; } diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 965f17a7a8..a277a5dd56 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) 2021-2022, Andreas Kling * Copyright (c) 2021-2022, Kenneth Myhra - * Copyright (c) 2021-2022, Sam Atkins + * Copyright (c) 2021-2023, Sam Atkins * Copyright (c) 2022, Matthias Zimmerman * * SPDX-License-Identifier: BSD-2-Clause @@ -380,6 +380,22 @@ ErrorOr fstat(int fd) return st; } +ErrorOr fstatat(int fd, StringView path, int flags) +{ + if (!path.characters_without_null_termination()) + return Error::from_syscall("fstatat"sv, -EFAULT); + + struct stat st = {}; +#ifdef AK_OS_SERENITY + Syscall::SC_stat_params params { { path.characters_without_null_termination(), path.length() }, &st, fd, !(flags & AT_SYMLINK_NOFOLLOW) }; + int rc = syscall(SC_stat, ¶ms); +#else + DeprecatedString path_string = path; + int rc = ::fstatat(fd, path_string.characters(), &st, flags); +#endif + HANDLE_SYSCALL_RETURN_VALUE("fstatat", rc, st); +} + ErrorOr fcntl(int fd, int command, ...) { va_list ap; diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 444f37a540..7dba27fe6f 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -116,6 +116,7 @@ ErrorOr signal(int signal, sig_t handler); ErrorOr signal(int signal, sighandler_t handler); #endif ErrorOr fstat(int fd); +ErrorOr fstatat(int fd, StringView path, int flags); ErrorOr fcntl(int fd, int command, ...); ErrorOr mmap(void* address, size_t, int protection, int flags, int fd, off_t, size_t alignment = 0, StringView name = {}); ErrorOr munmap(void* address, size_t); -- cgit v1.2.3