Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changing directory times #84

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading