From 112fa7ec23a769ea2ca4958e08e2d73eb9489e77 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 21 Oct 2021 19:10:15 +0200 Subject: [PATCH] Fix code style #659 --- .../Engine/Platform/Linux/LinuxPlatform.cpp | 88 +++++++++++-------- 1 file changed, 52 insertions(+), 36 deletions(-) diff --git a/Source/Engine/Platform/Linux/LinuxPlatform.cpp b/Source/Engine/Platform/Linux/LinuxPlatform.cpp index 664bc8541..7e5b55228 100644 --- a/Source/Engine/Platform/Linux/LinuxPlatform.cpp +++ b/Source/Engine/Platform/Linux/LinuxPlatform.cpp @@ -2771,74 +2771,93 @@ bool LinuxPlatform::SetEnvironmentVariable(const String& name, const String& val return setenv(StringAsANSI<>(*name).Get(), StringAsANSI<>(*value).Get(), true) != 0; } -int32 LinuxProcess(const StringView& cmdLine, const StringView& workingDir, const Dictionary& environment, bool waitForEnd, bool logOutput) { +int32 LinuxProcess(const StringView& cmdLine, const StringView& workingDir, const Dictionary& environment, bool waitForEnd, bool logOutput) +{ int fildes[2]; int32 returnCode = 0; - //String cmdLine(commandLine); - if (logOutput && pipe(fildes) < 0) { - LOG(Warning, "cannot create a pipe, errno={}", errno); + if (logOutput && pipe(fildes) < 0) + { + LOG(Warning, "Failed to create a pipe, errno={}", errno); } pid_t pid = fork(); - if (pid < 0) { - LOG(Warning, "cannot fork, errno={}", errno); + if (pid < 0) + { + LOG(Warning, "Failed to fork a process, errno={}", errno); return errno; - } else if (pid == 0) { + } + else if (pid == 0) + { // child process int ret; const char* const cmd[] = { "sh", "-c", StringAnsi(cmdLine).GetText(), (char *)0 }; // we could use the execve and supply a list of variable assignments but as we would have to build // and quote the values there is hardly any benefit over using setenv() calls - if (!environment.IsEmpty()) { - for (auto& e : environment) { - setenv(StringAnsi(e.Key).GetText(), StringAnsi(e.Value).GetText(), 1); - } - } - if (workingDir.HasChars()) { - if (chdir(StringAnsi(workingDir).GetText()) != 0) { - LOG(Warning, "cannot set working dir to {}, errno={}", workingDir, errno); - } - } - if (logOutput) { + for (auto& e : environment) + { + setenv(StringAnsi(e.Key).GetText(), StringAnsi(e.Value).GetText(), 1); + } + + if (workingDir.HasChars() && chdir(StringAnsi(workingDir).GetText()) != 0) + { + LOG(Warning, "Failed to set working directory to {}, errno={}", workingDir, errno); + } + if (logOutput) + { close(fildes[0]); // close the reading end of the pipe dup2(fildes[1], STDOUT_FILENO); // redirect stdout to pipe dup2(STDOUT_FILENO, STDERR_FILENO); // redirect stderr to stdout } ret = execv("/bin/sh", (char **)cmd); - if (ret < 0) { + if (ret < 0) + { LOG(Warning, " failed, errno={}", errno); } - } else { + } + else + { // parent process LOG(Info, "{} started, pid={}", cmdLine, pid); - if (waitForEnd) { + if (waitForEnd) + { int stat_loc; - if (logOutput) { + if (logOutput) + { char lineBuffer[1024]; close(fildes[1]); // close the writing end of the pipe FILE *stdPipe = fdopen(fildes[0], "r"); - while (fgets(lineBuffer, sizeof(lineBuffer), stdPipe) != NULL) { + while (fgets(lineBuffer, sizeof(lineBuffer), stdPipe) != NULL) + { char *p = lineBuffer + strlen(lineBuffer)-1; if (*p == '\n') *p=0; Log::Logger::Write(LogType::Info, String(lineBuffer)); } } - if (waitpid(pid, &stat_loc, 0) < 0) { - LOG(Warning, "waiting for pid {} failed, errno={}", pid, errno); + if (waitpid(pid, &stat_loc, 0) < 0) + { + LOG(Warning, "Waiting for pid {} failed, errno={}", pid, errno); returnCode = errno; - } else { - if (WIFEXITED(stat_loc)) { + } + else + { + if (WIFEXITED(stat_loc)) + { int error = WEXITSTATUS(stat_loc); - if (error != 0) { - LOG(Warning, "command exited with error code={}", error); + if (error != 0) + { + LOG(Warning, "Command exited with error code={}", error); returnCode = error; } - } else if (WIFSIGNALED(stat_loc)) { - LOG(Warning, "command was killed by signal#{}", WTERMSIG(stat_loc)); + } + else if (WIFSIGNALED(stat_loc)) + { + LOG(Warning, "Command was killed by signal#{}", WTERMSIG(stat_loc)); returnCode = EPIPE; - } else if (WIFSTOPPED(stat_loc)) { - LOG(Warning, "command was stopped by signal#{}", WSTOPSIG(stat_loc)); + } + else if (WIFSTOPPED(stat_loc)) + { + LOG(Warning, "Command was stopped by signal#{}", WSTOPSIG(stat_loc)); returnCode = EPIPE; } } @@ -2848,7 +2867,6 @@ int32 LinuxProcess(const StringView& cmdLine, const StringView& workingDir, cons return returnCode; } -// this will not log output by default int32 LinuxPlatform::StartProcess(const StringView& filename, const StringView& args, const StringView& workingDir, bool hiddenWindow, bool waitForEnd) { // hiddenWindow has hardly any meaning on UNIX/Linux/OSX as the program that is called decides whether it has a GUI or not @@ -2857,13 +2875,11 @@ int32 LinuxPlatform::StartProcess(const StringView& filename, const StringView& return LinuxProcess(cmdLine, workingDir, Dictionary(), waitForEnd, false); } -// this will log output int32 LinuxPlatform::RunProcess(const StringView& cmdLine, const StringView& workingDir, bool hiddenWindow) { return LinuxProcess(cmdLine, workingDir, Dictionary(), true, true); } -// this will log output int32 LinuxPlatform::RunProcess(const StringView& cmdLine, const StringView& workingDir, const Dictionary& environment, bool hiddenWindow) { return LinuxProcess(cmdLine, workingDir, environment, true, true);