Skip to content

Commit

Permalink
Don't recurse the access check
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Nov 20, 2023
1 parent 53792a0 commit af71797
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Shoko.Server/Commands/Import/CommandRequest_HashFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@ private long CanAccessFile(string fileName, bool writeAccess, ref Exception e)
var accessType = writeAccess ? FileAccess.ReadWrite : FileAccess.Read;
try
{
using (var fs = File.Open(fileName, FileMode.Open, accessType, FileShare.ReadWrite))
{
var size = fs.Seek(0, SeekOrigin.End);
return size;
}
return GetFileSize(fileName, accessType);
}
catch (IOException)
{
Expand All @@ -157,19 +153,16 @@ private long CanAccessFile(string fileName, bool writeAccess, ref Exception e)
catch (Exception ex)
{
// This shouldn't cause a recursion, as it'll throw if failing
Logger.LogTrace("File {FileName} is Read-Only, unmarking", fileName);
Logger.LogTrace("File {FileName} is Read-Only, attempting to unmark", fileName);
try
{
var info = new FileInfo(fileName);
if (info.IsReadOnly)
{
info.IsReadOnly = false;
}
if (info.IsReadOnly) info.IsReadOnly = false;

// check to see if it stuck. On linux, we can't just winapi hack our way out, so don't recurse in that case, anyway
if (!new FileInfo(fileName).IsReadOnly && !Utils.IsRunningOnLinuxOrMac())
{
return CanAccessFile(fileName, writeAccess, ref e);
return GetFileSize(fileName, accessType);
}
}
catch
Expand All @@ -182,6 +175,13 @@ private long CanAccessFile(string fileName, bool writeAccess, ref Exception e)
}
}

private static long GetFileSize(string fileName, FileAccess accessType)
{
using var fs = File.Open(fileName, FileMode.Open, accessType, FileShare.ReadWrite);
var size = fs.Seek(0, SeekOrigin.End);
return size;
}

private (bool existing, SVR_VideoLocal, SVR_VideoLocal_Place, SVR_ImportFolder) GetVideoLocal()
{
// hash and read media info for file
Expand Down

0 comments on commit af71797

Please sign in to comment.