1
0

Dokan changes

This commit is contained in:
Scott E. Graves
2017-02-11 01:46:15 -06:00
parent 6cbfd920c7
commit 6f21dd7880
7 changed files with 231 additions and 16 deletions

View File

@@ -5,7 +5,8 @@ using namespace Sia::Api;
CSiaApi::CSiaApi(const SiaHostConfig& hostConfig) : CSiaApi::CSiaApi(const SiaHostConfig& hostConfig) :
_siaCurl(hostConfig), _siaCurl(hostConfig),
_wallet(new CSiaWallet(_siaCurl)) _wallet(new CSiaWallet(_siaCurl)),
_renter(new CSiaRenter(_siaCurl))
{ {
} }
@@ -24,3 +25,8 @@ CSiaWalletPtr CSiaApi::GetWallet() const
{ {
return _wallet; return _wallet;
} }
CSiaRenterPtr CSiaApi::GetRenter() const
{
return _renter;
}

View File

@@ -52,6 +52,23 @@ public:
_SiaApiError GetUnonfirmedBalance(SiaCurrency& balance) const; _SiaApiError GetUnonfirmedBalance(SiaCurrency& balance) const;
}; };
class AFX_EXT_CLASS _CSiaRenter
{
friend CSiaApi;
private:
_CSiaRenter(CSiaCurl& siaCurl);
public:
~_CSiaRenter();
private:
CSiaCurl& _siaCurl;
public:
_SiaApiError FileExists(const String& siaPath, bool& exists) const;
_SiaApiError DeleteFile(const String& siaPath);
};
public: public:
CSiaApi(const SiaHostConfig& hostConfig); CSiaApi(const SiaHostConfig& hostConfig);
@@ -61,16 +78,19 @@ public:
private: private:
CSiaCurl _siaCurl; CSiaCurl _siaCurl;
std::shared_ptr<_CSiaWallet> _wallet; std::shared_ptr<_CSiaWallet> _wallet;
std::shared_ptr<_CSiaRenter> _renter;
public: public:
std::shared_ptr<_CSiaWallet> GetWallet() const; std::shared_ptr<_CSiaWallet> GetWallet() const;
std::shared_ptr<_CSiaRenter> GetRenter() const;
String GetServerVersion() const; String GetServerVersion() const;
}; };
typedef CSiaApi::_SiaApiError SiaApiError; typedef CSiaApi::_SiaApiError SiaApiError;
typedef CSiaApi::_SiaSeedLanguage SiaSeedLanguage; typedef CSiaApi::_SiaSeedLanguage SiaSeedLanguage;
typedef CSiaApi::_CSiaWallet CSiaWallet; typedef CSiaApi::_CSiaWallet CSiaWallet;
typedef std::shared_ptr<CSiaApi::_CSiaWallet> CSiaWalletPtr; typedef CSiaApi::_CSiaRenter CSiaRenter;
typedef std::shared_ptr<CSiaWallet> CSiaWalletPtr;
typedef std::shared_ptr<CSiaRenter> CSiaRenterPtr;
NS_END(2) NS_END(2)

View File

@@ -196,6 +196,7 @@
<ClCompile Include="SiaCurl.cpp" /> <ClCompile Include="SiaCurl.cpp" />
<ClCompile Include="SiaDrive.Api.cpp" /> <ClCompile Include="SiaDrive.Api.cpp" />
<ClCompile Include="SiaDriveConfig.cpp" /> <ClCompile Include="SiaDriveConfig.cpp" />
<ClCompile Include="SiaRenter.cpp" />
<ClCompile Include="SiaWallet.cpp" /> <ClCompile Include="SiaWallet.cpp" />
<ClCompile Include="stdafx.cpp"> <ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>

View File

@@ -39,6 +39,9 @@
<ClCompile Include="SiaCommon.cpp"> <ClCompile Include="SiaCommon.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="SiaRenter.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="SiaDrive.Api.h"> <ClInclude Include="SiaDrive.Api.h">

View File

@@ -0,0 +1,24 @@
#include "stdafx.h"
#include "SiaApi.h"
using namespace Sia::Api;
CSiaApi::_CSiaRenter::_CSiaRenter(CSiaCurl& siaCurl) :
_siaCurl(siaCurl)
{
}
CSiaApi::_CSiaRenter::~_CSiaRenter()
{
}
SiaApiError CSiaApi::_CSiaRenter::FileExists(const String& siaPath, bool& exists) const
{
return SiaApiError::NotImplemented;
}
SiaApiError CSiaApi::_CSiaRenter::DeleteFile(const String& siaPath)
{
return SiaApiError::NotImplemented;
}

