From d13f6bb870cdda71257f665367be8ef9fca86255 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Tue, 17 May 2022 11:47:01 +0200 Subject: Apply clang-format. --- src/envmodule.cpp | 207 +++++++++++++++++++++--------------------------------- 1 file changed, 80 insertions(+), 127 deletions(-) (limited to 'src/envmodule.cpp') diff --git a/src/envmodule.cpp b/src/envmodule.cpp index 5b1e7621..2bba4c41 100644 --- a/src/envmodule.cpp +++ b/src/envmodule.cpp @@ -1,7 +1,7 @@ #include "envmodule.h" #include "env.h" -#include #include +#include namespace env { @@ -13,14 +13,13 @@ using namespace MOBase; // while adding to the startup time constexpr bool UseMD5 = false; - Module::Module(QString path, std::size_t fileSize) - : m_path(std::move(path)), m_fileSize(fileSize) + : m_path(std::move(path)), m_fileSize(fileSize) { const auto fi = getFileInfo(); - m_version = getVersion(fi.ffi); - m_timestamp = getTimestamp(fi.ffi); + m_version = getVersion(fi.ffi); + m_timestamp = getTimestamp(fi.ffi); m_versionString = fi.fileDescription; if (UseMD5) { @@ -113,7 +112,7 @@ Module::FileInfo Module::getFileInfo() const const auto wspath = m_path.toStdWString(); // getting version info size - DWORD dummy = 0; + DWORD dummy = 0; const DWORD size = GetFileVersionInfoSizeW(wspath.c_str(), &dummy); if (size == 0) { @@ -130,9 +129,8 @@ Module::FileInfo Module::getFileInfo() const return {}; } - log::debug( - "GetFileVersionInfoSizeW() failed on '{}', {}", - m_path, formatSystemMessage(e)); + log::debug("GetFileVersionInfoSizeW() failed on '{}', {}", m_path, + formatSystemMessage(e)); return {}; } @@ -143,9 +141,8 @@ Module::FileInfo Module::getFileInfo() const if (!GetFileVersionInfoW(wspath.c_str(), 0, size, buffer.get())) { const auto e = GetLastError(); - log::error( - "GetFileVersionInfoW() failed on '{}', {}", - m_path, formatSystemMessage(e)); + log::error("GetFileVersionInfoW() failed on '{}', {}", m_path, + formatSystemMessage(e)); return {}; } @@ -154,7 +151,7 @@ Module::FileInfo Module::getFileInfo() const // set of strings FileInfo fi; - fi.ffi = getFixedFileInfo(buffer.get()); + fi.ffi = getFixedFileInfo(buffer.get()); fi.fileDescription = getFileDescription(buffer.get()); return fi; @@ -162,7 +159,7 @@ Module::FileInfo Module::getFileInfo() const VS_FIXEDFILEINFO Module::getFixedFileInfo(std::byte* buffer) const { - void* valuePointer = nullptr; + void* valuePointer = nullptr; unsigned int valueSize = 0; // the fixed version info is in the root @@ -177,9 +174,7 @@ VS_FIXEDFILEINFO Module::getFixedFileInfo(std::byte* buffer) const // signature is always 0xfeef04bd if (fi->dwSignature != 0xfeef04bd) { - log::error( - "bad file info signature {:#x} for '{}'", - fi->dwSignature, m_path); + log::error("bad file info signature {:#x} for '{}'", fi->dwSignature, m_path); return {}; } @@ -195,12 +190,12 @@ QString Module::getFileDescription(std::byte* buffer) const WORD wCodePage; }; - void* valuePointer = nullptr; + void* valuePointer = nullptr; unsigned int valueSize = 0; // getting list of available languages - auto ret = VerQueryValueW( - buffer, L"\\VarFileInfo\\Translation", &valuePointer, &valueSize); + auto ret = + VerQueryValueW(buffer, L"\\VarFileInfo\\Translation", &valuePointer, &valueSize); if (!ret || !valuePointer || valueSize == 0) { log::error("VerQueryValueW() for translations failed on '{}'", m_path); @@ -217,11 +212,11 @@ QString Module::getFileDescription(std::byte* buffer) const const auto* lcp = static_cast(valuePointer); const auto subBlock = QString("\\StringFileInfo\\%1%2\\FileVersion") - .arg(lcp->wLanguage, 4, 16, QChar('0')) - .arg(lcp->wCodePage, 4, 16, QChar('0')); + .arg(lcp->wLanguage, 4, 16, QChar('0')) + .arg(lcp->wCodePage, 4, 16, QChar('0')); - ret = VerQueryValueW( - buffer, subBlock.toStdWString().c_str(), &valuePointer, &valueSize); + ret = VerQueryValueW(buffer, subBlock.toStdWString().c_str(), &valuePointer, + &valueSize); if (!ret || !valuePointer || valueSize == 0) { // not an error, no file version @@ -229,8 +224,7 @@ QString Module::getFileDescription(std::byte* buffer) const } // valueSize includes the null terminator - return QString::fromWCharArray( - static_cast(valuePointer), valueSize - 1); + return QString::fromWCharArray(static_cast(valuePointer), valueSize - 1); } QString Module::getVersion(const VS_FIXEDFILEINFO& fi) const @@ -239,17 +233,16 @@ QString Module::getVersion(const VS_FIXEDFILEINFO& fi) const return {}; } - const DWORD major = (fi.dwFileVersionMS >> 16 ) & 0xffff; - const DWORD minor = (fi.dwFileVersionMS >> 0 ) & 0xffff; - const DWORD maintenance = (fi.dwFileVersionLS >> 16 ) & 0xffff; - const DWORD build = (fi.dwFileVersionLS >> 0 ) & 0xffff; + const DWORD major = (fi.dwFileVersionMS >> 16) & 0xffff; + const DWORD minor = (fi.dwFileVersionMS >> 0) & 0xffff; + const DWORD maintenance = (fi.dwFileVersionLS >> 16) & 0xffff; + const DWORD build = (fi.dwFileVersionLS >> 0) & 0xffff; if (major == 0 && minor == 0 && maintenance == 0 && build == 0) { return {}; } - return QString("%1.%2.%3.%4") - .arg(major).arg(minor).arg(maintenance).arg(build); + return QString("%1.%2.%3.%4").arg(major).arg(minor).arg(maintenance).arg(build); } QDateTime Module::getTimestamp(const VS_FIXEDFILEINFO& fi) const @@ -261,16 +254,15 @@ QDateTime Module::getTimestamp(const VS_FIXEDFILEINFO& fi) const // time on the file // opening the file - HandlePtr h(CreateFileW( - m_path.toStdWString().c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)); + HandlePtr h(CreateFileW(m_path.toStdWString().c_str(), GENERIC_READ, + FILE_SHARE_READ, nullptr, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, 0)); if (h.get() == INVALID_HANDLE_VALUE) { const auto e = GetLastError(); - log::debug( - "can't open file '{}' for timestamp, {}", - m_path, formatSystemMessage(e)); + log::debug("can't open file '{}' for timestamp, {}", m_path, + formatSystemMessage(e)); return {}; } @@ -279,50 +271,42 @@ QDateTime Module::getTimestamp(const VS_FIXEDFILEINFO& fi) const if (!GetFileTime(h.get(), &ft, nullptr, nullptr)) { const auto e = GetLastError(); - log::error( - "can't get file time for '{}', {}", - m_path, formatSystemMessage(e)); + log::error("can't get file time for '{}', {}", m_path, formatSystemMessage(e)); return {}; } } else { // use the time from the file info ft.dwHighDateTime = fi.dwFileDateMS; - ft.dwLowDateTime = fi.dwFileDateLS; + ft.dwLowDateTime = fi.dwFileDateLS; } - // converting to SYSTEMTIME SYSTEMTIME utc = {}; if (!FileTimeToSystemTime(&ft, &utc)) { log::error( - "FileTimeToSystemTime() failed on timestamp high={:#x} low={:#x} for '{}'", - ft.dwHighDateTime, ft.dwLowDateTime, m_path); + "FileTimeToSystemTime() failed on timestamp high={:#x} low={:#x} for '{}'", + ft.dwHighDateTime, ft.dwLowDateTime, m_path); return {}; } - return QDateTime( - QDate(utc.wYear, utc.wMonth, utc.wDay), - QTime(utc.wHour, utc.wMinute, utc.wSecond, utc.wMilliseconds)); + return QDateTime(QDate(utc.wYear, utc.wMonth, utc.wDay), + QTime(utc.wHour, utc.wMinute, utc.wSecond, utc.wMilliseconds)); } bool Module::interesting() const { static const auto windir = []() -> QString { - try - { - return QDir::toNativeSeparators( - MOBase::getKnownFolder(FOLDERID_Windows).path()) + "\\"; - } - catch(...) - { + try { + return QDir::toNativeSeparators(MOBase::getKnownFolder(FOLDERID_Windows).path()) + + "\\"; + } catch (...) { return "c:\\windows\\"; } }(); - if (m_path.startsWith(windir, Qt::CaseInsensitive)) { return false; } @@ -330,15 +314,10 @@ bool Module::interesting() const return true; } - QString Module::getMD5() const { static const std::set ignore = { - "\\windows\\", - "\\program files\\", - "\\program files (x86)\\", - "\\programdata\\" - }; + "\\windows\\", "\\program files\\", "\\program files (x86)\\", "\\programdata\\"}; // don't calculate md5 for system files, it's not really relevant and // it takes a while @@ -366,21 +345,13 @@ QString Module::getMD5() const return hash.result().toHex(); } +Process::Process() : Process(0, 0, {}) {} -Process::Process() - : Process(0, 0, {}) -{ -} - -Process::Process(HANDLE h) - : Process(::GetProcessId(h), 0, {}) -{ -} +Process::Process(HANDLE h) : Process(::GetProcessId(h), 0, {}) {} Process::Process(DWORD pid, DWORD ppid, QString name) - : m_pid(pid), m_ppid(ppid), m_name(std::move(name)) -{ -} + : m_pid(pid), m_ppid(ppid), m_name(std::move(name)) +{} bool Process::isValid() const { @@ -413,9 +384,9 @@ const QString& Process::name() const HandlePtr Process::openHandleForWait() const { const auto rights = - PROCESS_QUERY_LIMITED_INFORMATION | // exit code, image name, etc. - SYNCHRONIZE | // wait functions - PROCESS_SET_QUOTA | PROCESS_TERMINATE; // add to job + PROCESS_QUERY_LIMITED_INFORMATION | // exit code, image name, etc. + SYNCHRONIZE | // wait functions + PROCESS_SET_QUOTA | PROCESS_TERMINATE; // add to job // don't log errors, failure can happen if the process doesn't exist return HandlePtr(OpenProcess(rights, FALSE, m_pid)); @@ -426,8 +397,7 @@ HandlePtr Process::openHandleForWait() const // bool Process::canAccess() const { - HandlePtr h(OpenProcess( - PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, m_pid)); + HandlePtr h(OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, m_pid)); if (!h) { const auto e = GetLastError(); @@ -454,25 +424,22 @@ const std::vector& Process::children() const return m_children; } - std::vector getLoadedModules() { - HandlePtr snapshot(CreateToolhelp32Snapshot( - TH32CS_SNAPMODULE32 | TH32CS_SNAPMODULE, GetCurrentProcessId())); + HandlePtr snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPMODULE32 | TH32CS_SNAPMODULE, + GetCurrentProcessId())); - if (snapshot.get() == INVALID_HANDLE_VALUE) - { + if (snapshot.get() == INVALID_HANDLE_VALUE) { const auto e = GetLastError(); log::error("CreateToolhelp32Snapshot() failed, {}", formatSystemMessage(e)); return {}; } MODULEENTRY32 me = {}; - me.dwSize = sizeof(me); + me.dwSize = sizeof(me); // first module, this shouldn't fail because there's at least the executable - if (!Module32First(snapshot.get(), &me)) - { + if (!Module32First(snapshot.get(), &me)) { const auto e = GetLastError(); log::error("Module32First() failed, {}", formatSystemMessage(e)); return {}; @@ -480,8 +447,7 @@ std::vector getLoadedModules() std::vector v; - for (;;) - { + for (;;) { const auto path = QString::fromWCharArray(me.szExePath); if (!path.isEmpty()) { v.push_back(Module(path, me.modBaseSize)); @@ -503,26 +469,24 @@ std::vector getLoadedModules() // sorting by display name std::sort(v.begin(), v.end(), [](auto&& a, auto&& b) { return (a.displayPath().compare(b.displayPath(), Qt::CaseInsensitive) < 0); - }); + }); return v; } - template void forEachRunningProcess(F&& f) { HandlePtr snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); - if (snapshot.get() == INVALID_HANDLE_VALUE) - { + if (snapshot.get() == INVALID_HANDLE_VALUE) { const auto e = GetLastError(); log::error("CreateToolhelp32Snapshot() failed, {}", formatSystemMessage(e)); return; } PROCESSENTRY32 entry = {}; - entry.dwSize = sizeof(entry); + entry.dwSize = sizeof(entry); // first process, this shouldn't fail because there's at least one process // running @@ -532,15 +496,13 @@ void forEachRunningProcess(F&& f) return; } - for (;;) - { + for (;;) { if (!f(entry)) { break; } // next process - if (!Process32Next(snapshot.get(), &entry)) - { + if (!Process32Next(snapshot.get(), &entry)) { const auto e = GetLastError(); // no more processes is not an error @@ -557,10 +519,8 @@ std::vector getRunningProcesses() std::vector v; forEachRunningProcess([&](auto&& entry) { - v.push_back(Process( - entry.th32ProcessID, - entry.th32ParentProcessID, - QString::fromStdWString(entry.szExeFile))); + v.push_back(Process(entry.th32ProcessID, entry.th32ParentProcessID, + QString::fromStdWString(entry.szExeFile))); return true; }); @@ -580,13 +540,12 @@ void findChildren(Process& parent, const std::vector& processes) } } - Process getProcessTreeFromProcess(HANDLE h) { Process root; const auto parentPID = ::GetProcessId(h); - const auto v = getRunningProcesses(); + const auto v = getRunningProcesses(); for (auto&& p : v) { if (p.pid() == parentPID) { @@ -600,7 +559,6 @@ Process getProcessTreeFromProcess(HANDLE h) return root; } - std::vector processesInJob(HANDLE h) { const int MaxTries = 5; @@ -609,18 +567,17 @@ std::vector processesInJob(HANDLE h) DWORD maxIds = 100; // for logging - DWORD lastCount=0, lastAssigned=0; + DWORD lastCount = 0, lastAssigned = 0; - - for (int tries=0; tries buffer(std::malloc(bufferSize)); auto* ids = static_cast(buffer.get()); - const auto r = QueryInformationJobObject( - h, JobObjectBasicProcessIdList, ids, bufferSize, nullptr); + const auto r = QueryInformationJobObject(h, JobObjectBasicProcessIdList, ids, + bufferSize, nullptr); if (!r) { const auto e = GetLastError(); @@ -632,7 +589,7 @@ std::vector processesInJob(HANDLE h) if (ids->NumberOfProcessIdsInList >= ids->NumberOfAssignedProcesses) { std::vector v; - for (DWORD i=0; iNumberOfProcessIdsInList; ++i) { + for (DWORD i = 0; i < ids->NumberOfProcessIdsInList; ++i) { v.push_back(ids->ProcessIdList[i]); } @@ -643,18 +600,17 @@ std::vector processesInJob(HANDLE h) maxIds *= 2; // for logging - lastCount = ids->NumberOfProcessIdsInList; + lastCount = ids->NumberOfProcessIdsInList; lastAssigned = ids->NumberOfAssignedProcesses; } - log::error( - "failed to get processes in job, can't get a buffer large enough, " - "{}/{} ids", lastCount, lastAssigned); + log::error("failed to get processes in job, can't get a buffer large enough, " + "{}/{} ids", + lastCount, lastAssigned); return {}; } - void findChildProcesses(Process& parent, std::vector& processes) { // find all processes that are direct children of `parent` @@ -687,10 +643,8 @@ Process getProcessTreeFromJob(HANDLE h) forEachRunningProcess([&](auto&& entry) { for (auto&& id : ids) { if (entry.th32ProcessID == id) { - ps.push_back(Process( - entry.th32ProcessID, - entry.th32ParentProcessID, - QString::fromStdWString(entry.szExeFile))); + ps.push_back(Process(entry.th32ProcessID, entry.th32ParentProcessID, + QString::fromStdWString(entry.szExeFile))); break; } @@ -705,7 +659,7 @@ Process getProcessTreeFromJob(HANDLE h) // getting processes whose parent is not in the list for (auto&& possibleRoot : ps) { const auto ppid = possibleRoot.ppid(); - bool found = false; + bool found = false; for (auto&& p : ps) { if (p.pid() == ppid) { @@ -756,8 +710,8 @@ bool isJobHandle(HANDLE h) { JOBOBJECT_BASIC_ACCOUNTING_INFORMATION info = {}; - const auto r = ::QueryInformationJobObject( - h, JobObjectBasicAccountingInformation, &info, sizeof(info), nullptr); + const auto r = ::QueryInformationJobObject(h, JobObjectBasicAccountingInformation, + &info, sizeof(info), nullptr); return r; } @@ -792,7 +746,7 @@ QString getProcessName(HANDLE process) return badName; } - const DWORD bufferSize = MAX_PATH; + const DWORD bufferSize = MAX_PATH; wchar_t buffer[bufferSize + 1] = {}; const auto realSize = ::GetProcessImageFileNameW(process, buffer, bufferSize); @@ -829,10 +783,9 @@ DWORD getProcessParentID(DWORD pid) return ppid; } - DWORD getProcessParentID(HANDLE handle) { return getProcessParentID(GetProcessId(handle)); } -} // namespace +} // namespace env -- cgit v1.3.1