Skip to content

Commit

Permalink
use LastWin32Error, ToHRESULT, SystemError
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienfl-orc committed Nov 9, 2020
1 parent 72cbf65 commit 1dba352
Show file tree
Hide file tree
Showing 31 changed files with 80 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/OrcCommand/ExtractData_Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ HRESULT Main::CheckConfiguration()
const auto workingDir = GetWorkingDirectoryApi(ec);
if (ec)
{
Log::Error("Failed GetWorkingDirectory (code: {:#x})", ec.value());
return HRESULT_FROM_WIN32(ec.value());
Log::Error("Failed GetWorkingDirectory (code: {:#x})", ec);
return ToHRESULT(ec);
}

config.output.Configure(OutputSpec::Kind::Directory, workingDir.c_str());
Expand Down
2 changes: 1 addition & 1 deletion src/OrcCommand/GetSamples_Run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ HRESULT Main::LoadAutoRuns(TaskTracker& tk, LPCWSTR szTempDir)

if (command->ExitCode() != 0)
{
Log::Error(L"Autoruns failed (exitcode: {:#x})", command->ExitCode());
Log::Error(L"Autoruns failed (exit status: {:#x})", command->ExitCode());
return E_FAIL;
}

Expand Down
13 changes: 5 additions & 8 deletions src/OrcCommand/GetSectors_Run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ std::wstring Main::getBootDiskName()
ret = GetWindowsDirectoryW(systemDirectory, MAX_PATH);
if (ret == 0 || ret >= MAX_PATH)
{
Log::Error("Failed GetWindowsDirectory (code: {:#x})", HRESULT_FROM_WIN32(GetLastError()));
Log::Error("Failed GetWindowsDirectory (code: {:#x})", LastWin32Error());
return {};
}

Expand All @@ -113,7 +113,7 @@ std::wstring Main::getBootDiskName()
CreateFileW(volume.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
if (hVolume == INVALID_HANDLE_VALUE)
{
Log::Error(L"Failed CreateFileW to open volume '{}' (code: {:#x})", volume, HRESULT_FROM_WIN32(GetLastError()));
Log::Error(L"Failed CreateFileW to open volume '{}' (code: {:#x})", volume, LastWin32Error());
return {};
}

Expand Down Expand Up @@ -142,10 +142,7 @@ std::wstring Main::getBootDiskName()
}
else
{
Log::Error(
L"Failed to retrieve volume disk extents for '{}' (code: {:#x})",
volume,
HRESULT_FROM_WIN32(lastError));
Log::Error(L"Failed to retrieve volume disk extents for '{}' (code: {:#x})", volume, Win32Error(lastError));
return {};
}
}
Expand Down Expand Up @@ -307,7 +304,7 @@ HRESULT Main::DiskChunk::read()
Log::Error(
L"Failed QueryPerformanceCounter before reading at offset {} (code: {:#x})",
m_ulChunkOffset,
HRESULT_FROM_WIN32(GetLastError()));
LastWin32Error());
getReadingTime = false;
}

Expand Down Expand Up @@ -354,7 +351,7 @@ HRESULT Main::DiskChunk::read()
Log::Error(
L"Failed QueryPerformanceCounter before reading at offset {} (code: {:#x})",
m_ulChunkOffset,
HRESULT_FROM_WIN32(GetLastError()));
LastWin32Error());
getReadingTime = false;
}

