Skip to content

Fix divergent overwrite behavior of FileManager.copyItem(at:to:) on Linux and Windows #4808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Fix divergent overwrite behavior of FileManager.copyItem(at:to:) on Linux and Windows #4808

wants to merge 2 commits into from

Conversation

gwynne
Copy link
Contributor

@gwynne gwynne commented Aug 7, 2023

On Apple platforms, the FileManager APIs copyItem(atPath:toPath:) and copyItem(at:to:) refuse to overwrite the destination file (or link, or directory) if it already exists; this behavior is even explicitly documented. And indeed, any such attempt results in a fileWriteFileExists error.

However, on Linux and Windows, the destination is silently overwritten. This PR changes that behavior to match what's documented. On POSIX platforms, this is accomplished by adding the O_EXCL flag to the relevant open(2) call, which results in an EEXIST errno if the path already exists. For Windows, the bFailIfExists parameter of CopyFileW() is changed to true.

Fixes #3368

gwynne added a commit to vapor/api-docs that referenced this pull request Aug 7, 2023
gwynne added a commit to vapor/api-docs that referenced this pull request Aug 7, 2023
* Add some icons
* Pretty up generate-package-api-docs.swift a bit
* Work around swiftlang/swift-corelibs-foundation#4808
@compnerd
Copy link
Member

compnerd commented Aug 7, 2023

@swift-ci please test

@phausler phausler requested review from parkera and iCharlesHu August 8, 2023 15:39
@compnerd
Copy link
Member

@swift-ci please test macOS platform

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.

[SR-11963] FileManager.copyItem doesn't fail when there already is an item at destination path
3 participants