View File

@@ -1,16 +1,19 @@
#include "stdafx.h" #include "stdafx.h"
#include "SiaDokanDrive.h" #include "SiaDokanDrive.h"
#include <dokan\dokan.h> #include <dokan\dokan.h>
#include <bitset>
using namespace Sia::Api; using namespace Sia::Api;
using namespace Sia::Api::Dokan; using namespace Sia::Api::Dokan;
class DokanImpl class DokanImpl
{ {
private: private:
static std::mutex _dokanMutex; static std::mutex _dokanMutex;
static CSiaApi* _siaApi; static CSiaApi* _siaApi;
static DOKAN_OPERATIONS _dokanOps; static DOKAN_OPERATIONS _dokanOps;
static String _cacheLocation;
private: private:
static NTSTATUS DOKAN_CALLBACK SiaDrive_ZwCreateFile( static NTSTATUS DOKAN_CALLBACK SiaDrive_ZwCreateFile(
@@ -23,6 +26,162 @@ private:
ULONG CreateOptions, ULONG CreateOptions,
PDOKAN_FILE_INFO DokanFileInfo) 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 = (fileAttributesAndFlags & FILE_NON_DIRECTORY_FILE);
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, file operation should fail
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)))
{
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)
{
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 should exist, then check for it in cache location. If not found,
// it must be downloaded first
if (!isCreateOp && !PathFileExists(cacheFilePath.c_str()))
{
}
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( static NTSTATUS DOKAN_CALLBACK SiaDrive_FindFiles(
@@ -30,6 +189,7 @@ private:
PFillFindData FillFindData, PFillFindData FillFindData,
PDOKAN_FILE_INFO DokanFileInfo) PDOKAN_FILE_INFO DokanFileInfo)
{ {
return 0;
} }
public: public:
@@ -81,21 +241,20 @@ public:
}; };
std::mutex DokanImpl::_dokanMutex; std::mutex DokanImpl::_dokanMutex;
CSiaApi* DokanImpl::_siaApi = nullptr; CSiaApi* DokanImpl::_siaApi = nullptr;
DOKAN_OPERATIONS DokanImpl::_dokanOps = { 0 }; DOKAN_OPERATIONS DokanImpl::_dokanOps;
String DokanImpl::_cacheLocation;
CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi) : CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi) :
_siaApi(siaApi) _siaApi(siaApi),
_Mounted(false)
{ {
std::lock_guard<std::mutex> l(DokanImpl::GetMutex()); std::lock_guard<std::mutex> l(DokanImpl::GetMutex());
if (DokanImpl::IsInitialized()) if (DokanImpl::IsInitialized())
{
throw SiaDokanDriveException("Sia drive has already been activated"); throw SiaDokanDriveException("Sia drive has already been activated");
}
else
{
DokanImpl::Initialize(&_siaApi); DokanImpl::Initialize(&_siaApi);
} }
}
CSiaDokanDrive::~CSiaDokanDrive() CSiaDokanDrive::~CSiaDokanDrive()
{ {
@@ -104,10 +263,10 @@ CSiaDokanDrive::~CSiaDokanDrive()
DokanImpl::Shutdown(); DokanImpl::Shutdown();
} }
void CSiaDokanDrive::Mount(const wchar_t& driveLetter) void CSiaDokanDrive::Mount(const wchar_t& driveLetter, const String& cacheLocation, const std::uint64_t& maxCacheSizeBytes)
{ {
} }
void CSiaDokanDrive::Unmount() void CSiaDokanDrive::Unmount(const bool& clearCache)
{ {
} }

View File

@@ -29,10 +29,12 @@ public:
private: private:
CSiaApi& _siaApi; CSiaApi& _siaApi;
public: Property(bool, Mounted, public, private)
void Mount(const wchar_t& driveLetter);
void Unmount( ); public:
void Mount(const wchar_t& driveLetter, const String& cacheLocation, const std::uint64_t& maxCacheSizeBytes);
void Unmount(const bool& clearCache = false);
}; };
NS_END(3) NS_END(3)