summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2023-03-17 15:23:24 +0000
committerAndrew Kaster <andrewdkaster@gmail.com>2023-05-28 05:51:27 -0600
commitda8da79e6206920a6edc1240aaa8d7d11b23fe3d (patch)
tree17f723fae52aade59de51a30642898184981fcd9 /Userland/Libraries/LibCore
parentc140b67be328ad1e90b03c59bd61c4ce76257381 (diff)
downloadserenity-da8da79e6206920a6edc1240aaa8d7d11b23fe3d.zip
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.
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r--Userland/Libraries/LibCore/Directory.cpp5
-rw-r--r--Userland/Libraries/LibCore/Directory.h1
-rw-r--r--Userland/Libraries/LibCore/System.cpp18
-rw-r--r--Userland/Libraries/LibCore/System.h1
4 files changed, 24 insertions, 1 deletions
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<NonnullOwnPtr<File>> Directory::open(StringView filename, File::OpenMode
return File::adopt_fd(fd, mode);
}
+ErrorOr<struct stat> Directory::stat(StringView filename, int flags) const
+{
+ return System::fstatat(m_directory_fd, filename, flags);
+}
+
ErrorOr<struct stat> 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<Directory> adopt_fd(int fd, LexicalPath path);
ErrorOr<NonnullOwnPtr<File>> open(StringView filename, File::OpenMode mode) const;
+ ErrorOr<struct stat> stat(StringView filename, int flags) const;
ErrorOr<struct stat> 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 <kling@serenityos.org>
* Copyright (c) 2021-2022, Kenneth Myhra <kennethmyhra@serenityos.org>
- * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
+ * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
@@ -380,6 +380,22 @@ ErrorOr<struct stat> fstat(int fd)
return st;
}
+ErrorOr<struct stat> 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, &params);
+#else
+ DeprecatedString path_string = path;
+ int rc = ::fstatat(fd, path_string.characters(), &st, flags);
+#endif
+ HANDLE_SYSCALL_RETURN_VALUE("fstatat", rc, st);
+}
+
ErrorOr<int> 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<sig_t> signal(int signal, sig_t handler);
ErrorOr<sighandler_t> signal(int signal, sighandler_t handler);
#endif
ErrorOr<struct stat> fstat(int fd);
+ErrorOr<struct stat> fstatat(int fd, StringView path, int flags);
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);