From aff774c8ac6ca05a1f19b01833febedd8762f98b Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 30 Mar 2021 00:36:19 +0300 Subject: [PATCH] LibCore: Add Core::File is_device() helpers The helpers check if the file is a block device or a character device via stat and fstat. --- Userland/Libraries/LibCore/File.cpp | 16 ++++++++++++++++ Userland/Libraries/LibCore/File.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/Userland/Libraries/LibCore/File.cpp b/Userland/Libraries/LibCore/File.cpp index d04a8982b0..8b03404018 100644 --- a/Userland/Libraries/LibCore/File.cpp +++ b/Userland/Libraries/LibCore/File.cpp @@ -110,6 +110,22 @@ bool File::open_impl(IODevice::OpenMode mode, mode_t permissions) return true; } +bool File::is_device() const +{ + struct stat stat; + if (fstat(fd(), &stat) < 0) + return false; + return S_ISBLK(stat.st_mode) || S_ISCHR(stat.st_mode); +} + +bool File::is_device(const String& filename) +{ + struct stat st; + if (stat(filename.characters(), &st) < 0) + return false; + return S_ISBLK(st.st_mode) || 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 d85856e6b1..12b5223ed0 100644 --- a/Userland/Libraries/LibCore/File.h +++ b/Userland/Libraries/LibCore/File.h @@ -47,6 +47,9 @@ public: bool is_directory() const; static bool is_directory(const String& filename); + bool is_device() const; + static bool is_device(const String& filename); + static bool exists(const String& filename); static bool ensure_parent_directories(const String& path);