From 7b8088c78d66f4d4a551c626971276d148907820 Mon Sep 17 00:00:00 2001 From: Samuel Bowman Date: Sun, 26 Jun 2022 19:04:06 -0400 Subject: [PATCH] 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. --- Userland/Libraries/LibCore/File.cpp | 32 +++++++++++++++++++++++++++++ Userland/Libraries/LibCore/File.h | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index 1f9596cc810..436ac612bd5 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 b9394b53d7f..6037ff4c993 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);