Do not follow reparse points when removing a directory tree on Windows - #349
Open
YayoRazo wants to merge 1 commit into
Open
Do not follow reparse points when removing a directory tree on Windows#349YayoRazo wants to merge 1 commit into
YayoRazo wants to merge 1 commit into
Conversation
`_recursiveRemove` enumerated a directory's children and recursed into any entry with `FILE_ATTRIBUTE_DIRECTORY` set. A directory that is also a reparse point — a junction or a directory symlink — carries that attribute, so the routine recursed *through* the link and deleted the contents of its target, which lives outside the tree being removed. For a temporary directory holding a junction to, say, the user's documents, this was silent data loss. Skip reparse points instead: remove the link itself with RemoveDirectoryW, which deletes the reparse point without touching the target. This matches the POSIX sibling, which only recurses on real directories. While here, harden the enumeration in `forEachFile`: `FindNextFileW` returns false both at the end of a directory and on error, so check that the loop ended with ERROR_NO_MORE_FILES rather than treating a transient failure as end-of-directory (which would silently skip entries and leave the tree behind). This makes `forEachFile` `throws` rather than `rethrows`.
glessard
reviewed
Jul 18, 2026
| at path: FilePath, | ||
| _ body: (WIN32_FIND_DATAW) throws -> () | ||
| ) rethrows { | ||
| ) throws { |
Contributor
There was a problem hiding this comment.
This is a spurious change; this should still be rethrows until we change forEachFile() to throw an Errno (or an Error-constrained type parameter).
glessard
reviewed
Jul 18, 2026
Comment on lines
+90
to
+91
| return SystemString(platformString: $0.assumingMemoryBound( | ||
| to: CInterop.PlatformChar.self).baseAddress!) |
Contributor
There was a problem hiding this comment.
Let's replace this with memory rebinding. The older code likely predated the availability of SE-0333.
Suggested change
| return SystemString(platformString: $0.assumingMemoryBound( | |
| to: CInterop.PlatformChar.self).baseAddress!) | |
| $0.withMemoryRebound(to: CInterop.PlatformChar.self) { | |
| SystemString(platformString: $0.baseAddress!) | |
| } |
Contributor
|
This is indeed a bug, but it only involves an internal utility function. We can take our time. |
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.
Summary
_recursiveRemove(used bywithTemporaryFilePathcleanup on Windows) enumerated a directory's children and recursed into any entry withFILE_ATTRIBUTE_DIRECTORYset. A directory that is also a reparse point — a junction or a directory symlink — carries that attribute, so the routine recursed through the link and deleted the contents of its target, which lives outside the tree being removed.For a temporary directory that happens to contain a junction pointing at, say, the user's documents, this was silent data loss: the real files behind the link were deleted, not the link.
Fix
RemoveDirectoryW, which deletes the reparse point without touching its target. This matches the POSIX sibling (FilePathTempPosix.swift), which only recurses on real directories (DT_DIR) and never on symlinks.forEachFile:FindNextFileWreturnsfalseboth at the end of a directory and on error, so the loop now verifies it ended withERROR_NO_MORE_FILESrather than treating a transient failure as end-of-directory (which would silently skip the remaining entries and leave the tree partially deleted).forEachFilebecomesthrowsinstead ofrethrows.Testing
testCleanupDoesNotFollowReparsePoints: it creates a file in an external "victim" directory, places a directory junction to it inside the tree being removed (viamklink /J, which needs no special privilege, so the reparse path is exercised in CI), removes the tree, and asserts the victim file survives.swift testsuite green on Windows (x86_64-unknown-windows-msvc, Swift 6.3.3); no changes to other platforms.