mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 02:12:09 -05:00
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.
This commit is contained in:
parent
ab324c1dae
commit
7772309169
3 changed files with 16 additions and 8 deletions
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue