HackStudio: Use Core::System APIs where possible

This commit is contained in:
Sam Atkins 2024-01-11 16:55:49 +00:00 committed by Andrew Kaster
parent 16543a1918
commit 336b8ed80b
Notes: sideshowbarker 2024-07-17 22:09:47 +09:00
4 changed files with 23 additions and 15 deletions

View file

@ -480,11 +480,12 @@ void Editor::set_document(GUI::TextDocument& doc)
// Otherwise, if the file has already been opened before in some Editor instance, it should exist in the LanguageServer's
// FileDB, and the LanguageServer should already have its up-to-date content.
// So it's OK to just pass an fd here (rather than the TextDocument's content).
int fd = open(code_document.file_path().characters(), O_RDONLY | O_NOCTTY);
if (fd < 0) {
perror("open");
auto maybe_fd = Core::System::open(code_document.file_path(), O_RDONLY | O_NOCTTY);
if (maybe_fd.is_error()) {
warnln("Failed to open `{}`: {}", code_document.file_path(), maybe_fd.release_error());
return;
}
auto fd = maybe_fd.release_value();
m_language_client->open_file(code_document.file_path(), fd);
close(fd);
} else {

View file

@ -239,8 +239,8 @@ void HackStudioWidget::open_project(ByteString const& root_path)
{
if (warn_unsaved_changes("There are unsaved changes, do you want to save before closing current project?") == ContinueDecision::No)
return;
if (chdir(root_path.characters()) < 0) {
perror("chdir");
if (auto result = Core::System::chdir(root_path); result.is_error()) {
warnln("Failed to open project: {}", result.release_error());
exit(1);
}
if (m_project) {
@ -1024,7 +1024,7 @@ ErrorOr<NonnullRefPtr<GUI::Action>> HackStudioWidget::create_debug_action()
// The debugger calls wait() on the debuggee, so the TerminalWrapper can't do that.
auto ptm_res = m_terminal_wrapper->setup_master_pseudoterminal(TerminalWrapper::WaitForChildOnExit::No);
if (ptm_res.is_error()) {
perror("setup_master_pseudoterminal");
warnln("Failed to set up master pseudoterminal: {}", ptm_res.release_error());
return;
}

View file

@ -29,12 +29,12 @@ void ConnectionFromClient::die()
void ConnectionFromClient::greet(ByteString const& project_root)
{
m_filedb.set_project_root(project_root);
if (unveil(project_root.characters(), "r") < 0) {
perror("unveil");
if (auto result = Core::System::unveil(project_root, "r"sv); result.is_error()) {
warnln("Failed to unveil `{}`: {}", project_root, result.error());
exit(1);
}
if (unveil(nullptr, nullptr) < 0) {
perror("unveil");
if (auto result = Core::System::unveil(nullptr, nullptr); result.is_error()) {
warnln("Failed to lock the veil: {}", result.error());
exit(1);
}
}

View file

@ -93,18 +93,25 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
static bool make_is_available()
{
pid_t pid;
char const* argv[] = { "make", "--version", nullptr };
posix_spawn_file_actions_t action;
posix_spawn_file_actions_init(&action);
posix_spawn_file_actions_addopen(&action, STDOUT_FILENO, "/dev/null", O_WRONLY, 0);
if ((errno = posix_spawnp(&pid, "make", &action, nullptr, const_cast<char**>(argv), environ))) {
perror("posix_spawn");
auto maybe_pid = Core::System::posix_spawnp("make"sv, &action, nullptr, const_cast<char**>(argv), environ);
if (maybe_pid.is_error()) {
warnln("Failed to posix_spawn make: {}", maybe_pid.release_error());
return false;
}
int wstatus;
waitpid(pid, &wstatus, 0);
pid_t pid = maybe_pid.release_value();
auto waitpid_result = Core::System::waitpid(pid, 0);
if (waitpid_result.is_error()) {
warnln("Failed to waitpid for make: {}", waitpid_result.release_error());
return false;
}
int wstatus = waitpid_result.value().status;
posix_spawn_file_actions_destroy(&action);
return WEXITSTATUS(wstatus) == 0;
}