/* * Copyright (c) 2022, kleines Filmröllchen * Copyright (c) 2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include namespace Core { // Deal with real system directories. Any Directory instance always refers to a valid existing directory. class Directory { AK_MAKE_NONCOPYABLE(Directory); public: Directory(Directory&&); ~Directory(); // When this flag is set, both the directory attempted to instantiate as well as all of its parents are created with mode 0755 if necessary. enum class CreateDirectories : bool { No, Yes, }; static ErrorOr create(LexicalPath path, CreateDirectories, mode_t creation_mode = 0755); static ErrorOr create(ByteString path, CreateDirectories, mode_t creation_mode = 0755); static ErrorOr adopt_fd(int fd, LexicalPath path); ErrorOr> open(StringView filename, File::OpenMode mode) const; ErrorOr stat(StringView filename, int flags) const; ErrorOr stat() const; int fd() const { return m_directory_fd; } LexicalPath const& path() const { return m_path; } using ForEachEntryCallback = Function(DirectoryEntry const&, Directory const& parent)>; static ErrorOr for_each_entry(StringView path, DirIterator::Flags, ForEachEntryCallback); ErrorOr for_each_entry(DirIterator::Flags, ForEachEntryCallback); #ifndef AK_OS_WINDOWS ErrorOr chown(uid_t, gid_t); #endif static ErrorOr is_valid_directory(int fd); private: Directory(int directory_fd, LexicalPath path); static ErrorOr ensure_directory(LexicalPath const& path, mode_t creation_mode = 0755); LexicalPath m_path; int m_directory_fd; }; } namespace AK { template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, Core::Directory const& directory) { TRY(builder.put_string(directory.path().string())); return {}; } }; }