From 574a76d5e58434db98d7743f05fb9149e5a68f27 Mon Sep 17 00:00:00 2001 From: Gerard Smit Date: Tue, 9 Jul 2024 23:03:38 +0200 Subject: [PATCH 1/2] Fix cross-filesystem operations in MountFileSystem --- .../FileSystems/TestMemoryFileSystem.cs | 17 +++++++++++++++++ src/Zio/FileSystems/ComposeFileSystem.cs | 2 +- src/Zio/FileSystems/MountFileSystem.cs | 13 +++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs b/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs index e1bb9c6..26a60bd 100644 --- a/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestMemoryFileSystem.cs @@ -93,6 +93,23 @@ public void TestMoveFileCross() Assert.Equal(TriggerMemoryFileSystem.TriggerType.Move, fs.Triggered); } + [Fact] + public void TestMoveFileCrossMount() + { + var fs = new TriggerMemoryFileSystem(); + fs.CreateDirectory("/sub1"); + fs.CreateDirectory("/sub2"); + var mount = new MountFileSystem(); + var sub1 = new SubFileSystem(fs, "/sub1"); + var sub2 = new SubFileSystem(fs, "/sub2"); + mount.Mount("/sub2-mount", sub2); + sub1.WriteAllText("/file.txt", "test"); + sub1.MoveFileCross("/file.txt", mount, "/sub2-mount/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 diff --git a/src/Zio/FileSystems/ComposeFileSystem.cs b/src/Zio/FileSystems/ComposeFileSystem.cs index 1dc14b4..ff8b418 100644 --- a/src/Zio/FileSystems/ComposeFileSystem.cs +++ b/src/Zio/FileSystems/ComposeFileSystem.cs @@ -276,5 +276,5 @@ protected override UPath ConvertPathFromInternalImpl(string innerPath) protected abstract UPath ConvertPathFromDelegate(UPath path); protected override (IFileSystem FileSystem, UPath Path) ResolvePathImpl(UPath path) - => FallbackSafe.ResolvePath(ConvertPathToDelegate(path)); + => Fallback?.ResolvePath(ConvertPathToDelegate(path)) ?? (this, path); } \ No newline at end of file diff --git a/src/Zio/FileSystems/MountFileSystem.cs b/src/Zio/FileSystems/MountFileSystem.cs index 4b486af..514b17f 100644 --- a/src/Zio/FileSystems/MountFileSystem.cs +++ b/src/Zio/FileSystems/MountFileSystem.cs @@ -593,6 +593,19 @@ protected override bool TryResolveLinkTargetImpl(UPath linkPath, out UPath resol return true; } + /// + protected override (IFileSystem FileSystem, UPath Path) ResolvePathImpl(UPath path) + { + var mountfs = TryGetMountOrNext(ref path); + + if (mountfs is null) + { + return base.ResolvePathImpl(path); + } + + return mountfs.ResolvePath(path); + } + /// protected override IEnumerable EnumeratePathsImpl(UPath path, string searchPattern, SearchOption searchOption, SearchTarget searchTarget) { From 7e7f667af7ea922d589b96ceb5f49f4263d79623 Mon Sep 17 00:00:00 2001 From: Gerard Smit Date: Tue, 9 Jul 2024 23:09:12 +0200 Subject: [PATCH 2/2] Remove duplicated code --- src/Zio/FileSystems/ComposeFileSystem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Zio/FileSystems/ComposeFileSystem.cs b/src/Zio/FileSystems/ComposeFileSystem.cs index ff8b418..7605c7d 100644 --- a/src/Zio/FileSystems/ComposeFileSystem.cs +++ b/src/Zio/FileSystems/ComposeFileSystem.cs @@ -276,5 +276,5 @@ protected override UPath ConvertPathFromInternalImpl(string innerPath) protected abstract UPath ConvertPathFromDelegate(UPath path); protected override (IFileSystem FileSystem, UPath Path) ResolvePathImpl(UPath path) - => Fallback?.ResolvePath(ConvertPathToDelegate(path)) ?? (this, path); + => Fallback?.ResolvePath(ConvertPathToDelegate(path)) ?? base.ResolvePathImpl(path); } \ No newline at end of file