1
0
This repository has been archived on 2025-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
siadrive/SiaDrive.Dokan.Api/SiaDokanDrive.cpp
Scott E. Graves 0a865eb73e Dokan changes
2017-02-11 12:20:17 -06:00

319 lines
7.9 KiB
C++

#include "stdafx.h"
#include "SiaDokanDrive.h"
#include <dokan\dokan.h>
#include <bitset>
using namespace Sia::Api;
using namespace Sia::Api::Dokan;
class DokanImpl
{
private:
static std::mutex _dokanMutex;
static CSiaApi* _siaApi;
static DOKAN_OPERATIONS _dokanOps;
static String _cacheLocation;
private:
static bool AddFileToCache(const String& siaPath, const String& cachePath)
{
bool ret = false;
std::wstring tempPath;
tempPath.resize(MAX_PATH + 1);
if (GetTempPath(MAX_PATH + 1, &tempPath[0]))
{
ret = API_SUCCESS(SiaApiError, _siaApi->GetRenter()->DownloadFile(siaPath, tempPath));
if (ret)
{
}
}
return ret;
}
static NTSTATUS DOKAN_CALLBACK SiaDrive_ZwCreateFile(
LPCWSTR FileName,
PDOKAN_IO_SECURITY_CONTEXT SecurityContext,
ACCESS_MASK DesiredAccess,
ULONG FileAttributes,
ULONG ShareAccess,
ULONG CreateDisposition,
ULONG CreateOptions,
PDOKAN_FILE_INFO DokanFileInfo)
{
SECURITY_ATTRIBUTES securityAttrib;
securityAttrib.nLength = sizeof(securityAttrib);
securityAttrib.lpSecurityDescriptor = SecurityContext->AccessState.SecurityDescriptor;
securityAttrib.bInheritHandle = FALSE;
DWORD fileAttributesAndFlags;
DWORD creationDisposition;
DokanMapKernelToUserCreateFileFlags(FileAttributes, CreateOptions, CreateDisposition, &fileAttributesAndFlags, &creationDisposition);
ACCESS_MASK genericDesiredAccess = DokanMapStandardToGenericAccess(DesiredAccess);
NTSTATUS ret = STATUS_SUCCESS;
bool isFile = (FileAttributes & FILE_NON_DIRECTORY_FILE);
DokanFileInfo->IsDirectory = !isFile;
if (isFile)
{
// Formulate Sia path and cache path
String siaPath = PathSkipRoot(FileName); // Strip drive letter to get Sia path
String cacheFilePath;
cacheFilePath.resize(MAX_PATH + 1);
PathCombine(&cacheFilePath[0], _cacheLocation.c_str(), siaPath.c_str());
// If cache file already exists and is a directory, requested file operation doesn't make sense
if (GetFileAttributes(cacheFilePath.c_str()) & FILE_ATTRIBUTE_DIRECTORY)
{
ret = STATUS_OBJECT_NAME_COLLISION;
}
else
{
bool exists;
if (API_SUCCESS(SiaApiError, _siaApi->GetRenter()->FileExists(siaPath, exists)))
{
// Operations on existing files that are requested to be truncated, overwritten or re-created
// will first be deleted and then replaced if, after the file operation is done, the resulting file
// size is > 0. Sia doesn't support random access to files (upload/download/rename/delete).
bool isCreateOp = false;
bool isReplaceOp = false;
switch (creationDisposition)
{
case CREATE_ALWAYS:
{
isCreateOp = true;
isReplaceOp = exists;
}
break;
case CREATE_NEW:
{
if (exists)
{
ret = STATUS_OBJECT_NAME_EXISTS;
}
else
{
isCreateOp = true;
}
}
break;
case OPEN_ALWAYS:
{
if (!exists)
{
isCreateOp = true;
}
}
break;
case OPEN_EXISTING:
{
if (!exists)
{
ret = STATUS_NOT_FOUND;
}
}
break;
case TRUNCATE_EXISTING:
{
if (exists)
{
isCreateOp = isReplaceOp = true;
}
else
{
ret = STATUS_NOT_FOUND;
}
}
break;
}
if (ret == STATUS_SUCCESS)
{
if (isReplaceOp)
{
// Since this is a request to replace existing file, so make sure cache is deleted first.
// If file isn't cached, delete from Sia only
if (!PathFileExists(cacheFilePath.c_str()) || ::DeleteFile(cacheFilePath.c_str()))
{
// Delete from Sia
if (!API_SUCCESS(SiaApiError, _siaApi->GetRenter()->DeleteFile(siaPath)))
{
ret = STATUS_INVALID_SERVER_STATE;
}
}
else
{
ret = DokanNtStatusFromWin32(GetLastError());
}
}
if (ret == STATUS_SUCCESS)
{
// If file must exist, then check for it in cache location. If not found,
// it must be downloaded first and placed in cache
if (!isCreateOp && !PathFileExists(cacheFilePath.c_str()))
{
if (!AddFileToCache(siaPath, cacheFilePath))
{
ret = STATUS_INVALID_SERVER_STATE;
}
}
if (ret == STATUS_SUCCESS)
{
// Create file as specified
HANDLE handle = CreateFile(
cacheFilePath.c_str(),
genericDesiredAccess,
ShareAccess,
&securityAttrib,
creationDisposition,
fileAttributesAndFlags,
nullptr);
if (handle == INVALID_HANDLE_VALUE)
{
ret = DokanNtStatusFromWin32(GetLastError());
}
else
{
DokanFileInfo->Context = reinterpret_cast<ULONG64>(handle); // save the file handle in Context
/*if (creationDisposition == OPEN_ALWAYS ||
creationDisposition == CREATE_ALWAYS) {
error = GetLastError();
if (error == ERROR_ALREADY_EXISTS) {
DbgPrint(L"\tOpen an already existing file\n");
// Open succeed but we need to inform the driver
// that the file open and not created by returning STATUS_OBJECT_NAME_COLLISION
return STATUS_OBJECT_NAME_COLLISION;
}
}*/
}
}
}
}
}
else
{
ret = STATUS_INVALID_SERVER_STATE;
}
}
}
else // Folder Operation (cache operation only)
{
ret = STATUS_NOT_IMPLEMENTED;
}
return ret;
}
static NTSTATUS DOKAN_CALLBACK SiaDrive_FindFiles(
LPCWSTR FileName,
PFillFindData FillFindData,
PDOKAN_FILE_INFO DokanFileInfo)
{
return 0;
}
static void DOKAN_CALLBACK Sia_CloseFile(LPCWSTR FileName, PDOKAN_FILE_INFO DokanFileInfo)
{
if (DokanFileInfo->Context)
{
::CloseHandle(reinterpret_cast<HANDLE>(DokanFileInfo->Context));
DokanFileInfo->Context = 0;
QueueUploadIfChanged(FileName);
}
}
public:
static void Initialize(CSiaApi* siaApi)
{
_siaApi = siaApi;
_dokanOps.Cleanup = nullptr;
_dokanOps.CloseFile = Sia_CloseFile;
_dokanOps.DeleteDirectory = nullptr;
_dokanOps.DeleteFileW = nullptr;
_dokanOps.FindFiles = SiaDrive_FindFiles;
_dokanOps.FindFilesWithPattern = nullptr;
_dokanOps.FindStreams = nullptr;
_dokanOps.FlushFileBuffers = nullptr;
_dokanOps.GetDiskFreeSpaceW = nullptr;
_dokanOps.GetFileInformation = nullptr;
_dokanOps.GetFileSecurityW = nullptr;
_dokanOps.GetVolumeInformationW = nullptr;
_dokanOps.LockFile = nullptr;
_dokanOps.Mounted = nullptr;
_dokanOps.MoveFileW = nullptr;
_dokanOps.ReadFile = nullptr;
_dokanOps.SetAllocationSize = nullptr;
_dokanOps.SetEndOfFile = nullptr;
_dokanOps.SetFileAttributesW = nullptr;
_dokanOps.SetFileSecurityW = nullptr;
_dokanOps.SetFileTime = nullptr;
_dokanOps.UnlockFile = nullptr;
_dokanOps.Unmounted = nullptr;
_dokanOps.WriteFile = nullptr;
_dokanOps.ZwCreateFile = SiaDrive_ZwCreateFile;
}
static void Shutdown()
{
_siaApi = nullptr;
ZeroMemory(&_dokanOps, sizeof(_dokanOps));
}
static std::mutex& GetMutex()
{
return _dokanMutex;
}
static bool IsInitialized()
{
return _siaApi != nullptr;
}
};
std::mutex DokanImpl::_dokanMutex;
CSiaApi* DokanImpl::_siaApi = nullptr;
DOKAN_OPERATIONS DokanImpl::_dokanOps;
String DokanImpl::_cacheLocation;
CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi) :
_siaApi(siaApi),
_Mounted(false)
{
std::lock_guard<std::mutex> l(DokanImpl::GetMutex());
if (DokanImpl::IsInitialized())
throw SiaDokanDriveException("Sia drive has already been activated");
DokanImpl::Initialize(&_siaApi);
}
CSiaDokanDrive::~CSiaDokanDrive()
{
std::lock_guard<std::mutex> l(DokanImpl::GetMutex());
Unmount();
DokanImpl::Shutdown();
}
void CSiaDokanDrive::Mount(const wchar_t& driveLetter, const String& cacheLocation, const std::uint64_t& maxCacheSizeBytes)
{
std::lock_guard<std::mutex> l(DokanImpl::GetMutex());
}
void CSiaDokanDrive::Unmount(const bool& clearCache)
{
std::lock_guard<std::mutex> l(DokanImpl::GetMutex());
}
void CSiaDokanDrive::ClearCache()
{
}