LibCore: Call the mkstemp function in a slightly prettier way

This removes a FIXME, although the new version isn't less char-pointery.
This commit is contained in:
Arda Cinar 2023-01-10 17:30:04 +03:00 committed by Tim Flynn
parent 8ccd6a31dc
commit 3f99a8734e
Notes: sideshowbarker 2024-07-17 06:00:02 +09:00
2 changed files with 12 additions and 14 deletions

View file

@ -295,22 +295,18 @@ ErrorOr<void> Account::sync()
auto new_shadow_file_content = TRY(generate_shadow_file());
#endif
// FIXME: mkstemp taking Span<char> makes this code entirely un-AKable.
// Make this code less char-pointery.
char new_passwd_name[] = "/etc/passwd.XXXXXX";
size_t new_passwd_name_length = strlen(new_passwd_name);
char new_passwd_file[] = "/etc/passwd.XXXXXX";
#ifndef AK_OS_BSD_GENERIC
char new_shadow_name[] = "/etc/shadow.XXXXXX";
size_t new_shadow_name_length = strlen(new_shadow_name);
char new_shadow_file[] = "/etc/shadow.XXXXXX";
#endif
{
auto new_passwd_fd = TRY(Core::System::mkstemp({ new_passwd_name, new_passwd_name_length }));
auto new_passwd_fd = TRY(Core::System::mkstemp(new_passwd_file));
ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
TRY(Core::System::fchmod(new_passwd_fd, 0644));
#ifndef AK_OS_BSD_GENERIC
auto new_shadow_fd = TRY(Core::System::mkstemp({ new_shadow_name, new_shadow_name_length }));
auto new_shadow_fd = TRY(Core::System::mkstemp(new_shadow_file));
ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
TRY(Core::System::fchmod(new_shadow_fd, 0600));
#endif
@ -324,9 +320,11 @@ ErrorOr<void> Account::sync()
#endif
}
TRY(Core::System::rename({ new_passwd_name, new_passwd_name_length }, "/etc/passwd"sv));
auto new_passwd_file_view = StringView { new_passwd_file, sizeof(new_passwd_file) };
TRY(Core::System::rename(new_passwd_file_view, "/etc/passwd"sv));
#ifndef AK_OS_BSD_GENERIC
TRY(Core::System::rename({ new_shadow_name, new_shadow_name_length }, "/etc/shadow"sv));
auto new_shadow_file_view = StringView { new_shadow_file, sizeof(new_shadow_file) };
TRY(Core::System::rename(new_shadow_file_view, "/etc/shadow"sv));
#endif
return {};

View file

@ -51,11 +51,11 @@ ErrorOr<void> Group::sync()
auto new_group_file_content = TRY(generate_group_file());
char new_group_name[] = "/etc/group.XXXXXX";
size_t new_group_name_length = strlen(new_group_name);
char new_group_file[] = "/etc/group.XXXXXX";
auto new_group_file_view = StringView { new_group_file, sizeof(new_group_file) };
{
auto new_group_fd = TRY(Core::System::mkstemp({ new_group_name, new_group_name_length }));
auto new_group_fd = TRY(Core::System::mkstemp(new_group_file));
ScopeGuard new_group_fd_guard([new_group_fd] { close(new_group_fd); });
TRY(Core::System::fchmod(new_group_fd, 0664));
@ -63,7 +63,7 @@ ErrorOr<void> Group::sync()
VERIFY(static_cast<size_t>(nwritten) == new_group_file_content.length());
}
TRY(Core::System::rename({ new_group_name, new_group_name_length }, "/etc/group"sv));
TRY(Core::System::rename(new_group_file_view, "/etc/group"sv));
return {};
}