LibCore: Use StringView instead of char * in Account

This commit is contained in:
Lucas CHOLLET 2022-06-10 20:06:06 +02:00 committed by Sam Atkins
parent 0396b6da82
commit 507cb411c2
Notes: sideshowbarker 2024-07-17 06:34:55 +09:00
10 changed files with 33 additions and 36 deletions

View file

@ -98,9 +98,9 @@ ErrorOr<Account> Account::self([[maybe_unused]] Read options)
return Account(*pwd, spwd, extra_gids);
}
ErrorOr<Account> Account::from_name(char const* username, [[maybe_unused]] Read options)
ErrorOr<Account> Account::from_name(StringView username, [[maybe_unused]] Read options)
{
auto pwd = TRY(Core::System::getpwnam({ username, strlen(username) }));
auto pwd = TRY(Core::System::getpwnam(username));
if (!pwd.has_value())
return Error::from_string_literal("No such user");

View file

@ -32,11 +32,9 @@ public:
PasswdOnly
};
// FIXME: Convert the methods below to take StringViews instead.
static String parse_path_with_uid(StringView general_path, Optional<uid_t> force_uid = {});
static ErrorOr<Account> self(Read options = Read::All);
static ErrorOr<Account> from_name(char const* username, Read options = Read::All);
static ErrorOr<Account> from_name(StringView username, Read options = Read::All);
static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All);
bool authenticate(SecretString const& password) const;
@ -51,11 +49,11 @@ public:
// You must call sync to apply changes.
void set_password(SecretString const& password);
void set_password_enabled(bool enabled);
void set_home_directory(char const* home_directory) { m_home_directory = home_directory; }
void set_home_directory(StringView home_directory) { m_home_directory = home_directory; }
void set_uid(uid_t uid) { m_uid = uid; }
void set_gid(gid_t gid) { m_gid = gid; }
void set_shell(char const* shell) { m_shell = shell; }
void set_gecos(char const* gecos) { m_gecos = gecos; }
void set_shell(StringView shell) { m_shell = shell; }
void set_gecos(StringView gecos) { m_gecos = gecos; }
void delete_password();
// A null password means that this account was missing from /etc/shadow.

View file

@ -80,7 +80,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto fail_message = "Can't log in: invalid username or password."sv;
auto account = Core::Account::from_name(username.characters());
auto account = Core::Account::from_name(username);
if (account.is_error()) {
window->set_fail_message(fail_message);
dbgln("failed graphical login for user {}: {}", username, account.error());
@ -99,13 +99,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
login(account.value(), *window);
};
char const* auto_login = nullptr;
StringView auto_login;
Core::ArgsParser args_parser;
args_parser.add_option(auto_login, "automatically log in with no prompt", "auto-login", 'a', "username");
args_parser.parse(arguments);
if (!auto_login) {
if (auto_login.is_empty()) {
window->show();
} else {
auto account = Core::Account::from_name(auto_login);

View file

@ -299,7 +299,7 @@ Service::Service(Core::ConfigFile const& config, StringView name)
m_user = config.read_entry(name, "User");
if (!m_user.is_null()) {
auto result = Core::Account::from_name(m_user.characters(), Core::Account::Read::PasswdOnly);
auto result = Core::Account::from_name(m_user, Core::Account::Read::PasswdOnly);
if (result.is_error())
warnln("Failed to resolve user {}: {}", m_user, result.error());
else

View file

@ -47,8 +47,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
print_account_gids(account);
}
for (auto username : usernames) {
auto result = Core::Account::from_name(username.characters(), Core::Account::Read::PasswdOnly);
for (auto const& username : usernames) {
auto result = Core::Account::from_name(username, Core::Account::Read::PasswdOnly);
if (result.is_error()) {
warnln("{} '{}'", result.error(), username);
continue;

View file

@ -53,7 +53,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (auto user_id = user_str.to_uint(); user_id.has_value())
account = TRY(Core::Account::from_uid(user_id.value(), Core::Account::Read::PasswdOnly));
else
account = TRY(Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly));
account = TRY(Core::Account::from_name(user_str, Core::Account::Read::PasswdOnly));
} else {
account = TRY(Core::Account::self(Core::Account::Read::PasswdOnly));
}

View file

@ -46,7 +46,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// target_account is the account we are changing the password of.
auto target_account = TRY(!username.is_empty()
? Core::Account::from_name(username.characters())
? Core::Account::from_name(username)
: Core::Account::from_uid(current_uid));
setpwent();

View file

@ -21,7 +21,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (!TRY(Core::System::isatty(STDIN_FILENO)))
return Error::from_string_literal("Standard input is not a terminal");
char const* user = nullptr;
StringView user;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(user, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No);
@ -30,7 +30,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (geteuid() != 0)
return Error::from_string_literal("Not running as root :(");
auto account = TRY(user ? Core::Account::from_name(user) : Core::Account::from_uid(0));
auto account = TRY(user.is_empty() ? Core::Account::from_uid(0) : Core::Account::from_name(user));
TRY(Core::System::pledge("stdio tty exec id"));
@ -51,7 +51,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::setenv("HOME"sv, account.home_directory(), true));
execl(account.shell().characters(), account.shell().characters(), nullptr);
perror("execl");
TRY(Core::System::exec(account.shell(), Array<StringView, 1> { account.shell().view() }, Core::System::SearchInPath::No));
return 1;
}

View file

@ -33,7 +33,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::unveil("/etc/", "rwc"));
TRY(Core::System::unveil("/bin/rm", "x"));
char const* username = nullptr;
StringView username;
bool remove_home = false;
Core::ArgsParser args_parser;

