summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorKenneth Myhra <kennethmyhra@gmail.com>2021-12-23 09:41:00 +0100
committerAndreas Kling <kling@serenityos.org>2021-12-28 23:23:54 +0100
commit77723091696863e471cf8c27361fc203616a7b3e (patch)
treececd591945b88d351c5d409690cc65ccc51d783f /Userland
parentab324c1dae86cdaf084b92ca88854cc7a15640eb (diff)
downloadserenity-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')
-rw-r--r--Userland/Libraries/LibCore/System.cpp8
-rw-r--r--Userland/Libraries/LibCore/System.h4
-rw-r--r--Userland/Utilities/chown.cpp12
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<void> chown(StringView pathname, uid_t uid, gid_t gid)
#endif
}
-ErrorOr<struct passwd> getpwnam(StringView name)
+ErrorOr<Optional<struct passwd>> getpwnam(StringView name)
{
::setpwent();
if (errno)
@@ -408,10 +408,10 @@ ErrorOr<struct passwd> getpwnam(StringView name)
if (errno)
return Error::from_syscall("getpwnam"sv, -errno);
else
- return Error::from_string_literal("getpwnam: Unknown username"sv);
+ return Optional<struct passwd> {};
}
-ErrorOr<struct group> getgrnam(StringView name)
+ErrorOr<Optional<struct group>> getgrnam(StringView name)
{
::setgrent();
if (errno)
@@ -426,7 +426,7 @@ ErrorOr<struct group> getgrnam(StringView name)
if (errno)
return Error::from_syscall("getgrnam"sv, -errno);
else
- return Error::from_string_literal("getgrnam: Unknown username"sv);
+ return Optional<struct group> {};
}
ErrorOr<void> 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<struct termios> tcgetattr(int fd);
ErrorOr<void> tcsetattr(int fd, int optional_actions, struct termios const&);
ErrorOr<void> chmod(StringView pathname, mode_t mode);
ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid);
-ErrorOr<struct passwd> getpwnam(StringView name);
-ErrorOr<struct group> getgrnam(StringView name);
+ErrorOr<Optional<struct passwd>> getpwnam(StringView name);
+ErrorOr<Optional<struct group>> getgrnam(StringView name);
ErrorOr<void> clock_settime(clockid_t clock_id, struct timespec* ts);
ErrorOr<pid_t> 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<pid_t> 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<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;
}
}