Skip to content

Commit 7fc59bf

Browse files
authored
Foundation: adjust moveFile to support long paths on Windows (#4784)
Give `_moveItem(atPath:toPath:isURL:)` the long path treatment on Windows. This ensures that we are able to move items that may be deeply nested. This helps improve the test pass rate on DocC a small amount.
1 parent 866c512 commit 7fc59bf

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Sources/Foundation/FileManager+Win32.swift

+10-7
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,17 @@ extension FileManager {
601601
return
602602
}
603603

604-
guard !self.fileExists(atPath: dstPath) else {
605-
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteFileExists.rawValue, userInfo: [NSFilePathErrorKey : NSString(dstPath)])
606-
}
604+
try withNTPathRepresentation(of: dstPath) { wszDestination in
605+
var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init()
606+
if GetFileAttributesExW(wszDestination, GetFileExInfoStandard, &faAttributes) {
607+
throw CocoaError.error(.fileWriteFileExists, userInfo: [NSFilePathErrorKey:dstPath])
608+
}
607609

608-
try FileManager.default._fileSystemRepresentation(withPath: srcPath, andPath: dstPath) {
609-
if !MoveFileExW($0, $1, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) {
610-
throw _NSErrorWithWindowsError(GetLastError(), reading: false, paths: [srcPath, dstPath])
611-
}
610+
try withNTPathRepresentation(of: srcPath) { wszSource in
611+
if !MoveFileExW(wszSource, wszDestination, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) {
612+
throw _NSErrorWithWindowsError(GetLastError(), reading: false, paths: [srcPath, dstPath])
613+
}
614+
}
612615
}
613616
}
614617

0 commit comments

Comments
 (0)