Skip to content

Commit

Permalink
Refactor ResolveLinkTarget to TryResolveLinkTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardSmit committed May 24, 2024
1 parent d9f3442 commit 186c4e9
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Zio.Tests/FileSystems/FileSystemEntryRedirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UPath> EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget)
Expand Down
6 changes: 4 additions & 2 deletions src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
11 changes: 8 additions & 3 deletions src/Zio/FileSystems/ComposeFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,16 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget)
}

/// <inheritdoc />
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;
}

// ----------------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions src/Zio/FileSystems/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,18 @@ public void CreateSymbolicLink(UPath path, UPath pathToTarget)


/// <inheritdoc />
public UPath? ResolveLinkTarget(UPath linkPath)
public bool TryResolveLinkTarget(UPath linkPath, out UPath resolvedPath)
{
AssertNotDisposed();
return ResolveLinkTargetImpl(ValidatePath(linkPath));
return TryResolveLinkTargetImpl(ValidatePath(linkPath), out resolvedPath);
}

/// <summary>
/// Resolves the target of a symbolic link.
/// </summary>
/// <param name="linkPath">The path of the symbolic link to resolve.</param>
protected abstract UPath? ResolveLinkTargetImpl(UPath linkPath);
/// <param name="resolvedPath"></param>
protected abstract bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath);

// ----------------------------------------------
// Search API
Expand Down
5 changes: 3 additions & 2 deletions src/Zio/FileSystems/MemoryFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,10 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget)
}

/// <inheritdoc />
protected override UPath? ResolveLinkTargetImpl(UPath linkPath)
protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath)
{
return null;
resolvedPath = default;
return false;
}

// ----------------------------------------------
Expand Down
18 changes: 15 additions & 3 deletions src/Zio/FileSystems/MountFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,24 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget)
}

/// <inheritdoc />
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;
}

/// <inheritdoc />
Expand Down
14 changes: 11 additions & 3 deletions src/Zio/FileSystems/PhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget)
}

/// <inheritdoc />
protected override UPath? ResolveLinkTargetImpl(UPath linkPath)
protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath)
{
if (IsWithinSpecialDirectory(linkPath))
{
Expand All @@ -536,7 +536,8 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget)
}
else
{
return null;
resolvedPath = default;
return false;
}

#if NET7_0_OR_GREATER
Expand All @@ -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;
}

// ----------------------------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/Zio/FileSystems/ZipArchiveFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -824,9 +824,10 @@ protected override void CreateSymbolicLinkImpl(UPath path, UPath pathToTarget)
}

/// <inheritdoc />
protected override UPath? ResolveLinkTargetImpl(UPath linkPath)
protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resolvedPath)
{
return null;
resolvedPath = UPath.Empty;
return false;
}

/// <inheritdoc />
Expand Down
3 changes: 2 additions & 1 deletion src/Zio/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ public interface IFileSystem : IDisposable
/// Resolves the target of a symbolic link.
/// </summary>
/// <param name="linkPath">The path of the symbolic link to resolve.</param>
UPath? ResolveLinkTarget(UPath linkPath);
/// <param name="resolvedPath">The path of the symbolic link resolved if true is returned.</param>
bool TryResolveLinkTarget(UPath linkPath, out UPath resolvedPath);

// ----------------------------------------------
// Search API
Expand Down

0 comments on commit 186c4e9

Please sign in to comment.