2022-04-10 16:28:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-11-15 14:02:27 +00:00
|
|
|
#include <LibCore/Directory.h>
|
|
|
|
#include <LibCore/System.h>
|
2022-04-10 16:28:43 +00:00
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
// We assume that the fd is a valid directory.
|
2023-03-02 16:37:46 +00:00
|
|
|
Directory::Directory(int fd, LexicalPath path)
|
2022-04-10 16:28:43 +00:00
|
|
|
: m_path(move(path))
|
|
|
|
, m_directory_fd(fd)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Directory::Directory(Directory&& other)
|
|
|
|
: m_path(move(other.m_path))
|
|
|
|
, m_directory_fd(other.m_directory_fd)
|
|
|
|
{
|
|
|
|
other.m_directory_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Directory::~Directory()
|
|
|
|
{
|
|
|
|
if (m_directory_fd != -1)
|
|
|
|
MUST(System::close(m_directory_fd));
|
|
|
|
}
|
|
|
|
|
2024-11-15 14:02:27 +00:00
|
|
|
#ifndef AK_OS_WINDOWS
|
2022-08-15 12:10:26 +00:00
|
|
|
ErrorOr<void> Directory::chown(uid_t uid, gid_t gid)
|
|
|
|
{
|
|
|
|
if (m_directory_fd == -1)
|
|
|
|
return Error::from_syscall("fchown"sv, -EBADF);
|
|
|
|
TRY(Core::System::fchown(m_directory_fd, uid, gid));
|
|
|
|
return {};
|
|
|
|
}
|
2024-11-15 14:02:27 +00:00
|
|
|
#endif
|
2022-08-15 12:10:26 +00:00
|
|
|
|
2022-04-10 16:28:43 +00:00
|
|
|
ErrorOr<bool> Directory::is_valid_directory(int fd)
|
|
|
|
{
|
|
|
|
auto stat = TRY(System::fstat(fd));
|
|
|
|
return stat.st_mode & S_IFDIR;
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:37:46 +00:00
|
|
|
ErrorOr<Directory> Directory::adopt_fd(int fd, LexicalPath path)
|
2022-04-10 16:28:43 +00:00
|
|
|
{
|
|
|
|
// This will also fail if the fd is invalid in the first place.
|
|
|
|
if (!TRY(Directory::is_valid_directory(fd)))
|
|
|
|
return Error::from_errno(ENOTDIR);
|
|
|
|
return Directory { fd, move(path) };
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ErrorOr<Directory> Directory::create(ByteString path, CreateDirectories create_directories, mode_t creation_mode)
|
2022-04-10 16:28:43 +00:00
|
|
|
{
|
2022-04-13 10:53:46 +00:00
|
|
|
return create(LexicalPath { move(path) }, create_directories, creation_mode);
|
2022-04-10 16:28:43 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 10:53:46 +00:00
|
|
|
ErrorOr<Directory> Directory::create(LexicalPath path, CreateDirectories create_directories, mode_t creation_mode)
|
2022-04-10 16:28:43 +00:00
|
|
|
{
|
|
|
|
if (create_directories == CreateDirectories::Yes)
|
2022-04-13 10:53:46 +00:00
|
|
|
TRY(ensure_directory(path, creation_mode));
|
2022-04-10 16:28:43 +00:00
|
|
|
// FIXME: doesn't work on Linux probably
|
|
|
|
auto fd = TRY(System::open(path.string(), O_CLOEXEC));
|
|
|
|
return adopt_fd(fd, move(path));
|
|
|
|
}
|
|
|
|
|
2022-04-13 10:53:46 +00:00
|
|
|
ErrorOr<void> Directory::ensure_directory(LexicalPath const& path, mode_t creation_mode)
|
2022-04-10 16:28:43 +00:00
|
|
|
{
|
2024-11-15 14:02:27 +00:00
|
|
|
if (path.is_root() || path.string() == ".")
|
2022-04-10 16:28:43 +00:00
|
|
|
return {};
|
|
|
|
|
2022-04-13 10:53:46 +00:00
|
|
|
TRY(ensure_directory(path.parent(), creation_mode));
|
2022-04-10 16:28:43 +00:00
|
|
|
|
2022-04-13 10:53:46 +00:00
|
|
|
auto return_value = System::mkdir(path.string(), creation_mode);
|
2022-04-10 16:28:43 +00:00
|
|
|
// We don't care if the directory already exists.
|
|
|
|
if (return_value.is_error() && return_value.error().code() != EEXIST)
|
|
|
|
return return_value;
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-02-09 02:02:46 +00:00
|
|
|
ErrorOr<NonnullOwnPtr<File>> Directory::open(StringView filename, File::OpenMode mode) const
|
2022-04-10 16:28:43 +00:00
|
|
|
{
|
2023-02-09 02:02:46 +00:00
|
|
|
auto fd = TRY(System::openat(m_directory_fd, filename, File::open_mode_to_options(mode)));
|
|
|
|
return File::adopt_fd(fd, mode);
|
2022-04-10 16:28:43 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 15:23:24 +00:00
|
|
|
ErrorOr<struct stat> Directory::stat(StringView filename, int flags) const
|
|
|
|
{
|
|
|
|
return System::fstatat(m_directory_fd, filename, flags);
|
|
|
|
}
|
|
|
|
|
2022-04-10 16:28:43 +00:00
|
|
|
ErrorOr<struct stat> Directory::stat() const
|
|
|
|
{
|
|
|
|
return System::fstat(m_directory_fd);
|
|
|
|
}
|
|
|
|
|
2023-03-02 17:01:08 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-04-10 16:28:43 +00:00
|
|
|
}
|