Sfoglia il codice sorgente

LibCore: Fallback to fstat() on systems without d_type support

Niklas Poslovski 2 anni fa
parent
commit
6ea4be36b5

+ 4 - 0
Userland/Libraries/LibCore/DirIterator.cpp

@@ -56,7 +56,11 @@ bool DirIterator::advance_next()
             return false;
         }
 
+#ifdef AK_OS_SOLARIS
+        m_next = DirectoryEntry::from_stat(m_dir, *de);
+#else
         m_next = DirectoryEntry::from_dirent(*de);
+#endif
 
         if (m_next->name.is_empty())
             return false;

+ 40 - 3
Userland/Libraries/LibCore/DirectoryEntry.cpp

@@ -5,10 +5,34 @@
  */
 
 #include "DirectoryEntry.h"
-#include <dirent.h>
+#include <sys/stat.h>
 
 namespace Core {
 
+static DirectoryEntry::Type directory_entry_type_from_stat(mode_t st_mode)
+{
+    switch (st_mode) {
+    case S_IFIFO:
+        return DirectoryEntry::Type::NamedPipe;
+    case S_IFCHR:
+        return DirectoryEntry::Type::CharacterDevice;
+    case S_IFDIR:
+        return DirectoryEntry::Type::Directory;
+    case S_IFBLK:
+        return DirectoryEntry::Type::BlockDevice;
+    case S_IFREG:
+        return DirectoryEntry::Type::File;
+    case S_IFLNK:
+        return DirectoryEntry::Type::SymbolicLink;
+    case S_IFSOCK:
+        return DirectoryEntry::Type::Socket;
+    default:
+        return DirectoryEntry::Type::Unknown;
+    }
+    VERIFY_NOT_REACHED();
+}
+
+#ifndef AK_OS_SOLARIS
 static DirectoryEntry::Type directory_entry_type_from_posix(unsigned char dt_constant)
 {
     switch (dt_constant) {
@@ -28,14 +52,26 @@ static DirectoryEntry::Type directory_entry_type_from_posix(unsigned char dt_con
         return DirectoryEntry::Type::SymbolicLink;
     case DT_SOCK:
         return DirectoryEntry::Type::Socket;
-#ifndef AK_OS_OPENBSD
+#    ifndef AK_OS_OPENBSD
     case DT_WHT:
         return DirectoryEntry::Type::Whiteout;
-#endif
+#    endif
     }
     VERIFY_NOT_REACHED();
 }
+#endif
 
+DirectoryEntry DirectoryEntry::from_stat(DIR* d, dirent const& de)
+{
+    struct stat statbuf;
+    fstat(dirfd(d), &statbuf);
+    return DirectoryEntry {
+        .type = directory_entry_type_from_stat(statbuf.st_mode),
+        .name = de.d_name,
+    };
+}
+
+#ifndef AK_OS_SOLARIS
 DirectoryEntry DirectoryEntry::from_dirent(dirent const& de)
 {
     return DirectoryEntry {
@@ -43,5 +79,6 @@ DirectoryEntry DirectoryEntry::from_dirent(dirent const& de)
         .name = de.d_name,
     };
 }
+#endif
 
 }

+ 2 - 2
Userland/Libraries/LibCore/DirectoryEntry.h

@@ -7,8 +7,7 @@
 #pragma once
 
 #include <AK/DeprecatedString.h>
-
-struct dirent;
+#include <dirent.h>
 
 namespace Core {
 
@@ -29,6 +28,7 @@ struct DirectoryEntry {
     DeprecatedString name;
 
     static DirectoryEntry from_dirent(dirent const&);
+    static DirectoryEntry from_stat(DIR*, dirent const&);
 };
 
 }