1
0

Try to fix download

This commit is contained in:
Scott E. Graves
2017-04-06 18:52:03 -05:00
parent 9118fe6b86
commit 04a127053e

View File

@@ -49,6 +49,7 @@ private:
static std::unique_ptr<std::thread> _mountThread;
static NTSTATUS _mountStatus;
static SString _mountPoint;
static std::vector<SString> _activeDownloads;
private:
inline static const FilePath& GetCacheLocation()
@@ -64,22 +65,22 @@ private:
static bool AddFileToCache(OpenFileInfo& openFileInfo, PDOKAN_FILE_INFO dokanFileInfo)
{
bool active = true;
bool ret = false;
{
std::lock_guard<std::mutex> l(_fileTreeMutex);
_activeDownloads.push_back(openFileInfo.SiaPath);
}
std::thread th([&] {
FilePath tempFilePath = FilePath::GetTempDirectory();
tempFilePath.Append(GenerateSha256(openFileInfo.SiaPath) + ".siatmp");
// TODO Check cache size is large enough to hold new file
ret = ApiSuccess(_siaApi->GetRenter()->DownloadFile(openFileInfo.SiaPath, tempFilePath));
bool ret = ApiSuccess(_siaApi->GetRenter()->DownloadFile(openFileInfo.SiaPath, tempFilePath));
if (ret)
{
::CloseHandle(openFileInfo.FileHandle);
FilePath src(tempFilePath);
FilePath dest(GetCacheLocation(), openFileInfo.SiaPath);
ret = dest.DeleteFile() && src.MoveFile(dest);
ret = dest.DeleteFile();
ret = ret && src.MoveFile(dest);
if (ret)
{
HANDLE handle = ::CreateFile(&dest[0], GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
@@ -98,13 +99,10 @@ private:
src.DeleteFile();
}
}
active = false;
});
th.detach();
while (active)
{
::Sleep(10);
std::lock_guard<std::mutex> l(_fileTreeMutex);
_activeDownloads.erase(std::remove(_activeDownloads.begin(), _activeDownloads.end(), openFileInfo.SiaPath), _activeDownloads.end());
}
return ret;
@@ -302,6 +300,16 @@ private:
SString siaPath = CSiaApi::FormatToSiaPath(fileName); // Strip drive letter to get Sia path
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 cache file already exists and is a directory, requested file operation isn't valid
DWORD attribs = ::GetFileAttributes(&cacheFilePath[0]);
if ((attribs != INVALID_FILE_ATTRIBUTES) && (attribs & FILE_ATTRIBUTE_DIRECTORY))
@@ -462,6 +470,7 @@ private:
{
ret = STATUS_INVALID_SERVER_STATE;
}
}
}
}
else
@@ -1492,6 +1501,7 @@ std::unique_ptr<std::thread> DokanImpl::_fileListThread;
std::unique_ptr<std::thread> DokanImpl::_mountThread;
NTSTATUS DokanImpl::_mountStatus = STATUS_SUCCESS;
SString DokanImpl::_mountPoint;
std::vector<SString> DokanImpl::_activeDownloads;
CSiaDokanDrive::CSiaDokanDrive(CSiaApi& siaApi, CSiaDriveConfig* siaDriveConfig) :
_siaApi(siaApi),