View file

@ -30,11 +30,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
int gid = 0;
bool lock = false;
bool unlock = false;
char const* new_home_directory = nullptr;
StringView new_home_directory;
bool move_home = false;
char const* shell = nullptr;
char const* gecos = nullptr;
char const* username = nullptr;
StringView shell;
StringView gecos;
StringView username;
auto args_parser = Core::ArgsParser();
args_parser.set_general_help("Modify a user account");
@ -62,7 +62,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (move_home) {
TRY(Core::System::unveil(target_account.home_directory(), "c"sv));
TRY(Core::System::unveil({ new_home_directory, strlen(new_home_directory) }, "wc"sv));
TRY(Core::System::unveil(new_home_directory, "wc"sv));
}
unveil(nullptr, nullptr);
@ -98,11 +98,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
target_account.set_password_enabled(true);
}
if (new_home_directory) {
if (!new_home_directory.is_empty()) {
if (move_home) {
int rc = rename(target_account.home_directory().characters(), new_home_directory);
if (rc < 0) {
if (errno == EXDEV) {
auto maybe_error = Core::System::rename(target_account.home_directory(), new_home_directory);
if (maybe_error.is_error()) {
if (maybe_error.error().code() == EXDEV) {
auto result = Core::File::copy_file_or_directory(
new_home_directory, target_account.home_directory().characters(),
Core::File::RecursionMode::Allowed,
@ -113,11 +113,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), static_cast<Error const&>(result.error()));
return 1;
}
rc = unlink(target_account.home_directory().characters());
if (rc < 0)
warnln("usermod: unlink {} : {}", target_account.home_directory().characters(), strerror(errno));
maybe_error = Core::System::unlink(target_account.home_directory());
if (maybe_error.is_error())
warnln("usermod: unlink {} : {}", target_account.home_directory(), maybe_error.error().code());
} else {
warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), strerror(errno));
warnln("usermod: could not move directory {} : {}", target_account.home_directory(), maybe_error.error().code());
}
}
}
@ -125,11 +125,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
target_account.set_home_directory(new_home_directory);
}
if (shell) {
if (!shell.is_empty()) {
target_account.set_shell(shell);
}
if (gecos) {
if (!gecos.is_empty()) {
target_account.set_gecos(gecos);
}