Skip to content

Commit d36c490

Browse files
committed
Foundation: adjust _NSCleanupTemporaryFile on Windows
Prefer to use `withNTPathRepresentation` over the `_fileSystemRepresentation` usage as the former will not convert to the NT UNC path representation which may break on long paths.
1 parent 16f93e7 commit d36c490

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Sources/Foundation/NSPathUtilities.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -810,20 +810,24 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
810810
}
811811

812812
internal func _NSCleanupTemporaryFile(_ auxFilePath: String, _ filePath: String) throws {
813-
try FileManager.default._fileSystemRepresentation(withPath: auxFilePath, andPath: filePath, {
814813
#if os(Windows)
815-
let res = CopyFileW($0, $1, /*bFailIfExists=*/false)
816-
try? FileManager.default.removeItem(atPath: auxFilePath)
817-
if !res {
818-
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
814+
try withNTPathRepresentation(of: auxFilePath) { pwszSource in
815+
try withNTPathRepresentation(of: filePath) { pwszDestination in
816+
guard CopyFileW(pwszSource, pwszDestination, false) else {
817+
let dwErrorCode = GetLastError()
818+
try? FileManager.default.removeItem(atPath: auxFilePath)
819+
throw _NSErrorWithWindowsError(dwErrorCode, reading: false)
820+
}
819821
}
822+
}
820823
#else
824+
try FileManager.default._fileSystemRepresentation(withPath: auxFilePath, andPath: filePath, {
821825
if rename($0, $1) != 0 {
822826
let errorCode = errno
823827
try? FileManager.default.removeItem(atPath: auxFilePath)
824828
throw _NSErrorWithErrno(errorCode, reading: false, path: filePath)
825829
}
826-
#endif
827830
})
831+
#endif
828832
}
829833
#endif

0 commit comments

Comments
 (0)