diff --git a/SiaDrive.Dokan.Api/SiaDokanDrive.cpp b/SiaDrive.Dokan.Api/SiaDokanDrive.cpp index 737ea55..5e201fb 100644 --- a/SiaDrive.Dokan.Api/SiaDokanDrive.cpp +++ b/SiaDrive.Dokan.Api/SiaDokanDrive.cpp @@ -1,6 +1,7 @@ #include "stdafx.h" #include "SiaDokanDrive.h" #include +#include using namespace Sia::Api; using namespace Sia::Api::Dokan; @@ -19,12 +20,16 @@ private: static std::mutex _dokanMutex; static CSiaApi* _siaApi; static DOKAN_OPERATIONS _dokanOps; + static DOKAN_OPTIONS _dokanOptions; static String _cacheLocation; static std::unordered_map _openFileMap; static std::unique_ptr _fileListThread; static bool _fileListStopRequested; static CSiaFileTreePtr _siaFileTree; static std::mutex _fileTreeMutex; + static std::unique_ptr _mountThread; + static NTSTATUS _mountStatus; + static String _mountPoint; private: static bool AddFileToCache(const String& siaPath, const String& cachePath) @@ -375,12 +380,46 @@ public: _dokanOps.Unmounted = Sia_Unmounted; _dokanOps.WriteFile = nullptr; _dokanOps.ZwCreateFile = SiaDrive_ZwCreateFile; + + ZeroMemory(&_dokanOptions, sizeof(DOKAN_OPTIONS)); + _dokanOptions.Version = DOKAN_VERSION; + _dokanOptions.ThreadCount = 0; // use default + _dokanOptions.Options |= DOKAN_OPTION_CURRENT_SESSION; + + } + + static void Mount(const wchar_t& driveLetter) + { + if (_siaApi && !_mountThread) + { + wchar_t tmp[] = { driveLetter, ':', '\\', 0 }; + _mountPoint = tmp; + _mountThread.reset(new std::thread([&]() + { + _dokanOptions.MountPoint = _mountPoint.c_str(); + _mountStatus = DokanMain(&_dokanOptions, &_dokanOps); + })); + } + } + + static void Unmount() + { + if (_mountThread) + { + DokanRemoveMountPoint(_mountPoint.c_str()); + _mountThread->join(); + _mountThread.reset(nullptr); + _mountPoint.clear(); + } } static void Shutdown() { + Unmount(); + _siaApi = nullptr; ZeroMemory(&_dokanOps, sizeof(_dokanOps)); + ZeroMemory(&_dokanOptions, sizeof(_dokanOptions)); } static std::mutex& GetMutex() @@ -397,13 +436,16 @@ public: std::mutex DokanImpl::_dokanMutex; CSiaApi* DokanImpl::_siaApi = nullptr; DOKAN_OPERATIONS DokanImpl::_dokanOps; +DOKAN_OPTIONS DokanImpl::_dokanOptions; String DokanImpl::_cacheLocation; bool DokanImpl::_fileListStopRequested; CSiaFileTreePtr DokanImpl::_siaFileTree; std::mutex DokanImpl::_fileTreeMutex; std::unique_ptr DokanImpl::_fileListThread; std::unordered_map DokanImpl::_openFileMap; - +std::unique_ptr DokanImpl::_mountThread; +NTSTATUS DokanImpl::_mountStatus = STATUS_SUCCESS; +String DokanImpl::_mountPoint; CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi) : _siaApi(siaApi),