1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-18 17:35:06 +00:00
serenity/Userland/Libraries/LibCore/Directory.h
sin-ack 7456904a39 Meta+Userland: Simplify some formatters
These are mostly minor mistakes I've encountered while working on the
removal of StringView(char const*). The usage of builder.put_string over
Format<FormatString>::format is preferrable as it will avoid the
indirection altogether when there's no formatting to be done. Similarly,
there is no need to do format(builder, "{}", number) when
builder.put_u64(number) works equally well.

Additionally a few Strings where only constant strings were used are
replaced with StringViews.
2022-07-12 23:11:35 +02:00

71 lines
2 KiB
C++

/*
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/LexicalPath.h>
#include <AK/Noncopyable.h>
#include <AK/Optional.h>
#include <LibCore/Stream.h>
#include <dirent.h>
#include <sys/stat.h>
namespace Core {
class DirIterator;
// 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<Directory> create(LexicalPath path, CreateDirectories, mode_t creation_mode = 0755);
static ErrorOr<Directory> create(String path, CreateDirectories, mode_t creation_mode = 0755);
static ErrorOr<Directory> adopt_fd(int fd, Optional<LexicalPath> path = {});
ErrorOr<NonnullOwnPtr<Stream::File>> open(StringView filename, Stream::OpenMode mode) const;
ErrorOr<struct stat> stat() const;
ErrorOr<DirIterator> create_iterator() const;
ErrorOr<LexicalPath> path() const;
static ErrorOr<bool> is_valid_directory(int fd);
private:
Directory(int directory_fd, Optional<LexicalPath> path);
static ErrorOr<void> ensure_directory(LexicalPath const& path, mode_t creation_mode = 0755);
Optional<LexicalPath> m_path;
int m_directory_fd;
};
}
namespace AK {
template<>
struct Formatter<Core::Directory> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Core::Directory const& directory)
{
auto path = directory.path();
if (path.is_error())
TRY(builder.put_string("<unknown>"sv));
TRY(builder.put_string(path.release_value().string()));
return {};
}
};
}