From f9a24eb7ebc9e4b560f2fe5b31fd20fa1cd80608 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sat, 13 May 2023 18:55:15 +0200 Subject: [PATCH] LibCore: Migrate Command from Deprecated{File,String} This gives us free error-propagation in Core::command(...) and HackStudio::ProjectBuilder::for_each_library_dependencies. The comment about "String will be in the null state" has been misleading for a long time, so it is removed. --- Userland/DevTools/HackStudio/Git/GitRepo.cpp | 4 ++-- Userland/DevTools/HackStudio/ProjectBuilder.cpp | 6 +++--- Userland/Libraries/LibCore/Command.cpp | 17 +++++++---------- Userland/Libraries/LibCore/Command.h | 9 ++++----- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Userland/DevTools/HackStudio/Git/GitRepo.cpp b/Userland/DevTools/HackStudio/Git/GitRepo.cpp index eb14f9cef1..c289434eaf 100644 --- a/Userland/DevTools/HackStudio/Git/GitRepo.cpp +++ b/Userland/DevTools/HackStudio/Git/GitRepo.cpp @@ -80,10 +80,10 @@ DeprecatedString GitRepo::command(Vector const& command_parts) DeprecatedString GitRepo::command_wrapper(Vector const& command_parts, DeprecatedString const& chdir) { - auto result = Core::command("git", command_parts, LexicalPath(chdir)); + auto const result = Core::command("git", command_parts, LexicalPath(chdir)); if (result.is_error() || result.value().exit_code != 0) return {}; - return result.value().output; + return DeprecatedString(result.value().output.bytes()); } bool GitRepo::git_is_installed() diff --git a/Userland/DevTools/HackStudio/ProjectBuilder.cpp b/Userland/DevTools/HackStudio/ProjectBuilder.cpp index a41a9006b4..ae8e3e019c 100644 --- a/Userland/DevTools/HackStudio/ProjectBuilder.cpp +++ b/Userland/DevTools/HackStudio/ProjectBuilder.cpp @@ -209,7 +209,7 @@ void ProjectBuilder::for_each_library_definition(Function const parse_library_definition(R"~~~(.+:serenity_lib[c]?\((\w+) (\w+)\).*)~~~"); - for (auto& line : res.value().output.split('\n')) { + for (auto& line : StringView(res.value().output).split_view('\n')) { RegexResult result; if (!parse_library_definition.search(line, result)) continue; @@ -234,10 +234,10 @@ void ProjectBuilder::for_each_library_dependencies(Function const parse_library_definition(R"~~~(.+:target_link_libraries\((\w+) ([\w\s]+)\).*)~~~"); - for (auto& line : res.value().output.split('\n')) { - + for (auto& line : libraries) { RegexResult result; if (!parse_library_definition.search(line, result)) continue; diff --git a/Userland/Libraries/LibCore/Command.cpp b/Userland/Libraries/LibCore/Command.cpp index ee89735247..f6ffcce0aa 100644 --- a/Userland/Libraries/LibCore/Command.cpp +++ b/Userland/Libraries/LibCore/Command.cpp @@ -7,7 +7,7 @@ #include "Command.h" #include #include -#include +#include #include #include #include @@ -72,16 +72,13 @@ ErrorOr command(DeprecatedString const& program, Vectoropen(pipe[0], Core::OpenMode::ReadOnly, Core::DeprecatedFile::ShouldCloseFileDescriptor::Yes)) { - perror("open"); - VERIFY_NOT_REACHED(); - } - return DeprecatedString::copy(result_file->read_all()); + auto read_all_from_pipe = [](int pipe[2]) -> ErrorOr { + auto result_file_or_error = Core::File::adopt_fd(pipe[0], Core::File::OpenMode::Read, Core::File::ShouldCloseFileDescriptor::Yes); + auto result_file = TRY(result_file_or_error); + return result_file->read_until_eof(); }; - auto output = read_all_from_pipe(stdout_pipe); - auto error = read_all_from_pipe(stderr_pipe); + auto output = TRY(read_all_from_pipe(stdout_pipe)); + auto error = TRY(read_all_from_pipe(stderr_pipe)); int wstatus { 0 }; waitpid(pid, &wstatus, 0); diff --git a/Userland/Libraries/LibCore/Command.h b/Userland/Libraries/LibCore/Command.h index 6fa7cf7a99..170acc11b0 100644 --- a/Userland/Libraries/LibCore/Command.h +++ b/Userland/Libraries/LibCore/Command.h @@ -6,19 +6,18 @@ #pragma once -#include +#include #include #include +#include #include namespace Core { -// If the executed command fails, the returned String will be in the null state. - struct CommandResult { int exit_code { 0 }; - DeprecatedString output; - DeprecatedString error; + ByteBuffer output; + ByteBuffer error; }; ErrorOr command(DeprecatedString const& program, Vector const& arguments, Optional chdir);