From c87f96b70ee894fca682b18be5ed446dc4f91e93 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Sat, 16 Sep 2023 08:12:34 -0500 Subject: [PATCH 1/2] Fix silent-overwrite behavior of FileManager.copyItem() on Linux (cherry picked from commit 6aa0ee4266c412655a29c8e39db1d1d03fc67a08) --- Sources/Foundation/FileManager+POSIX.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Foundation/FileManager+POSIX.swift b/Sources/Foundation/FileManager+POSIX.swift index a1fd75635e..d90ece91e6 100644 --- a/Sources/Foundation/FileManager+POSIX.swift +++ b/Sources/Foundation/FileManager+POSIX.swift @@ -584,7 +584,7 @@ extension FileManager { } defer { close(srcfd) } - let dstfd = open(dstRep, O_WRONLY | O_CREAT | O_TRUNC, 0o666) + let dstfd = open(dstRep, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0o666) guard dstfd >= 0 else { throw _NSErrorWithErrno(errno, reading: false, path: dstPath, extraUserInfo: extraErrorInfo(srcPath: srcPath, dstPath: dstPath, userVariant: variant)) From 84adf64a63003d9079552f5a3dc5921fe509c286 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Sat, 16 Sep 2023 08:13:14 -0500 Subject: [PATCH 2/2] Fix silent-overwrite behavior of FileManager.copyItem() on Windows (cherry picked from commit cfb9ad15a003e726701801431e78e89584278e5c) --- Sources/Foundation/FileManager+Win32.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Foundation/FileManager+Win32.swift b/Sources/Foundation/FileManager+Win32.swift index 949bd14e76..ac0e96257e 100644 --- a/Sources/Foundation/FileManager+Win32.swift +++ b/Sources/Foundation/FileManager+Win32.swift @@ -550,7 +550,7 @@ extension FileManager { internal func _copyRegularFile(atPath srcPath: String, toPath dstPath: String, variant: String = "Copy") throws { try withNTPathRepresentation(of: srcPath) { wszSource in try withNTPathRepresentation(of: dstPath) { wszDestination in - if !CopyFileW(wszSource, wszDestination, false) { + if !CopyFileW(wszSource, wszDestination, true) { throw _NSErrorWithWindowsError(GetLastError(), reading: true, paths: [srcPath, dstPath]) } }