LibCore: Port DirIterator to Windows using vcpkg dirent.h

This commit is contained in:
stasoid 2024-10-24 22:56:43 +05:00 committed by Andrew Kaster
commit 16ab3c5f9d
Notes: github-actions[bot] 2024-11-05 17:43:44 +00:00
6 changed files with 34 additions and 14 deletions

View file

@ -21,6 +21,11 @@ endif()
serenity_lib(LibCoreMinimal coreminimal) serenity_lib(LibCoreMinimal coreminimal)
if (WIN32)
find_path(DIRENT_INCLUDE_DIR dirent.h REQUIRED)
target_include_directories(LibCoreMinimal PRIVATE ${DIRENT_INCLUDE_DIR})
endif()
if (LAGOM_TOOLS_ONLY) if (LAGOM_TOOLS_ONLY)
return() return()
endif() endif()

View file

@ -5,9 +5,8 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Vector.h>
#include <LibCore/DirIterator.h> #include <LibCore/DirIterator.h>
#include <errno.h> #include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -79,6 +78,7 @@ bool DirIterator::advance_next()
if (m_flags & Flags::SkipParentAndBaseDir && (m_next->name == "." || m_next->name == "..")) if (m_flags & Flags::SkipParentAndBaseDir && (m_next->name == "." || m_next->name == ".."))
continue; continue;
#ifndef AK_OS_WINDOWS
if constexpr (dirent_has_d_type) { if constexpr (dirent_has_d_type) {
// dirent structures from readdir aren't guaranteed to contain valid file types, // dirent structures from readdir aren't guaranteed to contain valid file types,
// as it is possible that the underlying filesystem doesn't keep track of those. // as it is possible that the underlying filesystem doesn't keep track of those.
@ -94,6 +94,7 @@ bool DirIterator::advance_next()
m_next->type = DirectoryEntry::directory_entry_type_from_stat(statbuf.st_mode); m_next->type = DirectoryEntry::directory_entry_type_from_stat(statbuf.st_mode);
} }
} }
#endif
return !m_next->name.is_empty(); return !m_next->name.is_empty();
} }
@ -135,11 +136,13 @@ ByteString DirIterator::next_full_path()
return builder.to_byte_string(); return builder.to_byte_string();
} }
#ifndef AK_OS_WINDOWS
int DirIterator::fd() const int DirIterator::fd() const
{ {
if (!m_dir) if (!m_dir)
return -1; return -1;
return dirfd(m_dir); return dirfd(m_dir);
} }
#endif
} }

View file

