diff --git a/SiaDrive.Api/UploadManager.cpp b/SiaDrive.Api/UploadManager.cpp index 75ef664..818de4f 100644 --- a/SiaDrive.Api/UploadManager.cpp +++ b/SiaDrive.Api/UploadManager.cpp @@ -129,14 +129,75 @@ void CUploadManager::FileThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig } } -void CUploadManager::FileAction(const CSiaCurl& siaCurl, const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove) +void CUploadManager::HandleFileRemove(const CSiaCurl& siaCurl, const String& siaPath, const String& siaDriveFilePath) { + try { - std::lock_guard l(_fileActionMutex); - _activeSiaPath = siaPath; - } + std::lock_guard l(_uploadMutex); + SQLite::Statement query(_uploadDatabase, QUERY_STATUS); + query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); + if (query.executeStep()) + { + String removeFilePath = CA2W(query.getColumn(3)).m_psz; + UploadStatus uploadStatus = static_cast(static_cast(query.getColumn(5))); + // Make sure status is still remove + if (uploadStatus == UploadStatus::Remove) + { + bool deleteFromDb = true; + if (::PathFileExists(removeFilePath.c_str())) + { + if (RetryDeleteFileIfExists(removeFilePath.c_str())) + { + if (!RetryDeleteFileIfExists(siaDriveFilePath)) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, removeFilePath, siaDriveFilePath))); + } + } + else + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(RemoveFileFailed(siaPath, removeFilePath))); + deleteFromDb = false; + } + } + + if (deleteFromDb) + { + json response; + SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete") + siaPath, {}, response); + if (ApiSuccess(cerror)) + { + // TODO validate response - if (remove) + SQLite::Statement del(_uploadDatabase, DELETE_UPLOAD); + del.bind("@sia_path", CW2A(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))); + } + } + else + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FailedToDeleteFromSia(siaPath, removeFilePath, cerror))); + } + } + + } + } + } + catch (SQLite::Exception e) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e))); + } +} + +void CUploadManager::HandleAddFile(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath) +{ + // Check for retry condition + if (!::PathFileExists(tempSourcePath.c_str()) && ::PathFileExists(siaDriveFilePath.c_str())) { try { @@ -145,53 +206,10 @@ void CUploadManager::FileAction(const CSiaCurl& siaCurl, const String& siaPath, query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); if (query.executeStep()) { - String removeFilePath = CA2W(query.getColumn(3)).m_psz; UploadStatus uploadStatus = static_cast(static_cast(query.getColumn(5))); - if (uploadStatus == UploadStatus::Remove) + if (uploadStatus == UploadStatus::Copying) { - bool deleteFromDb = false; - if (::PathFileExists(removeFilePath.c_str())) - { - if (RetryDeleteFileIfExists(removeFilePath.c_str())) - { - if (!RetryDeleteFileIfExists(siaDriveFilePath)) - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, removeFilePath, siaDriveFilePath))); - } - - deleteFromDb = true; - } - else - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(RemoveFileFailed(siaPath, removeFilePath))); - } - } - - if (deleteFromDb) - { - json response; - SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete") + siaPath, {}, response); - if (ApiSuccess(cerror)) - { - // TODO validate response - - SQLite::Statement del(_uploadDatabase, DELETE_UPLOAD); - del.bind("@sia_path", CW2A(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))); - } - } - else - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FailedToDeleteFromSia(siaPath, removeFilePath, cerror))); - } - } - + SET_STATUS(UploadStatus::Queued, UploadAddedToQueue, ModifyUploadStatusFailed) } } } @@ -202,98 +220,90 @@ void CUploadManager::FileAction(const CSiaCurl& siaCurl, const String& siaPath, } else { - // Check for retry queue condition - if (!::PathFileExists(tempSourcePath.c_str()) && ::PathFileExists(siaDriveFilePath.c_str())) + 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)) { - try + // Delete existing '.siadrive' file, if found + // !!Should never come here. If so, there was a problem with startup clean-up + if (!RetryDeleteFileIfExists(siaDriveFilePath)) { - std::lock_guard l(_uploadMutex); - SQLite::Statement query(_uploadDatabase, QUERY_STATUS); - query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); - if (query.executeStep()) - { - UploadStatus uploadStatus = static_cast(static_cast(query.getColumn(5))); - if (uploadStatus == UploadStatus::Copying) - { - SET_STATUS(UploadStatus::Queued, UploadAddedToQueue, ModifyUploadStatusFailed) - } - } + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, filePath, siaDriveFilePath))); } - catch (SQLite::Exception e) + + // 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)) { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e))); - } - } - 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)) - { - // Delete existing '.siadrive' file, if found - // !!Should never come here. If so, there was a problem with startup clean-up - if (!RetryDeleteFileIfExists(siaDriveFilePath)) - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, filePath, siaDriveFilePath))); - } - - // 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 (!RetryDeleteFileIfExists(tempSourcePath)) - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); - } - - try - { - std::lock_guard l(_uploadMutex); - SQLite::Statement query(_uploadDatabase, QUERY_STATUS); - query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); - if (query.executeStep()) - { - UploadStatus uploadStatus = static_cast(static_cast(query.getColumn(5))); - if (uploadStatus == UploadStatus::Copying) - { - SET_STATUS(UploadStatus::Queued, UploadAddedToQueue, ModifyUploadStatusFailed) - } - } - } - catch (SQLite::Exception e) - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e))); - } - } - else - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(RenamingTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath, siaDriveFilePath))); - if (!RetryDeleteFileIfExists(tempSourcePath)) - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); - } - - if (!RetryDeleteFileIfExists(siaDriveFilePath)) - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, filePath, siaDriveFilePath))); - } - - // Requeued - } - } - else - { - CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(CreatingTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); - - // If temp copy fails, try to delete - // If partial copy and file is unable to be deleted, log warning if (!RetryDeleteFileIfExists(tempSourcePath)) { CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); } + try + { + std::lock_guard l(_uploadMutex); + SQLite::Statement query(_uploadDatabase, QUERY_STATUS); + query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz); + if (query.executeStep()) + { + UploadStatus uploadStatus = static_cast(static_cast(query.getColumn(5))); + if (uploadStatus == UploadStatus::Copying) + { + SET_STATUS(UploadStatus::Queued, UploadAddedToQueue, ModifyUploadStatusFailed) + } + } + } + catch (SQLite::Exception e) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e))); + } + } + else + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(RenamingTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath, siaDriveFilePath))); + if (!RetryDeleteFileIfExists(tempSourcePath)) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); + } + + if (!RetryDeleteFileIfExists(siaDriveFilePath)) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, filePath, siaDriveFilePath))); + } + // Requeued } } + else + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(CreatingTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); + + // If temp copy fails, try to delete + // If partial copy and file is unable to be deleted, log warning + if (!RetryDeleteFileIfExists(tempSourcePath)) + { + CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteTemporarySiaDriveFileFailed(siaPath, filePath, tempSourcePath))); + } + + // Requeued + } + } +} + +void CUploadManager::FileAction(const CSiaCurl& siaCurl, const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove) +{ + { + std::lock_guard l(_fileActionMutex); + _activeSiaPath = siaPath; + } + + if (remove) + { + HandleFileRemove(siaCurl, siaPath, siaDriveFilePath); + } + else + { + HandleAddFile(siaPath, filePath, tempSourcePath, siaDriveFilePath); } { @@ -538,16 +548,6 @@ UploadError CUploadManager::AddOrUpdate(const String& siaPath, String filePath) return ret; } -void CUploadManager::PurgeCompleteStatus() -{ - -} - -void CUploadManager::PurgeErrorStatus() -{ - -} - UploadError CUploadManager::Remove(const String& siaPath) { UploadError ret = UploadError::Success; diff --git a/SiaDrive.Api/UploadManager.h b/SiaDrive.Api/UploadManager.h index 0822577..26f7ced 100644 --- a/SiaDrive.Api/UploadManager.h +++ b/SiaDrive.Api/UploadManager.h @@ -57,10 +57,12 @@ private: private: void FileAction(const CSiaCurl& siaCurl, const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove); + void FileThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig); + void HandleFileRemove(const CSiaCurl& siaCurl, const String& siaPath, const String& siaDriveFilePath); + void HandleAddFile(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath); protected: virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) override; - void FileThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig); public: static String UploadStatusToString(const _UploadStatus& uploadStatus); @@ -69,8 +71,6 @@ public: _UploadStatus GetUploadStatus(const String& siaPath); _UploadError AddOrUpdate(const String& siaPath, String filePath); _UploadError Remove(const String& siaPath); - void PurgeCompleteStatus(); - void PurgeErrorStatus(); }; typedef Sia::Api::CUploadManager::_UploadStatus UploadStatus;