2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2023-03-02 15:39:07 +00:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-16 18:46:33 +00:00
|
|
|
#include <LibCore/DirIterator.h>
|
2024-10-24 17:56:43 +00:00
|
|
|
#include <dirent.h>
|
2024-01-03 13:06:51 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2019-05-27 07:26:54 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
DirIterator::DirIterator(ByteString path, Flags flags)
|
2024-10-24 17:22:11 +00:00
|
|
|
: m_path(move(path))
|
2020-02-16 01:10:47 +00:00
|
|
|
, m_flags(flags)
|
2019-05-27 07:26:54 +00:00
|
|
|
{
|
2024-10-24 17:22:11 +00:00
|
|
|
m_dir = opendir(m_path.characters());
|
|
|
|
if (!m_dir) {
|
2023-03-01 15:55:15 +00:00
|
|
|
m_error = Error::from_errno(errno);
|
2019-05-27 07:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
DirIterator::~DirIterator()
|
2019-05-27 07:26:54 +00:00
|
|
|
{
|
2024-10-24 17:22:11 +00:00
|
|
|
if (m_dir) {
|
|
|
|
closedir(m_dir);
|
|
|
|
m_dir = nullptr;
|
2019-05-27 07:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:37:14 +00:00
|
|
|
DirIterator::DirIterator(DirIterator&& other)
|
2024-10-24 17:22:11 +00:00
|
|
|
: m_dir(other.m_dir)
|
2023-03-01 15:55:15 +00:00
|
|
|
, m_error(move(other.m_error))
|
2021-11-23 16:37:14 +00:00
|
|
|
, m_next(move(other.m_next))
|
|
|
|
, m_path(move(other.m_path))
|
|
|
|
, m_flags(other.m_flags)
|
|
|
|
{
|
2024-10-24 17:22:11 +00:00
|
|
|
other.m_dir = nullptr;
|
2021-11-23 16:37:14 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 22:26:00 +00:00
|
|
|
static constexpr bool dirent_has_d_type =
|
|
|
|
#if defined(AK_OS_SOLARIS) || defined(AK_OS_HAIKU)
|
|
|
|
false;
|
|
|
|
#else
|
|
|
|
true;
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool DirIterator::advance_next()
|
2019-05-27 07:26:54 +00:00
|
|
|
{
|
2024-10-24 17:22:11 +00:00
|
|
|
if (!m_dir)
|
2019-05-27 07:26:54 +00:00
|
|
|
return false;
|
|
|
|
|
2020-02-15 00:04:09 +00:00
|
|
|
while (true) {
|
2019-05-27 07:26:54 +00:00
|
|
|
errno = 0;
|
2024-10-24 17:22:11 +00:00
|
|
|
auto* de = readdir(m_dir);
|
2020-02-15 00:04:09 +00:00
|
|
|
if (!de) {
|
2023-03-17 16:04:19 +00:00
|
|
|
if (errno != 0) {
|
|
|
|
m_error = Error::from_errno(errno);
|
|
|
|
dbgln("DirIteration error: {}", m_error.value());
|
|
|
|
}
|
2023-03-02 15:39:07 +00:00
|
|
|
m_next.clear();
|
2020-02-15 00:04:09 +00:00
|
|
|
return false;
|
2019-05-27 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 22:26:00 +00:00
|
|
|
if constexpr (dirent_has_d_type)
|
|
|
|
m_next = DirectoryEntry::from_dirent(*de);
|
|
|
|
else
|
2024-10-24 17:22:11 +00:00
|
|
|
m_next = DirectoryEntry::from_stat(m_dir, *de);
|
2023-03-02 15:39:07 +00:00
|
|
|
|
|
|
|
if (m_next->name.is_empty())
|
2020-02-15 00:04:09 +00:00
|
|
|
return false;
|
2019-05-27 07:26:54 +00:00
|
|
|
|
2023-03-02 15:39:07 +00:00
|
|
|
if (m_flags & Flags::SkipDots && m_next->name.starts_with('.'))
|
2020-02-15 00:04:09 +00:00
|
|
|
continue;
|
|
|
|
|
2023-03-02 15:39:07 +00:00
|
|
|
if (m_flags & Flags::SkipParentAndBaseDir && (m_next->name == "." || m_next->name == ".."))
|
2020-02-15 00:06:08 +00:00
|
|
|
continue;
|
|
|
|
|
2024-10-24 17:56:43 +00:00
|
|
|
#ifndef AK_OS_WINDOWS
|
2024-01-06 22:26:00 +00:00
|
|
|
if constexpr (dirent_has_d_type) {
|
|
|
|
// dirent structures from readdir aren't guaranteed to contain valid file types,
|
|
|
|
// as it is possible that the underlying filesystem doesn't keep track of those.
|
2024-01-06 09:38:43 +00:00
|
|
|
// However, if we were requested to not do stat on the entries of this directory,
|
|
|
|
// the calling code will be given the raw unknown type.
|
|
|
|
if ((m_flags & Flags::NoStat) == 0 && m_next->type == DirectoryEntry::Type::Unknown) {
|
2024-01-06 22:26:00 +00:00
|
|
|
struct stat statbuf;
|
2024-10-24 17:22:11 +00:00
|
|
|
if (fstatat(dirfd(m_dir), de->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) < 0) {
|
2024-01-06 22:26:00 +00:00
|
|
|
m_error = Error::from_errno(errno);
|
|
|
|
dbgln("DirIteration error: {}", m_error.value());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_next->type = DirectoryEntry::directory_entry_type_from_stat(statbuf.st_mode);
|
|
|
|
}
|
|
|
|
}
|
2024-10-24 17:56:43 +00:00
|
|
|
#endif
|
2024-01-06 22:26:00 +00:00
|
|
|
|
2023-03-02 15:39:07 +00:00
|
|
|
return !m_next->name.is_empty();
|
2020-02-15 00:04:09 +00:00
|
|
|
}
|
2019-05-27 07:26:54 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
bool DirIterator::has_next()
|
2019-05-27 07:26:54 +00:00
|
|
|
{
|
2023-03-02 15:39:07 +00:00
|
|
|
if (m_next.has_value())
|
2019-05-27 07:26:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return advance_next();
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:39:07 +00:00
|
|
|
Optional<DirectoryEntry> DirIterator::next()
|
2019-05-27 07:26:54 +00:00
|
|
|
{
|
2023-03-02 15:39:07 +00:00
|
|
|
if (!m_next.has_value())
|
2019-05-27 07:26:54 +00:00
|
|
|
advance_next();
|
|
|
|
|
2023-03-02 15:39:07 +00:00
|
|
|
auto result = m_next;
|
|
|
|
m_next.clear();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString DirIterator::next_path()
|
2023-03-02 15:39:07 +00:00
|
|
|
{
|
|
|
|
auto entry = next();
|
|
|
|
if (entry.has_value())
|
|
|
|
return entry->name;
|
|
|
|
return "";
|
2019-05-27 07:26:54 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString DirIterator::next_full_path()
|
2020-02-16 01:10:47 +00:00
|
|
|
{
|
2021-07-03 10:32:46 +00:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(m_path);
|
|
|
|
if (!m_path.ends_with('/'))
|
|
|
|
builder.append('/');
|
|
|
|
builder.append(next_path());
|
2023-12-16 14:19:34 +00:00
|
|
|
return builder.to_byte_string();
|
2020-02-16 01:10:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 17:56:43 +00:00
|
|
|
#ifndef AK_OS_WINDOWS
|
2021-05-14 19:05:18 +00:00
|
|
|
int DirIterator::fd() const
|
|
|
|
{
|
2024-10-24 17:22:11 +00:00
|
|
|
if (!m_dir)
|
2021-05-14 19:05:18 +00:00
|
|
|
return -1;
|
2024-10-24 17:22:11 +00:00
|
|
|
return dirfd(m_dir);
|
2021-05-14 19:05:18 +00:00
|
|
|
}
|
2024-10-24 17:56:43 +00:00
|
|
|
#endif
|
2021-05-14 19:05:18 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
}
|