From 7f780e43a61c7f5b38d9d914a8a251f7a2db04ca Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 23 Nov 2021 11:37:14 -0500 Subject: [PATCH] LibCore: Allow moving, but not copying, DirIterator An explicit move constructor is required to null-out the moved-from directory descriptor. Otherwise, we would call closedir() twice when using ErrorOr::release_value(). --- Userland/Libraries/LibCore/DirIterator.cpp | 10 ++++++++++ Userland/Libraries/LibCore/DirIterator.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/Userland/Libraries/LibCore/DirIterator.cpp b/Userland/Libraries/LibCore/DirIterator.cpp index adbc3c1418..11758a328c 100644 --- a/Userland/Libraries/LibCore/DirIterator.cpp +++ b/Userland/Libraries/LibCore/DirIterator.cpp @@ -29,6 +29,16 @@ DirIterator::~DirIterator() } } +DirIterator::DirIterator(DirIterator&& other) + : m_dir(other.m_dir) + , m_error(other.m_error) + , m_next(move(other.m_next)) + , m_path(move(other.m_path)) + , m_flags(other.m_flags) +{ + other.m_dir = nullptr; +} + bool DirIterator::advance_next() { if (!m_dir) diff --git a/Userland/Libraries/LibCore/DirIterator.h b/Userland/Libraries/LibCore/DirIterator.h index 18fc8a218b..6fca1ae502 100644 --- a/Userland/Libraries/LibCore/DirIterator.h +++ b/Userland/Libraries/LibCore/DirIterator.h @@ -23,6 +23,9 @@ public: explicit DirIterator(String path, Flags = Flags::NoFlags); ~DirIterator(); + DirIterator(DirIterator&&); + DirIterator(DirIterator const&) = delete; + bool has_error() const { return m_error != 0; } int error() const { return m_error; } const char* error_string() const { return strerror(m_error); }