Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Improved symlink scanning.

nzbdav-dev1b8b3efb6ef4
diff --git a/backend/Tasks/RemoveUnlinkedFilesTask.cs b/backend/Tasks/RemoveUnlinkedFilesTask.cs
index b434e1b6..0fbbd1f7 100644
--- a/backend/Tasks/RemoveUnlinkedFilesTask.cs
+++ b/backend/Tasks/RemoveUnlinkedFilesTask.cs
@@ -110,7 +110,7 @@ public class RemoveUnlinkedFilesTask(
private HashSet<Guid> GetLinkedIds()
{
return OrganizedSymlinksUtil.GetLibrarySymlinkTargets(configManager)
- .Select(x => x.Target)
+ .Select(x => x.DavItemId)
.ToHashSet();
}
diff --git a/backend/Utils/OrganizedSymlinksUtil.cs b/backend/Utils/OrganizedSymlinksUtil.cs
index 941c66be..5db9b5e3 100644
--- a/backend/Utils/OrganizedSymlinksUtil.cs
+++ b/backend/Utils/OrganizedSymlinksUtil.cs
@@ -26,11 +26,11 @@ public static class OrganizedSymlinksUtil
/// </summary>
/// <param name="configManager">The application config</param>
/// <returns>All symlinks within the organized media library that point to nzbdav dav-items.</returns>
- public static IEnumerable<(FileInfo FileInfo, Guid Target)> GetLibrarySymlinkTargets(ConfigManager configManager)
+ public static IEnumerable<DavItemSymlink> GetLibrarySymlinkTargets(ConfigManager configManager)
{
var libraryRoot = configManager.GetLibraryDir()!;
- var allSymlinkPaths = Directory.EnumerateFileSystemEntries(libraryRoot, "*", SearchOption.AllDirectories);
- return GetSymlinkTargets(allSymlinkPaths, configManager);
+ var allSymlinks = SymlinkUtil.GetAllSymlinks(libraryRoot);
+ return GetDavItemSymlinks(allSymlinks, configManager);
}
private static bool TryGetSymlinkFromCache
@@ -46,8 +46,14 @@ public static class OrganizedSymlinksUtil
private static bool Verify(string symlink, DavItem targetDavItem, ConfigManager configManager)
{
- return GetSymlinkTargets([symlink], configManager)
- .Select(x => x.Target)
+ var fileInfo = new FileInfo(symlink);
+ var symlinkInfo = new SymlinkUtil.SymlinkInfo()
+ {
+ SymlinkPath = symlink,
+ TargetPath = fileInfo.LinkTarget ?? "",
+ };
+ return GetDavItemSymlinks([symlinkInfo], configManager)
+ .Select(x => x.DavItemId)
.FirstOrDefault() == targetDavItem.Id;
}
@@ -56,32 +62,33 @@ public static class OrganizedSymlinksUtil
string? result = null;
foreach (var symlink in GetLibrarySymlinkTargets(configManager))
{
- Cache[targetDavItem.Id] = symlink.FileInfo.FullName;
- if (symlink.Target == targetDavItem.Id)
- result = symlink.FileInfo.FullName;
+ Cache[targetDavItem.Id] = symlink.SymlinkPath;
+ if (symlink.DavItemId == targetDavItem.Id)
+ result = symlink.SymlinkPath;
}
return result;
}
- private static IEnumerable<(FileInfo FileInfo, Guid Target)> GetSymlinkTargets
+ private static IEnumerable<DavItemSymlink> GetDavItemSymlinks
(
- IEnumerable<string> symlinkPaths,
+ IEnumerable<SymlinkUtil.SymlinkInfo> symlinkInfos,
ConfigManager configManager
)
{
var mountDir = configManager.GetRcloneMountDir();
- return symlinkPaths
- .Select(x => new FileInfo(x))
- .Where(x => x.Attributes.HasFlag(FileAttributes.ReparsePoint))
- .Select(x => (FileInfo: x, Target: x.LinkTarget))
- .Where(x => x.Target is not null)
- .Select(x => (x.FileInfo, Target: x.Target!))
- .Where(x => x.Target.StartsWith(mountDir))
- .Select(x => (x.FileInfo, Target: x.Target.RemovePrefix(mountDir)))
- .Select(x => (x.FileInfo, Target: x.Target.StartsWith("/") ? x.Target : $"/{x.Target}"))
- .Where(x => x.Target.StartsWith("/.ids"))
- .Select(x => (x.FileInfo, Target: Path.GetFileNameWithoutExtension(x.Target)))
- .Select(x => (x.FileInfo, Target: Guid.Parse(x.Target)));
+ return symlinkInfos
+ .Where(x => x.TargetPath.StartsWith(mountDir))
+ .Select(x => x with { TargetPath = x.TargetPath.RemovePrefix(mountDir) })
+ .Select(x => x with { TargetPath = x.TargetPath.StartsWith('/') ? x.TargetPath : $"/{x.TargetPath}" })
+ .Where(x => x.TargetPath.StartsWith("/.ids"))
+ .Select(x => x with { TargetPath = Path.GetFileNameWithoutExtension(x.TargetPath) })
+ .Select(x => new DavItemSymlink() { SymlinkPath = x.SymlinkPath, DavItemId = Guid.Parse(x.TargetPath) });
+ }
+
+ public struct DavItemSymlink
+ {
+ public string SymlinkPath;
+ public Guid DavItemId;
}
}
\ No newline at end of file
diff --git a/backend/Utils/SymlinkUtil.cs b/backend/Utils/SymlinkUtil.cs
new file mode 100644
index 00000000..cb284393
--- /dev/null
+++ b/backend/Utils/SymlinkUtil.cs
@@ -0,0 +1,70 @@
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+
+namespace NzbWebDAV.Utils;
+
+public static class SymlinkUtil
+{
+ private static readonly bool IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
+
+ public static IEnumerable<SymlinkInfo> GetAllSymlinks(string directoryPath)
+ {
+ return IsLinux
+ ? GetAllSymlinksLinux(directoryPath)
+ : GetAllSymlinksWindows(directoryPath);
+ }
+
+ private static IEnumerable<SymlinkInfo> GetAllSymlinksLinux(string directoryPath)
+ {
+ const string command =
+ """
+ find . -type l -print0 | xargs -0 sh -c '
+ for link_path in \"$@\"; do
+ echo \"$link_path\"
+ echo \"$(readlink \"$link_path\")\"
+ done
+ ' sh
+ """;
+
+ var escapedDirectory = directoryPath.Replace("'", "'\"'\"'");
+ var startInfo = new ProcessStartInfo
+ {
+ FileName = "sh",
+ Arguments = $"-c \"cd '{escapedDirectory}' && {command}\"",
+ RedirectStandardOutput = true,
+ RedirectStandardError = true,
+ UseShellExecute = false,
+ CreateNoWindow = true
+ };
+
+ using var process = Process.Start(startInfo)!;
+ while (process.StandardOutput.EndOfStream == false)
+ {
+ var symlinkPath = process.StandardOutput.ReadLine();
+ if (symlinkPath == null) break;
+ var targetPath = process.StandardOutput.ReadLine();
+ if (targetPath == null) break;
+
+ yield return new SymlinkInfo()
+ {
+ SymlinkPath = Path.GetFullPath(symlinkPath, directoryPath),
+ TargetPath = targetPath
+ };
+ }
+ }
+
+ private static IEnumerable<SymlinkInfo> GetAllSymlinksWindows(string directoryPath)
+ {
+ return Directory.EnumerateFileSystemEntries(directoryPath, "*", SearchOption.AllDirectories)
+ .Select(x => new FileInfo(x))
+ .Where(x => x.Attributes.HasFlag(FileAttributes.ReparsePoint))
+ .Where(x => x.LinkTarget is not null)
+ .Select(x => new SymlinkInfo() { SymlinkPath = x.FullName, TargetPath = x.LinkTarget! });
+ }
+
+ public struct SymlinkInfo
+ {
+ public string SymlinkPath;
+ public string TargetPath;
+ }
+}
\ No newline at end of file