From 94f40a9670d118a22bc90257afd5ba24caf8b378 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 26 Jun 2023 08:32:13 -0700 Subject: [PATCH] Foundation: adjust `moveFile` to support long paths on Windows 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. --- Sources/Foundation/FileManager+Win32.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Sources/Foundation/FileManager+Win32.swift b/Sources/Foundation/FileManager+Win32.swift index 4537dc00a6..83c38c63e5 100644 --- a/Sources/Foundation/FileManager+Win32.swift +++ b/Sources/Foundation/FileManager+Win32.swift @@ -601,14 +601,17 @@ extension FileManager { return } - guard !self.fileExists(atPath: dstPath) else { - throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteFileExists.rawValue, userInfo: [NSFilePathErrorKey : NSString(dstPath)]) - } + try withNTPathRepresentation(of: dstPath) { wszDestination in + var faAttributes: WIN32_FILE_ATTRIBUTE_DATA = .init() + if GetFileAttributesExW(wszDestination, GetFileExInfoStandard, &faAttributes) { + throw CocoaError.error(.fileWriteFileExists, userInfo: [NSFilePathErrorKey:dstPath]) + } - try FileManager.default._fileSystemRepresentation(withPath: srcPath, andPath: dstPath) { - if !MoveFileExW($0, $1, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) { - throw _NSErrorWithWindowsError(GetLastError(), reading: false, paths: [srcPath, dstPath]) - } + try withNTPathRepresentation(of: srcPath) { wszSource in + if !MoveFileExW(wszSource, wszDestination, DWORD(MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) { + throw _NSErrorWithWindowsError(GetLastError(), reading: false, paths: [srcPath, dstPath]) + } + } } }