1
0

Dokan changes

This commit is contained in:
Scott E. Graves
2017-02-11 12:20:17 -06:00
parent d32218ce19
commit 0a865eb73e
4 changed files with 85 additions and 32 deletions

View File

@@ -68,6 +68,7 @@ public:
public: public:
_SiaApiError FileExists(const String& siaPath, bool& exists) const; _SiaApiError FileExists(const String& siaPath, bool& exists) const;
_SiaApiError DeleteFile(const String& siaPath); _SiaApiError DeleteFile(const String& siaPath);
_SiaApiError DownloadFile(const String& siaPath, const String& location) const;
}; };
public: public:

View File

@@ -19,6 +19,11 @@ SiaApiError CSiaApi::_CSiaRenter::FileExists(const String& siaPath, bool& exists
} }
SiaApiError CSiaApi::_CSiaRenter::DeleteFile(const String& siaPath) SiaApiError CSiaApi::_CSiaRenter::DeleteFile(const String& siaPath)
{
return SiaApiError::NotImplemented;
}
SiaApiError CSiaApi::_CSiaRenter::DownloadFile(const String& siaPath, const String& location) const
{ {
return SiaApiError::NotImplemented; return SiaApiError::NotImplemented;
} }

View File

@@ -16,6 +16,23 @@ private:
static String _cacheLocation; static String _cacheLocation;
private: 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( static NTSTATUS DOKAN_CALLBACK SiaDrive_ZwCreateFile(
LPCWSTR FileName, LPCWSTR FileName,
PDOKAN_IO_SECURITY_CONTEXT SecurityContext, PDOKAN_IO_SECURITY_CONTEXT SecurityContext,
@@ -38,17 +55,17 @@ private:
ACCESS_MASK genericDesiredAccess = DokanMapStandardToGenericAccess(DesiredAccess); ACCESS_MASK genericDesiredAccess = DokanMapStandardToGenericAccess(DesiredAccess);
NTSTATUS ret = STATUS_SUCCESS; NTSTATUS ret = STATUS_SUCCESS;
bool isFile = (fileAttributesAndFlags & FILE_NON_DIRECTORY_FILE); bool isFile = (FileAttributes & FILE_NON_DIRECTORY_FILE);
DokanFileInfo->IsDirectory = !isFile;
if (isFile) if (isFile)
{ {
// Formulate Sia path and cache path // Formulate Sia path and cache path
String siaPath = PathSkipRoot(FileName); // Strip drive letter to get Sia path String siaPath = PathSkipRoot(FileName); // Strip drive letter to get Sia path
String cacheFilePath; String cacheFilePath;
cacheFilePath.resize(MAX_PATH + 1); cacheFilePath.resize(MAX_PATH + 1);
PathCombine(&cacheFilePath[0], _cacheLocation.c_str(), siaPath.c_str()); PathCombine(&cacheFilePath[0], _cacheLocation.c_str(), siaPath.c_str());
// If cache file already exists and is a directory, file operation should fail // If cache file already exists and is a directory, requested file operation doesn't make sense
if (GetFileAttributes(cacheFilePath.c_str()) & FILE_ATTRIBUTE_DIRECTORY) if (GetFileAttributes(cacheFilePath.c_str()) & FILE_ATTRIBUTE_DIRECTORY)
{ {
ret = STATUS_OBJECT_NAME_COLLISION; ret = STATUS_OBJECT_NAME_COLLISION;
@@ -58,6 +75,9 @@ private:
bool exists; bool exists;
if (API_SUCCESS(SiaApiError, _siaApi->GetRenter()->FileExists(siaPath, 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 isCreateOp = false;
bool isReplaceOp = false; bool isReplaceOp = false;
switch (creationDisposition) switch (creationDisposition)
@@ -118,6 +138,8 @@ private:
{ {
if (isReplaceOp) 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())) if (!PathFileExists(cacheFilePath.c_str()) || ::DeleteFile(cacheFilePath.c_str()))
{ {
// Delete from Sia // Delete from Sia
@@ -134,40 +156,45 @@ private:
if (ret == STATUS_SUCCESS) if (ret == STATUS_SUCCESS)
{ {
// If file should exist, then check for it in cache location. If not found, // If file must exist, then check for it in cache location. If not found,
// it must be downloaded first and placed in cache // it must be downloaded first and placed in cache
if (!isCreateOp && !PathFileExists(cacheFilePath.c_str())) if (!isCreateOp && !PathFileExists(cacheFilePath.c_str()))
{ {
// Download to temp location if (!AddFileToCache(siaPath, cacheFilePath))
// Move to cache location {
ret = STATUS_INVALID_SERVER_STATE;
}
} }
// Create file as specified if (ret == STATUS_SUCCESS)
HANDLE handle = CreateFile(
cacheFilePath.c_str(),
genericDesiredAccess,
ShareAccess,
&securityAttrib,
creationDisposition,
fileAttributesAndFlags,
nullptr);
if (handle == INVALID_HANDLE_VALUE)
{ {
ret = DokanNtStatusFromWin32(GetLastError()); // Create file as specified
} HANDLE handle = CreateFile(
else cacheFilePath.c_str(),
{ genericDesiredAccess,
DokanFileInfo->Context = reinterpret_cast<ULONG64>(handle); // save the file handle in Context ShareAccess,
/*if (creationDisposition == OPEN_ALWAYS || &securityAttrib,
creationDisposition == CREATE_ALWAYS) { creationDisposition,
error = GetLastError(); fileAttributesAndFlags,
if (error == ERROR_ALREADY_EXISTS) { nullptr);
DbgPrint(L"\tOpen an already existing file\n"); if (handle == INVALID_HANDLE_VALUE)
// Open succeed but we need to inform the driver {
// that the file open and not created by returning STATUS_OBJECT_NAME_COLLISION ret = DokanNtStatusFromWin32(GetLastError());
return STATUS_OBJECT_NAME_COLLISION; }
} 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;
}
}*/
}
} }
} }
} }
@@ -194,12 +221,23 @@ private:
return 0; 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: public:
static void Initialize(CSiaApi* siaApi) static void Initialize(CSiaApi* siaApi)
{ {
_siaApi = siaApi; _siaApi = siaApi;
_dokanOps.Cleanup = nullptr; _dokanOps.Cleanup = nullptr;
_dokanOps.CloseFile = nullptr; _dokanOps.CloseFile = Sia_CloseFile;
_dokanOps.DeleteDirectory = nullptr; _dokanOps.DeleteDirectory = nullptr;
_dokanOps.DeleteFileW = nullptr; _dokanOps.DeleteFileW = nullptr;
_dokanOps.FindFiles = SiaDrive_FindFiles; _dokanOps.FindFiles = SiaDrive_FindFiles;
@@ -267,8 +305,15 @@ CSiaDokanDrive::~CSiaDokanDrive()
void CSiaDokanDrive::Mount(const wchar_t& driveLetter, const String& cacheLocation, const std::uint64_t& maxCacheSizeBytes) 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) void CSiaDokanDrive::Unmount(const bool& clearCache)
{ {
std::lock_guard<std::mutex> l(DokanImpl::GetMutex());
}
void CSiaDokanDrive::ClearCache()
{
} }

View File

@@ -35,6 +35,8 @@ public:
void Mount(const wchar_t& driveLetter, const String& cacheLocation, const std::uint64_t& maxCacheSizeBytes); void Mount(const wchar_t& driveLetter, const String& cacheLocation, const std::uint64_t& maxCacheSizeBytes);
void Unmount(const bool& clearCache = false); void Unmount(const bool& clearCache = false);
void ClearCache();
}; };
NS_END(3) NS_END(3)