Skip to content

Commit 3b034eb

Browse files
committed
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.
1 parent d36c490 commit 3b034eb

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Sources/Foundation/FileManager+Win32.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension URL {
2424
#"\\?\\#(CFURLCopyFileSystemPath(CFURLCopyAbsoluteURL(_cfObject), kCFURLWindowsPathStyle)!._swiftObject)"#
2525
}
2626

27-
fileprivate func withUnsafeNTPath<Result>(_ body: (UnsafePointer<WCHAR>) throws -> Result) rethrows -> Result {
27+
internal func withUnsafeNTPath<Result>(_ body: (UnsafePointer<WCHAR>) throws -> Result) rethrows -> Result {
2828
try self.NTPath.withCString(encodedAs: UTF16.self, body)
2929
}
3030
}

Sources/Foundation/NSData.swift

+6
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,16 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
209209

210210
internal static func contentsOf(url: URL, options readOptionsMask: ReadingOptions = []) throws -> (result: NSData, textEncodingNameIfAvailable: String?) {
211211
if url.isFileURL {
212+
#if os(Windows)
213+
return try url.withUnsafeNTPath {
214+
return (try NSData.readBytesFromFileWithExtendedAttributes(String(decodingCString: $0, as: UTF16.self), options: readOptionsMask).toNSData(), nil)
215+
}
216+
#else
212217
return try url.withUnsafeFileSystemRepresentation { (fsRep) -> (result: NSData, textEncodingNameIfAvailable: String?) in
213218
let data = try NSData.readBytesFromFileWithExtendedAttributes(String(cString: fsRep!), options: readOptionsMask)
214219
return (data.toNSData(), nil)
215220
}
221+
#endif
216222
} else {
217223
return try _NSNonfileURLContentLoader.current.contentsOf(url: url)
218224
}

0 commit comments

Comments
 (0)