From 7a8ef52408456617aa8442bb2adf7a795687efc5 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 20 May 2023 21:48:18 +0200 Subject: [PATCH] libos: Support not following symlinks --- libos/include/os/FileSystem.h | 6 +++--- libos/src/FileSystem.cpp | 13 +++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/libos/include/os/FileSystem.h b/libos/include/os/FileSystem.h index 029519e6..26653af8 100644 --- a/libos/include/os/FileSystem.h +++ b/libos/include/os/FileSystem.h @@ -9,11 +9,11 @@ namespace os { namespace FileSystem { - bool exists(const Path& path); + bool exists(const Path& path, bool follow_symlinks = true); - bool is_directory(const Path& path); + bool is_directory(const Path& path, bool follow_symlinks = false); - Result stat(const Path& path, struct stat& st); + Result stat(const Path& path, struct stat& st, bool follow_symlinks = true); Result create_directory(StringView path, mode_t mode); diff --git a/libos/src/FileSystem.cpp b/libos/src/FileSystem.cpp index e82716c1..bf7b2dc2 100644 --- a/libos/src/FileSystem.cpp +++ b/libos/src/FileSystem.cpp @@ -12,28 +12,29 @@ namespace os::FileSystem { - bool exists(const Path& path) + bool exists(const Path& path, bool follow_symlinks) { struct stat st; - if (stat(path, st).has_error()) return false; + if (stat(path, st, follow_symlinks).has_error()) return false; return true; } - bool is_directory(const Path& path) + bool is_directory(const Path& path, bool follow_symlinks) { struct stat st; - if (stat(path, st).has_error()) return false; + if (stat(path, st, follow_symlinks).has_error()) return false; return S_ISDIR(st.st_mode); } - Result stat(const Path& path, struct stat& st) + Result stat(const Path& path, struct stat& st, bool follow_symlinks) { long rc = - syscall(SYS_fstatat, path.dirfd(), path.name().chars(), &st, path.is_empty_path() ? AT_EMPTY_PATH : 0); + syscall(SYS_fstatat, path.dirfd(), path.name().chars(), &st, + (int)((path.is_empty_path() ? AT_EMPTY_PATH : 0) | (follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW))); return Result::from_syscall(rc); }