Skip to content

Commit

Permalink
Merge pull request #84 from GerardSmit/fix/directory-dates
Browse files Browse the repository at this point in the history
Fix changing directory times
  • Loading branch information
xoofx committed May 20, 2024
2 parents ae0b1ca + 64818b4 commit c23f0a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/Zio.Tests/FileSystems/TestPhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
37 changes: 34 additions & 3 deletions src/Zio/FileSystems/PhysicalFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -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);
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -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);
}
}

// ----------------------------------------------
Expand Down

0 comments on commit c23f0a0

Please sign in to comment.