Permalink
Browse files

Backport Size checking for non-locking FileSystems

  • Loading branch information...
da3dsoul committed Oct 28, 2018
1 parent 7186e23 commit 97e8f305a3ede4465738c23a7f2edc9359d56f18
Showing with 13 additions and 13 deletions.
  1. +13 −13 Shoko.Server/Commands/Import/CommandRequest_HashFile.cs
@@ -78,7 +78,6 @@ private long CanAccessFile(string fileName, bool writeAccess)
using (FileStream fs = File.Open(fileName, FileMode.Open, accessType, FileShare.None))
{
long size = fs.Seek(0, SeekOrigin.End);
fs.Close();
return size;
}
}
@@ -90,26 +89,24 @@ private long CanAccessFile(string fileName, bool writeAccess)
}

//Used to check if file has been modified within the last X seconds.
private bool FileModified(string FileName, int Seconds)
private bool FileModified(string FileName, int Seconds, ref long lastFileSize, bool writeAccess)
{
try
{
var lastWrite = System.IO.File.GetLastWriteTime(FileName);
var lastWrite = File.GetLastWriteTime(FileName);
var now = DateTime.Now;
if (lastWrite <= now && lastWrite.AddSeconds(Seconds) >= now)
{
// check that the size is also equal, since some copy utilities apply the previous modified date
var size = CanAccessFile(FileName, writeAccess);
if (lastWrite <= now && lastWrite.AddSeconds(Seconds) >= now && lastFileSize == size)
return true;
}
else
{
return false;
}

lastFileSize = size;
}
catch (Exception ex)
{
logger.Error(ex);
return false;
}
return false;
}

private void ProcessFile_LocalInfo()
@@ -142,9 +139,10 @@ private void ProcessFile_LocalInfo()
}

int numAttempts = 0;
bool writeAccess = folder.IsDropSource == 1;

// Wait 1 minute before giving up on trying to access the file
while ((filesize = CanAccessFile(FileName, folder.IsDropSource == 1)) == 0 && (numAttempts < 60))
while ((filesize = CanAccessFile(FileName, writeAccess)) == 0 && (numAttempts < 60))
{
numAttempts++;
Thread.Sleep(1000);
@@ -158,10 +156,12 @@ private void ProcessFile_LocalInfo()
return;
}

// At least 1s between to ensure that size has the chance to change
Thread.Sleep(1000);
numAttempts = 0;

//For systems with no locking
while (FileModified(FileName, 3) && numAttempts < 60)
while (FileModified(FileName, 3, ref filesize, writeAccess) && numAttempts < 60)
{
numAttempts++;
Thread.Sleep(1000);

0 comments on commit 97e8f30

Please sign in to comment.