Skip to content

MockFileSystem refuses read/write sharing when FileShare.Delete is combined with FileShare.ReadWrite #1090

Description

@Mpdreamz

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions