Improve FileUtils.forceDelete() tests on Windows#791
Merged
garydgregory merged 7 commits intomasterfrom Oct 2, 2025
Merged
Conversation
On Windows, the `DeleteFile` Win32 API has a little quirk: it refuses to delete files with the legacy **DOS read-only attribute** set. (Because apparently 99% of Windows users don’t realize that “deleting a file” is actually an operation on the *directory*, not the file itself 😉). So the usual drill is: clear the read-only flag first, then delete. * Until JDK 25, `File.delete()` did this for you behind the scenes: it quietly stripped the flag before calling into `DeleteFile`. That meant your file might be left behind (with the flag missing) if the *real* ACLs didn’t allow deletion. * From JDK 25 onward, `File.delete()` doesn’t touch the flag anymore. If the bit is set, `DeleteFile` fails, end of story. * `FileUtils.forceDelete()` already knows how to juggle the flag itself, so its behavior didn’t change. This PR: * Updates two tests that were (unfairly) comparing `File.delete` with `FileUtils.forceDelete`. With JDK 25, their expectations diverged. * Adds a new test to confirm that `FileUtils.forceDelete` restores the read-only flag if the actual deletion fails. > [!WARNING] > I didn’t develop this on a Windows box, so I couldn’t test it locally. Leaving this as a **draft PR** until CI tells us whether Windows agrees with me.
7b93b2b to
1308bbc
Compare
Introduce a helper that snapshots file and parent directory attributes before `setReadOnly` is applied. If a deletion attempt fails, the holder can restore the original attributes to keep the filesystem state consistent.
On Linux/Unix, both POSIX and DOS attribute views may be supported, while on Windows only DOS attributes are available. Check for POSIX first to ensure the correct view is used across platforms.
Member
|
Merged, thank you @ppkarwasz. |
Contributor
Author
|
Since the issues around I’ve opened three follow-up JIRA tickets to track the remaining problems:
Partial fixes for these are available in the |
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.
On Windows, the
DeleteFileWin32 API has a little quirk: it refuses to delete files with the legacy DOS read-only attribute set. (Because apparently 99% of Windows users don’t realize that “deleting a file” is actually an operation on the directory, not the file itself 😉).So the usual drill is: clear the read-only flag first, then delete.
File.delete()did this for you behind the scenes: it quietly stripped the flag before calling intoDeleteFile. That meant your file might be left behind (with the flag missing) if the real ACLs didn’t allow deletion.File.delete()doesn’t touch the flag anymore. If the bit is set,DeleteFilefails, end of story.FileUtils.forceDelete()already knows how to juggle the flag itself, so its behavior didn’t change.This PR:
File.deletewithFileUtils.forceDelete. With JDK 25, their expectations diverged.Adds a new test to confirm thatFileUtils.forceDeleterestores the read-only flag if the actual deletion fails.