Expand Down
5 changes: 2 additions & 3 deletions src/OrcCommand/NTFSInfo_Run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ void Main::SecurityDescriptorInformation(
if (!ConvertSecurityDescriptorToStringSecurityDescriptor(
&pEntry->SecurityDescriptor, SDDL_REVISION_1, InfoFlags, &szSDDL, NULL))
{
Log::Debug("Failed to convert security descriptor to SDDL (code: {:#x}))", HRESULT_FROM_WIN32(GetLastError()));
Log::Debug("Failed to convert security descriptor to SDDL (code: {:#x}))", LastWin32Error());
output.WriteNothing();
if (szSDDL != nullptr)
{
Expand All @@ -386,8 +386,7 @@ void Main::SecurityDescriptorInformation(
if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
szSDDL, SDDL_REVISION_1, &pNormalisedSecDescr, &ulNormalisedSecDescrLength))
{
Log::Debug(
"Failed to convert SDDL to security descriptor (code: {:#x})", HRESULT_FROM_WIN32(GetLastError()));
Log::Debug("Failed to convert SDDL to security descriptor (code: {:#x})", LastWin32Error());
output.WriteNothing();
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/OrcCommand/NTFSUtil_Run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ HANDLE OpenVolume(const std::wstring& volume)

if (hVolume == INVALID_HANDLE_VALUE)
{
Log::Debug(L"Failed to open volume: '{}' (code: {:#x})", volume, HRESULT_FROM_WIN32(GetLastError()));
Log::Debug(L"Failed to open volume: '{}' (code: {:#x})", volume, LastWin32Error());
return INVALID_HANDLE_VALUE;
}

Expand Down
4 changes: 2 additions & 2 deletions src/OrcCommand/RegInfo_Run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,14 @@ HRESULT Main::Run()
dwGLE = RegFlushKey(HKEY_LOCAL_MACHINE);
if (dwGLE != ERROR_SUCCESS)
{
Log::Error("Flushing HKEY_LOCAL_MACHINE failed (code: {:#x})", HRESULT_FROM_WIN32(dwGLE));
Log::Error("Flushing HKEY_LOCAL_MACHINE failed (code: {:#x})", Win32Error(dwGLE));
}

Log::Debug(L"Flushing HKEY_USERS");
dwGLE = RegFlushKey(HKEY_USERS);
if (dwGLE != ERROR_SUCCESS)
{
Log::Error(L"Flushing HKEY_USERS failed (code: {:#x})", HRESULT_FROM_WIN32(dwGLE));
Log::Error(L"Flushing HKEY_USERS failed (code: {:#x})", Win32Error(dwGLE));
}

hr = config.m_HiveQuery.BuildStreamList();
Expand Down
2 changes: 1 addition & 1 deletion src/OrcCommand/UtilitiesMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bool UtilitiesMain::IsProcessParent(LPCWSTR szImageName)
if (FAILED(pNtDll->NtQueryInformationProcess(
GetCurrentProcess(), ProcessBasicInformation, &pbi, (ULONG)sizeof(PROCESS_BASIC_INFORMATION), nullptr)))
{
Log::Error(L"Failed NtQueryInformationProcess (code: {:#x})", GetLastError());
Log::Error(L"Failed NtQueryInformationProcess (code: {:#x})", LastWin32Error());
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/OrcCommand/WolfExecution_Execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ HRESULT WolfExecution::CreateArchiveAgent()
L"Failed creating archive '{}': {} (code: {:#x})",
archive->Keyword(),
archive->Description(),
archive->GetHResult());
SystemError(archive->GetHResult()));

m_journal.Print(
archive->Keyword(),
operation,
L"Failed creating archive '{}': {} (code: {:#x})",
archive->GetFileName(),
archive->Description(),
archive->GetHResult());
SystemError(archive->GetHResult()));

return;
}
Expand Down
12 changes: 5 additions & 7 deletions src/OrcCommand/WolfLauncher_Run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ GetLocalOutputFileInformations(const Orc::Command::Wolf::WolfExecution& exec, Fi
if (!GetFileAttributesExW(exec.GetOutputFullPath().c_str(), GetFileExInfoStandard, &data))
{
Log::Warn(
L"Failed to obtain file attributes of '{}' (code: {:#x})",
exec.GetOutputFullPath(),
HRESULT_FROM_WIN32(GetLastError()));
L"Failed to obtain file attributes of '{}' (code: {:#x})", exec.GetOutputFullPath(), LastWin32Error());
return S_OK;
}

Expand Down Expand Up @@ -144,15 +142,15 @@ HRESULT Main::InitializeUpload(const OutputSpec::Upload& uploadspec)
L"UPLOAD: Operation for '{}' failed: '{}' (code: {:#x})",
upload->Source(),
upload->Description(),
upload->GetHResult());
SystemError(upload->GetHResult()));

m_journal.Print(
upload->Keyword(),
operation,
L"Failed upload for '{}': {} (code: {:#x})",
upload->Source(),
upload->Description(),
upload->GetHResult());
SystemError(upload->GetHResult()));

