소스 검색

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.
Idan Horowitz 4 년 전
부모
커밋
aff774c8ac
2개의 변경된 파일19개의 추가작업 그리고 0개의 파일을 삭제
  1. 16 0
      Userland/Libraries/LibCore/File.cpp
  2. 3 0
      Userland/Libraries/LibCore/File.h

+ 16 - 0
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;

+ 3 - 0
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);