mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +00:00
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. :^)
This commit is contained in:
parent
774f328783
commit
7864898f52
2 changed files with 8 additions and 18 deletions
|
@ -12,7 +12,7 @@
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
// We assume that the fd is a valid directory.
|
// We assume that the fd is a valid directory.
|
||||||
Directory::Directory(int fd, Optional<LexicalPath> path)
|
Directory::Directory(int fd, LexicalPath path)
|
||||||
: m_path(move(path))
|
: m_path(move(path))
|
||||||
, m_directory_fd(fd)
|
, m_directory_fd(fd)
|
||||||
{
|
{
|
||||||
|
@ -45,7 +45,7 @@ ErrorOr<bool> Directory::is_valid_directory(int fd)
|
||||||
return stat.st_mode & S_IFDIR;
|
return stat.st_mode & S_IFDIR;
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<Directory> Directory::adopt_fd(int fd, Optional<LexicalPath> path)
|
ErrorOr<Directory> Directory::adopt_fd(int fd, LexicalPath path)
|
||||||
{
|
{
|
||||||
// This will also fail if the fd is invalid in the first place.
|
// This will also fail if the fd is invalid in the first place.
|
||||||
if (!TRY(Directory::is_valid_directory(fd)))
|
if (!TRY(Directory::is_valid_directory(fd)))
|
||||||
|
@ -82,13 +82,6 @@ ErrorOr<void> Directory::ensure_directory(LexicalPath const& path, mode_t creati
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<LexicalPath> 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<NonnullOwnPtr<File>> Directory::open(StringView filename, File::OpenMode mode) const
|
ErrorOr<NonnullOwnPtr<File>> Directory::open(StringView filename, File::OpenMode mode) const
|
||||||
{
|
{
|
||||||
auto fd = TRY(System::openat(m_directory_fd, filename, File::open_mode_to_options(mode)));
|
auto fd = TRY(System::openat(m_directory_fd, filename, File::open_mode_to_options(mode)));
|
||||||
|
@ -102,7 +95,7 @@ ErrorOr<struct stat> Directory::stat() const
|
||||||
|
|
||||||
ErrorOr<DirIterator> Directory::create_iterator() const
|
ErrorOr<DirIterator> Directory::create_iterator() const
|
||||||
{
|
{
|
||||||
return DirIterator { TRY(path()).string() };
|
return DirIterator { path().string() };
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,23 +35,23 @@ public:
|
||||||
|
|
||||||
static ErrorOr<Directory> create(LexicalPath path, CreateDirectories, mode_t creation_mode = 0755);
|
static ErrorOr<Directory> create(LexicalPath path, CreateDirectories, mode_t creation_mode = 0755);
|
||||||
static ErrorOr<Directory> create(DeprecatedString path, CreateDirectories, mode_t creation_mode = 0755);
|
static ErrorOr<Directory> create(DeprecatedString path, CreateDirectories, mode_t creation_mode = 0755);
|
||||||
static ErrorOr<Directory> adopt_fd(int fd, Optional<LexicalPath> path = {});
|
static ErrorOr<Directory> adopt_fd(int fd, LexicalPath path);
|
||||||
|
|
||||||
ErrorOr<NonnullOwnPtr<File>> open(StringView filename, File::OpenMode mode) const;
|
ErrorOr<NonnullOwnPtr<File>> open(StringView filename, File::OpenMode mode) const;
|
||||||
ErrorOr<struct stat> stat() const;
|
ErrorOr<struct stat> stat() const;
|
||||||
ErrorOr<DirIterator> create_iterator() const;
|
ErrorOr<DirIterator> create_iterator() const;
|
||||||
|
|
||||||
ErrorOr<LexicalPath> path() const;
|
LexicalPath const& path() const { return m_path; }
|
||||||
|
|
||||||
ErrorOr<void> chown(uid_t, gid_t);
|
ErrorOr<void> chown(uid_t, gid_t);
|
||||||
|
|
||||||
static ErrorOr<bool> is_valid_directory(int fd);
|
static ErrorOr<bool> is_valid_directory(int fd);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Directory(int directory_fd, Optional<LexicalPath> path);
|
Directory(int directory_fd, LexicalPath path);
|
||||||
static ErrorOr<void> ensure_directory(LexicalPath const& path, mode_t creation_mode = 0755);
|
static ErrorOr<void> ensure_directory(LexicalPath const& path, mode_t creation_mode = 0755);
|
||||||
|
|
||||||
Optional<LexicalPath> m_path;
|
LexicalPath m_path;
|
||||||
int m_directory_fd;
|
int m_directory_fd;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -62,10 +62,7 @@ template<>
|
||||||
struct Formatter<Core::Directory> : Formatter<StringView> {
|
struct Formatter<Core::Directory> : Formatter<StringView> {
|
||||||
ErrorOr<void> format(FormatBuilder& builder, Core::Directory const& directory)
|
ErrorOr<void> format(FormatBuilder& builder, Core::Directory const& directory)
|
||||||
{
|
{
|
||||||
auto path = directory.path();
|
TRY(builder.put_string(directory.path().string()));
|
||||||
if (path.is_error())
|
|
||||||
TRY(builder.put_string("<unknown>"sv));
|
|
||||||
TRY(builder.put_string(path.release_value().string()));
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue