fixes cache calculation and wrong filetransfer cast

This commit is contained in:
Stanley Dimant
2022-07-05 19:54:57 +02:00
parent f2c59bf5ad
commit 7a509fb608
3 changed files with 14 additions and 8 deletions

View File

@@ -47,13 +47,14 @@ namespace MareSynchronos.Managers
public string WatchedPenumbraDirectory => (_penumbraDirWatcher?.EnableRaisingEvents ?? false) ? _penumbraDirWatcher!.Path : "Not watched"; public string WatchedPenumbraDirectory => (_penumbraDirWatcher?.EnableRaisingEvents ?? false) ? _penumbraDirWatcher!.Path : "Not watched";
public FileCache Create(string file) public FileCache Create(string file, CancellationToken token)
{ {
FileInfo fileInfo = new(file); FileInfo fileInfo = new(file);
while (IsFileLocked(fileInfo)) while (IsFileLocked(fileInfo))
{ {
Thread.Sleep(1000); Thread.Sleep(1000);
Logger.Debug("Waiting for file release " + fileInfo.FullName); Logger.Debug("Waiting for file release " + fileInfo.FullName);
token.ThrowIfCancellationRequested();
} }
var sha1Hash = Crypto.GetFileHash(fileInfo.FullName); var sha1Hash = Crypto.GetFileHash(fileInfo.FullName);
return new FileCache() return new FileCache()
@@ -144,11 +145,11 @@ namespace MareSynchronos.Managers
{ {
FileCacheSize = Directory.EnumerateFiles(_pluginConfiguration.CacheFolder).Sum(f => new FileInfo(f).Length); FileCacheSize = Directory.EnumerateFiles(_pluginConfiguration.CacheFolder).Sum(f => new FileInfo(f).Length);
if (FileCacheSize <= _pluginConfiguration.MaxLocalCacheInGiB * 1024 * 1024 * 1024) return; if (FileCacheSize < (long)_pluginConfiguration.MaxLocalCacheInGiB * 1024 * 1024 * 1024) return;
var allFiles = Directory.EnumerateFiles(_pluginConfiguration.CacheFolder) var allFiles = Directory.EnumerateFiles(_pluginConfiguration.CacheFolder)
.Select(f => new FileInfo(f)).OrderBy(f => f.LastAccessTime).ToList(); .Select(f => new FileInfo(f)).OrderBy(f => f.LastAccessTime).ToList();
while (FileCacheSize > _pluginConfiguration.MaxLocalCacheInGiB * 1024 * 1024 * 1024) while (FileCacheSize > (long)_pluginConfiguration.MaxLocalCacheInGiB * 1024 * 1024 * 1024)
{ {
var oldestFile = allFiles.First(); var oldestFile = allFiles.First();
FileCacheSize -= oldestFile.Length; FileCacheSize -= oldestFile.Length;
@@ -192,7 +193,7 @@ namespace MareSynchronos.Managers
else else
{ {
PluginLog.Verbose("Changed :" + item); PluginLog.Verbose("Changed :" + item);
var fileCache = Create(item); var fileCache = Create(item, _rescanTaskCancellationTokenSource.Token);
db.RemoveRange(db.FileCaches.Where(f => f.Hash == fileCache.Hash)); db.RemoveRange(db.FileCaches.Where(f => f.Hash == fileCache.Hash));
await db.AddAsync(fileCache, _rescanTaskCancellationTokenSource.Token); await db.AddAsync(fileCache, _rescanTaskCancellationTokenSource.Token);
} }
@@ -252,7 +253,7 @@ namespace MareSynchronos.Managers
} }
FileInfo fileInfo = new(cache.Filepath); FileInfo fileInfo = new(cache.Filepath);
if (fileInfo.LastWriteTimeUtc.Ticks == long.Parse(cache.LastModifiedDate)) return; if (fileInfo.LastWriteTimeUtc.Ticks == long.Parse(cache.LastModifiedDate)) return;
fileCachesToAdd.Add(Create(cache.Filepath)); fileCachesToAdd.Add(Create(cache.Filepath, ct));
fileCachesToDelete.Add(cache); fileCachesToDelete.Add(cache);
} }
@@ -271,7 +272,7 @@ namespace MareSynchronos.Managers
}, },
file => file =>
{ {
fileCachesToAdd.Add(Create(file.Key)); fileCachesToAdd.Add(Create(file.Key, ct));
var files = CurrentFileProgress; var files = CurrentFileProgress;
Interlocked.Increment(ref files); Interlocked.Increment(ref files);

View File

@@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<Authors></Authors> <Authors></Authors>
<Company></Company> <Company></Company>
<Version>0.0.4.0</Version> <Version>0.0.5.0</Version>
<Description></Description> <Description></Description>
<Copyright></Copyright> <Copyright></Copyright>
<PackageProjectUrl>https://github.com/Penumbra-Sync/client</PackageProjectUrl> <PackageProjectUrl>https://github.com/Penumbra-Sync/client</PackageProjectUrl>

View File

@@ -17,8 +17,13 @@ public abstract class FileTransfer
public string Hash => TransferDto.Hash; public string Hash => TransferDto.Hash;
public bool IsInTransfer => Transferred != Total && Transferred > 0; public bool IsInTransfer => Transferred != Total && Transferred > 0;
public bool IsTransferred => Transferred == Total; public bool IsTransferred => Transferred == Total;
public virtual bool CanBeTransferred => !TransferDto.IsForbidden && (((DownloadFileDto)TransferDto)?.FileExists ?? true); public virtual bool CanBeTransferred => !TransferDto.IsForbidden && (TransferDto is not DownloadFileDto dto || dto.FileExists);
public bool IsForbidden => TransferDto.IsForbidden; public bool IsForbidden => TransferDto.IsForbidden;
public override string ToString()
{
return Hash;
}
} }
public class UploadFileTransfer : FileTransfer public class UploadFileTransfer : FileTransfer