1
0

Fix download missing

This commit is contained in:
Scott E. Graves
2017-04-07 00:11:05 -05:00
parent 04a127053e
commit d9189c80bb

View File

@@ -32,6 +32,11 @@ private:
FilePath CacheFilePath; FilePath CacheFilePath;
bool Dummy; bool Dummy;
bool Changed; bool Changed;
ACCESS_MASK DesiredAccess;
ULONG ShareMode;
SECURITY_ATTRIBUTES SecurityAttrib;
ULONG CreateDisp;
DWORD AttributesAndFlags;
} OpenFileInfo; } OpenFileInfo;
private: private:
@@ -49,7 +54,7 @@ private:
static std::unique_ptr<std::thread> _mountThread; static std::unique_ptr<std::thread> _mountThread;
static NTSTATUS _mountStatus; static NTSTATUS _mountStatus;
static SString _mountPoint; static SString _mountPoint;
static std::vector<SString> _activeDownloads; static std::vector<OpenFileInfo*> _openFiles;
private: private:
inline static const FilePath& GetCacheLocation() inline static const FilePath& GetCacheLocation()
@@ -65,44 +70,48 @@ private:
static bool AddFileToCache(OpenFileInfo& openFileInfo, PDOKAN_FILE_INFO dokanFileInfo) static bool AddFileToCache(OpenFileInfo& openFileInfo, PDOKAN_FILE_INFO dokanFileInfo)
{ {
{
std::lock_guard<std::mutex> l(_fileTreeMutex);
_activeDownloads.push_back(openFileInfo.SiaPath);
}
FilePath tempFilePath = FilePath::GetTempDirectory(); FilePath tempFilePath = FilePath::GetTempDirectory();
tempFilePath.Append(GenerateSha256(openFileInfo.SiaPath) + ".siatmp"); tempFilePath.Append(GenerateSha256(openFileInfo.SiaPath) + ".siatmp");
bool ret = ApiSuccess(_siaApi->GetRenter()->DownloadFile(openFileInfo.SiaPath, tempFilePath)); bool ret = ApiSuccess(_siaApi->GetRenter()->DownloadFile(openFileInfo.SiaPath, tempFilePath));
if (ret) if (ret)
{ {
::CloseHandle(openFileInfo.FileHandle); // Find all open handles for requested file
std::vector<OpenFileInfo*> matched;
matched.push_back(&openFileInfo);
{
std::lock_guard<std::mutex> l(_fileTreeMutex);
std::for_each(_openFiles.begin(), _openFiles.end(), [&](OpenFileInfo* ofi)
{
if ((ofi->FileHandle != openFileInfo.FileHandle) && (ofi->SiaPath == openFileInfo.SiaPath))
{
matched.push_back(ofi);
}
});
}
// Close all to allow move to complete
for (auto* ofi : matched)
{
::CloseHandle(ofi->FileHandle);
}
FilePath src(tempFilePath); FilePath src(tempFilePath);
FilePath dest(GetCacheLocation(), openFileInfo.SiaPath); FilePath dest(openFileInfo.CacheFilePath);
ret = dest.DeleteFile(); ret = dest.DeleteFile() && src.MoveFile(dest);
ret = ret && src.MoveFile(dest);
if (ret) if (ret)
{
HANDLE handle = ::CreateFile(&dest[0], GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE)
{
ret = false;
}
else
{ {
openFileInfo.Dummy = false; openFileInfo.Dummy = false;
openFileInfo.FileHandle = handle;
}
} }
else else
{ {
src.DeleteFile(); src.DeleteFile();
} }
}
// Re-open all files
for (auto& ofi : matched)
{ {
std::lock_guard<std::mutex> l(_fileTreeMutex); ofi->FileHandle = ::CreateFile(&ofi->CacheFilePath[0], ofi->DesiredAccess, ofi->ShareMode, ofi->SecurityAttrib.nLength ? &ofi->SecurityAttrib : nullptr, ofi->CreateDisp, ofi->AttributesAndFlags, nullptr);
_activeDownloads.erase(std::remove(_activeDownloads.begin(), _activeDownloads.end(), openFileInfo.SiaPath), _activeDownloads.end()); }
} }
return ret; return ret;
@@ -152,6 +161,9 @@ private:
{ {
::CloseHandle(openFileInfo.FileHandle); ::CloseHandle(openFileInfo.FileHandle);
} }
std::lock_guard<std::mutex> l(_fileTreeMutex);
_openFiles.erase(std::remove(_openFiles.begin(), _openFiles.end(), &openFileInfo), _openFiles.end());
} }
static void StartFileListThread() static void StartFileListThread()
@@ -300,14 +312,6 @@ private:
SString siaPath = CSiaApi::FormatToSiaPath(fileName); // Strip drive letter to get Sia path SString siaPath = CSiaApi::FormatToSiaPath(fileName); // Strip drive letter to get Sia path
if (siaPath.Length()) if (siaPath.Length())
{ {
{
std::lock_guard<std::mutex> l(_fileTreeMutex);
if (std::find(_activeDownloads.begin(), _activeDownloads.end(), siaPath) != _activeDownloads.end())
{
ret = STATUS_ACCESS_DENIED;
}
}
if (ret == STATUS_SUCCESS) if (ret == STATUS_SUCCESS)
{ {
// If cache file already exists and is a directory, requested file operation isn't valid // If cache file already exists and is a directory, requested file operation isn't valid
@@ -456,11 +460,17 @@ private:
OpenFileInfo* ofi = new OpenFileInfo(); OpenFileInfo* ofi = new OpenFileInfo();
ofi->SiaPath = siaPath; ofi->SiaPath = siaPath;
ofi->CacheFilePath = cacheFilePath; ofi->CacheFilePath = cacheFilePath;
// TODO Detect if file is read-only
ofi->Dummy = isDummy; ofi->Dummy = isDummy;
ofi->Changed = false; ofi->Changed = false;
ofi->FileHandle = handle; ofi->FileHandle = handle;
ofi->DesiredAccess = genericDesiredAccess;
ofi->ShareMode = shareAccess;
ofi->SecurityAttrib = securityAttrib;
ofi->CreateDisp = createDisposition;
ofi->AttributesAndFlags = fileAttributesAndFlags;
dokanFileInfo->Context = reinterpret_cast<ULONG64>(ofi); dokanFileInfo->Context = reinterpret_cast<ULONG64>(ofi);
std::lock_guard<std::mutex> l(_fileTreeMutex);
_openFiles.push_back(ofi);
} }
} }
} }
@@ -674,7 +684,6 @@ private:
handleFileInfo->dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE; handleFileInfo->dwFileAttributes = FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE;
handleFileInfo->nFileSizeHigh = li.HighPart; handleFileInfo->nFileSizeHigh = li.HighPart;
handleFileInfo->nFileSizeLow = li.LowPart; handleFileInfo->nFileSizeLow = li.LowPart;
ret = STATUS_SUCCESS;
} }
else if (!::GetFileInformationByHandle(tempHandle, handleFileInfo)) else if (!::GetFileInformationByHandle(tempHandle, handleFileInfo))
{ {
@@ -986,7 +995,12 @@ private:
ofi.CacheFilePath = filePath; ofi.CacheFilePath = filePath;
ofi.Changed = true; ofi.Changed = true;
ofi.Dummy = false; ofi.Dummy = false;
ofi.SiaPath = CSiaApi::FormatToSiaPath(fileName);; ofi.SiaPath = CSiaApi::FormatToSiaPath(fileName);
ofi.AttributesAndFlags = 0;
ofi.CreateDisp = OPEN_EXISTING;
ofi.DesiredAccess = GENERIC_WRITE;
ofi.SecurityAttrib = { 0 };
ofi.ShareMode = FILE_SHARE_WRITE;
HandleSiaFileClose(ofi, li.QuadPart, false); HandleSiaFileClose(ofi, li.QuadPart, false);
} }
@@ -1424,9 +1438,9 @@ public:
ZeroMemory(&_dokanOptions, sizeof(DOKAN_OPTIONS)); ZeroMemory(&_dokanOptions, sizeof(DOKAN_OPTIONS));
_dokanOptions.Version = DOKAN_VERSION; _dokanOptions.Version = DOKAN_VERSION;
_dokanOptions.ThreadCount = 0; // use default _dokanOptions.ThreadCount = 0; // use default
_dokanOptions.Timeout = (60 * 1000) * 60;
#ifdef _DEBUG #ifdef _DEBUG
_dokanOptions.Options = DOKAN_OPTION_DEBUG | DOKAN_OPTION_DEBUG_LOG_FILE; _dokanOptions.Options = DOKAN_OPTION_DEBUG | DOKAN_OPTION_DEBUG_LOG_FILE;
_dokanOptions.Timeout = (60 * 1000) * 60;
#else #else
_dokanOptions.Options = 0; _dokanOptions.Options = 0;
#endif #endif
@@ -1501,7 +1515,7 @@ std::unique_ptr<std::thread> DokanImpl::_fileListThread;
std::unique_ptr<std::thread> DokanImpl::_mountThread; std::unique_ptr<std::thread> DokanImpl::_mountThread;
NTSTATUS DokanImpl::_mountStatus = STATUS_SUCCESS; NTSTATUS DokanImpl::_mountStatus = STATUS_SUCCESS;
SString DokanImpl::_mountPoint; SString DokanImpl::_mountPoint;
std::vector<SString> DokanImpl::_activeDownloads; std::vector<DokanImpl::OpenFileInfo*> DokanImpl::_openFiles;
CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi, CSiaDriveConfig* siaDriveConfig) : CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi, CSiaDriveConfig* siaDriveConfig) :
_siaApi(siaApi), _siaApi(siaApi),