summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-08-15 14:10:26 +0200
committerAndreas Kling <kling@serenityos.org>2022-08-15 17:18:11 +0200
commitc1af2f28e32fc7d0c5528c17561b68dc29004c4d (patch)
treee65ff3f7f39b67bd5436caf800c8d963eeae3188
parent3f14582b85d0ff363d21fba79e2cc99c6be25647 (diff)
downloadserenity-c1af2f28e32fc7d0c5528c17561b68dc29004c4d.zip
LibCore: Add Directory::chown() API and use it in Core::Account
Since we already have the directory open, let's have an API to fchown() the underlying file descriptor instead of forcing clients to do another path lookup.
-rw-r--r--Userland/Libraries/LibCore/Account.cpp4
-rw-r--r--Userland/Libraries/LibCore/Directory.cpp8
-rw-r--r--Userland/Libraries/LibCore/Directory.h2
3 files changed, 12 insertions, 2 deletions
diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp
index 4135028211..e2ab5eba2e 100644
--- a/Userland/Libraries/LibCore/Account.cpp
+++ b/Userland/Libraries/LibCore/Account.cpp
@@ -152,8 +152,8 @@ bool Account::authenticate(SecretString const& password) const
ErrorOr<void> Account::create_user_temporary_directory_if_needed() const
{
auto const temporary_directory = String::formatted("/tmp/user/{}", m_uid);
- TRY(Core::Directory::create(temporary_directory, Core::Directory::CreateDirectories::Yes));
- TRY(Core::System::chown(temporary_directory, m_uid, m_gid));
+ auto directory = TRY(Core::Directory::create(temporary_directory, Core::Directory::CreateDirectories::Yes));
+ TRY(directory.chown(m_uid, m_gid));
return {};
}
diff --git a/Userland/Libraries/LibCore/Directory.cpp b/Userland/Libraries/LibCore/Directory.cpp
index e2e53d4944..f406399b94 100644
--- a/Userland/Libraries/LibCore/Directory.cpp
+++ b/Userland/Libraries/LibCore/Directory.cpp
@@ -31,6 +31,14 @@ Directory::~Directory()
MUST(System::close(m_directory_fd));
}
+ErrorOr<void> Directory::chown(uid_t uid, gid_t gid)
+{
+ if (m_directory_fd == -1)
+ return Error::from_syscall("fchown"sv, -EBADF);
+ TRY(Core::System::fchown(m_directory_fd, uid, gid));
+ return {};
+}
+
ErrorOr<bool> Directory::is_valid_directory(int fd)
{
auto stat = TRY(System::fstat(fd));
diff --git a/Userland/Libraries/LibCore/Directory.h b/Userland/Libraries/LibCore/Directory.h
index 1405477bce..a2e567b376 100644
--- a/Userland/Libraries/LibCore/Directory.h
+++ b/Userland/Libraries/LibCore/Directory.h
@@ -43,6 +43,8 @@ public:
ErrorOr<LexicalPath> path() const;
+ ErrorOr<void> chown(uid_t, gid_t);
+
static ErrorOr<bool> is_valid_directory(int fd);
private: