Skip to content
Permalink
Browse files

Give the error when File cannot be accessed

  • Loading branch information...
da3dsoul committed Jan 29, 2019
1 parent 06a091d commit 77345c3b52da092f9dc34fd3275ba7ac1f4cc7fb
Showing with 13 additions and 9 deletions.
  1. +13 −9 Shoko.Server/Commands/Import/CommandRequest_HashFile.cs
@@ -70,7 +70,7 @@ 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, bool writeAccess)
private static long CanAccessFile(string fileName, bool writeAccess, ref Exception e)
{
var accessType = writeAccess ? FileAccess.ReadWrite : FileAccess.Read;
try
@@ -81,22 +81,23 @@ private long CanAccessFile(string fileName, bool writeAccess)
return size;
}
}
catch
catch (Exception ex)
{
e = ex;
return 0;
}
}

//Used to check if file has been modified within the last X seconds.
private bool FileModified(string FileName, int Seconds, ref long lastFileSize, bool writeAccess)
private static bool FileModified(string FileName, int Seconds, ref long lastFileSize, bool writeAccess, ref Exception e)
{
try
{
var lastWrite = File.GetLastWriteTime(FileName);
var creation = File.GetCreationTime(FileName);
var now = DateTime.Now;
// check that the size is also equal, since some copy utilities apply the previous modified date
var size = CanAccessFile(FileName, writeAccess);
var size = CanAccessFile(FileName, writeAccess, ref e);
if (lastWrite <= now && lastWrite.AddSeconds(Seconds) >= now ||
creation <= now && creation.AddSeconds(Seconds) > now ||
lastFileSize != size)
@@ -105,9 +106,9 @@ private bool FileModified(string FileName, int Seconds, ref long lastFileSize, b
return true;
}
}
catch
catch (Exception ex)
{
// ignore
e = ex;
}
return false;
}
@@ -133,6 +134,7 @@ private void ProcessFile_LocalInfo()
return;
}
long filesize = 0;
Exception e = null;
if (folder.CloudID == null) // Local Access
{
if (!File.Exists(FileName))
@@ -146,7 +148,7 @@ private void ProcessFile_LocalInfo()

// Wait 1 minute before giving up on trying to access the file
// first only do read to not get in something's way
while ((filesize = CanAccessFile(FileName, false)) == 0 && (numAttempts < 60))
while ((filesize = CanAccessFile(FileName, false, ref e)) == 0 && (numAttempts < 60))
{
numAttempts++;
Thread.Sleep(1000);
@@ -157,6 +159,7 @@ private void ProcessFile_LocalInfo()
if (numAttempts >= 60)
{
logger.Error("Could not access file: " + FileName);
logger.Error(e);
return;
}

@@ -170,7 +173,7 @@ private void ProcessFile_LocalInfo()

//For systems with no locking
// TODO make this a setting as well
while (FileModified(FileName, seconds, ref filesize, writeAccess) && numAttempts < 60)
while (FileModified(FileName, seconds, ref filesize, writeAccess, ref e) && numAttempts < 60)
{
numAttempts++;
Thread.Sleep(waitTime);
@@ -179,9 +182,10 @@ private void ProcessFile_LocalInfo()
}

// if we failed to access the file, get ouuta here
if (numAttempts >= 60)
if (numAttempts >= 60 || filesize == 0)
{
logger.Error("Could not access file: " + FileName);
logger.Error(e);
return;
}
}

0 comments on commit 77345c3

Please sign in to comment.
You can’t perform that action at this time.