From e1ec9bd9e3eeb4e30765f06f19188c0493f8a040 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 8 Jul 2024 15:13:06 -0500 Subject: ensure non null FileInfo --- src/slskd/Files/FileService.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src') 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 /// /// Resolves an instance of for the specified , 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. /// /// The fully qualified filename for which to resolve the FileInfo instance. /// The resolved FileInfo instance. /// Thrown if the specified filename is null or contains only whitespace. /// Thrown if the specified or linked file is restricted. - /// Thrown if the specified or linked file can't be accessed for some reason. + /// Thrown if the specified or linked file can't be resolved for some reason. 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; } /// -- cgit v1.2.3