diff --git a/Userland/Libraries/LibCore/CMakeLists.txt b/Userland/Libraries/LibCore/CMakeLists.txt index 81c655ceaa7..200dfffaccb 100644 --- a/Userland/Libraries/LibCore/CMakeLists.txt +++ b/Userland/Libraries/LibCore/CMakeLists.txt @@ -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) diff --git a/Userland/Libraries/LibCore/DirIterator.cpp b/Userland/Libraries/LibCore/DirIterator.cpp index 28df35591f7..76f41a61d74 100644 --- a/Userland/Libraries/LibCore/DirIterator.cpp +++ b/Userland/Libraries/LibCore/DirIterator.cpp @@ -5,6 +5,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -12,36 +13,32 @@ namespace Core { -struct DirIterator::Impl { - DIR* dir { nullptr }; -}; - DirIterator::DirIterator(ByteString path, Flags flags) - : m_impl(make()) - , 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); } } diff --git a/Userland/Libraries/LibCore/DirIterator.h b/Userland/Libraries/LibCore/DirIterator.h index d6abe550f8f..262ef5844ae 100644 --- a/Userland/Libraries/LibCore/DirIterator.h +++ b/Userland/Libraries/LibCore/DirIterator.h @@ -8,8 +8,9 @@ #pragma once #include -#include #include +#include +#include namespace Core { @@ -37,8 +38,7 @@ public: int fd() const; private: - struct Impl; - OwnPtr m_impl; + DIR* m_dir = nullptr; Optional m_error; Optional m_next; ByteString m_path; diff --git a/Userland/Libraries/LibCore/DirIteratorWindows.cpp b/Userland/Libraries/LibCore/DirIteratorWindows.cpp deleted file mode 100644 index db3280e2b66..00000000000 --- a/Userland/Libraries/LibCore/DirIteratorWindows.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright (c) 2023, Cameron Youell - * Copyright (c) 2024, stasoid - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include - -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()) - , 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 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(); -} - -} diff --git a/Userland/Libraries/LibCore/DirectoryEntry.h b/Userland/Libraries/LibCore/DirectoryEntry.h index 7c66e4fb8c3..c33d4adf750 100644 --- a/Userland/Libraries/LibCore/DirectoryEntry.h +++ b/Userland/Libraries/LibCore/DirectoryEntry.h @@ -8,11 +8,7 @@ #include #include -#ifdef AK_OS_WINDOWS -using WIN32_FIND_DATA = struct _WIN32_FIND_DATAA; -#else -# include -#endif +#include 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 }; } diff --git a/Userland/Libraries/LibCore/DirectoryEntryWindows.cpp b/Userland/Libraries/LibCore/DirectoryEntryWindows.cpp deleted file mode 100644 index b76fbee31d4..00000000000 --- a/Userland/Libraries/LibCore/DirectoryEntryWindows.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2023, Cameron Youell - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include - -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, - }; -} - -}