Skip to content

Commit

Permalink
Foundation: adjust _NSCleanupTemporaryFile on Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
compnerd committed Jul 24, 2023
1 parent 16f93e7 commit d36c490
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Sources/Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -810,20 +810,24 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
}

internal func _NSCleanupTemporaryFile(_ auxFilePath: String, _ filePath: String) throws {
try FileManager.default._fileSystemRepresentation(withPath: auxFilePath, andPath: filePath, {
#if os(Windows)
let res = CopyFileW($0, $1, /*bFailIfExists=*/false)
try? FileManager.default.removeItem(atPath: auxFilePath)
if !res {
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
try withNTPathRepresentation(of: auxFilePath) { pwszSource in
try withNTPathRepresentation(of: filePath) { pwszDestination in
guard CopyFileW(pwszSource, pwszDestination, false) else {
let dwErrorCode = GetLastError()
try? FileManager.default.removeItem(atPath: auxFilePath)
throw _NSErrorWithWindowsError(dwErrorCode, reading: false)
}
}
}
#else
try FileManager.default._fileSystemRepresentation(withPath: auxFilePath, andPath: filePath, {
if rename($0, $1) != 0 {
let errorCode = errno
try? FileManager.default.removeItem(atPath: auxFilePath)
throw _NSErrorWithErrno(errorCode, reading: false, path: filePath)
}
#endif
})
#endif
}
#endif

0 comments on commit d36c490

Please sign in to comment.