aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-07-07 18:22:14 -0500
committerJP Dillingham <jp@dillingham.me>2024-07-07 18:22:14 -0500
commit02444a36017bc094d9f28fa71a782dabb320190d (patch)
tree9ffd20a8fd4e7c0a93df22038f53e826bf3d73bb
parentc8a4aedf91f296a0de4c292c198cf8020cb5fd35 (diff)
downloadslskd-02444a36017bc094d9f28fa71a782dabb320190d.tar.gz
add ResolveFileInfo()
-rw-r--r--src/slskd/Files/FileService.cs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs
index c5c0382f..ccd1f904 100644
--- a/src/slskd/Files/FileService.cs
+++ b/src/slskd/Files/FileService.cs
@@ -23,6 +23,7 @@ namespace slskd.Files
using System.Collections.Generic;
using System.IO;
using System.Linq;
+ using System.Security;
using System.Threading.Tasks;
using OneOf;
using Serilog;
@@ -51,6 +52,57 @@ namespace slskd.Files
private IOptionsMonitor<Options> OptionsMonitor { get; }
/// <summary>
+ /// Resolves an instance of <see cref="FileInfo"/> for the specified <paramref name="filename"/>, following
+ /// any symlinks that may be present to their final target.
+ /// </summary>
+ /// <param name="filename">The fully qualified filename for which to resolve the FileInfo instance.</param>
+ /// <returns>The resolved FileInfo instance.</returns>
+ /// <exception cref="ArgumentException">Thrown if the specified filename is null or contains only whitespace.</exception>
+ /// <exception cref="UnauthorizedException">Thrown if the specified or linked file is restricted.</exception>
+ /// <exception cref="IOException">Thrown if the specified or linked file can't be accessed for some reason.</exception>
+ public virtual FileInfo ResolveFileInfo(string filename)
+ {
+ ArgumentException.ThrowIfNullOrWhiteSpace(filename, nameof(filename));
+
+ FileInfo info;
+
+ try
+ {
+ info = new FileInfo(filename);
+ }
+ catch (Exception ex) when (ex is UnauthorizedAccessException || ex is SecurityException)
+ {
+ throw new UnauthorizedException($"Access to the file '{filename}' was denied: {ex.Message}", ex);
+ }
+ catch (Exception ex)
+ {
+ throw new IOException($"Failed to access file '{filename}': {ex.Message}", ex);
+ }
+
+ // if the above didn't throw we are guaranteed a valid instance of FileInfo regardless of whether
+ // the file exists or if it is a symlink. if it doesn't exist it can't be a symlink, so just return it
+ if (!info.Exists)
+ {
+ return info;
+ }
+
+ // LinkTarget is guaranteed to be null if the file isn't a symlink. docs:
+ // > Gets the target path of the link located in FullName, or null if this FileSystemInfo instance doesn't represent a link.
+ if (info.LinkTarget is null)
+ {
+ return info;
+ }
+
+ // ResolveLinkTarget returns an instance of FileSystemInfo (which is being cast to FileInfo here) as long as
+ // the link itself exists (which we've checked), and regardless of whether the link target itself exists.
+ // we should only get this far if:
+ // 1) the given file exists and
+ // 2) it is a symlink, meaning
+ // this line _SHOULD_ be guaranteed to return an instance of FileInfo.
+ return (FileInfo)info.ResolveLinkTarget(returnFinalTarget: true);
+ }
+
+ /// <summary>
/// Deletes the specified <paramref name="directories"/>.
/// </summary>
/// <remarks>