return;
}
Expand Down Expand Up @@ -446,8 +444,8 @@ HRESULT Main::Run_Execute()
m_logging.fileSink()->Open(config.Log.Path, ec);
if (ec)
{
Log::Error("Failed to create log stream (code: {:#x})", ec.value());
return ec.value();
Log::Error("Failed to create log stream (code: {:#x})", ec);
return ToHRESULT(ec);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/OrcCommand/WolfTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "StdAfx.h"

#include "WolfTask.h"
#include "Utils/Result.h"

using namespace Orc;
using namespace Orc::Command::Wolf;
Expand Down Expand Up @@ -141,12 +142,14 @@ HRESULT WolfTask::ApplyNotification(
m_Status = Failed;
break;
case CommandNotification::ProcessAbnormalTermination:
m_dwExitCode = notification->GetExitCode();

Log::Critical(
L"{} (pid: {}): Abnormal termination (code: {:#x})",
m_command,
m_dwPID == 0 ? notification->GetProcessID() : m_dwPID,
notification->GetExitCode());
m_dwExitCode = notification->GetExitCode();
Win32Error(m_dwExitCode));

m_Status = Failed;
break;
case CommandNotification::ProcessMemoryLimit:
Expand Down
2 changes: 1 addition & 1 deletion src/OrcLib/ArchiveUpdateCallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ STDMETHODIMP ArchiveUpdateCallback::SetOperationResult(Int32 operationResult)
if (operationResult != NArchive::NUpdate::NOperationResult::kOK)
{
HRESULT hr = E_FAIL;
Log::Error(L"Failed operation on: '{}' (code: {:#x})", m_Items[m_curIndex].NameInArchive, operationResult);
Log::Error(L"Failed operation on: '{}' (result: {:#x})", m_Items[m_curIndex].NameInArchive, operationResult);
if (item.m_archivedCallback)
{
item.m_archivedCallback(hr);
Expand Down
11 changes: 4 additions & 7 deletions src/OrcLib/Authenticode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Authenticode::Authenticode()
L"MY");
if (m_hMachineStore == NULL)
{
Log::Warn("Failed to open cert store (code: {:#x})", HRESULT_FROM_WIN32(GetLastError()));
Log::Warn("Failed to open cert store (code: {:#x})", LastWin32Error());
return;
}
}
Expand Down Expand Up @@ -520,8 +520,7 @@ HRESULT Authenticode::VerifyAnySignatureWithCatalogs(LPCWSTR szFileName, const P

if (!CryptCATAdminReleaseCatalogContext(m_hContext, hCatalog, 0))
{
Log::Warn(
"Failed CryptCATAdminReleaseCatalogContext (code: {:#x})", HRESULT_FROM_WIN32(GetLastError()));
Log::Warn("Failed CryptCATAdminReleaseCatalogContext (code: {:#x})", LastWin32Error());
}

return hr;
Expand All @@ -541,8 +540,7 @@ HRESULT Authenticode::VerifyAnySignatureWithCatalogs(LPCWSTR szFileName, const P

if (!CryptCATAdminReleaseCatalogContext(m_hContext, hCatalog, 0))
{
Log::Warn(
"Failed CryptCATAdminReleaseCatalogContext (code: {:#x})", HRESULT_FROM_WIN32(GetLastError()));
Log::Warn("Failed CryptCATAdminReleaseCatalogContext (code: {:#x})", LastWin32Error());
}

return hr;
Expand All @@ -562,8 +560,7 @@ HRESULT Authenticode::VerifyAnySignatureWithCatalogs(LPCWSTR szFileName, const P

if (!CryptCATAdminReleaseCatalogContext(m_hContext, hCatalog, 0))
{
Log::Warn(
"Failed CryptCATAdminReleaseCatalogContext (code: {:#x})", HRESULT_FROM_WIN32(GetLastError()));
Log::Warn("Failed CryptCATAdminReleaseCatalogContext (code: {:#x})", LastWin32Error());
}

return hr;
Expand Down
2 changes: 1 addition & 1 deletion src/OrcLib/BITSAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ HRESULT BITSAgent::Initialize()

if ((dwRet = WNetAddConnection2(&nr, szPass, szUser, CONNECT_TEMPORARY)) != NO_ERROR)
{
Log::Error(L"Failed to add a connection to {} (code: {:#x})", szUNC, HRESULT_FROM_WIN32(dwRet));
Log::Error(L"Failed to add a connection to {} (code: {:#x})", szUNC, Win32Error(dwRet));
}
else
{
Expand Down
16 changes: 6 additions & 10 deletions src/OrcLib/CommandAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,9 @@ DWORD WINAPI CommandAgent::JobObjectNotificationRoutine(__in LPVOID lpParameter)
if (err != ERROR_INVALID_HANDLE && err != ERROR_ABANDONED_WAIT_0)
{
// Error invalid handle "somewhat" expected. Log the others
Log::Error(L"Failed GetQueuedCompletionStatus (code: {:#x})", HRESULT_FROM_WIN32(err));
Log::Error(L"Failed GetQueuedCompletionStatus (code: {:#x})", Win32Error(err));
}

return 0;
}
}
Expand Down Expand Up @@ -693,10 +694,7 @@ void CommandAgent::run()
m_hCompletionPort = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)this, 0);
if (m_hCompletionPort == NULL)
{
Log::Error(
L"Failed to create IO Completion port for job object (code: {:#x})",
m_Keyword,
HRESULT_FROM_WIN32(GetLastError()));
Log::Error(L"Failed to create IO Completion port for job object (code: {:#x})", m_Keyword, LastWin32Error());
done();
return;
}
Expand Down Expand Up @@ -790,7 +788,7 @@ void CommandAgent::run()

if (!m_Job.IsValid())
{
Log::Error(L"Failed to create '{}' job object (code: {:#x})", m_Keyword, HRESULT_FROM_WIN32(GetLastError()));
Log::Error(L"Failed to create '{}' job object (code: {:#x})", m_Keyword, LastWin32Error());
done();
return;
}
Expand Down Expand Up @@ -835,9 +833,7 @@ void CommandAgent::run()
sizeof(JOBOBJECT_END_OF_JOB_TIME_INFORMATION),
NULL))
{
Log::Error(
"Failed to retrieve end of job behavior on job object (code: {:#x})",
HRESULT_FROM_WIN32(GetLastError()));
Log::Error("Failed to retrieve end of job behavior on job object (code: {:#x})", LastWin32Error());
done();
return;
}
Expand All @@ -854,7 +850,7 @@ void CommandAgent::run()
{
Log::Warn(
L"Failed to set end of job behavior (to post at end of job) on job object (code: {:#x})",
HRESULT_FROM_WIN32(GetLastError()));
LastWin32Error());
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/OrcLib/CommandExecute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,7 @@ HRESULT CommandExecute::CompleteExecution(ArchiveMessage::ITarget* pCab)
if (!DeleteFile(action->Fullpath().c_str()))
{
Log::Error(
L"Failed to delete file '{}' (code: {:#x})",
action->Fullpath(),
HRESULT_FROM_WIN32(GetLastError()));
L"Failed to delete file '{}' (code: {:#x})", action->Fullpath(), LastWin32Error());
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/OrcLib/CopyFileAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ HRESULT CopyFileAgent::Initialize()

