aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Files/FileService.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2024-07-08 15:13:06 -0500
committerJP Dillingham <jp@dillingham.me>2024-07-08 15:13:06 -0500
commite1ec9bd9e3eeb4e30765f06f19188c0493f8a040 (patch)
tree41f58c71feb74e28396c03c536f107b94cb711e9 /src/slskd/Files/FileService.cs
parenta18bb0366ef70670f91a881e55ef10a73f968337 (diff)
downloadslskd-e1ec9bd9e3eeb4e30765f06f19188c0493f8a040.tar.gz
ensure non null FileInfo
Diffstat (limited to 'src/slskd/Files/FileService.cs')
-rw-r--r--src/slskd/Files/FileService.cs22
1 files changed, 19 insertions, 3 deletions
diff --git a/src/slskd/Files/FileService.cs b/src/slskd/Files/FileService.cs
index ccd1f904..aa2e42d4 100644
--- a/src/slskd/Files/FileService.cs
+++ b/src/slskd/Files/FileService.cs
@@ -53,13 +53,13 @@ namespace slskd.Files
/// <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.
+ /// 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 accessed for some reason.</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));
@@ -99,7 +99,23 @@ namespace slskd.Files
// 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);
+ try
+ {
+ info = (FileInfo)info.ResolveLinkTarget(returnFinalTarget: true);
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Failed to resolve FileInfo for file '{File}': {Message}", filename, ex.Message);
+ throw new IOException($"Failed to resolve FileInfo for file '{filename}': {ex.Message}", ex);
+ }
+
+ if (info is null)
+ {
+ Log.Error("Resolved FileInfo for file '{File}' was unexpectedly null", filename);
+ throw new IOException($"An unexpected error was encountered while resolving FileInfo for '{filename}'");
+ }
+
+ return info;
}
/// <summary>