aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2026-09-06 14:03:08 -0500
committerJP Dillingham <jp@dillingham.me>2026-09-06 14:03:08 -0500
commitfbeb97958bafe339b470f41db2780d2db505427f (patch)
tree6b4771f09d2d1b99a0000a8a31aadcc8a8f12864
parent7140111857675a5c6f43de3eac262e1683ec0b46 (diff)
downloadslskd-fbeb97958bafe339b470f41db2780d2db505427f.tar.gz
add an overload of ResolveFileInfo accepting a FileInfo instance
-rw-r--r--src/slskd/Files/FileService.cs87
1 files changed, 53 insertions, 34 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs
index 62d17f20..8450ba48 100644
--- a/src/slskd/Files/FileService.cs
+++ b/src/slskd/Files/FileService.cs
@@ -67,42 +67,19 @@ namespace slskd.Files
private IOptionsMonitor<Options> OptionsMonitor { get; }
/// <summary>
- /// Resolves an instance of <see cref="FileInfo"/> for the specified <paramref name="filename"/>, following
+ /// Resolves an instance of <see cref="FileInfo"/> for the specified <paramref name="info"/>, following
/// any symlinks that may be present to their final target. A non-null return value is guaranteed.
/// </summary>
- /// <param name="filename">The fully qualified filename for which to resolve the FileInfo instance.</param>
+ /// <param name="info">The FileInfo instance for which to follow symlinks.</param>
/// <returns>The resolved FileInfo instance.</returns>
- /// <exception cref="ArgumentException">Thrown if the specified filename is null or contains only whitespace.</exception>
+ /// <exception cref="ArgumentException">Thrown if the specified FileInfo is null.</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 resolved for some reason.</exception>
- public virtual FileInfo ResolveFileInfo(string filename)
+ public virtual FileInfo ResolveFileInfo(FileInfo info)
{
- ArgumentException.ThrowIfNullOrWhiteSpace(filename, nameof(filename));
-
- if (Path.GetFullPath(filename) != filename)
- {
- throw new ArgumentException("Only absolute paths may be specified", nameof(filename));
- }
-
- if (FileSafety.ContainsTraversalSegments(filename))
- {
- Log.Warning("Suspicious attempt to resolve file info for a filename containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {File}", filename);
- throw new ArgumentException("Filenames containing traversal segments are not allowed", 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)
+ if (info is null)
{
- throw new IOException($"Failed to access file '{filename}': {ex.Message}", ex);
+ throw new ArgumentException("The specified FileInfo is null", nameof(info));
}
// if the above didn't throw we are guaranteed a valid instance of FileInfo regardless of whether
@@ -130,24 +107,66 @@ namespace slskd.Files
}
catch (Exception ex) when (ex is UnauthorizedAccessException || ex is SecurityException)
{
- throw new UnauthorizedException($"Access to the linked file '{filename}->{info.LinkTarget}' was denied: {ex.Message}", ex);
+ throw new UnauthorizedException($"Access to the linked file '{info.Name}->{info.LinkTarget}' was denied: {ex.Message}", ex);
}
catch (Exception ex)
{
- Log.Error(ex, "Failed to resolve FileInfo for linked file '{File}->{Link}': {Message}", filename, info.LinkTarget, ex.Message);
- throw new IOException($"Failed to resolve FileInfo for linked file '{filename}->{info.LinkTarget}': {ex.Message}", ex);
+ Log.Error(ex, "Failed to resolve FileInfo for linked file '{File}->{Link}': {Message}", info.Name, info.LinkTarget, ex.Message);
+ throw new IOException($"Failed to resolve FileInfo for linked file '{info.Name}->{info.LinkTarget}': {ex.Message}", ex);
}
if (info is null)
{
- Log.Error("Resolved FileInfo for linked file '{File}->{Link}' was unexpectedly null", filename, info.LinkTarget);
- throw new IOException($"An unexpected error was encountered while resolving FileInfo for linked file '{filename}->{info.LinkTarget}'");
+ Log.Error("Resolved FileInfo for linked file '{File}->{Link}' was unexpectedly null", info.Name, info.LinkTarget);
+ throw new IOException($"An unexpected error was encountered while resolving FileInfo for linked file '{info.Name}->{info.LinkTarget}'");
}
return info;
}
/// <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. A non-null return value is guaranteed.
+ /// </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 resolved for some reason.</exception>
+ public virtual FileInfo ResolveFileInfo(string filename)
+ {
+ ArgumentException.ThrowIfNullOrWhiteSpace(filename, nameof(filename));
+
+ if (Path.GetFullPath(filename) != filename)
+ {
+ throw new ArgumentException("Only absolute paths may be specified", nameof(filename));
+ }
+
+ if (FileSafety.ContainsTraversalSegments(filename))
+ {
+ Log.Warning("Suspicious attempt to resolve file info for a filename containing unsafe path segments (one or more of path traversal characters '.' and '..'). Requested file: {File}", filename);
+ throw new ArgumentException("Filenames containing traversal segments are not allowed", 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);
+ }
+
+ return ResolveFileInfo(info);
+ }
+
+ /// <summary>
/// Deletes the specified <paramref name="directories"/>.
/// </summary>
/// <remarks>