Sibling of #1084, in the same method family. FileHandle.CheckAccessWithShare tests two of its three cases with HasFlag and the third with equality:
switch (access)
{
case FileAccess.Read:
return share.HasFlag(FileShare.Read);
case FileAccess.Write:
return share.HasFlag(FileShare.Write);
default:
return share == FileShare.ReadWrite; // FileAccess.ReadWrite
}
FileShare.ReadWrite | FileShare.Delete shares strictly more than FileShare.ReadWrite, but fails the equality test, so two openers that both permit read/write and deletion are refused where the same pair without Delete succeeds.
Reproduction
MockFileSystem fileSystem = new(o => o.SimulatingOperatingSystem(SimulationMode.Windows));
fileSystem.File.WriteAllText("file.txt", "content");
const FileShare share = FileShare.ReadWrite | FileShare.Delete;
using FileSystemStream first = fileSystem.File.Open("file.txt", FileMode.Open, FileAccess.ReadWrite, share);
using FileSystemStream second = fileSystem.File.Open("file.txt", FileMode.Open, FileAccess.ReadWrite, share);
// IOException: The process cannot access the file ... because it is being used by another process.
Dropping FileShare.Delete from both makes it succeed. A real Windows file system allows both.
Expected
FileAccess.ReadWrite is granted whenever the share carries both the Read and Write flags, consistent with the two branches above it.
Fix
share.HasFlag(FileShare.ReadWrite). PR follows.
Sibling of #1084, in the same method family.
FileHandle.CheckAccessWithSharetests two of its three cases withHasFlagand the third with equality:FileShare.ReadWrite | FileShare.Deleteshares strictly more thanFileShare.ReadWrite, but fails the equality test, so two openers that both permit read/write and deletion are refused where the same pair withoutDeletesucceeds.Reproduction
Dropping
FileShare.Deletefrom both makes it succeed. A real Windows file system allows both.Expected
FileAccess.ReadWriteis granted whenever the share carries both theReadandWriteflags, consistent with the two branches above it.Fix
share.HasFlag(FileShare.ReadWrite). PR follows.