1
0
This commit is contained in:
Scott E. Graves
2017-03-15 19:41:59 -05:00
parent 836c6838fe
commit cba53aef8b
6 changed files with 86 additions and 60 deletions

View File

@@ -5,18 +5,25 @@ using namespace Sia::Api;
CEventSystem CEventSystem::EventSystem;
CEventSystem::CEventSystem() :
#ifdef _WIN32
_stopEvent(INVALID_HANDLE_VALUE)
#endif
{
}
CEventSystem::~CEventSystem()
{
Stop();
#ifdef _WIN32
::CloseHandle(_stopEvent);
#else
#endif
}
void CEventSystem::ProcessEvents()
{
#ifdef _WIN32
while (::WaitForSingleObject(_stopEvent, 10) == WAIT_TIMEOUT)
{
CEventPtr eventData;
@@ -44,6 +51,9 @@ void CEventSystem::ProcessEvents()
}
} while (eventData);
}
#else
#endif
}
void CEventSystem::NotifyEvent(CEventPtr eventData)
@@ -67,7 +77,11 @@ void CEventSystem::Start()
{
if (!_processThread)
{
#ifdef _WIN32
_stopEvent = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
#else
#endif
_processThread.reset(new std::thread([this]() {ProcessEvents(); }));
}
}
@@ -76,7 +90,11 @@ void CEventSystem::Stop()
{
if (_processThread)
{
#ifdef _WIN32
::SetEvent(_stopEvent);
#else
#endif
_processThread->join();
_processThread.reset();
}

View File

@@ -1,6 +1,8 @@
#include <uploadmanager.h>
#include <SQLiteCpp/Exception.h>
#include <siaapi.h>
#include <eventsystem.h>
#include <siadriveconfig.h>
using namespace Sia::Api;
@@ -21,11 +23,11 @@ bool statusUpdated = false;\
try\
{\
SQLite::Statement update(_uploadDatabase, UPDATE_STATUS);\
update.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);\
update.bind("@sia_path", SString::ToUtf8(siaPath).c_str());\
update.bind("@status", static_cast<unsigned>(status));\
if (update.exec() != 1)\
{\
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(fail_event(siaPath, filePath, status, CA2W(update.getErrorMsg()).m_psz)));\
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(fail_event(siaPath, filePath, status, update.getErrorMsg())));\
}\
else\
{\
@@ -41,11 +43,11 @@ catch (SQLite::Exception e)\
template <typename... Ts>
SString fmt(const SString &fmt, Ts... vs)
{
size_t required = _sntprintf(nullptr, 0, fmt.c_str(), vs...);
size_t required = _snwprintf(nullptr, 0, fmt.str().c_str(), vs...);
SString ret;
ret.resize(required);
_sntprintf(&ret[0], required, fmt.c_str(), vs...);
ret.Resize(required);
_snwprintf(&ret[0], required, fmt.str().c_str(), vs...);
return ret;
}
@@ -53,7 +55,7 @@ SString fmt(const SString &fmt, Ts... vs)
static void CreateTableIfNotFound(SQLite::Database* database, const SString& tableName, const SString& columns)
{
SString sqlCreate = fmt(TABLE_CREATE, &tableName[0], &columns[0]);
database->exec(CW2A(sqlCreate.c_str()));
database->exec(SString::ToUtf8(sqlCreate).c_str());
}
SString CUploadManager::UploadStatusToString(const UploadStatus& uploadStatus)
@@ -97,12 +99,12 @@ CUploadManager::CUploadManager(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriv
CreateTableIfNotFound(&_uploadDatabase, UPLOAD_TABLE, UPLOAD_TABLE_COLUMNS);
// Clean-up cache folder
if (!RecurDeleteFilesByExtentsion(CA2W(siaDriveConfig->GetCacheFolder().c_str()).m_psz, L".siadrive"))
if (!RecurDeleteFilesByExtentsion(siaDriveConfig->GetCacheFolder(), L".siadrive"))
{
throw StartupException(L"Failed to remove '.siadrive' files");
}
if (!RecurDeleteFilesByExtentsion(CA2W(siaDriveConfig->GetCacheFolder().c_str()).m_psz, L".siadrive.temp"))
if (!RecurDeleteFilesByExtentsion(siaDriveConfig->GetCacheFolder(), L".siadrive.temp"))
{
throw StartupException(L"Failed to remove '.siadrive.temp' files");
}
@@ -135,9 +137,9 @@ void CUploadManager::UpdateFileQueueOnStartup()
query.bind("@status1", static_cast<unsigned>(UploadStatus::Remove));
while (query.executeStep())
{
SString siaPath = CA2W(query.getColumn(query.getColumnIndex("sia_path"))).m_psz;
SString filePath = CA2W(query.getColumn(query.getColumnIndex("file_path"))).m_psz;
SString siaDriveFilePath = CA2W(query.getColumn(query.getColumnIndex("sd_file_path"))).m_psz;
SString siaPath = static_cast<const char*>(query.getColumn(query.getColumnIndex("sia_path")));
SString filePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("file_path")));
SString siaDriveFilePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("sd_file_path")));
UploadStatus uploadStatus = static_cast<UploadStatus>(query.getColumn(query.getColumnIndex("status")).getUInt());
SString temp = filePath;
@@ -149,13 +151,13 @@ void CUploadManager::UpdateFileQueueOnStartup()
SString siaDriveFileName = GenerateSha256(&filePath[3]) + L".siadrive";
SString tempSourcePath;
tempSourcePath.resize(MAX_PATH + 1);
PathCombine(&tempSourcePath[0], rootPath.c_str(), (siaDriveFileName + L".temp").c_str());
tempSourcePath.Resize(MAX_PATH + 1);
::PathCombine(&tempSourcePath[0], rootPath.str().c_str(), (siaDriveFileName + L".temp").str().c_str());
std::lock_guard<std::mutex> l(_fileQueueMutex);
if (uploadStatus == UploadStatus::Remove)
{
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, nullptr, siaDriveFilePath, true); });
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, "", siaDriveFilePath, true); });
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoveAdded(siaPath)));
}
else
@@ -167,7 +169,7 @@ void CUploadManager::UpdateFileQueueOnStartup()
}
catch (SQLite::Exception e)
{
throw StartupException(e.getErrorStr());
throw StartupException(SString(e.getErrorStr()));
}
}
@@ -214,18 +216,18 @@ void CUploadManager::HandleFileRemove(const CSiaCurl& siaCurl, const SString& si
{
std::lock_guard<std::mutex> l(_uploadMutex);
SQLite::Statement query(_uploadDatabase, QUERY_STATUS);
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
query.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
if (query.executeStep())
{
SString removeFilePath = CA2W(query.getColumn(query.getColumnIndex("file_path"))).m_psz;
SString removeFilePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("file_path")));
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(query.getColumnIndex("status"))));
// Make sure status is still remove
if (uploadStatus == UploadStatus::Remove)
{
bool deleteFromDb = true;
if (::PathFileExists(removeFilePath.c_str()))
if (::PathFileExists(removeFilePath.str().c_str()))
{
if (RetryDeleteFileIfExists(removeFilePath.c_str()))
if (RetryDeleteFileIfExists(removeFilePath.str().c_str()))
{
if (!RetryDeleteFileIfExists(siaDriveFilePath))
{
@@ -248,14 +250,14 @@ void CUploadManager::HandleFileRemove(const CSiaCurl& siaCurl, const SString& si
// TODO validate response
SQLite::Statement del(_uploadDatabase, DELETE_UPLOAD);
del.bind("@sia_path", CW2A(siaPath.c_str()));
del.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
if (del.exec() == 1)
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoved(siaPath, removeFilePath)));
}
else
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseDeleteFailed(siaPath, removeFilePath, CA2W(del.getErrorMsg()).m_psz)));
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseDeleteFailed(siaPath, removeFilePath, del.getErrorMsg())));
}
}
else
@@ -276,13 +278,13 @@ void CUploadManager::HandleFileRemove(const CSiaCurl& siaCurl, const SString& si
void CUploadManager::HandleAddFile(const SString& siaPath, const SString& filePath, const SString& tempSourcePath, const SString& siaDriveFilePath)
{
// Check for retry condition
if (!::PathFileExists(tempSourcePath.c_str()) && ::PathFileExists(siaDriveFilePath.c_str()))
if (!::PathFileExists(tempSourcePath.str().c_str()) && ::PathFileExists(siaDriveFilePath.str().c_str()))
{
try
{
std::lock_guard<std::mutex> l(_uploadMutex);
SQLite::Statement query(_uploadDatabase, QUERY_STATUS);
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
query.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
if (query.executeStep())
{
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(query.getColumnIndex("status"))));
@@ -307,7 +309,7 @@ void CUploadManager::HandleAddFile(const SString& siaPath, const SString& filePa
else
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(CreatingTemporarySiaDriveFile(siaPath, filePath, tempSourcePath)));
if (RetryableAction(::CopyFile(filePath.c_str(), tempSourcePath.c_str(), FALSE), DEFAULT_RETRY_COUNT, DEFAULT_RETRY_DELAY_MS))
if (RetryableAction(::CopyFile(filePath.str().c_str(), tempSourcePath.str().c_str(), FALSE), DEFAULT_RETRY_COUNT, DEFAULT_RETRY_DELAY_MS))
{
// Delete existing '.siadrive' file, if found
// !!Should never come here. If so, there was a problem with startup clean-up
@@ -318,7 +320,7 @@ void CUploadManager::HandleAddFile(const SString& siaPath, const SString& filePa
// Rename '.siadrive.temp' to '.siadrive'
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(RenamingTemporarySiaDriveFile(siaPath, filePath, tempSourcePath, siaDriveFilePath)));
if (RetryableAction(::MoveFile(tempSourcePath.c_str(), siaDriveFilePath.c_str()), DEFAULT_RETRY_COUNT, DEFAULT_RETRY_DELAY_MS))
if (RetryableAction(::MoveFile(tempSourcePath.str().c_str(), siaDriveFilePath.str().c_str()), DEFAULT_RETRY_COUNT, DEFAULT_RETRY_DELAY_MS))
{
if (!RetryDeleteFileIfExists(tempSourcePath))
{
@@ -329,7 +331,7 @@ void CUploadManager::HandleAddFile(const SString& siaPath, const SString& filePa
{
std::lock_guard<std::mutex> l(_uploadMutex);
SQLite::Statement query(_uploadDatabase, QUERY_STATUS);
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
query.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
if (query.executeStep())
{
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(query.getColumnIndex("status"))));
@@ -394,7 +396,7 @@ void CUploadManager::FileAction(const CSiaCurl& siaCurl, const SString& siaPath,
{
std::lock_guard<std::mutex> l(_fileActionMutex);
_activeSiaPath.empty();
_activeSiaPath = "";
}
}
@@ -418,9 +420,9 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
fileTree->BuildTree(result);
if (query.executeStep())
{
SString siaPath = CA2W(query.getColumn(query.getColumnIndex("sia_path"))).m_psz;
SString filePath = CA2W(query.getColumn(query.getColumnIndex("file_path"))).m_psz;
SString siaDriveFilePath = CA2W(query.getColumn(query.getColumnIndex("sd_file_path"))).m_psz;
SString siaPath = static_cast<const char*>(query.getColumn(query.getColumnIndex("sia_path")));
SString filePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("file_path")));
SString siaDriveFilePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("sd_file_path")));
UploadStatus uploadStatus = static_cast<UploadStatus>(query.getColumn(query.getColumnIndex("status")).getUInt());
auto fileList = fileTree->GetFileList();
@@ -436,7 +438,7 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
if (statusUpdated)
{
std::lock_guard<std::mutex> l2(_fileQueueMutex);
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, nullptr, siaDriveFilePath, true); });
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, "", siaDriveFilePath, true); });
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoveAdded(siaPath)));
}
@@ -497,8 +499,8 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
// start again later
if (query.executeStep())
{
SString siaPath = CA2W(query.getColumn(query.getColumnIndex("sia_path"))).m_psz;
SString filePath = CA2W(query.getColumn(query.getColumnIndex("file_path"))).m_psz;
SString siaPath = static_cast<const char*>(query.getColumn(query.getColumnIndex("sia_path")));
SString filePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("file_path")));
// TODO Validate response
json response;
@@ -522,7 +524,7 @@ UploadStatus CUploadManager::GetUploadStatus(const SString& siaPath)
UploadStatus uploadStatus = UploadStatus::NotFound;
SQLite::Statement query(_uploadDatabase, QUERY_UPLOADS_BY_SIA_PATH);
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
query.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
if (query.executeStep())
{
uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(query.getColumnIndex("status"))));
@@ -554,10 +556,10 @@ UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath
SString rootPath;
{
SString temp;
if (::PathIsRelative(filePath.c_str()))
if (::PathIsRelative(filePath.str().c_str()))
{
temp.resize(MAX_PATH + 1);
filePath = _wfullpath(&temp[0], filePath.c_str(), MAX_PATH);
temp.Resize(MAX_PATH + 1);
filePath = _wfullpath(&temp[0], filePath.str().c_str(), MAX_PATH);
}
temp = filePath;
@@ -565,7 +567,7 @@ UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath
rootPath = temp;
}
if (::PathFileExists(filePath.c_str()))
if (::PathFileExists(filePath.str().c_str()))
{
// Lock here - if file is modified again before a prior upload is complete, delete it and
// start again later
@@ -574,7 +576,7 @@ UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath
try
{
SQLite::Statement query(_uploadDatabase, QUERY_UPLOADS_BY_SIA_PATH_AND_2_STATUS);
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
query.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
query.bind("@status1", static_cast<unsigned>(UploadStatus::Uploading));
query.bind("@status2", static_cast<unsigned>(UploadStatus::Modified));
// Check copying
@@ -595,20 +597,20 @@ UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath
SString siaDriveFileName = GenerateSha256(&filePath[3]) + L".siadrive";
SString siaDriveFilePath;
siaDriveFilePath.resize(MAX_PATH + 1);
PathCombine(&siaDriveFilePath[0], rootPath.c_str(), siaDriveFileName.c_str());
siaDriveFilePath.Resize(MAX_PATH + 1);
::PathCombine(&siaDriveFilePath[0], rootPath.str().c_str(), siaDriveFileName.str().c_str());
SString tempSourcePath;
tempSourcePath.resize(MAX_PATH + 1);
PathCombine(&tempSourcePath[0], rootPath.c_str(), (siaDriveFileName + L".temp").c_str());
tempSourcePath.Resize(MAX_PATH + 1);
::PathCombine(&tempSourcePath[0], rootPath.str().c_str(), (siaDriveFileName + L".temp").str().c_str());
// Add to db
try
{
SQLite::Statement insert(_uploadDatabase, INSERT_UPLOAD);
insert.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
insert.bind("@file_path", CW2A(filePath.c_str()).m_psz);
insert.bind("@sd_file_path", CW2A(siaDriveFileName.c_str()).m_psz);
insert.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
insert.bind("@file_path", SString::ToUtf8(filePath).c_str());
insert.bind("@sd_file_path", SString::ToUtf8(siaDriveFileName).c_str());
insert.bind("@status", static_cast<unsigned>(UploadStatus::Copying));
if (insert.exec() == 1)
{
@@ -619,7 +621,7 @@ UploadError CUploadManager::AddOrUpdate(const SString& siaPath, SString filePath
}
else
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseInsertFailed(siaPath, filePath, CA2W(insert.getErrorMsg()).m_psz)));
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseInsertFailed(siaPath, filePath, insert.getErrorMsg())));
ret = UploadError::DatabaseError;
}
}
@@ -654,11 +656,11 @@ UploadError CUploadManager::Remove(const SString& siaPath)
bool remove = false;
SQLite::Statement query(_uploadDatabase, QUERY_STATUS);
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
query.bind("@sia_path", SString::ToUtf8(siaPath).c_str());
if (query.executeStep())
{
SString filePath = CA2W(query.getColumn(query.getColumnIndex("file_path"))).m_psz;
SString siaDriveFilePath = CA2W(query.getColumn(query.getColumnIndex("sd_file_path"))).m_psz;
SString filePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("file_path")));
SString siaDriveFilePath = static_cast<const char*>(query.getColumn(query.getColumnIndex("sd_file_path")));
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(query.getColumnIndex("status"))));
switch (uploadStatus)
{
@@ -687,7 +689,7 @@ UploadError CUploadManager::Remove(const SString& siaPath)
if (remove)
{
std::lock_guard<std::mutex> l2(_fileQueueMutex);
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, nullptr, siaDriveFilePath, true); });
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, "", siaDriveFilePath, true); });
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoveAdded(siaPath)));
}