mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
LibCore: Add Windows version of DirIterator
Co-authored-by: stasoid <stasoid@yahoo.com>
This commit is contained in:
parent
c5219b0193
commit
94601e1ffd
Notes:
github-actions[bot]
2024-10-20 00:15:43 +00:00
Author: https://github.com/stasoid Commit: https://github.com/LadybirdBrowser/ladybird/commit/94601e1ffda Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1832 Reviewed-by: https://github.com/ADKaster ✅
8 changed files with 238 additions and 21 deletions
36
AK/Error.cpp
36
AK/Error.cpp
|
@ -1,10 +1,17 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
* 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
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Error.h>
|
#include <AK/Error.h>
|
||||||
|
#ifdef AK_OS_WINDOWS
|
||||||
|
# include <AK/ByteString.h>
|
||||||
|
# include <AK/HashMap.h>
|
||||||
|
# include <windows.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace AK {
|
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);
|
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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
#include <AK/Variant.h>
|
#include <AK/Variant.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#ifdef AK_OS_WINDOWS
|
||||||
|
typedef unsigned long DWORD;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
@ -25,6 +28,10 @@ public:
|
||||||
return Error(code);
|
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
|
// NOTE: For calling this method from within kernel code, we will simply print
|
||||||
// the error message and return the errno code.
|
// the error message and return the errno code.
|
||||||
// For calling this method from userspace programs, we will simply return from
|
// For calling this method from userspace programs, we will simply return from
|
||||||
|
|
|
@ -5,8 +5,6 @@ include(vulkan)
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
ArgsParser.cpp
|
ArgsParser.cpp
|
||||||
Directory.cpp
|
Directory.cpp
|
||||||
DirectoryEntry.cpp
|
|
||||||
DirIterator.cpp
|
|
||||||
Environment.cpp
|
Environment.cpp
|
||||||
File.cpp
|
File.cpp
|
||||||
StandardPaths.cpp
|
StandardPaths.cpp
|
||||||
|
@ -14,6 +12,18 @@ set(SOURCES
|
||||||
Version.cpp
|
Version.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (WIN32)
|
||||||
|
list(APPEND SOURCES
|
||||||
|
DirectoryEntryWindows.cpp
|
||||||
|
DirIteratorWindows.cpp
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
list(APPEND SOURCES
|
||||||
|
DirectoryEntry.cpp
|
||||||
|
DirIterator.cpp
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
serenity_lib(LibCoreMinimal coreminimal)
|
serenity_lib(LibCoreMinimal coreminimal)
|
||||||
|
|
||||||
if (LAGOM_TOOLS_ONLY)
|
if (LAGOM_TOOLS_ONLY)
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
* 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 <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -13,32 +12,36 @@
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
struct DirIterator::Impl {
|
||||||
|
DIR* dir { nullptr };
|
||||||
|
};
|
||||||
|
|
||||||
DirIterator::DirIterator(ByteString path, Flags flags)
|
DirIterator::DirIterator(ByteString path, Flags flags)
|
||||||
: m_path(move(path))
|
: m_impl(make<Impl>())
|
||||||
|
, m_path(move(path))
|
||||||
, m_flags(flags)
|
, m_flags(flags)
|
||||||
{
|
{
|
||||||
m_dir = opendir(m_path.characters());
|
m_impl->dir = opendir(m_path.characters());
|
||||||
if (!m_dir) {
|
if (!m_impl->dir) {
|
||||||
m_error = Error::from_errno(errno);
|
m_error = Error::from_errno(errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DirIterator::~DirIterator()
|
DirIterator::~DirIterator()
|
||||||
{
|
{
|
||||||
if (m_dir) {
|
if (m_impl && m_impl->dir) {
|
||||||
closedir(m_dir);
|
closedir(m_impl->dir);
|
||||||
m_dir = nullptr;
|
m_impl->dir = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DirIterator::DirIterator(DirIterator&& other)
|
DirIterator::DirIterator(DirIterator&& other)
|
||||||
: m_dir(other.m_dir)
|
: m_impl(move(other.m_impl))
|
||||||
, m_error(move(other.m_error))
|
, m_error(move(other.m_error))
|
||||||
, m_next(move(other.m_next))
|
, m_next(move(other.m_next))
|
||||||
, m_path(move(other.m_path))
|
, m_path(move(other.m_path))
|
||||||
, m_flags(other.m_flags)
|
, m_flags(other.m_flags)
|
||||||
{
|
{
|
||||||
other.m_dir = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr bool dirent_has_d_type =
|
static constexpr bool dirent_has_d_type =
|
||||||
|
@ -50,12 +53,12 @@ static constexpr bool dirent_has_d_type =
|
||||||
|
|
||||||
bool DirIterator::advance_next()
|
bool DirIterator::advance_next()
|
||||||
{
|
{
|
||||||
if (!m_dir)
|
if (!m_impl || !m_impl->dir)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
auto* de = readdir(m_dir);
|
auto* de = readdir(m_impl->dir);
|
||||||
if (!de) {
|
if (!de) {
|
||||||
if (errno != 0) {
|
if (errno != 0) {
|
||||||
m_error = Error::from_errno(errno);
|
m_error = Error::from_errno(errno);
|
||||||
|
@ -68,7 +71,7 @@ bool DirIterator::advance_next()
|
||||||
if constexpr (dirent_has_d_type)
|
if constexpr (dirent_has_d_type)
|
||||||
m_next = DirectoryEntry::from_dirent(*de);
|
m_next = DirectoryEntry::from_dirent(*de);
|
||||||
else
|
else
|
||||||
m_next = DirectoryEntry::from_stat(m_dir, *de);
|
m_next = DirectoryEntry::from_stat(m_impl->dir, *de);
|
||||||
|
|
||||||
if (m_next->name.is_empty())
|
if (m_next->name.is_empty())
|
||||||
return false;
|
return false;
|
||||||
|
@ -86,7 +89,7 @@ bool DirIterator::advance_next()
|
||||||
// the calling code will be given the raw unknown type.
|
// the calling code will be given the raw unknown type.
|
||||||
if ((m_flags & Flags::NoStat) == 0 && m_next->type == DirectoryEntry::Type::Unknown) {
|
if ((m_flags & Flags::NoStat) == 0 && m_next->type == DirectoryEntry::Type::Unknown) {
|
||||||
struct stat statbuf;
|
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);
|
m_error = Error::from_errno(errno);
|
||||||
dbgln("DirIteration error: {}", m_error.value());
|
dbgln("DirIteration error: {}", m_error.value());
|
||||||
return false;
|
return false;
|
||||||
|
@ -137,9 +140,9 @@ ByteString DirIterator::next_full_path()
|
||||||
|
|
||||||
int DirIterator::fd() const
|
int DirIterator::fd() const
|
||||||
{
|
{
|
||||||
if (!m_dir)
|
if (!m_impl || !m_impl->dir)
|
||||||
return -1;
|
return -1;
|
||||||
return dirfd(m_dir);
|
return dirfd(m_impl->dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/ByteString.h>
|
#include <AK/ByteString.h>
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
#include <LibCore/DirectoryEntry.h>
|
#include <LibCore/DirectoryEntry.h>
|
||||||
#include <dirent.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
@ -38,7 +37,8 @@ public:
|
||||||
int fd() const;
|
int fd() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
DIR* m_dir = nullptr;
|
struct Impl;
|
||||||
|
OwnPtr<Impl> m_impl;
|
||||||
Optional<Error> m_error;
|
Optional<Error> m_error;
|
||||||
Optional<DirectoryEntry> m_next;
|
Optional<DirectoryEntry> m_next;
|
||||||
ByteString m_path;
|
ByteString m_path;
|
||||||
|
|
122
Userland/Libraries/LibCore/DirIteratorWindows.cpp
Normal file
122
Userland/Libraries/LibCore/DirIteratorWindows.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -8,7 +8,11 @@
|
||||||
|
|
||||||
#include <AK/ByteString.h>
|
#include <AK/ByteString.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <dirent.h>
|
#ifdef AK_OS_WINDOWS
|
||||||
|
using WIN32_FIND_DATA = struct _WIN32_FIND_DATAA;
|
||||||
|
#else
|
||||||
|
# include <dirent.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
|
@ -27,6 +31,7 @@ struct DirectoryEntry {
|
||||||
Type type;
|
Type type;
|
||||||
// FIXME: Once we have a special Path string class, use that.
|
// FIXME: Once we have a special Path string class, use that.
|
||||||
ByteString name;
|
ByteString name;
|
||||||
|
#if !defined(AK_OS_WINDOWS)
|
||||||
ino_t inode_number;
|
ino_t inode_number;
|
||||||
|
|
||||||
static StringView posix_name_from_directory_entry_type(Type);
|
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 Type directory_entry_type_from_stat(mode_t st_mode);
|
||||||
static DirectoryEntry from_dirent(dirent const&);
|
static DirectoryEntry from_dirent(dirent const&);
|
||||||
static DirectoryEntry from_stat(DIR*, dirent const&);
|
static DirectoryEntry from_stat(DIR*, dirent const&);
|
||||||
|
#else
|
||||||
|
static DirectoryEntry from_find_data(WIN32_FIND_DATA const&);
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
31
Userland/Libraries/LibCore/DirectoryEntryWindows.cpp
Normal file
31
Userland/Libraries/LibCore/DirectoryEntryWindows.cpp
Normal 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue