From e94d7169e47da9816aa59f204da8277c4179fe97 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Wed, 29 Mar 2017 17:14:55 -0500 Subject: [PATCH] Fix delete directory --- include/siadrive_api/siaapi.h | 1 + src/siadrive_api/siarenter.cpp | 30 +++++++++---- src/siadrive_dokan_api/siadokandrive.cpp | 55 +++++++++++++++--------- 3 files changed, 57 insertions(+), 29 deletions(-) diff --git a/include/siadrive_api/siaapi.h b/include/siadrive_api/siaapi.h index dab5630..46e2c58 100644 --- a/include/siadrive_api/siaapi.h +++ b/include/siadrive_api/siaapi.h @@ -181,6 +181,7 @@ public: _SiaApiError GetFileTree(std::shared_ptr<_CSiaFileTree>& siaFileTree) const; _SiaRenterAllowance GetAllowance() const; _SiaApiError SetAllowance(const _SiaRenterAllowance& renterAllowance); + _SiaApiError RefreshFileTree( ); }; class SIADRIVE_EXPORTABLE _CSiaConsensus : diff --git a/src/siadrive_api/siarenter.cpp b/src/siadrive_api/siarenter.cpp index daa68e8..e27b1a9 100644 --- a/src/siadrive_api/siarenter.cpp +++ b/src/siadrive_api/siarenter.cpp @@ -79,14 +79,11 @@ void CSiaApi::_CSiaRenter::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* sia SetRenewWindow(renewWindow); SetPeriod(period); _currentAllowance = { funds, hosts, period, renewWindow }; - CSiaFileTreePtr fileTree(new CSiaFileTree(siaCurl, siaDriveConfig)); - if (ApiSuccess(siaCurl.Get(L"/renter/files", result))) - { - fileTree->BuildTree(result); - { - std::lock_guard l(_fileTreeMutex); - _fileTree = fileTree; - } + + if (ApiSuccess(RefreshFileTree())) + { + CSiaFileTreePtr fileTree; + GetFileTree(fileTree); auto fileList = fileTree->GetFileList(); if (fileList.size()) @@ -129,6 +126,23 @@ void CSiaApi::_CSiaRenter::Refresh(const CSiaCurl& siaCurl, CSiaDriveConfig* sia } } +SiaApiError CSiaApi::_CSiaRenter::RefreshFileTree( ) +{ + SiaApiError ret = SiaApiError::RequestError; + CSiaFileTreePtr tempTree(new CSiaFileTree(GetSiaCurl(), &GetSiaDriveConfig())); + json result; + if (ApiSuccess(GetSiaCurl().Get(L"/renter/files", result))) + { + tempTree->BuildTree(result); + { + std::lock_guard l(_fileTreeMutex); + _fileTree = tempTree; + } + } + + return ret; +} + SiaApiError CSiaApi::_CSiaRenter::FileExists(const SString& siaPath, bool& exists) const { CSiaFileTreePtr siaFileTree; diff --git a/src/siadrive_dokan_api/siadokandrive.cpp b/src/siadrive_dokan_api/siadokandrive.cpp index d93b585..f1ef773 100644 --- a/src/siadrive_dokan_api/siadokandrive.cpp +++ b/src/siadrive_dokan_api/siadokandrive.cpp @@ -127,12 +127,7 @@ private: { do { - CSiaFileTreePtr siaFileTree; - _siaApi->GetRenter()->GetFileTree(siaFileTree); - { - std::lock_guard l(_fileTreeMutex); - _siaFileTree = siaFileTree; - } + RefreshActiveFileTree(); } while (::WaitForSingleObject(_fileListStopEvent, 1000) == WAIT_TIMEOUT); })); } @@ -149,6 +144,21 @@ private: } } + static void RefreshActiveFileTree(const bool& force = false) + { + if (force) + { + _siaApi->GetRenter()->RefreshFileTree(); + } + + CSiaFileTreePtr siaFileTree; + _siaApi->GetRenter()->GetFileTree(siaFileTree); + { + std::lock_guard l(_fileTreeMutex); + _siaFileTree = siaFileTree; + } + } + // Dokan callbacks private: static NTSTATUS DOKAN_CALLBACK Sia_ZwCreateFile( @@ -183,7 +193,7 @@ private: { // When filePath is a directory, needs to change the flag so that the file can // be opened. - FilePath cacheFilePath(GetCacheLocation(), &fileName[1]); + FilePath cacheFilePath(GetCacheLocation(), fileName); DWORD fileAttr = ::GetFileAttributes(&cacheFilePath[0]); if ((fileAttr != INVALID_FILE_ATTRIBUTES) && @@ -211,7 +221,7 @@ private: } else if (creationDisposition == OPEN_ALWAYS) { - if (cacheFilePath.CreateDirectory()) + if (!cacheFilePath.CreateDirectory()) { DWORD error = GetLastError(); if (error != ERROR_ALREADY_EXISTS) @@ -228,20 +238,22 @@ private: !(fileAttr & FILE_ATTRIBUTE_DIRECTORY) && (createOptions & FILE_DIRECTORY_FILE)) { - return STATUS_NOT_A_DIRECTORY; - } - - // FILE_FLAG_BACKUP_SEMANTICS is required for opening directory handles - HANDLE handle = ::CreateFile(&cacheFilePath[0], genericDesiredAccess, shareAccess, &securityAttrib, OPEN_EXISTING, fileAttributesAndFlags | FILE_FLAG_BACKUP_SEMANTICS, nullptr); - if (handle == INVALID_HANDLE_VALUE) - { - DWORD error = GetLastError(); - ret = DokanNtStatusFromWin32(error); - } - else - { - dokanFileInfo->Context = reinterpret_cast(handle); // save the file handle in Context + ret = STATUS_NOT_A_DIRECTORY; } + else + { + // FILE_FLAG_BACKUP_SEMANTICS is required for opening directory handles + HANDLE handle = ::CreateFile(&cacheFilePath[0], genericDesiredAccess, shareAccess, &securityAttrib, OPEN_EXISTING, fileAttributesAndFlags | FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (handle == INVALID_HANDLE_VALUE) + { + DWORD error = GetLastError(); + ret = DokanNtStatusFromWin32(error); + } + else + { + dokanFileInfo->Context = reinterpret_cast(handle); // save the file handle in Context + } + } } } else // File (cache and/or Sia operation) @@ -927,6 +939,7 @@ private: { } } + RefreshActiveFileTree(true); } }