Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Sources/Foundation/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,12 @@ open class FileHandle : NSObject {
}

internal convenience init?(path: String, flags: Int32, createMode: Int) {
guard let fd: Int32 = try? FileManager.default._fileSystemRepresentation(withPath: path, {
_CFOpenFileWithMode($0, flags, mode_t(createMode))
}), fd > 0 else { return nil }
guard let fd: CInt = try? withNTPathRepresentation(of: path, {
_CFOpenFileWithMode($0, flags, mode_t(createMode))
}), fd > 0 else { return nil }

self.init(fileDescriptor: fd, closeOnDealloc: true)
if self._handle == INVALID_HANDLE_VALUE { return nil }
self.init(fileDescriptor: fd, closeOnDealloc: true)
if self._handle == INVALID_HANDLE_VALUE { return nil }
}
#else
public init(fileDescriptor fd: Int32, closeOnDealloc closeopt: Bool) {
Expand Down
5 changes: 4 additions & 1 deletion Sources/Foundation/FileManager+Win32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension URL {
}


private func withNTPathRepresentation<Result>(of path: String, _ body: (UnsafePointer<WCHAR>) throws -> Result) throws -> Result {
internal func withNTPathRepresentation<Result>(of path: String, _ body: (UnsafePointer<WCHAR>) throws -> Result) throws -> Result {
guard !path.isEmpty else {
throw CocoaError.error(.fileReadInvalidFileName, userInfo: [NSFilePathErrorKey:path])
}
Expand Down Expand Up @@ -72,6 +72,9 @@ private func withNTPathRepresentation<Result>(of path: String, _ body: (UnsafePo
_ = GetFullPathNameW(pwszPath, DWORD($0.count), $0.baseAddress, nil)
return String(decodingCString: $0.baseAddress!, as: UTF16.self)
}
guard !path.hasPrefix(#"\\"#) else {
return try path.withCString(encodedAs: UTF16.self, body)
}
return try #"\\?\\#(path)"#.withCString(encodedAs: UTF16.self, body)
}
}
Expand Down