mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:37:44 +00:00
LibCore: Add an optional permissions mask to Directory::create()
Being able to pass a mask is the one feature `mkdir()` had that this didn't. Adding it here means everyone can use the nicer Directory API.
This commit is contained in:
parent
6967d37678
commit
72893b4f9e
2 changed files with 10 additions and 10 deletions
|
@ -45,28 +45,28 @@ ErrorOr<Directory> Directory::adopt_fd(int fd, Optional<LexicalPath> path)
|
||||||
return Directory { fd, move(path) };
|
return Directory { fd, move(path) };
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<Directory> Directory::create(String path, CreateDirectories create_directories)
|
ErrorOr<Directory> Directory::create(String path, CreateDirectories create_directories, mode_t creation_mode)
|
||||||
{
|
{
|
||||||
return create(LexicalPath { move(path) }, create_directories);
|
return create(LexicalPath { move(path) }, create_directories, creation_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<Directory> Directory::create(LexicalPath path, CreateDirectories create_directories)
|
ErrorOr<Directory> Directory::create(LexicalPath path, CreateDirectories create_directories, mode_t creation_mode)
|
||||||
{
|
{
|
||||||
if (create_directories == CreateDirectories::Yes)
|
if (create_directories == CreateDirectories::Yes)
|
||||||
TRY(ensure_directory(path));
|
TRY(ensure_directory(path, creation_mode));
|
||||||
// FIXME: doesn't work on Linux probably
|
// FIXME: doesn't work on Linux probably
|
||||||
auto fd = TRY(System::open(path.string(), O_CLOEXEC));
|
auto fd = TRY(System::open(path.string(), O_CLOEXEC));
|
||||||
return adopt_fd(fd, move(path));
|
return adopt_fd(fd, move(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<void> Directory::ensure_directory(LexicalPath const& path)
|
ErrorOr<void> Directory::ensure_directory(LexicalPath const& path, mode_t creation_mode)
|
||||||
{
|
{
|
||||||
if (path.basename() == "/" || path.basename() == ".")
|
if (path.basename() == "/" || path.basename() == ".")
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
TRY(ensure_directory(path.parent()));
|
TRY(ensure_directory(path.parent(), creation_mode));
|
||||||
|
|
||||||
auto return_value = System::mkdir(path.string(), 0755);
|
auto return_value = System::mkdir(path.string(), creation_mode);
|
||||||
// We don't care if the directory already exists.
|
// We don't care if the directory already exists.
|
||||||
if (return_value.is_error() && return_value.error().code() != EEXIST)
|
if (return_value.is_error() && return_value.error().code() != EEXIST)
|
||||||
return return_value;
|
return return_value;
|
||||||
|
|
|
@ -33,8 +33,8 @@ public:
|
||||||
Yes,
|
Yes,
|
||||||
};
|
};
|
||||||
|
|
||||||
static ErrorOr<Directory> create(LexicalPath path, CreateDirectories);
|
static ErrorOr<Directory> create(LexicalPath path, CreateDirectories, mode_t creation_mode = 0755);
|
||||||
static ErrorOr<Directory> create(String path, CreateDirectories);
|
static ErrorOr<Directory> create(String path, CreateDirectories, mode_t creation_mode = 0755);
|
||||||
static ErrorOr<Directory> adopt_fd(int fd, Optional<LexicalPath> path = {});
|
static ErrorOr<Directory> adopt_fd(int fd, Optional<LexicalPath> path = {});
|
||||||
|
|
||||||
ErrorOr<NonnullOwnPtr<Stream::File>> open(StringView filename, Stream::OpenMode mode) const;
|
ErrorOr<NonnullOwnPtr<Stream::File>> open(StringView filename, Stream::OpenMode mode) const;
|
||||||
|
@ -47,7 +47,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Directory(int directory_fd, Optional<LexicalPath> path);
|
Directory(int directory_fd, Optional<LexicalPath> path);
|
||||||
static ErrorOr<void> ensure_directory(LexicalPath const& path);
|
static ErrorOr<void> ensure_directory(LexicalPath const& path, mode_t creation_mode = 0755);
|
||||||
|
|
||||||
Optional<LexicalPath> m_path;
|
Optional<LexicalPath> m_path;
|
||||||
int m_directory_fd;
|
int m_directory_fd;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue