From 77723091696863e471cf8c27361fc203616a7b3e Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Thu, 23 Dec 2021 09:41:00 +0100 Subject: LibCore+chown: Return ErrorOr> for getgrnam and getpwnam This patch returns an empty Optional<...> instead of an Error for Core::System::getgrname and Core::System::getpwnam if we can't find a matching group or user entry. It also updates the 'chown' utility to support this new behavior. --- Userland/Libraries/LibCore/System.cpp | 8 ++++---- Userland/Libraries/LibCore/System.h | 4 ++-- Userland/Utilities/chown.cpp | 12 ++++++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 19ad7c9d26..ef46ff3d6e 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -393,7 +393,7 @@ ErrorOr chown(StringView pathname, uid_t uid, gid_t gid) #endif } -ErrorOr getpwnam(StringView name) +ErrorOr> getpwnam(StringView name) { ::setpwent(); if (errno) @@ -408,10 +408,10 @@ ErrorOr getpwnam(StringView name) if (errno) return Error::from_syscall("getpwnam"sv, -errno); else - return Error::from_string_literal("getpwnam: Unknown username"sv); + return Optional {}; } -ErrorOr getgrnam(StringView name) +ErrorOr> getgrnam(StringView name) { ::setgrent(); if (errno) @@ -426,7 +426,7 @@ ErrorOr getgrnam(StringView name) if (errno) return Error::from_syscall("getgrnam"sv, -errno); else - return Error::from_string_literal("getgrnam: Unknown username"sv); + return Optional {}; } ErrorOr clock_settime(clockid_t clock_id, struct timespec* ts) diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index e86a795469..b27df73ff5 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -68,8 +68,8 @@ ErrorOr tcgetattr(int fd); ErrorOr tcsetattr(int fd, int optional_actions, struct termios const&); ErrorOr chmod(StringView pathname, mode_t mode); ErrorOr chown(StringView pathname, uid_t uid, gid_t gid); -ErrorOr getpwnam(StringView name); -ErrorOr getgrnam(StringView name); +ErrorOr> getpwnam(StringView name); +ErrorOr> getgrnam(StringView name); ErrorOr clock_settime(clockid_t clock_id, struct timespec* ts); ErrorOr posix_spawnp(StringView const path, posix_spawn_file_actions_t* const file_actions, posix_spawnattr_t* const attr, char* const arguments[], char* const envp[]); ErrorOr waitpid(pid_t waitee, int* wstatus, int options); diff --git a/Userland/Utilities/chown.cpp b/Userland/Utilities/chown.cpp index 08d1c04bbd..85d667baff 100644 --- a/Userland/Utilities/chown.cpp +++ b/Userland/Utilities/chown.cpp @@ -42,7 +42,11 @@ ErrorOr serenity_main(Main::Arguments arguments) new_uid = number.value(); } else { auto passwd = TRY(Core::System::getpwnam(parts[0])); - new_uid = passwd.pw_uid; + if (!passwd.has_value()) { + warnln("Unknown user '{}'", parts[0]); + return 1; + } + new_uid = passwd->pw_uid; } if (parts.size() == 2) { @@ -51,7 +55,11 @@ ErrorOr serenity_main(Main::Arguments arguments) new_gid = number.value(); } else { auto group = TRY(Core::System::getgrnam(parts[1])); - new_gid = group.gr_gid; + if (!group.has_value()) { + warnln("Unknown group '{}'", parts[1]); + return 1; + } + new_gid = group->gr_gid; } } -- cgit v1.2.3