aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/shellcommand.cpp
diff options
context:
space:
mode:
authorAlexander Smirnov <alex@ydb.tech>2024-11-20 11:14:58 +0000
committerAlexander Smirnov <alex@ydb.tech>2024-11-20 11:14:58 +0000
commit31773f157bf8164364649b5f470f52dece0a4317 (patch)
tree33d0f7eef45303ab68cf08ab381ce5e5e36c5240 /util/system/shellcommand.cpp
parent2c7938962d8689e175574fc1e817c05049f27905 (diff)
parenteff600952d5dfe17942f38f510a8ac2b203bb3a5 (diff)
downloadydb-31773f157bf8164364649b5f470f52dece0a4317.tar.gz
Merge branch 'rightlib' into mergelibs-241120-1113
Diffstat (limited to 'util/system/shellcommand.cpp')
-rw-r--r--util/system/shellcommand.cpp33
1 files changed, 22 insertions, 11 deletions
diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp
index e4bfb165bc..51d91039bf 100644
--- a/util/system/shellcommand.cpp
+++ b/util/system/shellcommand.cpp
@@ -89,16 +89,18 @@ namespace {
constexpr static size_t MAX_COMMAND_LINE = 32 * 1024;
std::wstring GetWString(const char* astring) {
- if (!astring)
+ if (!astring) {
return std::wstring();
+ }
std::string str(astring);
return std::wstring(str.begin(), str.end());
}
std::string GetAString(const wchar_t* wstring) {
- if (!wstring)
+ if (!wstring) {
return std::string();
+ }
std::wstring str(wstring);
return std::string(str.begin(), str.end());
@@ -130,8 +132,9 @@ public:
bool Close() noexcept {
bool ok = true;
- if (Fd_ != INVALID_REALPIPEHANDLE)
+ if (Fd_ != INVALID_REALPIPEHANDLE) {
ok = CloseHandle(Fd_);
+ }
Fd_ = INVALID_REALPIPEHANDLE;
return ok;
}
@@ -156,22 +159,25 @@ public:
ssize_t Read(void* buffer, size_t byteCount) const noexcept {
DWORD doneBytes;
- if (!ReadFile(Fd_, buffer, byteCount, &doneBytes, nullptr))
+ if (!ReadFile(Fd_, buffer, byteCount, &doneBytes, nullptr)) {
return -1;
+ }
return doneBytes;
}
ssize_t Write(const void* buffer, size_t byteCount) const noexcept {
DWORD doneBytes;
- if (!WriteFile(Fd_, buffer, byteCount, &doneBytes, nullptr))
+ if (!WriteFile(Fd_, buffer, byteCount, &doneBytes, nullptr)) {
return -1;
+ }
return doneBytes;
}
static void Pipe(TRealPipeHandle& reader, TRealPipeHandle& writer, EOpenMode mode) {
(void)mode;
REALPIPEHANDLE fds[2];
- if (!CreatePipe(&fds[0], &fds[1], nullptr /* handles are not inherited */, 0))
+ if (!CreatePipe(&fds[0], &fds[1], nullptr /* handles are not inherited */, 0)) {
ythrow TFileError() << "failed to create a pipe";
+ }
TRealPipeHandle(fds[0]).Swap(reader);
TRealPipeHandle(fds[1]).Swap(writer);
}
@@ -497,8 +503,9 @@ void TShellCommand::TImpl::StartProcess(TShellCommand::TImpl::TPipes& pipes) {
}
}
if (InputMode != TShellCommandOptions::HANDLE_INHERIT) {
- if (!SetHandleInformation(pipes.InputPipeFd[0], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
+ if (!SetHandleInformation(pipes.InputPipeFd[0], HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)) {
ythrow TSystemError() << "cannot set handle info";
+ }
}
// A sockets do not work as std streams for some reason
@@ -881,8 +888,9 @@ void TShellCommand::TImpl::Communicate(TProcessInfo* pi) {
streamThreads.emplace_back(new TThread(&TImpl::WriteStream, &pumps[2]));
}
- for (auto& threadHolder : streamThreads)
+ for (auto& threadHolder : streamThreads) {
threadHolder->Start();
+ }
#else
TBuffer buffer(DATA_BUFFER_SIZE);
TBuffer inputBuffer(DATA_BUFFER_SIZE);
@@ -1042,8 +1050,9 @@ void TShellCommand::TImpl::Communicate(TProcessInfo* pi) {
if (!GetExitCodeProcess(pi->Parent->Pid, &exitCode)) {
ythrow yexception() << "GetExitCodeProcess: " << LastSystemErrorText();
}
- if (exitCode == 0)
+ if (exitCode == 0) {
cleanExit = true;
+ }
processExitCode = static_cast<int>(exitCode);
DBG(Cerr << "exit code: " << exitCode << Endl);
}
@@ -1056,11 +1065,13 @@ void TShellCommand::TImpl::Communicate(TProcessInfo* pi) {
}
#if defined(_win_)
- for (auto& threadHolder : streamThreads)
+ for (auto& threadHolder : streamThreads) {
threadHolder->Join();
+ }
for (const auto pump : pumps) {
- if (!pump.InternalError.empty())
+ if (!pump.InternalError.empty()) {
throw yexception() << pump.InternalError;
+ }
}
#else
// Now let's read remaining stdout/stderr