Permalink
Browse files

Check for Drop Source and Test Write Access, otherwise only Read. Adj…

…ust the File Modified Check
  • Loading branch information...
da3dsoul committed Oct 10, 2018
1 parent bb8adad commit 462be924dd325c58996ec363f27b4c8fd5e54cfa
Showing with 8 additions and 5 deletions.
  1. +8 −5 Shoko.Server/Commands/Import/CommandRequest_HashFile.cs
@@ -70,11 +70,12 @@ public override void ProcessCommand()
}

//Added size return, since symbolic links return 0, we use this function also to return the size of the file.
private long CanAccessFile(string fileName)
private long CanAccessFile(string fileName, bool writeAccess)
{
var accessType = writeAccess ? FileAccess.ReadWrite : FileAccess.Read;
try
{
using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
using (FileStream fs = File.Open(fileName, FileMode.Open, accessType, FileShare.None))
{
long size = fs.Seek(0, SeekOrigin.End);
fs.Close();
@@ -93,7 +94,9 @@ private bool FileModified(string FileName, int Seconds)
{
try
{
if (System.IO.File.GetLastWriteTime(FileName).AddSeconds(Seconds) >= DateTime.Now)
var lastWrite = System.IO.File.GetLastWriteTime(FileName);
var now = DateTime.Now;
if (lastWrite <= now && lastWrite.AddSeconds(Seconds) >= now)
{
return true;
}
@@ -141,7 +144,7 @@ private void ProcessFile_LocalInfo()
int numAttempts = 0;

// Wait 1 minute before giving up on trying to access the file
while ((filesize = CanAccessFile(FileName)) == 0 && (numAttempts < 60))
while ((filesize = CanAccessFile(FileName, folder.IsDropSource == 1)) == 0 && (numAttempts < 60))
{
numAttempts++;
Thread.Sleep(1000);
@@ -159,7 +162,7 @@ private void ProcessFile_LocalInfo()
while (FileModified(FileName, 3))
{
Thread.Sleep(1000);
logger.Error($@"An external process is modifying the file, {FileName}");
logger.Warn($@"The modified date is too soon. Waiting to ensure that no processes are writing to it. {FileName}");
}
}

0 comments on commit 462be92

Please sign in to comment.