From 4eeab4cfc8e9bc5de9704a1504f4065a4fbaa1c2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Nov 2021 11:41:48 +0100 Subject: [PATCH] LibCore: Replace Result use with ErrorOr in Core::Account --- Userland/Libraries/LibCore/Account.cpp | 16 +++++++--------- Userland/Libraries/LibCore/Account.h | 7 +++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibCore/Account.cpp b/Userland/Libraries/LibCore/Account.cpp index 106773b702..a16407e475 100644 --- a/Userland/Libraries/LibCore/Account.cpp +++ b/Userland/Libraries/LibCore/Account.cpp @@ -55,7 +55,7 @@ static Vector get_extra_gids(const passwd& pwd) return extra_gids; } -Result Account::from_passwd(const passwd& pwd, const spwd& spwd) +ErrorOr Account::from_passwd(const passwd& pwd, const spwd& spwd) { Account account(pwd, spwd, get_extra_gids(pwd)); endpwent(); @@ -107,15 +107,14 @@ Account Account::self(Read options) return Account(*pwd, *spwd, extra_gids); } -Result Account::from_name(const char* username, Read options) +ErrorOr Account::from_name(const char* username, Read options) { errno = 0; auto* pwd = getpwnam(username); if (!pwd) { if (errno == 0) - return String("No such user"); - - return String(strerror(errno)); + return Error::from_string_literal("No such user"sv); + return Error::from_errno(errno); } spwd spwd_dummy = {}; spwd_dummy.sp_namp = const_cast(username); @@ -133,15 +132,14 @@ Result Account::from_name(const char* username, Read options) return from_passwd(*pwd, *spwd); } -Result Account::from_uid(uid_t uid, Read options) +ErrorOr Account::from_uid(uid_t uid, Read options) { errno = 0; auto* pwd = getpwuid(uid); if (!pwd) { if (errno == 0) - return String("No such user"); - - return String(strerror(errno)); + return Error::from_string_literal("No such user"sv); + return Error::from_errno(errno); } spwd spwd_dummy = {}; spwd_dummy.sp_namp = pwd->pw_name; diff --git a/Userland/Libraries/LibCore/Account.h b/Userland/Libraries/LibCore/Account.h index ade6fec5c0..d60bf45088 100644 --- a/Userland/Libraries/LibCore/Account.h +++ b/Userland/Libraries/LibCore/Account.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include #include @@ -34,8 +33,8 @@ public: }; static Account self(Read options = Read::All); - static Result from_name(const char* username, Read options = Read::All); - static Result from_uid(uid_t uid, Read options = Read::All); + static ErrorOr from_name(char const* username, Read options = Read::All); + static ErrorOr from_uid(uid_t uid, Read options = Read::All); bool authenticate(SecretString const& password) const; bool login() const; @@ -68,7 +67,7 @@ public: bool sync(); private: - static Result from_passwd(const passwd&, const spwd&); + static ErrorOr from_passwd(passwd const&, spwd const&); Account(const passwd& pwd, const spwd& spwd, Vector extra_gids);