Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Work folder sweep: never follow a link, and one unreadable folder stops nothing

KnifeLemone4872f3df478
diff --git a/src/Filee.Core/Conversion/JobQueue.cs b/src/Filee.Core/Conversion/JobQueue.cs
index cfa0b825..36c2a8e6 100644
--- a/src/Filee.Core/Conversion/JobQueue.cs
+++ b/src/Filee.Core/Conversion/JobQueue.cs
@@ -712,12 +712,23 @@ public sealed class JobQueue : IAsyncDisposable
{
try
{
- if (!Directory.Exists(workRoot))
+ var root = new DirectoryInfo(workRoot);
+ // A temp folder everyone can write in (/tmp on Linux) may hold a link to somewhere else: never follow one.
+ if (!root.Exists || root.LinkTarget is not null)
return;
- foreach (var folder in Directory.EnumerateDirectories(workRoot))
+ foreach (var folder in root.EnumerateDirectories())
{
- if (IsJobId(Path.GetFileName(folder)) && DateTime.UtcNow - LastWrite(folder) > age)
- TryDelete(folder);
+ if (!IsJobId(folder.Name) || folder.LinkTarget is not null)
+ continue;
+ try
+ {
+ if (DateTime.UtcNow - LastWrite(folder) > age)
+ TryDelete(folder.FullName);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ // Something in it can't be read: it stays, and the other folders are still looked at.
+ }
}
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
@@ -726,11 +737,18 @@ public sealed class JobQueue : IAsyncDisposable
static bool IsJobId(string name) => name.Length == 10 && name.All(char.IsAsciiHexDigitLower);
- // A folder's own time changes only when an entry right inside it does: a long job writes deeper down.
- static DateTime LastWrite(string folder)
+ // A folder's own time changes only when an entry right inside it does: a long job writes deeper down. Links
+ // inside are not followed, and a folder that can't be read throws (the job's folder then stays).
+ static DateTime LastWrite(DirectoryInfo folder)
{
- var last = Directory.GetLastWriteTimeUtc(folder);
- foreach (var entry in new DirectoryInfo(folder).EnumerateFileSystemInfos("*", SearchOption.AllDirectories))
+ var last = folder.LastWriteTimeUtc;
+ var everything = new EnumerationOptions
+ {
+ RecurseSubdirectories = true,
+ AttributesToSkip = FileAttributes.ReparsePoint,
+ IgnoreInaccessible = false,
+ };
+ foreach (var entry in folder.EnumerateFileSystemInfos("*", everything))
{
if (entry.LastWriteTimeUtc > last)
last = entry.LastWriteTimeUtc;
diff --git a/tests/Filee.Core.Tests/JobQueueTests.cs b/tests/Filee.Core.Tests/JobQueueTests.cs
index 3114e7ac..84a50bfd 100644
--- a/tests/Filee.Core.Tests/JobQueueTests.cs
+++ b/tests/Filee.Core.Tests/JobQueueTests.cs
@@ -570,6 +570,30 @@ public class JobQueueTests
Assert.True(Directory.Exists(other));
}
+ [Fact]
+ public void A_link_named_like_a_job_is_never_followed_by_the_work_folder_sweep()
+ {
+ using var root = new TempDir();
+ using var elsewhere = new TempDir();
+ var precious = Path.Combine(elsewhere.Path, "keep.txt");
+ File.WriteAllText(precious, "the user's");
+ File.SetLastWriteTimeUtc(precious, DateTime.UtcNow.AddDays(-30));
+ Directory.SetLastWriteTimeUtc(elsewhere.Path, DateTime.UtcNow.AddDays(-30));
+ var link = Path.Combine(root.Path, "0123456789");
+ try
+ {
+ Directory.CreateSymbolicLink(link, elsewhere.Path);
+ }
+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
+ {
+ Assert.Skip("This machine doesn't let the test make a symbolic link.");
+ }
+
+ JobQueue.DeleteStaleWorkFolders(root.Path, TimeSpan.FromDays(1));
+
+ Assert.True(File.Exists(precious));
+ }
+
[Fact]
public async Task Enqueue_runs_in_background_and_raises_finished()
{