mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
Partially revert "LibCore: Add Windows version of DirIterator"
This reverts commit d92a96c40a
, but
keeps changes to AK/Error{.h,.cpp} (function Error::from_windows_error)
This commit is contained in:
parent
75e26af117
commit
cf198d0d60
Notes:
github-actions[bot]
2024-11-05 17:43:51 +00:00
Author: https://github.com/stasoid Commit: https://github.com/LadybirdBrowser/ladybird/commit/cf198d0d606 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1957 Reviewed-by: https://github.com/ADKaster ✅
6 changed files with 23 additions and 193 deletions
|
@ -5,6 +5,8 @@ include(vulkan)
|
|||
set(SOURCES
|
||||
ArgsParser.cpp
|
||||
Directory.cpp
|
||||
DirectoryEntry.cpp
|
||||
DirIterator.cpp
|
||||
Environment.cpp
|
||||
File.cpp
|
||||
StandardPaths.cpp
|
||||
|
@ -12,17 +14,9 @@ set(SOURCES
|
|||
)
|
||||
|
||||
if (WIN32)
|
||||
list(APPEND SOURCES
|
||||
DirectoryEntryWindows.cpp
|
||||
DirIteratorWindows.cpp
|
||||
SystemWindows.cpp
|
||||
)
|
||||
list(APPEND SOURCES SystemWindows.cpp)
|
||||
else()
|
||||
list(APPEND SOURCES
|
||||
DirectoryEntry.cpp
|
||||
DirIterator.cpp
|
||||
System.cpp
|
||||
)
|
||||
list(APPEND SOURCES System.cpp)
|
||||
endif()
|
||||
|
||||
serenity_lib(LibCoreMinimal coreminimal)
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
@ -12,36 +13,32 @@
|
|||
|
||||
namespace Core {
|
||||
|
||||
struct DirIterator::Impl {
|
||||
DIR* dir { nullptr };
|
||||
};
|
||||
|
||||
DirIterator::DirIterator(ByteString path, Flags flags)
|
||||
: m_impl(make<Impl>())
|
||||
, m_path(move(path))
|
||||
: m_path(move(path))
|
||||
, m_flags(flags)
|
||||
{
|
||||
m_impl->dir = opendir(m_path.characters());
|
||||
if (!m_impl->dir) {
|
||||
m_dir = opendir(m_path.characters());
|
||||
if (!m_dir) {
|
||||
m_error = Error::from_errno(errno);
|
||||
}
|
||||
}
|
||||
|
||||
DirIterator::~DirIterator()
|
||||
{
|
||||
if (m_impl && m_impl->dir) {
|
||||
closedir(m_impl->dir);
|
||||
m_impl->dir = nullptr;
|
||||
if (m_dir) {
|
||||
closedir(m_dir);
|
||||
m_dir = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
DirIterator::DirIterator(DirIterator&& other)
|
||||
: m_impl(move(other.m_impl))
|
||||
: m_dir(other.m_dir)
|
||||
, 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 =
|
||||
|
@ -53,12 +50,12 @@ static constexpr bool dirent_has_d_type =
|
|||
|
||||
bool DirIterator::advance_next()
|
||||
{
|
||||
if (!m_impl || !m_impl->dir)
|
||||
if (!m_dir)
|
||||
return false;
|
||||
|
||||
while (true) {
|
||||
errno = 0;
|
||||
auto* de = readdir(m_impl->dir);
|
||||
auto* de = readdir(m_dir);
|
||||
if (!de) {
|
||||
if (errno != 0) {
|
||||
m_error = Error::from_errno(errno);
|
||||
|
@ -71,7 +68,7 @@ bool DirIterator::advance_next()
|
|||
if constexpr (dirent_has_d_type)
|
||||
m_next = DirectoryEntry::from_dirent(*de);
|
||||
else
|
||||
m_next = DirectoryEntry::from_stat(m_impl->dir, *de);
|
||||
m_next = DirectoryEntry::from_stat(m_dir, *de);
|
||||
|
||||
if (m_next->name.is_empty())
|
||||
return false;
|
||||
|
@ -89,7 +86,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_impl->dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0) {
|
||||
if (fstatat(dirfd(m_dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0) {
|
||||
m_error = Error::from_errno(errno);
|
||||
dbgln("DirIteration error: {}", m_error.value());
|
||||
return false;
|
||||
|
@ -140,9 +137,9 @@ ByteString DirIterator::next_full_path()
|
|||
|
||||
int DirIterator::fd() const
|
||||
{
|
||||
if (!m_impl || !m_impl->dir)
|
||||
if (!m_dir)
|
||||
return -1;
|
||||
return dirfd(m_impl->dir);
|
||||
return dirfd(m_dir);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,8 +8,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/ByteString.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <LibCore/DirectoryEntry.h>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
|
@ -37,8 +38,7 @@ public:
|
|||
int fd() const;
|
||||
|
||||
private:
|
||||
struct Impl;
|
||||
OwnPtr<Impl> m_impl;
|
||||
DIR* m_dir = nullptr;
|
||||
Optional<Error> m_error;
|
||||
Optional<DirectoryEntry> m_next;
|
||||
ByteString m_path;
|
||||
|
|
|
@ -1,122 +0,0 @@
|
|||
/*
|
||||
* 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,11 +8,7 @@
|
|||
|
||||
#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
|
||||
#include <dirent.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
|
@ -31,7 +27,6 @@ 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);
|
||||
|
@ -39,9 +34,6 @@ 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
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
/*
|
||||
* 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