mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:17:34 +00:00
LibCore: Introduce a new directory iteration API
`Core::Directory::for_each_entry()` takes a callback which is passed the DirectoryEntry and the parent Directory. It returns any error from creating the iterator, iterating the entries, or returned from the callback. As a simple example, this: ```c++ Core::DirIterator piece_set_iterator { "/res/icons/chess/sets/", Core::DirIterator::SkipParentAndBaseDir }; while (piece_set_iterator.has_next()) m_piece_sets.append(piece_set_iterator.next_path()); ``` becomes this: ```c++ TRY(Core::Directory::for_each_entry("/res/icons/chess/sets/"sv, Core::DirIterator::SkipParentAndBaseDir, [&](auto const& entry, auto&) -> ErrorOr<IterationDecision> { TRY(m_piece_sets.try_append(entry.name)); return IterationDecision::Continue; })); ```
This commit is contained in:
parent
ceaed7440e
commit
23aec16e8b
2 changed files with 37 additions and 2 deletions
|
@ -93,4 +93,32 @@ ErrorOr<struct stat> Directory::stat() const
|
||||||
return System::fstat(m_directory_fd);
|
return System::fstat(m_directory_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> Directory::for_each_entry(DirIterator::Flags flags, Core::Directory::ForEachEntryCallback callback)
|
||||||
|
{
|
||||||
|
DirIterator iterator { path().string(), flags };
|
||||||
|
if (iterator.has_error())
|
||||||
|
return iterator.error();
|
||||||
|
|
||||||
|
while (iterator.has_next()) {
|
||||||
|
if (iterator.has_error())
|
||||||
|
return iterator.error();
|
||||||
|
|
||||||
|
auto entry = iterator.next();
|
||||||
|
if (!entry.has_value())
|
||||||
|
break;
|
||||||
|
|
||||||
|
auto decision = TRY(callback(entry.value(), *this));
|
||||||
|
if (decision == IterationDecision::Break)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
ErrorOr<void> Directory::for_each_entry(AK::StringView path, DirIterator::Flags flags, Core::Directory::ForEachEntryCallback callback)
|
||||||
|
{
|
||||||
|
auto directory = TRY(Directory::create(path, CreateDirectories::No));
|
||||||
|
return directory.for_each_entry(flags, move(callback));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
||||||
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -8,17 +9,19 @@
|
||||||
|
|
||||||
#include <AK/Error.h>
|
#include <AK/Error.h>
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
|
#include <AK/Function.h>
|
||||||
|
#include <AK/IterationDecision.h>
|
||||||
#include <AK/LexicalPath.h>
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/Noncopyable.h>
|
#include <AK/Noncopyable.h>
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
|
#include <LibCore/DirIterator.h>
|
||||||
|
#include <LibCore/DirectoryEntry.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
class DirIterator;
|
|
||||||
|
|
||||||
// Deal with real system directories. Any Directory instance always refers to a valid existing directory.
|
// Deal with real system directories. Any Directory instance always refers to a valid existing directory.
|
||||||
class Directory {
|
class Directory {
|
||||||
AK_MAKE_NONCOPYABLE(Directory);
|
AK_MAKE_NONCOPYABLE(Directory);
|
||||||
|
@ -43,6 +46,10 @@ public:
|
||||||
|
|
||||||
LexicalPath const& path() const { return m_path; }
|
LexicalPath const& path() const { return m_path; }
|
||||||
|
|
||||||
|
using ForEachEntryCallback = Function<ErrorOr<IterationDecision>(DirectoryEntry const&, Directory const& parent)>;
|
||||||
|
static ErrorOr<void> for_each_entry(StringView path, DirIterator::Flags, ForEachEntryCallback);
|
||||||
|
ErrorOr<void> for_each_entry(DirIterator::Flags, ForEachEntryCallback);
|
||||||
|
|
||||||
ErrorOr<void> chown(uid_t, gid_t);
|
ErrorOr<void> chown(uid_t, gid_t);
|
||||||
|
|
||||||
static ErrorOr<bool> is_valid_directory(int fd);
|
static ErrorOr<bool> is_valid_directory(int fd);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue