Skip to content

Commit 1a43257

Browse files
committed
Foundation: expose _handle from FileHandle on Windows
Add a SPI for Windows to get the raw `HANDLE` to enable use of `Pipe` where a raw file descriptor may be needed.
1 parent 6787b8a commit 1a43257

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

Sources/Foundation/FileHandle.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ extension NSError {
4949

5050
open class FileHandle : NSObject {
5151
#if os(Windows)
52-
private var _handle: HANDLE
52+
internal private(set) var handle: HANDLE
5353

54-
internal var handle: HANDLE {
55-
return _handle
54+
public var _handle: HANDLE {
55+
return self.handle
5656
}
5757

5858
@available(Windows, unavailable, message: "Cannot perform non-owning handle to fd conversion")
@@ -61,11 +61,11 @@ open class FileHandle : NSObject {
6161
}
6262

6363
private func _checkFileHandle() {
64-
precondition(_handle != INVALID_HANDLE_VALUE, "Invalid file handle")
64+
precondition(self.handle != INVALID_HANDLE_VALUE, "Invalid file handle")
6565
}
6666

6767
internal var _isPlatformHandleValid: Bool {
68-
return _handle != INVALID_HANDLE_VALUE
68+
return self.handle != INVALID_HANDLE_VALUE
6969
}
7070
#else
7171
private var _fd: Int32
@@ -228,15 +228,15 @@ open class FileHandle : NSObject {
228228
return NSData.NSDataReadResult(bytes: nil, length: 0, deallocator: nil)
229229
}
230230

231-
if GetFileType(_handle) == FILE_TYPE_DISK {
231+
if GetFileType(self.handle) == FILE_TYPE_DISK {
232232
var fiFileInfo: BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION()
233-
if !GetFileInformationByHandle(_handle, &fiFileInfo) {
233+
if !GetFileInformationByHandle(self.handle, &fiFileInfo) {
234234
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
235235
}
236236

237237
if options.contains(.alwaysMapped) {
238238
let hMapping: HANDLE =
239-
CreateFileMappingA(_handle, nil, DWORD(PAGE_READONLY), 0, 0, nil)
239+
CreateFileMappingA(self.handle, nil, DWORD(PAGE_READONLY), 0, 0, nil)
240240
if hMapping == HANDLE(bitPattern: 0) {
241241
fatalError("CreateFileMappingA failed")
242242
}
@@ -272,7 +272,7 @@ open class FileHandle : NSObject {
272272
}
273273

274274
var BytesRead: DWORD = 0
275-
if !ReadFile(_handle, buffer.advanced(by: total), BytesToRead, &BytesRead, nil) {
275+
if !ReadFile(self.handle, buffer.advanced(by: total), BytesToRead, &BytesRead, nil) {
276276
let err = GetLastError()
277277
if err == ERROR_BROKEN_PIPE {
278278
break
@@ -370,7 +370,7 @@ open class FileHandle : NSObject {
370370
#if os(Windows)
371371
var BytesRead: DWORD = 0
372372
let BytesToRead: DWORD = DWORD(length)
373-
if !ReadFile(_handle, buffer, BytesToRead, &BytesRead, nil) {
373+
if !ReadFile(self.handle, buffer, BytesToRead, &BytesRead, nil) {
374374
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
375375
}
376376
return Int(BytesRead)
@@ -413,7 +413,7 @@ open class FileHandle : NSObject {
413413

414414
#if os(Windows)
415415
internal init(handle: HANDLE, closeOnDealloc closeopt: Bool) {
416-
_handle = handle
416+
self.handle = handle
417417
_closeOnDealloc = closeopt
418418
}
419419

@@ -424,10 +424,10 @@ open class FileHandle : NSObject {
424424
fatalError("DuplicateHandle() failed: \(GetLastError())")
425425
}
426426
_close(fd)
427-
_handle = handle!
427+
self.handle = handle!
428428
_closeOnDealloc = true
429429
} else {
430-
_handle = HANDLE(bitPattern: _get_osfhandle(fd))!
430+
self.handle = HANDLE(bitPattern: _get_osfhandle(fd))!
431431
_closeOnDealloc = false
432432
}
433433
}
@@ -443,7 +443,7 @@ open class FileHandle : NSObject {
443443
}), fd > 0 else { return nil }
444444

445445
self.init(fileDescriptor: fd, closeOnDealloc: true)
446-
if _handle == INVALID_HANDLE_VALUE { return nil }
446+
if self.handle == INVALID_HANDLE_VALUE { return nil }
447447
}
448448
#else
449449
public init(fileDescriptor fd: Int32, closeOnDealloc closeopt: Bool) {
@@ -525,7 +525,7 @@ open class FileHandle : NSObject {
525525

526526
#if os(Windows)
527527
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
528-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_CURRENT)) else {
528+
guard SetFilePointerEx(self.handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_CURRENT)) else {
529529
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
530530
}
531531
return UInt64(liPointer.QuadPart)
@@ -545,7 +545,7 @@ open class FileHandle : NSObject {
545545

546546
#if os(Windows)
547547
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
548-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_END)) else {
548+
guard SetFilePointerEx(self.handle, LARGE_INTEGER(QuadPart: 0), &liPointer, DWORD(FILE_END)) else {
549549
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
550550
}
551551
return UInt64(liPointer.QuadPart)
@@ -563,7 +563,7 @@ open class FileHandle : NSObject {
563563
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadUnknown.rawValue) }
564564

565565
#if os(Windows)
566-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
566+
guard SetFilePointerEx(self.handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
567567
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
568568
}
569569
#else
@@ -578,10 +578,10 @@ open class FileHandle : NSObject {
578578
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue) }
579579

580580
#if os(Windows)
581-
guard SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
581+
guard SetFilePointerEx(self.handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)), nil, DWORD(FILE_BEGIN)) else {
582582
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
583583
}
584-
guard SetEndOfFile(_handle) else {
584+
guard SetEndOfFile(self.handle) else {
585585
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
586586
}
587587
#else
@@ -595,7 +595,7 @@ open class FileHandle : NSObject {
595595
guard self != FileHandle._nulldeviceFileHandle else { return }
596596

597597
#if os(Windows)
598-
guard FlushFileBuffers(_handle) else {
598+
guard FlushFileBuffers(self.handle) else {
599599
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
600600
}
601601
#else
@@ -638,10 +638,10 @@ open class FileHandle : NSObject {
638638
privateAsyncVariablesLock.unlock()
639639

640640
#if os(Windows)
641-
guard CloseHandle(_handle) else {
641+
guard CloseHandle(self.handle) else {
642642
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
643643
}
644-
_handle = INVALID_HANDLE_VALUE
644+
self.handle = INVALID_HANDLE_VALUE
645645
#else
646646
guard _close(_fd) >= 0 else {
647647
throw _NSErrorWithErrno(errno, reading: true)

0 commit comments

Comments
 (0)