1
0

Dokan changes

This commit is contained in:
Scott E. Graves
2017-02-11 15:07:51 -06:00
parent 0a865eb73e
commit a70d22b568
6 changed files with 194 additions and 11 deletions

View File

@@ -1,19 +1,30 @@
#include "stdafx.h"
#include "SiaDokanDrive.h"
#include <dokan\dokan.h>
#include <bitset>
using namespace Sia::Api;
using namespace Sia::Api::Dokan;
class DokanImpl
{
private:
struct OpenFileInfo
{
String SiaPath;
String CacheFilePath;
bool ReadOnly;
};
private:
static std::mutex _dokanMutex;
static CSiaApi* _siaApi;
static DOKAN_OPERATIONS _dokanOps;
static String _cacheLocation;
static std::unordered_map<ULONG64, OpenFileInfo> _openFileMap;
static std::unique_ptr<std::thread> _fileListThread;
static bool _fileListStopRequested;
static CSiaFileTreePtr _siaFileTree;
static std::mutex _fileTreeMutex;
private:
static bool AddFileToCache(const String& siaPath, const String& cachePath)
@@ -33,6 +44,62 @@ private:
return ret;
}
static void QueueUploadIfChanged(const ULONG64& id, const std::uint64_t& size)
{
if (!_openFileMap[id].ReadOnly)
{
if (size > 0)
{
// TODO Always save for now - need to change to detect modifications
_siaApi->GetRenter()->QueueUploadFile(_openFileMap[id].SiaPath, _openFileMap[id].CacheFilePath);
}
else
{
// Treat 0 length files as deleted in Sia - cache retains 0-length
// TODO Retain 0 length in cache?
_siaApi->GetRenter()->DeleteFile(_openFileMap[id].SiaPath);
}
}
}
static void StartFileListThread()
{
if (!_fileListThread)
{
_fileListStopRequested = false;
_fileListThread.reset(new std::thread([]()
{
while (!_fileListStopRequested)
{
CSiaFileTreePtr siaFileTree;
_siaApi->GetRenter()->GetFileTree(siaFileTree);
{
std::lock_guard<std::mutex> l(_fileTreeMutex);
_siaFileTree = siaFileTree;
}
if (!_fileListStopRequested)
{
// TODO Change to WaitForSingleObject() for immediate termination
Sleep(5000);
}
}
}));
}
}
static void StopFileListThread()
{
if (_fileListThread)
{
_fileListStopRequested = true;
_fileListThread->join();
_fileListThread.reset(nullptr);
}
}
// Dokan callbacks
private:
static NTSTATUS DOKAN_CALLBACK SiaDrive_ZwCreateFile(
LPCWSTR FileName,
PDOKAN_IO_SECURITY_CONTEXT SecurityContext,
@@ -43,6 +110,7 @@ private:
ULONG CreateOptions,
PDOKAN_FILE_INFO DokanFileInfo)
{
std::lock_guard<std::mutex> l(_dokanMutex);
SECURITY_ATTRIBUTES securityAttrib;
securityAttrib.nLength = sizeof(securityAttrib);
securityAttrib.lpSecurityDescriptor = SecurityContext->AccessState.SecurityDescriptor;
@@ -142,7 +210,7 @@ private:
// If file isn't cached, delete from Sia only
if (!PathFileExists(cacheFilePath.c_str()) || ::DeleteFile(cacheFilePath.c_str()))
{
// Delete from Sia
// Delete from Sia since this file will be replaced
if (!API_SUCCESS(SiaApiError, _siaApi->GetRenter()->DeleteFile(siaPath)))
{
ret = STATUS_INVALID_SERVER_STATE;
@@ -184,6 +252,17 @@ private:
else
{
DokanFileInfo->Context = reinterpret_cast<ULONG64>(handle); // save the file handle in Context
if (isFile)
{
OpenFileInfo ofi;
ofi.SiaPath = siaPath;
ofi.CacheFilePath = cacheFilePath;
// TODO Detect if file is read-only
// TODO Quick hash to detect changes
ofi.ReadOnly = false;
_openFileMap.insert({ DokanFileInfo->Context, ofi });
}
/*if (creationDisposition == OPEN_ALWAYS ||
creationDisposition == CREATE_ALWAYS) {
error = GetLastError();
@@ -218,20 +297,55 @@ private:
PFillFindData FillFindData,
PDOKAN_FILE_INFO DokanFileInfo)
{
return 0;
std::lock_guard<std::mutex> l(_dokanMutex);
return STATUS_NOT_IMPLEMENTED;
}
static void DOKAN_CALLBACK Sia_CloseFile(LPCWSTR FileName, PDOKAN_FILE_INFO DokanFileInfo)
{
if (DokanFileInfo->Context)
std::lock_guard<std::mutex> l(_dokanMutex);
ULONG64 id = DokanFileInfo->Context;
if (id)
{
::CloseHandle(reinterpret_cast<HANDLE>(DokanFileInfo->Context));
DokanFileInfo->Context = 0;
HANDLE handle = reinterpret_cast<HANDLE>(DokanFileInfo->Context);
// Ignore directories in Sia - reside in cache only.
if (DokanFileInfo->IsDirectory)
{
::CloseHandle(handle);
}
else
{
LARGE_INTEGER li = { 0 };
BOOL sizeOk = GetFileSizeEx(handle, &li);
QueueUploadIfChanged(FileName);
::CloseHandle(handle);
// TODO If it's not ok, why and what to do?
if (sizeOk)
{
QueueUploadIfChanged(id, li.QuadPart);
}
_openFileMap.erase(id);
}
DokanFileInfo->Context = 0;
}
}
static NTSTATUS DOKAN_CALLBACK Sia_Mounted(PDOKAN_FILE_INFO DokanFileInfo)
{
std::lock_guard<std::mutex> l(_dokanMutex);
StartFileListThread();
return STATUS_SUCCESS;
}
static NTSTATUS DOKAN_CALLBACK Sia_Unmounted(PDOKAN_FILE_INFO DokanFileInfo)
{
std::lock_guard<std::mutex> l(_dokanMutex);
StopFileListThread();
return STATUS_SUCCESS;
}
public:
static void Initialize(CSiaApi* siaApi)
{
@@ -249,7 +363,7 @@ public:
_dokanOps.GetFileSecurityW = nullptr;
_dokanOps.GetVolumeInformationW = nullptr;
_dokanOps.LockFile = nullptr;
_dokanOps.Mounted = nullptr;
_dokanOps.Mounted = Sia_Mounted;
_dokanOps.MoveFileW = nullptr;
_dokanOps.ReadFile = nullptr;
_dokanOps.SetAllocationSize = nullptr;
@@ -258,7 +372,7 @@ public:
_dokanOps.SetFileSecurityW = nullptr;
_dokanOps.SetFileTime = nullptr;
_dokanOps.UnlockFile = nullptr;
_dokanOps.Unmounted = nullptr;
_dokanOps.Unmounted = Sia_Unmounted;
_dokanOps.WriteFile = nullptr;
_dokanOps.ZwCreateFile = SiaDrive_ZwCreateFile;
}
@@ -279,10 +393,16 @@ public:
return _siaApi != nullptr;
}
};
// Static member variables
std::mutex DokanImpl::_dokanMutex;
CSiaApi* DokanImpl::_siaApi = nullptr;
DOKAN_OPERATIONS DokanImpl::_dokanOps;
String DokanImpl::_cacheLocation;
bool DokanImpl::_fileListStopRequested;
CSiaFileTreePtr DokanImpl::_siaFileTree;
std::mutex DokanImpl::_fileTreeMutex;
std::unique_ptr<std::thread> DokanImpl::_fileListThread;
std::unordered_map<ULONG64, DokanImpl::OpenFileInfo> DokanImpl::_openFileMap;
CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi) :
@@ -315,5 +435,5 @@ void CSiaDokanDrive::Unmount(const bool& clearCache)
void CSiaDokanDrive::ClearCache()
{
std::lock_guard<std::mutex> l(DokanImpl::GetMutex());
}