diff options
author | Kenneth Myhra <kennethmyhra@gmail.com> | 2021-12-23 09:41:00 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-12-28 23:23:54 +0100 |
commit | 77723091696863e471cf8c27361fc203616a7b3e (patch) | |
tree | cecd591945b88d351c5d409690cc65ccc51d783f /Userland/Utilities | |
parent | ab324c1dae86cdaf084b92ca88854cc7a15640eb (diff) | |
download | serenity-77723091696863e471cf8c27361fc203616a7b3e.zip |
LibCore+chown: Return ErrorOr<Optional<...>> 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.
Diffstat (limited to 'Userland/Utilities')
-rw-r--r-- | Userland/Utilities/chown.cpp | 12 |
1 files changed, 10 insertions, 2 deletions
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<int> 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<int> 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; } } |