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

Refactor FileStatus.Unix. #62721

Merged
merged 4 commits into from
Jan 26, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ protected override void AssertLinkExists(FileSystemInfo link)
}
else
{
// Unix implementation detail:
// When the directory target does not exist FileStatus.GetExists returns false because:
// - We check _exists (which whould be true because the link itself exists).
// - We check InitiallyDirectory, which is the initial expected object type (which would be true).
// - We check _directory (false because the target directory does not exist)
// Unix requires the target to be a directory that exists.
Assert.False(link.Exists);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ protected override void AssertLinkExists(FileSystemInfo link)
}
else
{
// Unix implementation detail:
// When the directory target does not exist FileStatus.GetExists returns false because:
// - We check _exists (which whould be true because the link itself exists).
// - We check InitiallyDirectory, which is the initial expected object type (which would be true).
// - We check _directory (false because the target directory does not exist)
// Unix requires the target to be a directory that exists.
Assert.False(link.Exists);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace System.IO.Enumeration
public unsafe ref partial struct FileSystemEntry
{
private Interop.Sys.DirectoryEntry _directoryEntry;
private bool _isDirectory;
private FileStatus _status;
private Span<char> _pathBuffer;
private ReadOnlySpan<char> _fullPath;
Expand All @@ -32,24 +33,24 @@ namespace System.IO.Enumeration
entry._pathBuffer = pathBuffer;
entry._fullPath = ReadOnlySpan<char>.Empty;
entry._fileName = ReadOnlySpan<char>.Empty;
entry._isDirectory = false;
entry._status.InvalidateCaches();
entry._status.InitiallyDirectory = false;

bool isDirectory = directoryEntry.InodeType == Interop.Sys.NodeType.DT_DIR;
bool isSymlink = directoryEntry.InodeType == Interop.Sys.NodeType.DT_LNK;
bool isUnknown = directoryEntry.InodeType == Interop.Sys.NodeType.DT_UNKNOWN;

if (isDirectory)
{
entry._status.InitiallyDirectory = true;
entry._isDirectory = true;
}
else if (isSymlink)
{
entry._status.InitiallyDirectory = entry._status.IsDirectory(entry.FullPath, continueOnError: true);
entry._isDirectory = entry._status.IsDirectory(entry.FullPath, continueOnError: true);
}
else if (isUnknown)
{
entry._status.InitiallyDirectory = entry._status.IsDirectory(entry.FullPath, continueOnError: true);
entry._isDirectory = entry._status.IsDirectory(entry.FullPath, continueOnError: true);
if (entry._status.IsSymbolicLink(entry.FullPath, continueOnError: true))
{
entry._directoryEntry.InodeType = Interop.Sys.NodeType.DT_LNK;
Expand Down Expand Up @@ -145,16 +146,17 @@ public FileAttributes Attributes
public DateTimeOffset CreationTimeUtc => _status.GetCreationTime(FullPath, continueOnError: true);
public DateTimeOffset LastAccessTimeUtc => _status.GetLastAccessTime(FullPath, continueOnError: true);
public DateTimeOffset LastWriteTimeUtc => _status.GetLastWriteTime(FullPath, continueOnError: true);

public bool IsHidden => _status.IsFileSystemEntryHidden(FullPath, FileName);
internal bool IsReadOnly => _status.IsReadOnly(FullPath, continueOnError: true);

public bool IsDirectory => _status.InitiallyDirectory;
public bool IsDirectory => _isDirectory;
internal bool IsSymbolicLink => _directoryEntry.InodeType == Interop.Sys.NodeType.DT_LNK;

public FileSystemInfo ToFileSystemInfo()
{
string fullPath = ToFullPath();
return FileSystemInfo.Create(fullPath, new string(FileName), ref _status);
return FileSystemInfo.Create(fullPath, new string(FileName), _isDirectory, ref _status);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace System.IO
{
internal partial struct FileStatus
{
internal void SetCreationTime(string path, DateTimeOffset time)
internal void SetCreationTime(string path, DateTimeOffset time, bool asDirectory)
{
// Try to set the attribute on the file system entry using setattrlist,
// if we get ENOTSUP then it means that "The volume does not support
Expand All @@ -27,11 +27,11 @@ internal void SetCreationTime(string path, DateTimeOffset time)
}
else if (error == Interop.Error.ENOTSUP)
{
SetAccessOrWriteTimeCore(path, time, isAccessTime: false, checkCreationTime: false);
SetAccessOrWriteTimeCore(path, time, isAccessTime: false, checkCreationTime: false, asDirectory);
}
else
{
Interop.CheckIo(error, path, InitiallyDirectory);
Interop.CheckIo(error, path, asDirectory);
}
}

Expand All @@ -54,7 +54,7 @@ private unsafe Interop.Error SetCreationTimeCore(string path, long seconds, long
return error;
}

private void SetAccessOrWriteTime(string path, DateTimeOffset time, bool isAccessTime) =>
SetAccessOrWriteTimeCore(path, time, isAccessTime, checkCreationTime: true);
private void SetAccessOrWriteTime(string path, DateTimeOffset time, bool isAccessTime, bool asDirectory) =>
SetAccessOrWriteTimeCore(path, time, isAccessTime, checkCreationTime: true, asDirectory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace System.IO
{
internal partial struct FileStatus
{
internal void SetCreationTime(string path, DateTimeOffset time) =>
SetLastWriteTime(path, time);
internal void SetCreationTime(string path, DateTimeOffset time, bool asDirectory) =>
SetLastWriteTime(path, time, asDirectory);

private void SetAccessOrWriteTime(string path, DateTimeOffset time, bool isAccessTime) =>
SetAccessOrWriteTimeCore(path, time, isAccessTime, checkCreationTime: false);
private void SetAccessOrWriteTime(string path, DateTimeOffset time, bool isAccessTime, bool asDirectory) =>
SetAccessOrWriteTimeCore(path, time, isAccessTime, checkCreationTime: false, asDirectory);

// This is not used on these platforms, but is needed for source compat
private Interop.Error SetCreationTimeCore(string path, long seconds, long nanoseconds) =>
Expand Down
Loading