summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Bowman <sam@sambowman.tech>2022-06-26 19:04:06 -0400
committerLinus Groh <mail@linusgroh.de>2022-07-21 20:13:44 +0100
commit7b8088c78d66f4d4a551c626971276d148907820 (patch)
tree3ac27ff4953d87a53166200efb458ff101179e84
parent380ce43afc7072ca8ff9fb3915771a029e34ac43 (diff)
downloadserenity-7b8088c78d66f4d4a551c626971276d148907820.zip
LibCore: Add File::is_{block,char}_device() helpers
The existing File::is_device() helpers don't distinguish between block and char devices. This commit adds File::is_block_device() and File::is_char_device() helpers which are more specific.
-rw-r--r--Userland/Libraries/LibCore/File.cpp32
-rw-r--r--Userland/Libraries/LibCore/File.h4
2 files changed, 36 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp
index 1f9596cc81..436ac612bd 100644
--- a/Userland/Libraries/LibCore/File.cpp
+++ b/Userland/Libraries/LibCore/File.cpp
@@ -115,6 +115,38 @@ bool File::is_device(String const& filename)
return S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode);
}
+bool File::is_block_device() const
+{
+ struct stat stat;
+ if (fstat(fd(), &stat) < 0)
+ return false;
+ return S_ISBLK(stat.st_mode);
+}
+
+bool File::is_block_device(String const& filename)
+{
+ struct stat st;
+ if (stat(filename.characters(), &st) < 0)
+ return false;
+ return S_ISBLK(st.st_mode);
+}
+
+bool File::is_char_device() const
+{
+ struct stat stat;
+ if (fstat(fd(), &stat) < 0)
+ return false;
+ return S_ISCHR(stat.st_mode);
+}
+
+bool File::is_char_device(String const& filename)
+{
+ struct stat st;
+ if (stat(filename.characters(), &st) < 0)
+ return false;
+ return S_ISCHR(st.st_mode);
+}
+
bool File::is_directory() const
{
struct stat stat;
diff --git a/Userland/Libraries/LibCore/File.h b/Userland/Libraries/LibCore/File.h
index b9394b53d7..6037ff4c99 100644
--- a/Userland/Libraries/LibCore/File.h
+++ b/Userland/Libraries/LibCore/File.h
@@ -28,6 +28,10 @@ public:
bool is_device() const;
static bool is_device(String const& filename);
+ bool is_block_device() const;
+ static bool is_block_device(String const& filename);
+ bool is_char_device() const;
+ static bool is_char_device(String const& filename);
bool is_link() const;
static bool is_link(String const& filename);