Skip to content

Commit 1319097

Browse files
authored
Fix divergent overwrite behavior of FileManager.copyItem(at:to:) on Linux and Windows (#4821)
* Fix silent-overwrite behavior of FileManager.copyItem() on Linux * Fix silent-overwrite behavior of FileManager.copyItem() on Windows
1 parent 9f53cc5 commit 1319097

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

Sources/Foundation/FileManager+POSIX.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ extension FileManager {
584584
}
585585
defer { close(srcfd) }
586586

587-
let dstfd = open(dstRep, O_WRONLY | O_CREAT | O_TRUNC, 0o666)
587+
let dstfd = open(dstRep, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0o666)
588588
guard dstfd >= 0 else {
589589
throw _NSErrorWithErrno(errno, reading: false, path: dstPath,
590590
extraUserInfo: extraErrorInfo(srcPath: srcPath, dstPath: dstPath, userVariant: variant))

Sources/Foundation/FileManager+Win32.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ extension FileManager {
550550
internal func _copyRegularFile(atPath srcPath: String, toPath dstPath: String, variant: String = "Copy") throws {
551551
try withNTPathRepresentation(of: srcPath) { wszSource in
552552
try withNTPathRepresentation(of: dstPath) { wszDestination in
553-
if !CopyFileW(wszSource, wszDestination, false) {
553+
if !CopyFileW(wszSource, wszDestination, true) {
554554
throw _NSErrorWithWindowsError(GetLastError(), reading: true, paths: [srcPath, dstPath])
555555
}
556556
}

0 commit comments

Comments
 (0)