@@ -12,29 +12,41 @@ import CoreFoundation
12
12
open class FileHandle : NSObject , NSSecureCoding {
13
13
#if os(Windows)
14
14
private var _handle : HANDLE
15
+
16
+ @available ( windows, unavailable, message: " Cannot perform non-owning handle to fd conversion " )
17
+ open var fileDescriptor : Int32 {
18
+ NSUnsupported ( )
19
+ }
20
+
21
+ private func _checkFileHandle( ) {
22
+ precondition ( _handle != INVALID_HANDLE_VALUE, " Invalid file handle " )
23
+ }
24
+
15
25
#else
16
26
private var _fd : Int32
17
- #endif
18
- private var _closeOnDealloc : Bool
19
27
20
- @available ( windows, unavailable,
21
- message: " cannot perform non-owning handle to fd conversion " )
22
28
open var fileDescriptor : Int32 {
23
- #if os(Windows)
24
- return - 1
25
- #else
26
29
return _fd
27
- #endif
28
30
}
29
31
32
+ private func _checkFileHandle( ) {
33
+ precondition ( _fd >= 0 , " Bad file descriptor " )
34
+ }
35
+ #endif
36
+
37
+ private var _closeOnDealloc : Bool
38
+
39
+
30
40
open var readabilityHandler : ( ( FileHandle ) -> Void ) ? = {
31
41
( FileHandle) -> Void in NSUnimplemented ( )
32
42
}
43
+
33
44
open var writeabilityHandler : ( ( FileHandle ) -> Void ) ? = {
34
45
( FileHandle) -> Void in NSUnimplemented ( )
35
46
}
36
47
37
48
open var availableData : Data {
49
+ _checkFileHandle ( )
38
50
do {
39
51
let readResult = try _readDataOfLength ( Int . max, untilEOF: false )
40
52
return readResult. toData ( )
@@ -44,10 +56,12 @@ open class FileHandle : NSObject, NSSecureCoding {
44
56
}
45
57
46
58
open func readDataToEndOfFile( ) -> Data {
59
+ _checkFileHandle ( )
47
60
return readData ( ofLength: Int . max)
48
61
}
49
62
50
63
open func readData( ofLength length: Int ) -> Data {
64
+ _checkFileHandle ( )
51
65
do {
52
66
let readResult = try _readDataOfLength ( length, untilEOF: true )
53
67
return readResult. toData ( )
@@ -58,7 +72,6 @@ open class FileHandle : NSObject, NSSecureCoding {
58
72
59
73
internal func _readDataOfLength( _ length: Int , untilEOF: Bool , options: NSData . ReadingOptions = [ ] ) throws -> NSData . NSDataReadResult {
60
74
#if os(Windows)
61
- precondition ( _handle != INVALID_HANDLE_VALUE, " invalid file handle " )
62
75
63
76
if length == 0 && !untilEOF {
64
77
// Nothing requested, return empty response
@@ -131,7 +144,6 @@ open class FileHandle : NSObject, NSSecureCoding {
131
144
free ( buffer)
132
145
}
133
146
#else
134
- precondition ( _fd >= 0 , " Bad file descriptor " )
135
147
if length == 0 && !untilEOF {
136
148
// Nothing requested, return empty response
137
149
return NSData . NSDataReadResult ( bytes: nil , length: 0 , deallocator: nil )
@@ -202,8 +214,8 @@ open class FileHandle : NSObject, NSSecureCoding {
202
214
}
203
215
204
216
open func write( _ data: Data ) {
217
+ _checkFileHandle ( )
205
218
#if os(Windows)
206
- precondition ( _handle != INVALID_HANDLE_VALUE, " invalid file handle " )
207
219
data. enumerateBytes ( ) { ( bytes, range, stop) in
208
220
do {
209
221
try NSData . write ( toHandle: self . _handle, path: nil ,
@@ -214,7 +226,6 @@ open class FileHandle : NSObject, NSSecureCoding {
214
226
}
215
227
}
216
228
#else
217
- guard _fd >= 0 else { return }
218
229
data. enumerateBytes ( ) { ( bytes, range, stop) in
219
230
do {
220
231
try NSData . write ( toFileDescriptor: self . _fd, path: nil , buf: UnsafeRawPointer ( bytes. baseAddress!) , length: bytes. count)
@@ -228,52 +239,49 @@ open class FileHandle : NSObject, NSSecureCoding {
228
239
// TODO: Error handling.
229
240
230
241
open var offsetInFile : UInt64 {
242
+ _checkFileHandle ( )
231
243
#if os(Windows)
232
- precondition ( _handle != INVALID_HANDLE_VALUE, " invalid file handle " )
233
244
var liPointer : LARGE_INTEGER = LARGE_INTEGER ( QuadPart: 0 )
234
245
if SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: 0 ) ,
235
246
& liPointer, DWORD ( FILE_CURRENT) ) == FALSE {
236
247
fatalError ( " SetFilePointerEx failed " )
237
248
}
238
249
return UInt64 ( liPointer. QuadPart)
239
250
#else
240
- precondition ( _fd >= 0 , " Bad file descriptor " )
241
251
return UInt64 ( lseek ( _fd, 0 , SEEK_CUR) )
242
252
#endif
243
253
}
244
254
245
255
@discardableResult
246
256
open func seekToEndOfFile( ) -> UInt64 {
257
+ _checkFileHandle ( )
247
258
#if os(Windows)
248
- precondition ( _handle != INVALID_HANDLE_VALUE, " invalid file handle " )
249
259
var liPointer : LARGE_INTEGER = LARGE_INTEGER ( QuadPart: 0 )
250
260
if SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: 0 ) ,
251
261
& liPointer, DWORD ( FILE_END) ) == FALSE {
252
262
fatalError ( " SetFilePointerEx failed " )
253
263
}
254
264
return UInt64 ( liPointer. QuadPart)
255
265
#else
256
- precondition ( _fd >= 0 , " Bad file descriptor " )
257
266
return UInt64 ( lseek ( _fd, 0 , SEEK_END) )
258
267
#endif
259
268
}
260
269
261
270
open func seek( toFileOffset offset: UInt64 ) {
271
+ _checkFileHandle ( )
262
272
#if os(Windows)
263
- precondition ( _handle != INVALID_HANDLE_VALUE, " invalid file handle " )
264
273
if SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: LONGLONG ( offset) ) ,
265
274
nil , DWORD ( FILE_BEGIN) ) == FALSE {
266
275
fatalError ( " SetFilePointerEx failed " )
267
276
}
268
277
#else
269
- precondition ( _fd >= 0 , " Bad file descriptor " )
270
278
lseek ( _fd, off_t ( offset) , SEEK_SET)
271
279
#endif
272
280
}
273
281
274
282
open func truncateFile( atOffset offset: UInt64 ) {
283
+ _checkFileHandle ( )
275
284
#if os(Windows)
276
- precondition ( _handle != INVALID_HANDLE_VALUE, " invalid file handle " )
277
285
if SetFilePointerEx ( _handle, LARGE_INTEGER ( QuadPart: LONGLONG ( offset) ) ,
278
286
nil , DWORD ( FILE_BEGIN) ) == FALSE {
279
287
fatalError ( " SetFilePointerEx failed " )
@@ -282,20 +290,18 @@ open class FileHandle : NSObject, NSSecureCoding {
282
290
fatalError ( " SetEndOfFile failed " )
283
291
}
284
292
#else
285
- precondition ( _fd >= 0 , " Bad file descriptor " )
286
293
if lseek ( _fd, off_t ( offset) , SEEK_SET) < 0 { fatalError ( " lseek() failed. " ) }
287
294
if ftruncate ( _fd, off_t ( offset) ) < 0 { fatalError ( " ftruncate() failed. " ) }
288
295
#endif
289
296
}
290
297
291
298
open func synchronizeFile( ) {
299
+ _checkFileHandle ( )
292
300
#if os(Windows)
293
- precondition ( _handle != INVALID_HANDLE_VALUE, " invalid file handle " )
294
301
if FlushFileBuffers ( _handle) == FALSE {
295
302
fatalError ( " FlushFileBuffers failed: \( GetLastError ( ) ) " )
296
303
}
297
304
#else
298
- precondition ( _fd >= 0 , " Bad file descriptor " )
299
305
fsync ( _fd)
300
306
#endif
301
307
}
0 commit comments