From b2d33c5689340ba86fb839d243e96d50a525d129 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Fri, 7 Jul 2023 00:29:31 +1200 Subject: [PATCH] LibFileSystem: Add FileSystem::is_regular_file --- .../Libraries/LibFileSystem/FileSystem.cpp | 18 ++++++++++++++++++ Userland/Libraries/LibFileSystem/FileSystem.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/Userland/Libraries/LibFileSystem/FileSystem.cpp b/Userland/Libraries/LibFileSystem/FileSystem.cpp index 4a1d2774e2..059f1392c1 100644 --- a/Userland/Libraries/LibFileSystem/FileSystem.cpp +++ b/Userland/Libraries/LibFileSystem/FileSystem.cpp @@ -121,6 +121,24 @@ bool is_char_device(int fd) return S_ISCHR(st.st_mode); } +bool is_regular_file(StringView path) +{ + auto st_or_error = Core::System::stat(path); + if (st_or_error.is_error()) + return false; + auto st = st_or_error.release_value(); + return S_ISREG(st.st_mode); +} + +bool is_regular_file(int fd) +{ + auto st_or_error = Core::System::fstat(fd); + if (st_or_error.is_error()) + return false; + auto st = st_or_error.release_value(); + return S_ISREG(st.st_mode); +} + bool is_directory(StringView path) { auto st_or_error = Core::System::stat(path); diff --git a/Userland/Libraries/LibFileSystem/FileSystem.h b/Userland/Libraries/LibFileSystem/FileSystem.h index 46ac6f85fd..fd633c0f14 100644 --- a/Userland/Libraries/LibFileSystem/FileSystem.h +++ b/Userland/Libraries/LibFileSystem/FileSystem.h @@ -25,6 +25,9 @@ ErrorOr real_path(StringView path); bool exists(StringView path); bool exists(int fd); +bool is_regular_file(StringView path); +bool is_regular_file(int fd); + bool is_directory(StringView path); bool is_directory(int fd);