if ((dwRet = WNetAddConnection2(&nr, szPass, szUser, CONNECT_TEMPORARY)) != NO_ERROR)
{
Log::Error(L"Failed to add a connection to '{}' (code: {:#x})", szUNC, HRESULT_FROM_WIN32(dwRet));
Log::Error(L"Failed to add a connection to '{}' (code: {:#x})", szUNC, Win32Error(dwRet));
}
else
{
Expand Down
10 changes: 5 additions & 5 deletions src/OrcLib/DebugAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ HRESULT DebugAgent::CreateMinidump(DEBUG_EVENT& debug_event)
HANDLE hThread = OpenThread(THREAD_GET_CONTEXT, FALSE, debug_event.dwThreadId);
if (hThread == NULL)
{
hr = HRESULT_FROM_WIN32(GetLastError());
Log::Error(L"Failed OpenThread (tid: {}, code: {:#x})", debug_event.dwThreadId, hr);
return hr;
const auto ec = LastWin32Error();
Log::Error(L"Failed OpenThread (tid: {}, code: {:#x})", debug_event.dwThreadId, ec);
return ToHRESULT(ec);
}

CONTEXT threadContext;
Expand Down Expand Up @@ -187,7 +187,7 @@ void DebugAgent::DebugLoop()
{
Log::Debug(
"Exception occured in child process (code: {:#x})",
m_Event.u.Exception.ExceptionRecord.ExceptionCode);
SystemError(m_Event.u.Exception.ExceptionRecord.ExceptionCode));
if (SUCCEEDED(hr = CreateMinidump(m_Event)))
{
Log::Debug(L"Dump file created: '{}'", m_Dumps.back());
Expand Down Expand Up @@ -219,7 +219,7 @@ void DebugAgent::DebugLoop()
Log::Debug("DebugEvent: UNLOAD_DLL_DEBUG_EVENT");
break;
default:
Log::Debug("DebugEvent: Unknown Debug event code: {:#x}", m_Event.dwDebugEventCode);
Log::Debug("DebugEvent: Unknown Debug event code: {}", m_Event.dwDebugEventCode);
}

// Let the debuggee continue
Expand Down
9 changes: 5 additions & 4 deletions src/OrcLib/DiskExtent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ HRESULT CDiskExtent::Open(DWORD dwShareMode, DWORD dwCreationDisposition, DWORD
if (liLength.LowPart == INVALID_FILE_SIZE && ((lastError = GetLastError()) != NO_ERROR))
{
Log::Warn(
L"CDiskExtent: Unable to determine disk size (GetFileSize code: {:#x}, IOCTL_DISK_GET_LENGTH_INFO "
L"code: {:#x})",
HRESULT_FROM_WIN32(lastError),
ioctlLastError);
L"CDiskExtent: Unable to determine disk size with GetFileSize (code: {:#x})", Win32Error(lastError));

Log::Warn(
L"CDiskExtent: Unable to determine disk size with IOCTL_DISK_GET_LENGTH_INFO (code: {:#x})",
Win32Error(ioctlLastError));
m_Length = 0;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/OrcLib/EmbeddedResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Archive.h"

#include "Log/Log.h"
#include "Utils/Result.h"

#include <regex>

Expand Down
Loading

0 comments on commit 1dba352

Please sign in to comment.