From 8a1437388a3bc36fef2630d747f0c94ba7ac10cd Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 12 Feb 2017 22:03:07 -0600 Subject: [PATCH] Dokan changes --- SiaDrive.Api/SiaApi.h | 26 +++++++++++ SiaDrive.Api/SiaDrive.Api.vcxproj | 1 + SiaDrive.Api/SiaDrive.Api.vcxproj.filters | 3 ++ SiaDrive.Api/SiaFile.cpp | 15 +++++++ SiaDrive.Api/SiaFileTree.cpp | 52 +++++++++++++++++++--- SiaDrive.Dokan.Api/SiaDokanDrive.cpp | 21 ++++++++- SiaDrive/SiaDrive.rc | Bin 11344 -> 11378 bytes 7 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 SiaDrive.Api/SiaFile.cpp diff --git a/SiaDrive.Api/SiaApi.h b/SiaDrive.Api/SiaApi.h index b3861b5..52b6de3 100644 --- a/SiaDrive.Api/SiaApi.h +++ b/SiaDrive.Api/SiaApi.h @@ -26,6 +26,22 @@ public: Japanese }; + class AFX_EXT_CLASS _CSiaFile + { + friend CSiaApi; + private: + _CSiaFile(CSiaCurl& siaCurl); + + public: + ~_CSiaFile(); + + private: + CSiaCurl& _siaCurl; + + // Properties + Property(String, SiaPath, public, private) + }; + class AFX_EXT_CLASS _CSiaFileTree { friend CSiaApi; @@ -38,9 +54,16 @@ public: private: CSiaCurl& _siaCurl; + private: + std::vector> _fileList; + public: void BuildTree(const json& result); + std::vector> QueryFiles(String query) const; + + std::shared_ptr<_CSiaFile> GetFile(const String& siaPath) const; + bool FileExists(const String& siaPath) const; }; @@ -117,6 +140,9 @@ typedef CSiaApi::_CSiaWallet CSiaWallet; typedef CSiaApi::_CSiaRenter CSiaRenter; typedef std::shared_ptr CSiaWalletPtr; typedef std::shared_ptr CSiaRenterPtr; +typedef CSiaApi::_CSiaFile CSiaFile; +typedef std::shared_ptr CSiaFilePtr; +typedef std::vector CSiaFileCollection; typedef CSiaApi::_CSiaFileTree CSiaFileTree; typedef std::shared_ptr CSiaFileTreePtr; diff --git a/SiaDrive.Api/SiaDrive.Api.vcxproj b/SiaDrive.Api/SiaDrive.Api.vcxproj index a316ece..462eb04 100644 --- a/SiaDrive.Api/SiaDrive.Api.vcxproj +++ b/SiaDrive.Api/SiaDrive.Api.vcxproj @@ -196,6 +196,7 @@ + diff --git a/SiaDrive.Api/SiaDrive.Api.vcxproj.filters b/SiaDrive.Api/SiaDrive.Api.vcxproj.filters index c0fe47b..54175b3 100644 --- a/SiaDrive.Api/SiaDrive.Api.vcxproj.filters +++ b/SiaDrive.Api/SiaDrive.Api.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + diff --git a/SiaDrive.Api/SiaFile.cpp b/SiaDrive.Api/SiaFile.cpp new file mode 100644 index 0000000..bd72308 --- /dev/null +++ b/SiaDrive.Api/SiaFile.cpp @@ -0,0 +1,15 @@ +#include "StdAfx.h" +#include "SiaApi.h" + +using namespace Sia::Api; + +CSiaApi::_CSiaFile::_CSiaFile(CSiaCurl& siaCurl) : + _siaCurl(siaCurl) +{ + +} + +CSiaApi::_CSiaFile::~_CSiaFile() +{ + +} \ No newline at end of file diff --git a/SiaDrive.Api/SiaFileTree.cpp b/SiaDrive.Api/SiaFileTree.cpp index 7727ff4..f40709c 100644 --- a/SiaDrive.Api/SiaFileTree.cpp +++ b/SiaDrive.Api/SiaFileTree.cpp @@ -1,8 +1,21 @@ #include "StdAfx.h" #include "SiaApi.h" +#include using namespace Sia::Api; +static String& ReplaceStringInPlace(String& subject, const String& search, const String& replace) +{ + size_t pos = 0; + while ((pos = subject.find(search, pos)) != std::string::npos) + { + subject.replace(pos, search.length(), replace); + pos += replace.length(); + } + + return subject; +} + CSiaApi::_CSiaFileTree::_CSiaFileTree(CSiaCurl& siaCurl) : _siaCurl(siaCurl) { @@ -14,12 +27,41 @@ CSiaApi::_CSiaFileTree::~_CSiaFileTree() } -bool CSiaApi::_CSiaFileTree::FileExists(const String& siaPath) const -{ - return false; -} - void CSiaApi::_CSiaFileTree::BuildTree(const json& result) { +} + +bool CSiaApi::_CSiaFileTree::FileExists(const String& siaPath) const +{ + auto result = std::find_if(_fileList.begin(), _fileList.end(), [&](const CSiaFilePtr& item)->bool + { + return (item->GetSiaPath().compare(siaPath) == 0); + }); + + return (result != _fileList.end()); +} + +CSiaFilePtr CSiaApi::_CSiaFileTree::GetFile(const String& siaPath) const +{ + auto result = std::find_if(_fileList.begin(), _fileList.end(), [&](const CSiaFilePtr& item)->bool + { + return (item->GetSiaPath().compare(siaPath) == 0); + }); + return ((result != _fileList.end()) ? *result : nullptr); +} + +CSiaFileCollection CSiaApi::_CSiaFileTree::QueryFiles(String query) const +{ + query = CSiaApi::FormatToSiaPath(query); + ReplaceStringInPlace(ReplaceStringInPlace(ReplaceStringInPlace(query, L".", L"\\."), L"*", L".+"), L"?", L".?"); + std::wregex r(query); + + CSiaFileCollection ret; + std::copy_if(_fileList.begin(), _fileList.end(), std::back_inserter(ret), [&](const CSiaFilePtr& v) -> bool + { + return std::regex_match(v->GetSiaPath(), r); + }); + + return std::move(ret); } \ No newline at end of file diff --git a/SiaDrive.Dokan.Api/SiaDokanDrive.cpp b/SiaDrive.Dokan.Api/SiaDokanDrive.cpp index e46e9af..6ab0882 100644 --- a/SiaDrive.Dokan.Api/SiaDokanDrive.cpp +++ b/SiaDrive.Dokan.Api/SiaDokanDrive.cpp @@ -6,6 +6,10 @@ using namespace Sia::Api; using namespace Sia::Api::Dokan; +// The general idea is that normal file I/O occurs in a local cache folder and once the file is closed, it is scheduled for upload into Sia. +// Files requested to be openned that are not cached will be downloaded first. If the file is not found in Sia, it will be treated as new. +// Keeping cache and Sia in synch will be a bit of a hastle, so it's strongly suggested to treat the cache folder as if it doesn't exist; +// however, simply deleting files in the cache folder should not be an issue as long as the drive is not mounted. class DokanImpl { private: @@ -318,7 +322,21 @@ private: PDOKAN_FILE_INFO DokanFileInfo) { std::lock_guard l(_dokanMutex); - return STATUS_NOT_IMPLEMENTED; + auto siaFileTree = _siaFileTree; + if (siaFileTree) + { + String siaQuery = CSiaApi::FormatToSiaPath(PathSkipRoot(FileName)); + auto fileList = siaFileTree->QueryFiles(siaQuery); + for (auto& file : fileList) + { + WIN32_FIND_DATA fd = { 0 }; + wcscpy_s(fd.cFileName, PathFindFileName(file->GetSiaPath().c_str())); + + FillFindData(&fd, DokanFileInfo); + } + } + + return STATUS_SUCCESS; } static void DOKAN_CALLBACK Sia_CloseFile(LPCWSTR FileName, PDOKAN_FILE_INFO DokanFileInfo) @@ -461,6 +479,7 @@ std::unique_ptr DokanImpl::_mountThread; NTSTATUS DokanImpl::_mountStatus = STATUS_SUCCESS; String DokanImpl::_mountPoint; + CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi) : _siaApi(siaApi), _Mounted(false) diff --git a/SiaDrive/SiaDrive.rc b/SiaDrive/SiaDrive.rc index dcafb2b3e363dd559c9e479f52fa0bc67070739f..17e2280b1468f06106d8bb78a8a48163f8d8f2ec 100644 GIT binary patch delta 77 zcmcZ*@hM`1od~ZngE4~+g93vQgDHc_