diff options
author | nae202 <nae202@yandex-team.com> | 2024-11-20 12:52:01 +0300 |
---|---|---|
committer | nae202 <nae202@yandex-team.com> | 2024-11-20 13:14:09 +0300 |
commit | 0b9d91e900b52bccd6eabd033acbb57c4ee173fc (patch) | |
tree | 6839ed69fbda4d5214eb418acc4554924bc64160 /util/system | |
parent | 878e26057d11cce46b7bc3a6c838209d4686e28b (diff) | |
download | ydb-0b9d91e900b52bccd6eabd033acbb57c4ee173fc.tar.gz |
Part of PR. Style
Часть большого ПР REVIEW:7264088
commit_hash:0f5b03fbbed0ac30f734943309e3ef5cd4d7a30e
Diffstat (limited to 'util/system')
-rw-r--r-- | util/system/dynlib.cpp | 6 | ||||
-rw-r--r-- | util/system/dynlib.h | 16 | ||||
-rw-r--r-- | util/system/error.cpp | 9 | ||||
-rw-r--r-- | util/system/filemap.h | 15 | ||||
-rw-r--r-- | util/system/fs_win.cpp | 12 | ||||
-rw-r--r-- | util/system/fs_win_ut.cpp | 3 | ||||
-rw-r--r-- | util/system/fstat.cpp | 9 | ||||
-rw-r--r-- | util/system/mlock.cpp | 18 | ||||
-rw-r--r-- | util/system/protect.cpp | 3 | ||||
-rw-r--r-- | util/system/rusage.cpp | 3 | ||||
-rw-r--r-- | util/system/sem.cpp | 6 | ||||
-rw-r--r-- | util/system/shellcommand.cpp | 33 | ||||
-rw-r--r-- | util/system/shellcommand.h | 6 | ||||
-rw-r--r-- | util/system/sysstat.cpp | 3 | ||||
-rw-r--r-- | util/system/user.cpp | 5 | ||||
-rw-r--r-- | util/system/ut/stdin_osfhandle/main.cpp | 3 |
16 files changed, 99 insertions, 51 deletions
diff --git a/util/system/dynlib.cpp b/util/system/dynlib.cpp index 9d2541c25f..55348288c8 100644 --- a/util/system/dynlib.cpp +++ b/util/system/dynlib.cpp @@ -34,10 +34,12 @@ inline TString DLLERR() { char* msg = 0; DWORD cnt = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char*)&msg, 0, nullptr); - if (!msg) + if (!msg) { return "DLLERR() unknown error"; - while (cnt && isspace(msg[cnt - 1])) + } + while (cnt && isspace(msg[cnt - 1])) { --cnt; + } TString err(msg, 0, cnt); LocalFree(msg); return err; diff --git a/util/system/dynlib.h b/util/system/dynlib.h index 66eaf4a5c1..03e4b3dff8 100644 --- a/util/system/dynlib.h +++ b/util/system/dynlib.h @@ -55,18 +55,20 @@ public: TExternalSymbol(const TExternalSymbol& es) { PLib = nullptr; DLib = nullptr; - if (es.IsDynamic()) + if (es.IsDynamic()) { Open(es.LibName().data(), es.VtblName().data()); - else if (es.IsStatic()) + } else if (es.IsStatic()) { SetSym(es.Symbol()); + } } TExternalSymbol& operator=(const TExternalSymbol& es) { if (this != &es) { Close(); - if (es.IsDynamic()) + if (es.IsDynamic()) { Open(es.LibName().data(), es.VtblName().data()); - else if (es.IsStatic()) + } else if (es.IsStatic()) { SetSym(es.Symbol()); + } } return *this; } @@ -75,8 +77,9 @@ public: } // set the symbol from dynamic source void Open(const char* lib_name, const char* vtbl_name) { - if (DLib != nullptr || PLib != nullptr) + if (DLib != nullptr || PLib != nullptr) { return; + } try { DLib = new TDynamicLibrary(); DLib->Open(lib_name); @@ -91,8 +94,9 @@ public: } // set the symbol from static source void SetSym(TLib* pl) noexcept { - if (DLib == nullptr && PLib == nullptr) + if (DLib == nullptr && PLib == nullptr) { PLib = pl; + } } void Close() noexcept { delete DLib; diff --git a/util/system/error.cpp b/util/system/error.cpp index 164921af9e..52b1bbb104 100644 --- a/util/system/error.cpp +++ b/util/system/error.cpp @@ -24,13 +24,15 @@ int LastSystemError() { #if defined(_win_) int ret = GetLastError(); - if (ret) + if (ret) { return ret; + } ret = WSAGetLastError(); - if (ret) + if (ret) { return ret; + } // when descriptors number are over maximum, errno set in this variable ret = *(_errno()); return ret; @@ -68,8 +70,9 @@ static char* Strip(char* s) { size_t len = strlen(s); const char* ptr = s; Strip(ptr, len); - if (ptr != s) + if (ptr != s) { memmove(s, ptr, len); + } s[len] = 0; return s; } diff --git a/util/system/filemap.h b/util/system/filemap.h index 25a7911715..200774593b 100644 --- a/util/system/filemap.h +++ b/util/system/filemap.h @@ -233,8 +233,9 @@ public: return Size_; } const T& GetAt(size_t pos) const { - if (pos < Size_) + if (pos < Size_) { return Ptr_[pos]; + } return Dummy(); } void SetDummy(const T& n_Dummy) { @@ -334,8 +335,9 @@ public: TMappedArray(size_t siz = 0) : TMappedAllocation(0) { - if (siz) + if (siz) { Create(siz); + } } ~TMappedArray() { Destroy(); @@ -343,18 +345,21 @@ public: T* Create(size_t siz) { Y_ASSERT(MappedSize() == 0 && Ptr() == nullptr); T* arr = (T*)Alloc((sizeof(T) * siz)); - if (!arr) + if (!arr) { return nullptr; + } Y_ASSERT(MappedSize() == sizeof(T) * siz); - for (size_t n = 0; n < siz; n++) + for (size_t n = 0; n < siz; n++) { new (&arr[n]) T(); + } return arr; } void Destroy() { T* arr = (T*)Ptr(); if (arr) { - for (size_t n = 0; n < size(); n++) + for (size_t n = 0; n < size(); n++) { arr[n].~T(); + } Dealloc(); } } diff --git a/util/system/fs_win.cpp b/util/system/fs_win.cpp index ff8a254782..65e695bee4 100644 --- a/util/system/fs_win.cpp +++ b/util/system/fs_win.cpp @@ -12,8 +12,9 @@ namespace NFsPrivate { static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) { wstr.resize(str.size()); size_t written = 0; - if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) + if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) { return nullptr; + } wstr.erase(written); static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); return (const WCHAR*)wstr.data(); @@ -64,8 +65,9 @@ namespace NFsPrivate { fad.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; ::SetFileAttributesW(wname, fad.dwFileAttributes); } - if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { return ::RemoveDirectoryW(wname) != 0; + } return ::DeleteFileW(wname) != 0; } @@ -76,8 +78,9 @@ namespace NFsPrivate { TString tName(targetName); { size_t pos; - while ((pos = tName.find('/')) != TString::npos) + while ((pos = tName.find('/')) != TString::npos) { tName.replace(pos, 1, LOCSLASH_S); + } } TUtf16String tstr; LPCWSTR wname = UTF8ToWCHAR(tName, tstr); @@ -127,8 +130,9 @@ namespace NFsPrivate { TTempBuf result; LPWSTR buf = reinterpret_cast<LPWSTR>(result.Data()); int r = GetCurrentDirectoryW(result.Size() / sizeof(WCHAR), buf); - if (r == 0) + if (r == 0) { throw TIoSystemError() << "failed to GetCurrentDirectory"; + } return WCHARToUTF8(buf, r); } diff --git a/util/system/fs_win_ut.cpp b/util/system/fs_win_ut.cpp index 5317344cef..ce9c0d86e5 100644 --- a/util/system/fs_win_ut.cpp +++ b/util/system/fs_win_ut.cpp @@ -20,8 +20,9 @@ static void Touch(const TFsPath& path) { static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) { wstr.resize(str.size()); size_t written = 0; - if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) + if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) { return nullptr; + } wstr.erase(written); static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); return (const WCHAR*)wstr.data(); diff --git a/util/system/fstat.cpp b/util/system/fstat.cpp index cfc5bed323..55d95463cf 100644 --- a/util/system/fstat.cpp +++ b/util/system/fstat.cpp @@ -44,8 +44,9 @@ static ui32 GetWinFileType(DWORD fileAttributes, ULONG reparseTag) { static ui32 GetFileMode(DWORD fileAttributes, ULONG reparseTag) { ui32 mode = 0; - if (fileAttributes == 0xFFFFFFFF) + if (fileAttributes == 0xFFFFFFFF) { return mode; + } mode |= GetWinFileType(fileAttributes, reparseTag); @@ -229,8 +230,9 @@ bool TFileStat::IsSymlink() const noexcept { i64 GetFileLength(FHANDLE fd) { #if defined(_win_) LARGE_INTEGER pos; - if (!::GetFileSizeEx(fd, &pos)) + if (!::GetFileSizeEx(fd, &pos)) { return -1L; + } return pos.QuadPart; #elif defined(_unix_) struct stat statbuf; @@ -252,8 +254,9 @@ i64 GetFileLength(const char* name) { #if defined(_win_) WIN32_FIND_DATA fData; HANDLE h = FindFirstFileA(name, &fData); - if (h == INVALID_HANDLE_VALUE) + if (h == INVALID_HANDLE_VALUE) { return -1; + } FindClose(h); return (((i64)fData.nFileSizeHigh) * (i64(MAXDWORD) + 1)) + (i64)fData.nFileSizeLow; #elif defined(_unix_) diff --git a/util/system/mlock.cpp b/util/system/mlock.cpp index 27895bbacc..3205e188a8 100644 --- a/util/system/mlock.cpp +++ b/util/system/mlock.cpp @@ -34,12 +34,15 @@ void LockMemory(const void* addr, size_t len) { #elif defined(_win_) HANDLE hndl = GetCurrentProcess(); SIZE_T min, max; - if (!GetProcessWorkingSetSize(hndl, &min, &max)) + if (!GetProcessWorkingSetSize(hndl, &min, &max)) { ythrow yexception() << LastSystemErrorText(); - if (!SetProcessWorkingSetSize(hndl, min + len, max + len)) + } + if (!SetProcessWorkingSetSize(hndl, min + len, max + len)) { ythrow yexception() << LastSystemErrorText(); - if (!VirtualLock((LPVOID)addr, len)) + } + if (!VirtualLock((LPVOID)addr, len)) { ythrow yexception() << LastSystemErrorText(); + } #endif } @@ -58,12 +61,15 @@ void UnlockMemory(const void* addr, size_t len) { #elif defined(_win_) HANDLE hndl = GetCurrentProcess(); SIZE_T min, max; - if (!GetProcessWorkingSetSize(hndl, &min, &max)) + if (!GetProcessWorkingSetSize(hndl, &min, &max)) { ythrow yexception() << LastSystemErrorText(); - if (!SetProcessWorkingSetSize(hndl, min - len, max - len)) + } + if (!SetProcessWorkingSetSize(hndl, min - len, max - len)) { ythrow yexception() << LastSystemErrorText(); - if (!VirtualUnlock((LPVOID)addr, len)) + } + if (!VirtualUnlock((LPVOID)addr, len)) { ythrow yexception() << LastSystemErrorText(); + } #endif } diff --git a/util/system/protect.cpp b/util/system/protect.cpp index 6e435e648e..271a275722 100644 --- a/util/system/protect.cpp +++ b/util/system/protect.cpp @@ -88,7 +88,8 @@ void ProtectMemory(void* addr, const size_t length, const EProtectMemory mode) { break; } DWORD oldMode = 0; - if (!VirtualProtect(addr, length, mpMode, &oldMode)) + if (!VirtualProtect(addr, length, mpMode, &oldMode)) { ythrow TSystemError() << "Memory protection failed for mode " << ModeToString(mode) << ". "; + } #endif } diff --git a/util/system/rusage.cpp b/util/system/rusage.cpp index 8bcaade7e3..2f9d861025 100644 --- a/util/system/rusage.cpp +++ b/util/system/rusage.cpp @@ -49,8 +49,9 @@ size_t TRusage::GetCurrentRSS() { struct mach_task_basic_info info; mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT; if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, - (task_info_t)&info, &infoCount) != KERN_SUCCESS) + (task_info_t)&info, &infoCount) != KERN_SUCCESS) { return (size_t)0L; /* Can't access? */ + } return (size_t)info.resident_size; #elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__) /* Linux ---------------------------------------------------- */ diff --git a/util/system/sem.cpp b/util/system/sem.cpp index 984e9d14c0..eb56da691b 100644 --- a/util/system/sem.cpp +++ b/util/system/sem.cpp @@ -70,12 +70,14 @@ namespace { size_t len = strlen(name); key = (char*)alloca(len + 1); strcpy(key, name); - if (len > MAX_PATH) + if (len > MAX_PATH) { *(key + MAX_PATH) = 0; + } char* p = key; while (*p) { - if (*p == '\\') + if (*p == '\\') { *p = '/'; + } ++p; } } 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 diff --git a/util/system/shellcommand.h b/util/system/shellcommand.h index 6c2b9e276c..530178d60c 100644 --- a/util/system/shellcommand.h +++ b/util/system/shellcommand.h @@ -119,8 +119,9 @@ public: */ inline TShellCommandOptions& SetAsync(bool async) { AsyncMode = async; - if (AsyncMode) + if (AsyncMode) { PollDelayMs = 0; + } return *this; } @@ -215,8 +216,9 @@ public: */ inline TShellCommandOptions& SetUseShell(bool useShell) { UseShell = useShell; - if (!useShell) + if (!useShell) { QuoteArguments = false; + } return *this; } diff --git a/util/system/sysstat.cpp b/util/system/sysstat.cpp index db3338b02e..3486817484 100644 --- a/util/system/sysstat.cpp +++ b/util/system/sysstat.cpp @@ -11,8 +11,9 @@ int Chmod(const char* fname, int mode) { return -1; } ui32 fAttr = ::GetFileAttributesA(fname); - if (fAttr == 0xffffffff) + if (fAttr == 0xffffffff) { return -1; + } if (mode & _S_IWRITE) { fAttr &= ~FILE_ATTRIBUTE_READONLY; } else { diff --git a/util/system/user.cpp b/util/system/user.cpp index 05439f8939..53767e8ff9 100644 --- a/util/system/user.cpp +++ b/util/system/user.cpp @@ -25,10 +25,11 @@ TString GetUsername() { DWORD len = (DWORD)Min(nameBuf.Size(), size_t(32767)); if (!GetUserNameA(nameBuf.Data(), &len)) { DWORD err = GetLastError(); - if ((err == ERROR_INSUFFICIENT_BUFFER) && (nameBuf.Size() <= 32767)) + if ((err == ERROR_INSUFFICIENT_BUFFER) && (nameBuf.Size() <= 32767)) { nameBuf = TTempBuf((size_t)len); - else + } else { ythrow TSystemError(err) << " GetUserName failed"; + } } else { return TString(nameBuf.Data(), (size_t)(len - 1)); } diff --git a/util/system/ut/stdin_osfhandle/main.cpp b/util/system/ut/stdin_osfhandle/main.cpp index fe2ea836a9..cf364c7a7e 100644 --- a/util/system/ut/stdin_osfhandle/main.cpp +++ b/util/system/ut/stdin_osfhandle/main.cpp @@ -9,7 +9,8 @@ int main() { // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/get-osfhandle // _get_osfhandle returns INVALID_HANDLE_VALUE - 1 without any sign of error if specified fd was closed. // Working with such handle will lead to future various errors. - if (handle + 1 == (unsigned long long)INVALID_HANDLE_VALUE) + if (handle + 1 == (unsigned long long)INVALID_HANDLE_VALUE) { return 1; + } return 0; } |