From 64818b4131c1fd444fec10e0f3e19e40d0c19339 Mon Sep 17 00:00:00 2001 From: Gerard Smit Date: Wed, 15 May 2024 12:50:51 +0200 Subject: [PATCH] Fix changing directory times --- .../FileSystems/TestPhysicalFileSystem.cs | 13 +++++++ src/Zio/FileSystems/PhysicalFileSystem.cs | 37 +++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs b/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs index b2651ba..20c3620 100644 --- a/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs +++ b/src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs @@ -89,6 +89,19 @@ public void TestDirectory() fs.CreateDirectory(pathToCreate); Assert.True(Directory.Exists(systemPathToCreate)); + // LastAccessTime + // LastWriteTime + // CreationTime + var lastWriteTime = DateTime.Now + TimeSpan.FromSeconds(10); + var lastAccessTime = DateTime.Now + TimeSpan.FromSeconds(11); + var creationTime = DateTime.Now + TimeSpan.FromSeconds(12); + fs.SetLastWriteTime(pathToCreate, lastWriteTime); + fs.SetLastAccessTime(pathToCreate, lastAccessTime); + fs.SetCreationTime(pathToCreate, creationTime); + Assert.Equal(lastWriteTime, fs.GetLastWriteTime(pathToCreate)); + Assert.Equal(lastAccessTime, fs.GetLastAccessTime(pathToCreate)); + Assert.Equal(creationTime, fs.GetCreationTime(pathToCreate)); + // DirectoryExists Assert.True(fs.DirectoryExists(pathToCreate)); Assert.False(fs.DirectoryExists(pathToCreate / "not_found")); diff --git a/src/Zio/FileSystems/PhysicalFileSystem.cs b/src/Zio/FileSystems/PhysicalFileSystem.cs index d393723..cccfa1e 100644 --- a/src/Zio/FileSystems/PhysicalFileSystem.cs +++ b/src/Zio/FileSystems/PhysicalFileSystem.cs @@ -302,7 +302,17 @@ protected override void SetCreationTimeImpl(UPath path, DateTime time) throw new UnauthorizedAccessException($"Cannot set creation time on system directory `{path}`"); } - File.SetCreationTime(ConvertPathToInternal(path), time); + var internalPath = ConvertPathToInternal(path); + var attributes = File.GetAttributes(internalPath); + + if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) + { + Directory.SetCreationTime(internalPath, time); + } + else + { + File.SetCreationTime(internalPath, time); + } } /// @@ -353,7 +363,18 @@ protected override void SetLastAccessTimeImpl(UPath path, DateTime time) } throw new UnauthorizedAccessException($"Cannot set last access time on system directory `{path}`"); } - File.SetLastAccessTime(ConvertPathToInternal(path), time); + + var internalPath = ConvertPathToInternal(path); + var attributes = File.GetAttributes(internalPath); + + if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) + { + Directory.SetLastAccessTime(internalPath, time); + } + else + { + File.SetLastAccessTime(internalPath, time); + } } /// @@ -405,7 +426,17 @@ protected override void SetLastWriteTimeImpl(UPath path, DateTime time) throw new UnauthorizedAccessException($"Cannot set last write time on system directory `{path}`"); } - File.SetLastWriteTime(ConvertPathToInternal(path), time); + var internalPath = ConvertPathToInternal(path); + var attributes = File.GetAttributes(internalPath); + + if ((attributes & FileAttributes.Directory) == FileAttributes.Directory) + { + Directory.SetLastWriteTime(internalPath, time); + } + else + { + File.SetLastWriteTime(internalPath, time); + } } // ----------------------------------------------