@ -7,10 +7,7 @@
#pragma once #pragma once
#include <AK/ByteString.h>
#include <LibCore/DirectoryEntry.h> #include <LibCore/DirectoryEntry.h>
#include <dirent.h>
#include <string.h>
namespace Core { namespace Core {

View file

@ -5,6 +5,7 @@
*/ */
#include "DirectoryEntry.h" #include "DirectoryEntry.h"
#include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
namespace Core { namespace Core {
@ -68,14 +69,16 @@ DirectoryEntry::Type DirectoryEntry::directory_entry_type_from_stat(mode_t st_mo
return DirectoryEntry::Type::CharacterDevice; return DirectoryEntry::Type::CharacterDevice;
case S_IFDIR: case S_IFDIR:
return DirectoryEntry::Type::Directory; return DirectoryEntry::Type::Directory;
case S_IFBLK:
return DirectoryEntry::Type::BlockDevice;
case S_IFREG: case S_IFREG:
return DirectoryEntry::Type::File; return DirectoryEntry::Type::File;
case S_IFLNK: case S_IFLNK:
return DirectoryEntry::Type::SymbolicLink; return DirectoryEntry::Type::SymbolicLink;
#ifndef AK_OS_WINDOWS
case S_IFBLK:
return DirectoryEntry::Type::BlockDevice;
case S_IFSOCK: case S_IFSOCK:
return DirectoryEntry::Type::Socket; return DirectoryEntry::Type::Socket;
#endif
default: default:
return DirectoryEntry::Type::Unknown; return DirectoryEntry::Type::Unknown;
} }
@ -83,7 +86,7 @@ DirectoryEntry::Type DirectoryEntry::directory_entry_type_from_stat(mode_t st_mo
} }
#if !defined(AK_OS_SOLARIS) && !defined(AK_OS_HAIKU) #if !defined(AK_OS_SOLARIS) && !defined(AK_OS_HAIKU)
static DirectoryEntry::Type directory_entry_type_from_posix(unsigned char dt_constant) static DirectoryEntry::Type directory_entry_type_from_posix(int dt_constant)
{ {
switch (dt_constant) { switch (dt_constant) {
case DT_UNKNOWN: case DT_UNKNOWN:
@ -94,15 +97,17 @@ static DirectoryEntry::Type directory_entry_type_from_posix(unsigned char dt_con
return DirectoryEntry::Type::CharacterDevice; return DirectoryEntry::Type::CharacterDevice;
case DT_DIR: case DT_DIR:
return DirectoryEntry::Type::Directory; return DirectoryEntry::Type::Directory;
case DT_BLK:
return DirectoryEntry::Type::BlockDevice;
case DT_REG: case DT_REG:
return DirectoryEntry::Type::File; return DirectoryEntry::Type::File;
case DT_LNK: case DT_LNK:
return DirectoryEntry::Type::SymbolicLink; return DirectoryEntry::Type::SymbolicLink;
# ifndef AK_OS_WINDOWS
case DT_BLK:
return DirectoryEntry::Type::BlockDevice;
case DT_SOCK: case DT_SOCK:
return DirectoryEntry::Type::Socket; return DirectoryEntry::Type::Socket;
# ifndef AK_OS_OPENBSD # endif
# if !defined AK_OS_OPENBSD && !defined AK_OS_WINDOWS
case DT_WHT: case DT_WHT:
return DirectoryEntry::Type::Whiteout; return DirectoryEntry::Type::Whiteout;
# endif # endif
@ -111,6 +116,7 @@ static DirectoryEntry::Type directory_entry_type_from_posix(unsigned char dt_con
} }
#endif #endif
#ifndef AK_OS_WINDOWS
DirectoryEntry DirectoryEntry::from_stat(DIR* d, dirent const& de) DirectoryEntry DirectoryEntry::from_stat(DIR* d, dirent const& de)
{ {
struct stat statbuf; struct stat statbuf;
@ -121,6 +127,7 @@ DirectoryEntry DirectoryEntry::from_stat(DIR* d, dirent const& de)
.inode_number = de.d_ino, .inode_number = de.d_ino,
}; };
} }
#endif
#if !defined(AK_OS_SOLARIS) && !defined(AK_OS_HAIKU) #if !defined(AK_OS_SOLARIS) && !defined(AK_OS_HAIKU)
DirectoryEntry DirectoryEntry::from_dirent(dirent const& de) DirectoryEntry DirectoryEntry::from_dirent(dirent const& de)
@ -128,7 +135,7 @@ DirectoryEntry DirectoryEntry::from_dirent(dirent const& de)
return DirectoryEntry { return DirectoryEntry {
.type = directory_entry_type_from_posix(de.d_type), .type = directory_entry_type_from_posix(de.d_type),
.name = de.d_name, .name = de.d_name,
.inode_number = de.d_ino, .inode_number = (ino_t)de.d_ino,
}; };
} }
#endif #endif

View file

@ -7,8 +7,12 @@
#pragma once #pragma once
#include <AK/ByteString.h> #include <AK/ByteString.h>
#include <AK/StringView.h> #ifdef AK_OS_WINDOWS
#include <dirent.h> struct dirent;
struct DIR;
#else
# include <dirent.h>
#endif
namespace Core { namespace Core {

View file

@ -8,6 +8,10 @@
"http2" "http2"
] ]
}, },
{
"name": "dirent",
"platform": "windows"
},
{ {
"name": "fontconfig", "name": "fontconfig",
"platform": "linux | freebsd | openbsd | osx | windows" "platform": "linux | freebsd | openbsd | osx | windows"