From 186c4e9f3e37d09b1cf798ae4d145f3141efe507 Mon Sep 17 00:00:00 2001 From: Gerard Smit Date: Fri, 24 May 2024 14:50:51 +0200 Subject: [PATCH] Refactor ResolveLinkTarget to TryResolveLinkTarget --- .../FileSystems/FileSystemEntryRedirect.cs | 4 ++-- .../FileSystems/TestPhysicalFileSystem.cs | 6 ++++-- src/Zio/FileSystems/ComposeFileSystem.cs | 11 ++++++++--- src/Zio/FileSystems/FileSystem.cs | 7 ++++--- src/Zio/FileSystems/MemoryFileSystem.cs | 5 +++-- src/Zio/FileSystems/MountFileSystem.cs | 18 +++++++++++++++--- src/Zio/FileSystems/PhysicalFileSystem.cs | 14 +++++++++++--- src/Zio/FileSystems/ZipArchiveFileSystem.cs | 5 +++-- src/Zio/IFileSystem.cs | 3 ++- 9 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/Zio.Tests/FileSystems/FileSystemEntryRedirect.cs b/src/Zio.Tests/FileSystems/FileSystemEntryRedirect.cs index e1237cf..9f4f4e5 100644 --- a/src/Zio.Tests/FileSystems/FileSystemEntryRedirect.cs +++ b/src/Zio.Tests/FileSystems/FileSystemEntryRedirect.cs @@ -125,9 +125,9 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) _fs.CreateSymbolicLink(path, pathToTarget); } - protected override UPath? ResolveLinkTargetImpl(UPath linkPath) + protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath) { - return _fs.ResolveLinkTarget(linkPath); + return _fs.TryResolveLinkTarget(linkPath, out resolvedPath); } protected override IEnumerable EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) diff --git a/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs b/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs index a14669c..e09a873 100644 --- a/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs @@ -443,7 +443,8 @@ public void TestDirectorySymlink() fs.CreateSymbolicLink(pathDest, pathSource); // ResolveSymbolicLink - Assert.Equal(pathSource, fs.ResolveLinkTarget(pathDest)); + Assert.True(fs.TryResolveLinkTarget(pathDest, out var resolvedPath)); + Assert.Equal(pathSource, resolvedPath); // FileExists Assert.True(fs.FileExists(filePathDest)); @@ -491,7 +492,8 @@ public void TestFileSymlink() fs.CreateSymbolicLink(pathDest, pathSource); // ResolveSymbolicLink - Assert.Equal(pathSource, fs.ResolveLinkTarget(pathDest)); + Assert.True(fs.TryResolveLinkTarget(pathDest, out var resolvedPath)); + Assert.Equal(pathSource, resolvedPath); // FileExists Assert.True(fs.FileExists(pathDest)); diff --git a/src/Zio/FileSystems/ComposeFileSystem.cs b/src/Zio/FileSystems/ComposeFileSystem.cs index 546ddb7..f201821 100644 --- a/src/Zio/FileSystems/ComposeFileSystem.cs +++ b/src/Zio/FileSystems/ComposeFileSystem.cs @@ -193,11 +193,16 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) } /// - protected override UPath? ResolveLinkTargetImpl(UPath linkPath) + protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath) { - var path = FallbackSafe.ResolveLinkTarget(ConvertPathToDelegate(linkPath)); + if (!FallbackSafe.TryResolveLinkTarget(ConvertPathToDelegate(linkPath), out var resolvedPathDelegate)) + { + resolvedPath = default; + return false; + } - return path.HasValue ? ConvertPathFromDelegate(path.Value) : default(UPath?); + resolvedPath = ConvertPathFromDelegate(resolvedPathDelegate); + return true; } // ---------------------------------------------- diff --git a/src/Zio/FileSystems/FileSystem.cs b/src/Zio/FileSystems/FileSystem.cs index ce4ea0f..da7f714 100644 --- a/src/Zio/FileSystems/FileSystem.cs +++ b/src/Zio/FileSystems/FileSystem.cs @@ -447,17 +447,18 @@ public void CreateSymbolicLink(UPath path, UPath pathToTarget) /// - public UPath? ResolveLinkTarget(UPath linkPath) + public bool TryResolveLinkTarget(UPath linkPath, out UPath resolvedPath) { AssertNotDisposed(); - return ResolveLinkTargetImpl(ValidatePath(linkPath)); + return TryResolveLinkTargetImpl(ValidatePath(linkPath), out resolvedPath); } /// /// Resolves the target of a symbolic link. /// /// The path of the symbolic link to resolve. - protected abstract UPath? ResolveLinkTargetImpl(UPath linkPath); + /// + protected abstract bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath); // ---------------------------------------------- // Search API diff --git a/src/Zio/FileSystems/MemoryFileSystem.cs b/src/Zio/FileSystems/MemoryFileSystem.cs index 88669c6..3a14757 100644 --- a/src/Zio/FileSystems/MemoryFileSystem.cs +++ b/src/Zio/FileSystems/MemoryFileSystem.cs @@ -760,9 +760,10 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) } /// - protected override UPath? ResolveLinkTargetImpl(UPath linkPath) + protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath) { - return null; + resolvedPath = default; + return false; } // ---------------------------------------------- diff --git a/src/Zio/FileSystems/MountFileSystem.cs b/src/Zio/FileSystems/MountFileSystem.cs index f08b7a5..2539e38 100644 --- a/src/Zio/FileSystems/MountFileSystem.cs +++ b/src/Zio/FileSystems/MountFileSystem.cs @@ -566,12 +566,24 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) } /// - protected override UPath? ResolveLinkTargetImpl(UPath linkPath) + protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath) { var mountfs = TryGetMountOrNext(ref linkPath, out var mountPath); - var path = mountfs?.ResolveLinkTarget(linkPath); - return path != null ? CombinePrefix(mountPath, path.Value) : default(UPath?); + if (mountfs is null) + { + resolvedPath = default; + return false; + } + + if (!mountfs.TryResolveLinkTarget(linkPath, out var resolved)) + { + resolvedPath = default; + return false; + } + + resolvedPath = CombinePrefix(mountPath, resolved); + return true; } /// diff --git a/src/Zio/FileSystems/PhysicalFileSystem.cs b/src/Zio/FileSystems/PhysicalFileSystem.cs index 0b9ec43..67a7657 100644 --- a/src/Zio/FileSystems/PhysicalFileSystem.cs +++ b/src/Zio/FileSystems/PhysicalFileSystem.cs @@ -516,7 +516,7 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) } /// - protected override UPath? ResolveLinkTargetImpl(UPath linkPath) + protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath) { if (IsWithinSpecialDirectory(linkPath)) { @@ -536,7 +536,8 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) } else { - return null; + resolvedPath = default; + return false; } #if NET7_0_OR_GREATER @@ -545,7 +546,14 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) var systemResult = IsOnWindows ? Interop.Windows.GetFinalPathName(systemPath) : Interop.Unix.readlink(systemPath); #endif - return systemResult != null ? ConvertPathFromInternal(systemResult) : default(UPath?); + if (systemResult == null) + { + resolvedPath = default; + return false; + } + + resolvedPath = ConvertPathFromInternal(systemResult); + return true; } // ---------------------------------------------- diff --git a/src/Zio/FileSystems/ZipArchiveFileSystem.cs b/src/Zio/FileSystems/ZipArchiveFileSystem.cs index 575c7ad..fdc81de 100644 --- a/src/Zio/FileSystems/ZipArchiveFileSystem.cs +++ b/src/Zio/FileSystems/ZipArchiveFileSystem.cs @@ -824,9 +824,10 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget) } /// - protected override UPath? ResolveLinkTargetImpl(UPath linkPath) + protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath) { - return null; + resolvedPath = UPath.Empty; + return false; } /// diff --git a/src/Zio/IFileSystem.cs b/src/Zio/IFileSystem.cs index 7d665a3..7210000 100644 --- a/src/Zio/IFileSystem.cs +++ b/src/Zio/IFileSystem.cs @@ -174,7 +174,8 @@ public interface IFileSystem : IDisposable /// Resolves the target of a symbolic link. /// /// The path of the symbolic link to resolve. - UPath? ResolveLinkTarget(UPath linkPath); + /// The path of the symbolic link resolved if true is returned. + bool TryResolveLinkTarget(UPath linkPath, out UPath resolvedPath); // ---------------------------------------------- // Search API