Skip to content

Commit

Permalink
Optimize cross-filesystem operations
Browse files Browse the repository at this point in the history
Adds a new  function to  that resolves a path
through all underlying filesystems. Allows cross-file system operations
to resolve to the final FS and call operations directly on that file system.
  • Loading branch information
agocke committed Jun 23, 2024
1 parent a4b9faf commit 7a83a45
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 45 deletions.
59 changes: 56 additions & 3 deletions src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Zio.FileSystems;
Expand Down Expand Up @@ -36,10 +36,10 @@ public void TestCopyFileSystemSubFolder()
fs.CopyTo(dest, subFolder, true);

var destSubFileSystem = dest.GetOrCreateSubFileSystem(subFolder);

AssertFileSystemEqual(fs, destSubFileSystem);
}


[Fact]
public void TestWatcher()
Expand Down Expand Up @@ -80,4 +80,57 @@ public void TestDispose()
memfs.Dispose();
Assert.Throws<ObjectDisposedException>(() => memfs.DirectoryExists("/"));
}

[Fact]
public void TestCopyFileCross()
{
var fs = new TriggerMemoryFileSystem();
fs.CreateDirectory("/sub1");
fs.CreateDirectory("/sub2");
var sub1 = new SubFileSystem(fs, "/sub1");
var sub2 = new SubFileSystem(fs, "/sub2");
sub1.WriteAllText("/file.txt", "test");
sub1.CopyFileCross("/file.txt", sub2, "/file.txt", overwrite: false);
Assert.Equal("test", sub2.ReadAllText("/file.txt"));
Assert.Equal(TriggerMemoryFileSystem.TriggerType.Copy, fs.Triggered);
}

[Fact]
public void TestMoveFileCross()
{
var fs = new TriggerMemoryFileSystem();
fs.CreateDirectory("/sub1");
fs.CreateDirectory("/sub2");
var sub1 = new SubFileSystem(fs, "/sub1");
var sub2 = new SubFileSystem(fs, "/sub2");
sub1.WriteAllText("/file.txt", "test");
sub1.MoveFileCross("/file.txt", sub2, "/file.txt");
Assert.Equal("test", sub2.ReadAllText("/file.txt"));
Assert.False(sub1.FileExists("/file.txt"));
Assert.Equal(TriggerMemoryFileSystem.TriggerType.Move, fs.Triggered);
}

private sealed class TriggerMemoryFileSystem : MemoryFileSystem
{
public enum TriggerType
{
None,
Copy,
Move
}

public TriggerType Triggered { get; private set; } = TriggerType.None;

protected override void CopyFileImpl(UPath srcPath, UPath destPath, bool overwrite)
{
Triggered = TriggerType.Copy;
base.CopyFileImpl(srcPath, destPath, overwrite);
}

protected override void MoveFileImpl(UPath srcPath, UPath destPath)
{
Triggered = TriggerType.Move;
base.MoveFileImpl(srcPath, destPath);
}
}
}
14 changes: 12 additions & 2 deletions src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.IO;
Expand Down Expand Up @@ -352,7 +352,7 @@ public void TestEnumerate()
var expectedPaths = Directory.EnumerateFileSystemEntries(Path.GetFullPath(Path.Combine(SystemPath, "../.."))).ToList();
Assert.Equal(expectedPaths, paths);
}

[SkippableFact]
public void TestFileWindowsExceptions()
{
Expand Down Expand Up @@ -552,4 +552,14 @@ public void TestFileSymlink()
SafeDeleteFile(systemPathDest);
}
}

[Fact]
public void TestResolvePath()
{
var fs = new PhysicalFileSystem();
var testPath = fs.ConvertPathFromInternal(SystemPath);
var (resFs, resPath) = fs.ResolvePath(testPath);
Assert.Equal(testPath, resPath);
Assert.Equal(fs, resFs);
}
}
25 changes: 23 additions & 2 deletions src/Zio.Tests/FileSystems/TestSubFileSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using System.IO;
Expand Down Expand Up @@ -36,7 +36,7 @@ public void TestBasic()
}

// TODO: We could add another test just to make sure that files can be created...etc. But the test above should already cover the code provided in SubFileSystem
}
}

[Fact]
public void TestGetOrCreateFileSystem()
Expand Down Expand Up @@ -74,6 +74,27 @@ public void TestWatcher()
Assert.True(gotChange);
}

[Fact]
public void TestResolvePath()
{
var fs = GetCommonMemoryFileSystem();
var subFs = fs.GetOrCreateSubFileSystem("/a/b");
var (resFs, resPath) = subFs.ResolvePath("/c");
Assert.Equal("/a/b/c", resPath);
Assert.NotEqual(subFs, resFs);
Assert.Equal(fs, resFs);
(resFs, resPath) = subFs.ResolvePath("/c/d");
Assert.Equal("/a/b/c/d", resPath);
Assert.NotEqual(subFs, resFs);
Assert.Equal(fs, resFs);

var subFs2 = subFs.GetOrCreateSubFileSystem("/q");
(resFs, resPath) = subFs2.ResolvePath("/c");
Assert.Equal("/a/b/q/c", resPath);
Assert.NotEqual(subFs2, resFs);
Assert.Equal(fs, resFs);
}

[SkippableFact]
public void TestDirectorySymlink()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Zio.Tests/Zio.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net472</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>10</LangVersion>
</PropertyGroup>
Expand Down
Loading

0 comments on commit 7a83a45

Please sign in to comment.