1
0
This repository has been archived on 2025-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
siadrive/UnitTests/UnitTestConfig.cpp
Scott E. Graves 9e61c30ba9 Upgrade SIA
2017-03-03 16:48:32 -06:00

94 lines
2.3 KiB
C++

#include "stdafx.h"
#include <SiaCommon.h>
#include "UnitTestConfig.h"
std::uint16_t Daemon::_iter = 0;
static const String SIA_PATH = L"..\\..\\..\\..\\3rd-party\\Sia-v1.1.0-windows-amd64";
Daemon::Daemon()
{
_i = _iter++;
Cleanup();
}
Daemon::~Daemon()
{
Stop();
Cleanup();
}
BOOL Daemon::DirectoryExists(const String& path)
{
DWORD dwAttrib = GetFileAttributes(path.c_str());
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
void Daemon::Cleanup()
{
BOOL failed = TRUE;
String path;
path.resize(MAX_PATH + 1);
GetFullPathName((SIA_PATH + L"\\data" + std::to_wstring(_i)).c_str(), MAX_PATH, &path[0], nullptr);
while (failed)
{
PROCESS_INFORMATION pi2;
STARTUPINFO si2;
ZeroMemory(&si2, sizeof(si2));
si2.cb = sizeof(si2);
ZeroMemory(&pi2, sizeof(pi2));
String szCmdline = L"cmd.exe /c del /s /q \"" + path + L"\"";
CreateProcess(nullptr, &szCmdline[0], nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si2, &pi2);
if (!(failed = (pi2.hProcess == 0)))
{
WaitForSingleObject(pi2.hProcess, INFINITE);
CloseHandle(pi2.hProcess);
CloseHandle(pi2.hThread);
}
ZeroMemory(&si2, sizeof(si2));
si2.cb = sizeof(si2);
ZeroMemory(&pi2, sizeof(pi2));
szCmdline = L"cmd.exe /c rd /s /q \"" + path + L"\"";
CreateProcess(nullptr, &szCmdline[0], nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si2, &pi2);
if (!(failed = (pi2.hProcess == 0)))
{
WaitForSingleObject(pi2.hProcess, INFINITE);
CloseHandle(pi2.hProcess);
CloseHandle(pi2.hThread);
}
Sleep(100);
}
}
void Daemon::Start()
{
String path;
path.resize(MAX_PATH + 1);
GetFullPathName((SIA_PATH + L"\\").c_str(), MAX_PATH, &path[0], nullptr);
String exec;
exec.resize(MAX_PATH + 1);
GetFullPathName((SIA_PATH + L"\\siad.exe").c_str(), MAX_PATH, &exec[0], nullptr);
String szCmdline = L" -d .\\data" + std::to_wstring(_i) + L" --api-addr " + String(TEST_SERVER_HOST) + L":" + std::to_wstring(TEST_SERVER_PORT) + L" --no-bootstrap";
CreateProcess(exec.c_str(), &szCmdline[0], nullptr, nullptr, FALSE, 0, nullptr, path.c_str(), &si, &pi);
}
void Daemon::Stop()
{
if (pi.hProcess)
{
TerminateProcess(pi.hProcess, -1);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
}
}