LibCore: Add Windows version of DirIterator

Co-authored-by: stasoid <stasoid@yahoo.com>
This commit is contained in:
Cameron Youell 2023-08-30 17:19:29 +10:00 committed by Andrew Kaster
parent c5219b0193
commit 94601e1ffd
Notes: github-actions[bot] 2024-10-20 00:15:43 +00:00
8 changed files with 238 additions and 21 deletions

View file

@ -1,10 +1,17 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Error.h>
#ifdef AK_OS_WINDOWS
# include <AK/ByteString.h>
# include <AK/HashMap.h>
# include <windows.h>
#endif
namespace AK {
@ -13,4 +20,33 @@ Error Error::from_string_view_or_print_error_and_return_errno(StringView string_
return Error::from_string_view(string_literal);
}
#ifdef AK_OS_WINDOWS
Error Error::from_windows_error(DWORD code)
{
static HashMap<DWORD, ByteString> windows_errors;
auto string = windows_errors.get(code);
if (string.has_value()) {
return Error::from_string_view(string->view());
} else {
char* message = nullptr;
auto size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&message,
0,
nullptr);
if (size == 0)
return Error::from_string_view_or_print_error_and_return_errno("Unknown error"sv, code);
windows_errors.set(code, { message, size });
LocalFree(message);
return from_windows_error(code);
}
}
#endif
}

View file

@ -11,6 +11,9 @@
#include <AK/Variant.h>
#include <errno.h>
#include <string.h>
#ifdef AK_OS_WINDOWS
typedef unsigned long DWORD;
#endif
namespace AK {
@ -25,6 +28,10 @@ public:
return Error(code);
}
#ifdef AK_OS_WINDOWS
static Error from_windows_error(DWORD code);
#endif
// NOTE: For calling this method from within kernel code, we will simply print
// the error message and return the errno code.
// For calling this method from userspace programs, we will simply return from

View file

