From 7864898f52f52f4db82f06747ac817247b850b2d Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 2 Mar 2023 16:37:46 +0000 Subject: LibCore: Ensure that Directory always has a path `Directory::path()` returning `ErrorOr` makes it awkward to use, and all current users create a Directory with a path. If we find we need pathless directories later, we can come up with a clever solution then. :^) --- Userland/Libraries/LibCore/Directory.cpp | 13 +++---------- Userland/Libraries/LibCore/Directory.h | 13 +++++-------- 2 files changed, 8 insertions(+), 18 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibCore/Directory.cpp b/Userland/Libraries/LibCore/Directory.cpp index 5e821d16fc..fce9f65103 100644 --- a/Userland/Libraries/LibCore/Directory.cpp +++ b/Userland/Libraries/LibCore/Directory.cpp @@ -12,7 +12,7 @@ namespace Core { // We assume that the fd is a valid directory. -Directory::Directory(int fd, Optional path) +Directory::Directory(int fd, LexicalPath path) : m_path(move(path)) , m_directory_fd(fd) { @@ -45,7 +45,7 @@ ErrorOr Directory::is_valid_directory(int fd) return stat.st_mode & S_IFDIR; } -ErrorOr Directory::adopt_fd(int fd, Optional path) +ErrorOr Directory::adopt_fd(int fd, LexicalPath path) { // This will also fail if the fd is invalid in the first place. if (!TRY(Directory::is_valid_directory(fd))) @@ -82,13 +82,6 @@ ErrorOr Directory::ensure_directory(LexicalPath const& path, mode_t creati return {}; } -ErrorOr Directory::path() const -{ - if (!m_path.has_value()) - return Error::from_string_literal("Directory wasn't created with a path"); - return m_path.value(); -} - ErrorOr> Directory::open(StringView filename, File::OpenMode mode) const { auto fd = TRY(System::openat(m_directory_fd, filename, File::open_mode_to_options(mode))); @@ -102,7 +95,7 @@ ErrorOr Directory::stat() const ErrorOr Directory::create_iterator() const { - return DirIterator { TRY(path()).string() }; + return DirIterator { path().string() }; } } diff --git a/Userland/Libraries/LibCore/Directory.h b/Userland/Libraries/LibCore/Directory.h index 8743e1964e..4cb72c735e 100644 --- a/Userland/Libraries/LibCore/Directory.h +++ b/Userland/Libraries/LibCore/Directory.h @@ -35,23 +35,23 @@ public: static ErrorOr create(LexicalPath path, CreateDirectories, mode_t creation_mode = 0755); static ErrorOr create(DeprecatedString path, CreateDirectories, mode_t creation_mode = 0755); - static ErrorOr adopt_fd(int fd, Optional path = {}); + static ErrorOr adopt_fd(int fd, LexicalPath path); ErrorOr> open(StringView filename, File::OpenMode mode) const; ErrorOr stat() const; ErrorOr create_iterator() const; - ErrorOr path() const; + LexicalPath const& path() const { return m_path; } ErrorOr chown(uid_t, gid_t); static ErrorOr is_valid_directory(int fd); private: - Directory(int directory_fd, Optional path); + Directory(int directory_fd, LexicalPath path); static ErrorOr ensure_directory(LexicalPath const& path, mode_t creation_mode = 0755); - Optional m_path; + LexicalPath m_path; int m_directory_fd; }; @@ -62,10 +62,7 @@ template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, Core::Directory const& directory) { - auto path = directory.path(); - if (path.is_error()) - TRY(builder.put_string(""sv)); - TRY(builder.put_string(path.release_value().string())); + TRY(builder.put_string(directory.path().string())); return {}; } }; -- cgit v1.2.3