@@ -49,23 +49,19 @@ extension NSError {
49
49
50
50
open class FileHandle : NSObject {
51
51
#if os(Windows)
52
- private var _handle : HANDLE
53
-
54
- internal var handle : HANDLE {
55
- return _handle
56
- }
52
+ public private( set) var _handle : HANDLE
57
53
58
54
@available ( Windows, unavailable, message: " Cannot perform non-owning handle to fd conversion " )
59
55
open var fileDescriptor : Int32 {
60
56
NSUnsupported ( )
61
57
}
62
58
63
59
private func _checkFileHandle( ) {
64
- precondition ( _handle != INVALID_HANDLE_VALUE, " Invalid file handle " )
60
+ precondition ( self . _handle != INVALID_HANDLE_VALUE, " Invalid file handle " )
65
61
}
66
62
67
63
internal var _isPlatformHandleValid : Bool {
68
- return _handle != INVALID_HANDLE_VALUE
64
+ return self . _handle != INVALID_HANDLE_VALUE
69
65
}
70
66
#else
71
67
private var _fd : Int32
@@ -120,7 +116,7 @@ open class FileHandle : NSObject {
120
116
// Closing the file descriptor while Dispatch is monitoring it leads to undefined behavior; guard against that.
121
117
#if os(Windows)
122
118
var dupHandle : HANDLE ?
123
- if !DuplicateHandle( GetCurrentProcess ( ) , handle , GetCurrentProcess ( ) , & dupHandle,
119
+ if !DuplicateHandle( GetCurrentProcess ( ) , self . _handle , GetCurrentProcess ( ) , & dupHandle,
124
120
/*dwDesiredAccess:*/0 , /*bInheritHandle:*/true , DWORD ( DUPLICATE_SAME_ACCESS) ) {
125
121
fatalError ( " DuplicateHandleFailed: \( GetLastError ( ) ) " )
126
122
}
@@ -228,15 +224,15 @@ open class FileHandle : NSObject {
228
224
return NSData . NSDataReadResult ( bytes: nil , length: 0 , deallocator: nil )
229
225
}
230
226
231
- if GetFileType ( _handle) == FILE_TYPE_DISK {
227
+ if GetFileType ( self . _handle) == FILE_TYPE_DISK {
232
228
var fiFileInfo : BY_HANDLE_FILE_INFORMATION = BY_HANDLE_FILE_INFORMATION ( )
233
- if !GetFileInformationByHandle( _handle, & fiFileInfo) {
229
+ if !GetFileInformationByHandle( self . _handle, & fiFileInfo) {
234
230
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
235
231
}
236
232
237
233
if options. contains ( . alwaysMapped) {
238
234
let hMapping : HANDLE =
239
- CreateFileMappingA ( _handle, nil , DWORD ( PAGE_READONLY) , 0 , 0 , nil )
235
+ CreateFileMappingA ( self . _handle, nil , DWORD ( PAGE_READONLY) , 0 , 0 , nil )
240
236
if hMapping == HANDLE ( bitPattern: 0 ) {
241
237
fatalError ( " CreateFileMappingA failed " )
242
238
}
@@ -272,7 +268,7 @@ open class FileHandle : NSObject {
272
268
}
273
269
274
270
var BytesRead : DWORD = 0
275
- if !ReadFile( _handle, buffer. advanced ( by: total) , BytesToRead, & BytesRead, nil ) {
271
+ if !ReadFile( self . _handle, buffer. advanced ( by: total) , BytesToRead, & BytesRead, nil ) {
276
272
let err = GetLastError ( )
277
273
if err == ERROR_BROKEN_PIPE {
278
274
break
@@ -370,7 +366,7 @@ open class FileHandle : NSObject {
370
366
#if os(Windows)
371
367
var BytesRead : DWORD = 0
372
368
let BytesToRead : DWORD = DWORD ( length)
373
- if !ReadFile( _handle, buffer, BytesToRead, & BytesRead, nil ) {
369
+ if !ReadFile( self . _handle, buffer, BytesToRead, & BytesRead, nil ) {
374
370
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
375
371
}
376
372
return Int ( BytesRead)
@@ -388,7 +384,7 @@ open class FileHandle : NSObject {
388
384
var bytesRemaining = length
389
385
while bytesRemaining > 0 {
390
386
var bytesWritten : DWORD = 0
391
- if !WriteFile( handle , buf. advanced ( by: length - bytesRemaining) , DWORD ( bytesRemaining) , & bytesWritten, nil ) {
387
+ if !WriteFile( self . _handle , buf. advanced ( by: length - bytesRemaining) , DWORD ( bytesRemaining) , & bytesWritten, nil ) {
392
388
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
393
389
}
394
390
if bytesWritten == 0 {
@@ -413,7 +409,7 @@ open class FileHandle : NSObject {
413
409
414
410
#if os(Windows)
415
411
internal init ( handle: HANDLE , closeOnDealloc closeopt: Bool ) {
416
- _handle = handle
412
+ self . _handle = handle
417
413
_closeOnDealloc = closeopt
418
414
}
419
415
@@ -424,10 +420,10 @@ open class FileHandle : NSObject {
424
420
fatalError ( " DuplicateHandle() failed: \( GetLastError ( ) ) " )
425
421
}
426
422
_close ( fd)
427
- _handle = handle!
423
+ self . _handle = handle!
428
424
_closeOnDealloc = true
429
425
} else {
430
- _handle = HANDLE ( bitPattern: _get_osfhandle ( fd) ) !
426
+ self . _handle = HANDLE ( bitPattern: _get_osfhandle ( fd) ) !
431
427
_closeOnDealloc = false
432
428
}
433
429
}
@@ -443,7 +439,7 @@ open class FileHandle : NSObject {
443
439
} ) , fd > 0 else { return nil }
444
440
445
441
self . init ( fileDescriptor: fd, closeOnDealloc: true )
446
- if _handle == INVALID_HANDLE_VALUE { return nil }
442
+ if self . _handle == INVALID_HANDLE_VALUE { return nil }
447
443
}
448
444
#else
449
445
public init ( fileDescriptor fd: Int32 , closeOnDealloc closeopt: Bool ) {
@@ -525,7 +521,7 @@ open class FileHandle : NSObject {
525
521
526
522
#if os(Windows)
527
523
var liPointer : LARGE_INTEGER = LARGE_INTEGER ( QuadPart: 0 )
528
- guard SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: 0 ) , & liPointer, DWORD ( FILE_CURRENT) ) else {
524
+ guard SetFilePointerEx ( self . _handle, LARGE_INTEGER ( QuadPart: 0 ) , & liPointer, DWORD ( FILE_CURRENT) ) else {
529
525
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
530
526
}
531
527
return UInt64 ( liPointer. QuadPart)
@@ -545,7 +541,7 @@ open class FileHandle : NSObject {
545
541
546
542
#if os(Windows)
547
543
var liPointer : LARGE_INTEGER = LARGE_INTEGER ( QuadPart: 0 )
548
- guard SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: 0 ) , & liPointer, DWORD ( FILE_END) ) else {
544
+ guard SetFilePointerEx ( self . _handle, LARGE_INTEGER ( QuadPart: 0 ) , & liPointer, DWORD ( FILE_END) ) else {
549
545
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
550
546
}
551
547
return UInt64 ( liPointer. QuadPart)
@@ -563,7 +559,7 @@ open class FileHandle : NSObject {
563
559
guard _isPlatformHandleValid else { throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileReadUnknown. rawValue) }
564
560
565
561
#if os(Windows)
566
- guard SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: LONGLONG ( offset) ) , nil , DWORD ( FILE_BEGIN) ) else {
562
+ guard SetFilePointerEx ( self . _handle, LARGE_INTEGER ( QuadPart: LONGLONG ( offset) ) , nil , DWORD ( FILE_BEGIN) ) else {
567
563
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
568
564
}
569
565
#else
@@ -578,10 +574,10 @@ open class FileHandle : NSObject {
578
574
guard _isPlatformHandleValid else { throw NSError ( domain: NSCocoaErrorDomain, code: CocoaError . fileWriteUnknown. rawValue) }
579
575
580
576
#if os(Windows)
581
- guard SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: LONGLONG ( offset) ) , nil , DWORD ( FILE_BEGIN) ) else {
577
+ guard SetFilePointerEx ( self . _handle, LARGE_INTEGER ( QuadPart: LONGLONG ( offset) ) , nil , DWORD ( FILE_BEGIN) ) else {
582
578
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
583
579
}
584
- guard SetEndOfFile ( _handle) else {
580
+ guard SetEndOfFile ( self . _handle) else {
585
581
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
586
582
}
587
583
#else
@@ -595,7 +591,7 @@ open class FileHandle : NSObject {
595
591
guard self != FileHandle . _nulldeviceFileHandle else { return }
596
592
597
593
#if os(Windows)
598
- guard FlushFileBuffers ( _handle) else {
594
+ guard FlushFileBuffers ( self . _handle) else {
599
595
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: false )
600
596
}
601
597
#else
@@ -638,10 +634,10 @@ open class FileHandle : NSObject {
638
634
privateAsyncVariablesLock. unlock ( )
639
635
640
636
#if os(Windows)
641
- guard CloseHandle ( _handle) else {
637
+ guard CloseHandle ( self . _handle) else {
642
638
throw _NSErrorWithWindowsError ( GetLastError ( ) , reading: true )
643
639
}
644
- _handle = INVALID_HANDLE_VALUE
640
+ self . _handle = INVALID_HANDLE_VALUE
645
641
#else
646
642
guard _close ( _fd) >= 0 else {
647
643
throw _NSErrorWithErrno ( errno, reading: true )
@@ -864,7 +860,7 @@ extension FileHandle {
864
860
var translatedError = error
865
861
if error == ERROR_ACCESS_DENIED {
866
862
var fileInfo = BY_HANDLE_FILE_INFORMATION ( )
867
- GetFileInformationByHandle ( self . handle , & fileInfo)
863
+ GetFileInformationByHandle ( self . _handle , & fileInfo)
868
864
if fileInfo. dwFileAttributes & DWORD ( FILE_ATTRIBUTE_DIRECTORY) == DWORD ( FILE_ATTRIBUTE_DIRECTORY) {
869
865
translatedError = ERROR_DIRECTORY_NOT_SUPPORTED
870
866
}
@@ -881,7 +877,7 @@ extension FileHandle {
881
877
}
882
878
883
879
#if os(Windows)
884
- DispatchIO . read ( fromHandle: handle , maxLength: 1024 * 1024 , runningHandlerOn: queue) { ( data, error) in
880
+ DispatchIO . read ( fromHandle: self . _handle , maxLength: 1024 * 1024 , runningHandlerOn: queue) { ( data, error) in
885
881
operation ( data, error)
886
882
}
887
883
#else
0 commit comments