@ -5,8 +5,6 @@ include(vulkan)
set(SOURCES
ArgsParser.cpp
Directory.cpp
DirectoryEntry.cpp
DirIterator.cpp
Environment.cpp
File.cpp
StandardPaths.cpp
@ -14,6 +12,18 @@ set(SOURCES
Version.cpp
)
if (WIN32)
list(APPEND SOURCES
DirectoryEntryWindows.cpp
DirIteratorWindows.cpp
)
else()
list(APPEND SOURCES
DirectoryEntry.cpp
DirIterator.cpp
)
endif()
serenity_lib(LibCoreMinimal coreminimal)
if (LAGOM_TOOLS_ONLY)

View file

@ -5,7 +5,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Vector.h>
#include <LibCore/DirIterator.h>
#include <errno.h>
#include <fcntl.h>
@ -13,32 +12,36 @@
namespace Core {
struct DirIterator::Impl {
DIR* dir { nullptr };
};
DirIterator::DirIterator(ByteString path, Flags flags)
: m_path(move(path))
: m_impl(make<Impl>())
, m_path(move(path))
, m_flags(flags)
{
m_dir = opendir(m_path.characters());
if (!m_dir) {
m_impl->dir = opendir(m_path.characters());
if (!m_impl->dir) {
m_error = Error::from_errno(errno);
}
}
DirIterator::~DirIterator()
{
if (m_dir) {
closedir(m_dir);
m_dir = nullptr;
if (m_impl && m_impl->dir) {
closedir(m_impl->dir);
m_impl->dir = nullptr;
}
}
DirIterator::DirIterator(DirIterator&& other)
: m_dir(other.m_dir)
: m_impl(move(other.m_impl))
, m_error(move(other.m_error))
, m_next(move(other.m_next))
, m_path(move(other.m_path))
, m_flags(other.m_flags)
{
other.m_dir = nullptr;
}
static constexpr bool dirent_has_d_type =
@ -50,12 +53,12 @@ static constexpr bool dirent_has_d_type =
bool DirIterator::advance_next()
{
if (!m_dir)
if (!m_impl || !m_impl->dir)
return false;
while (true) {
errno = 0;
auto* de = readdir(m_dir);
auto* de = readdir(m_impl->dir);
if (!de) {
if (errno != 0) {
m_error = Error::from_errno(errno);
@ -68,7 +71,7 @@ bool DirIterator::advance_next()
if constexpr (dirent_has_d_type)
m_next = DirectoryEntry::from_dirent(*de);
else
m_next = DirectoryEntry::from_stat(m_dir, *de);
m_next = DirectoryEntry::from_stat(m_impl->dir, *de);
if (m_next->name.is_empty())
return false;
@ -86,7 +89,7 @@ bool DirIterator::advance_next()
// the calling code will be given the raw unknown type.
if ((m_flags & Flags::NoStat) == 0 && m_next->type == DirectoryEntry::Type::Unknown) {
struct stat statbuf;
if (fstatat(dirfd(m_dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0) {
if (fstatat(dirfd(m_impl->dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0) {
m_error = Error::from_errno(errno);
dbgln("DirIteration error: {}", m_error.value());
return false;
@ -137,9 +140,9 @@ ByteString DirIterator::next_full_path()
int DirIterator::fd() const
{
if (!m_dir)
if (!m_impl || !m_impl->dir)
return -1;
return dirfd(m_dir);
return dirfd(m_impl->dir);
}
}

View file

@ -8,9 +8,8 @@
#pragma once
#include <AK/ByteString.h>
#include <AK/OwnPtr.h>
#include <LibCore/DirectoryEntry.h>
#include <dirent.h>
#include <string.h>
namespace Core {
@ -38,7 +37,8 @@ public:
int fd() const;
private:
DIR* m_dir = nullptr;
struct Impl;
OwnPtr<Impl> m_impl;
Optional<Error> m_error;
Optional<DirectoryEntry> m_next;
ByteString m_path;

View file

@ -0,0 +1,122 @@
/*
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/DirIterator.h>
#include <windows.h>
namespace Core {
struct DirIterator::Impl {
HANDLE handle { INVALID_HANDLE_VALUE };
WIN32_FIND_DATA find_data;
bool initialized { false };
};
DirIterator::DirIterator(ByteString path, Flags flags)
: m_impl(make<Impl>())
, m_path(move(path))
, m_flags(flags)
{
}
DirIterator::~DirIterator()
{
if (m_impl && m_impl->handle != INVALID_HANDLE_VALUE) {
FindClose(m_impl->handle);
m_impl->handle = INVALID_HANDLE_VALUE;
}
}
DirIterator::DirIterator(DirIterator&& other)
: m_impl(move(other.m_impl))
, m_error(move(other.m_error))
, m_next(move(other.m_next))
, m_path(move(other.m_path))
, m_flags(other.m_flags)
{
}
bool DirIterator::advance_next()
{
if (!m_impl)
return false;
while (true) {
if (!m_impl->initialized) {
m_impl->initialized = true;
auto path = ByteString::formatted("{}/*", m_path);
m_impl->handle = FindFirstFile(path.characters(), &m_impl->find_data);
if (m_impl->handle == INVALID_HANDLE_VALUE) {
m_error = Error::from_windows_error(GetLastError());
return false;
}
} else {
if (!FindNextFile(m_impl->handle, &m_impl->find_data)) {
if (GetLastError() != ERROR_NO_MORE_FILES)
m_error = Error::from_windows_error(GetLastError());
return false;
}
}
m_next = DirectoryEntry::from_find_data(m_impl->find_data);
if (m_next->name.is_empty())
return false;
if (m_flags & Flags::SkipDots && m_next->name.starts_with('.'))
continue;
if (m_flags & Flags::SkipParentAndBaseDir && (m_next->name == "." || m_next->name == ".."))
continue;
return !m_next->name.is_empty();
}
}
bool DirIterator::has_next()
{
if (m_next.has_value())
return true;
return advance_next();
}
Optional<DirectoryEntry> DirIterator::next()
{
if (!m_next.has_value())
advance_next();
auto result = m_next;
m_next.clear();
return result;
}
ByteString DirIterator::next_path()
{
auto entry = next();
if (entry.has_value())
return entry->name;
return "";
}
ByteString DirIterator::next_full_path()
{
StringBuilder builder;
builder.append(m_path);
if (!m_path.ends_with('/'))
builder.append('/');
builder.append(next_path());
return builder.to_byte_string();
}
int DirIterator::fd() const
{
dbgln("DirIterator::fd() not implemented");
VERIFY_NOT_REACHED();
}
}

View file

@ -8,7 +8,11 @@
#include <AK/ByteString.h>
#include <AK/StringView.h>
#ifdef AK_OS_WINDOWS
using WIN32_FIND_DATA = struct _WIN32_FIND_DATAA;
#else
# include <dirent.h>
#endif
namespace Core {
@ -27,6 +31,7 @@ struct DirectoryEntry {
Type type;
// FIXME: Once we have a special Path string class, use that.
ByteString name;
#if !defined(AK_OS_WINDOWS)
ino_t inode_number;
static StringView posix_name_from_directory_entry_type(Type);
@ -34,6 +39,9 @@ struct DirectoryEntry {
static Type directory_entry_type_from_stat(mode_t st_mode);
static DirectoryEntry from_dirent(dirent const&);
static DirectoryEntry from_stat(DIR*, dirent const&);
#else
static DirectoryEntry from_find_data(WIN32_FIND_DATA const&);
#endif
};
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/DirectoryEntry.h>
#include <windows.h>
namespace Core {
static DirectoryEntry::Type directory_entry_type_from_win32(DWORD file_attributes)
{
if (file_attributes & FILE_ATTRIBUTE_DIRECTORY)
return DirectoryEntry::Type::Directory;
if (file_attributes & FILE_ATTRIBUTE_DEVICE)
return DirectoryEntry::Type::CharacterDevice;
if (file_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
return DirectoryEntry::Type::SymbolicLink;
return DirectoryEntry::Type::File;
}
DirectoryEntry DirectoryEntry::from_find_data(WIN32_FIND_DATA const& de)
{
return DirectoryEntry {
.type = directory_entry_type_from_win32(de.dwFileAttributes),
.name = de.cFileName,
};
}
}