libos: Support not following symlinks

This commit is contained in:
apio 2023-05-20 21:48:18 +02:00
parent cb205c851c
commit 7a8ef52408
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 10 additions and 9 deletions

View File

@ -9,11 +9,11 @@ namespace os
{ {
namespace FileSystem 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<void> stat(const Path& path, struct stat& st); Result<void> stat(const Path& path, struct stat& st, bool follow_symlinks = true);
Result<void> create_directory(StringView path, mode_t mode); Result<void> create_directory(StringView path, mode_t mode);

View File

@ -12,28 +12,29 @@
namespace os::FileSystem namespace os::FileSystem
{ {
bool exists(const Path& path) bool exists(const Path& path, bool follow_symlinks)
{ {
struct stat st; struct stat st;
if (stat(path, st).has_error()) return false; if (stat(path, st, follow_symlinks).has_error()) return false;
return true; return true;
} }
bool is_directory(const Path& path) bool is_directory(const Path& path, bool follow_symlinks)
{ {
struct stat st; 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); return S_ISDIR(st.st_mode);
} }
Result<void> stat(const Path& path, struct stat& st) Result<void> stat(const Path& path, struct stat& st, bool follow_symlinks)
{ {
long rc = 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<void>::from_syscall(rc); return Result<void>::from_syscall(rc);
} }