Skip to content

Commit

Permalink
Foundation: alter NSData.contents(of:options:) on Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
compnerd committed Jul 25, 2023
1 parent d36c490 commit 3b034eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Foundation/FileManager+Win32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension URL {
#"\\?\\#(CFURLCopyFileSystemPath(CFURLCopyAbsoluteURL(_cfObject), kCFURLWindowsPathStyle)!._swiftObject)"#
}

fileprivate func withUnsafeNTPath<Result>(_ body: (UnsafePointer<WCHAR>) throws -> Result) rethrows -> Result {
internal func withUnsafeNTPath<Result>(_ body: (UnsafePointer<WCHAR>) throws -> Result) rethrows -> Result {
try self.NTPath.withCString(encodedAs: UTF16.self, body)
}
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3b034eb

Please sign in to comment.