diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-03-30 00:36:19 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-30 11:29:52 +0200 |
commit | aff774c8ac6ca05a1f19b01833febedd8762f98b (patch) | |
tree | 5746b926a1df000e1207b29070639dddec5e653c /Userland/Libraries | |
parent | 4ee23752a5d86c237654b63a80de8fa3e6abb5bb (diff) | |
download | serenity-aff774c8ac6ca05a1f19b01833febedd8762f98b.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/File.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/File.h | 3 |
2 files changed, 19 insertions, 0 deletions
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); |