Skip to content

fix: respect AllowedFileShare in recursive Directory.Delete - #1607

Open
mohsinaliwork686-rgb wants to merge 1 commit into
TestableIO:mainfrom
mohsinaliwork686-rgb:fix/directory-delete-locked-file
Open

mohsinaliwork686-rgb wants to merge 1 commit into
TestableIO:mainfrom
mohsinaliwork686-rgb:fix/directory-delete-locked-file

Conversation

@mohsinaliwork686-rgb

Copy link
Copy Markdown
Contributor

Fixes #747

Directory.Delete(path, true) deleted files whose AllowedFileShare doesn't include FileShare.Delete, while File.Delete on the same file throws IOException. DirectoryInfo.Delete(true) calls the same method, so it had the same gap.

What was wrong

MockFile.Delete checks AllowedFileShare.HasFlag(FileShare.Delete) before it removes a file. MockDirectory.Delete(string, bool) collects every path under the directory and removes them one by one, without that check.

What changed

Before it removes anything, MockDirectory.Delete now goes through the affected files. If one of them doesn't allow FileShare.Delete, it throws the same exception as File.Delete (CommonExceptions.ProcessCannotAccessFileInUse, "The process cannot access the file '...' because it is being used by another process.") with that file's full path. Only files are checked, not the directories in the tree, since AllowedFileShare is documented as the file's sharing mode.

Because the check runs first, nothing is deleted when it throws. Real Windows is different here: it deletes every file it can, removes the folders that end up empty, and then throws for the first file it couldn't delete. I kept the all-or-nothing version I proposed on the issue. It leaves the file system as it was when the call throws, like File.Delete does, and copying Windows exactly would mean working out which folders still hold a locked file and keeping only those. Happy to change it if you'd rather match Windows.

How it's tested

I added three tests to MockFileLockTests, next to MockFile_Lock_FileShareNoneThrowsDelete:

  • Directory.Delete(dir, true) on a folder with a locked file and an unlocked one throws the IOException with the locked file's path, and both files are still there
  • DirectoryInfo.Delete(true) with a locked file two folders down throws the same way
  • a file with FileShare.Delete is still deleted along with its folder

The first two fail on main and pass with the fix. The third passes on both and makes sure the new check doesn't block files that allow deletion.

I ran this on Linux in the mcr.microsoft.com/dotnet/sdk:11.0.100 container, with the .NET 10, 9 and 8 SDKs added. The Release build of the solution has no warnings or errors. The Wrappers, TestingHelpers and Parity tests all pass on net11.0, net10.0, net9.0 and net8.0, and the API tests pass too. On macOS, the TestingHelpers, Wrappers and Parity tests also pass on net10.0, net9.0 and net8.0 (there is no .NET 11 SDK on that machine).

I couldn't run the Windows tests or net472. The new tests build their paths with XFS.Path like the tests around them, so they also run on Windows.

@mergify

mergify Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MockDirectory.Delete recursive does not consider nested files MockFileData.AllowedFileShare property

1 participant