Repository navigation
fix: respect AllowedFileShare in recursive Directory.Delete - #1607
Open
mohsinaliwork686-rgb wants to merge 1 commit into
Open
mohsinaliwork686-rgb wants to merge 1 commit into
mohsinaliwork686-rgb wants to merge 1 commit into
Conversation
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #747
Directory.Delete(path, true)deleted files whoseAllowedFileSharedoesn't includeFileShare.Delete, whileFile.Deleteon the same file throwsIOException.DirectoryInfo.Delete(true)calls the same method, so it had the same gap.What was wrong
MockFile.DeletechecksAllowedFileShare.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.Deletenow goes through the affected files. If one of them doesn't allowFileShare.Delete, it throws the same exception asFile.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, sinceAllowedFileShareis 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.Deletedoes, 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 toMockFile_Lock_FileShareNoneThrowsDelete:Directory.Delete(dir, true)on a folder with a locked file and an unlocked one throws theIOExceptionwith the locked file's path, and both files are still thereDirectoryInfo.Delete(true)with a locked file two folders down throws the same wayFileShare.Deleteis still deleted along with its folderThe first two fail on
mainand 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.100container, 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.Pathlike the tests around them, so they also run on Windows.