2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-09-16 18:55:41 +02:00
|
|
|
#include <AK/Vector.h>
|
2020-09-16 20:46:33 +02:00
|
|
|
#include <LibCore/DirIterator.h>
|
2020-02-02 12:34:39 +01:00
|
|
|
#include <errno.h>
|
2021-03-12 17:29:37 +01:00
|
|
|
#include <unistd.h>
|
2019-05-27 09:26:54 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
2021-04-17 00:48:07 +02:00
|
|
|
DirIterator::DirIterator(String path, Flags flags)
|
|
|
|
: m_path(move(path))
|
2020-02-16 14:10:47 +13:00
|
|
|
, m_flags(flags)
|
2019-05-27 09:26:54 +02:00
|
|
|
{
|
2021-04-17 00:48:07 +02:00
|
|
|
m_dir = opendir(m_path.characters());
|
2020-02-15 13:04:09 +13:00
|
|
|
if (!m_dir) {
|
2019-05-27 09:26:54 +02:00
|
|
|
m_error = errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
DirIterator::~DirIterator()
|
2019-05-27 09:26:54 +02:00
|
|
|
{
|
2020-02-15 13:04:09 +13:00
|
|
|
if (m_dir) {
|
2019-05-27 09:26:54 +02:00
|
|
|
closedir(m_dir);
|
|
|
|
m_dir = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
bool DirIterator::advance_next()
|
2019-05-27 09:26:54 +02:00
|
|
|
{
|
2020-02-15 13:04:09 +13:00
|
|
|
if (!m_dir)
|
2019-05-27 09:26:54 +02:00
|
|
|
return false;
|
|
|
|
|
2020-02-15 13:04:09 +13:00
|
|
|
while (true) {
|
2019-05-27 09:26:54 +02:00
|
|
|
errno = 0;
|
|
|
|
auto* de = readdir(m_dir);
|
2020-02-15 13:04:09 +13:00
|
|
|
if (!de) {
|
2019-05-27 09:26:54 +02:00
|
|
|
m_error = errno;
|
|
|
|
m_next = String();
|
2020-02-15 13:04:09 +13:00
|
|
|
return false;
|
2019-05-27 09:26:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 13:04:09 +13:00
|
|
|
m_next = de->d_name;
|
|
|
|
if (m_next.is_null())
|
|
|
|
return false;
|
2019-05-27 09:26:54 +02:00
|
|
|
|
2020-02-15 13:04:09 +13:00
|
|
|
if (m_flags & Flags::SkipDots && m_next.starts_with('.'))
|
|
|
|
continue;
|
|
|
|
|
2020-02-15 13:06:08 +13:00
|
|
|
if (m_flags & Flags::SkipParentAndBaseDir && (m_next == "." || m_next == ".."))
|
|
|
|
continue;
|
|
|
|
|
2020-02-15 13:04:09 +13:00
|
|
|
return !m_next.is_empty();
|
|
|
|
}
|
2019-05-27 09:26:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
bool DirIterator::has_next()
|
2019-05-27 09:26:54 +02:00
|
|
|
{
|
|
|
|
if (!m_next.is_null())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return advance_next();
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
String DirIterator::next_path()
|
2019-05-27 09:26:54 +02:00
|
|
|
{
|
|
|
|
if (m_next.is_null())
|
|
|
|
advance_next();
|
|
|
|
|
|
|
|
auto tmp = m_next;
|
|
|
|
m_next = String();
|
|
|
|
return tmp;
|
|
|
|
}
|
2020-02-02 12:34:39 +01:00
|
|
|
|
2020-02-16 14:10:47 +13:00
|
|
|
String DirIterator::next_full_path()
|
|
|
|
{
|
2021-07-03 12:32:46 +02:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(m_path);
|
|
|
|
if (!m_path.ends_with('/'))
|
|
|
|
builder.append('/');
|
|
|
|
builder.append(next_path());
|
|
|
|
return builder.to_string();
|
2020-02-16 14:10:47 +13:00
|
|
|
}
|
|
|
|
|
2020-09-16 18:55:41 +02:00
|
|
|
String find_executable_in_path(String filename)
|
|
|
|
{
|
|
|
|
if (filename.starts_with('/')) {
|
|
|
|
if (access(filename.characters(), X_OK) == 0)
|
|
|
|
return filename;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-09-16 20:46:33 +02:00
|
|
|
for (auto directory : String { getenv("PATH") }.split(':')) {
|
2020-10-15 13:21:23 +02:00
|
|
|
auto fullpath = String::formatted("{}/{}", directory, filename);
|
2020-09-16 18:55:41 +02:00
|
|
|
|
|
|
|
if (access(fullpath.characters(), X_OK) == 0)
|
|
|
|
return fullpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:05:18 +02:00
|
|
|
int DirIterator::fd() const
|
|
|
|
{
|
|
|
|
if (!m_dir)
|
|
|
|
return -1;
|
|
|
|
return dirfd(m_dir);
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|