From 3b034ebe8e299bc4f77927d2c3068aadb3aa93ff Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 24 Jul 2023 11:14:15 -0700 Subject: [PATCH] Foundation: alter `NSData.contents(of:options:)` on Windows We would previously use the FSR for the path, however, due to the expectations of FSR, we cannot support long paths with it. Use the internal `withUnsafeNTPath` extension on `URL` to use a long path representation to support extended paths (>260 characters) on Windows. --- Sources/Foundation/FileManager+Win32.swift | 2 +- Sources/Foundation/NSData.swift | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/Foundation/FileManager+Win32.swift b/Sources/Foundation/FileManager+Win32.swift index 46ece380b1..949bd14e76 100644 --- a/Sources/Foundation/FileManager+Win32.swift +++ b/Sources/Foundation/FileManager+Win32.swift @@ -24,7 +24,7 @@ extension URL { #"\\?\\#(CFURLCopyFileSystemPath(CFURLCopyAbsoluteURL(_cfObject), kCFURLWindowsPathStyle)!._swiftObject)"# } - fileprivate func withUnsafeNTPath(_ body: (UnsafePointer) throws -> Result) rethrows -> Result { + internal func withUnsafeNTPath(_ body: (UnsafePointer) throws -> Result) rethrows -> Result { try self.NTPath.withCString(encodedAs: UTF16.self, body) } } diff --git a/Sources/Foundation/NSData.swift b/Sources/Foundation/NSData.swift index 9892d0968b..ed9e7f0529 100644 --- a/Sources/Foundation/NSData.swift +++ b/Sources/Foundation/NSData.swift @@ -209,10 +209,16 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding { internal static func contentsOf(url: URL, options readOptionsMask: ReadingOptions = []) throws -> (result: NSData, textEncodingNameIfAvailable: String?) { if url.isFileURL { +#if os(Windows) + return try url.withUnsafeNTPath { + return (try NSData.readBytesFromFileWithExtendedAttributes(String(decodingCString: $0, as: UTF16.self), options: readOptionsMask).toNSData(), nil) + } +#else return try url.withUnsafeFileSystemRepresentation { (fsRep) -> (result: NSData, textEncodingNameIfAvailable: String?) in let data = try NSData.readBytesFromFileWithExtendedAttributes(String(cString: fsRep!), options: readOptionsMask) return (data.toNSData(), nil) } +#endif } else { return try _NSNonfileURLContentLoader.current.contentsOf(url: url) }