Skip to content

Commit c40c883

Browse files
committed
Fix feedback.
1 parent 55c404b commit c40c883

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

Foundation/FileHandle.swift

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ open class FileHandle : NSObject, NSSecureCoding {
3434
precondition(_handle != INVALID_HANDLE_VALUE, "Invalid file handle")
3535
}
3636

37+
private var _isPlatformHandleValid: Bool {
38+
return _handle != INVALID_HANDLE_VALUE
39+
}
3740
#else
3841
private var _fd: Int32
3942

@@ -44,6 +47,10 @@ open class FileHandle : NSObject, NSSecureCoding {
4447
private func _checkFileHandle() {
4548
precondition(_fd >= 0, "Bad file descriptor")
4649
}
50+
51+
private var _isPlatformHandleValid: Bool {
52+
return fileDescriptor >= 0
53+
}
4754
#endif
4855

4956
private var _closeOnDealloc: Bool
@@ -281,30 +288,28 @@ open class FileHandle : NSObject, NSSecureCoding {
281288
return true
282289
}
283290

284-
private var _isPlatformHandleValid: Bool {
285-
#if os(Windows)
286-
return _handle != INVALID_HANDLE_VALUE
287-
#else
288-
return fileDescriptor >= 0
289-
#endif
290-
}
291-
292291
// MARK: -
293292
// MARK: New API.
294293

295294
@available(swift 5.0)
296295
public func readToEnd() throws -> Data? {
296+
guard self != FileHandle._nulldeviceFileHandle else { return nil }
297+
297298
return try read(upToCount: Int.max)
298299
}
299300

300301
@available(swift 5.0)
301302
public func read(upToCount count: Int) throws -> Data? {
303+
guard self != FileHandle._nulldeviceFileHandle else { return nil }
304+
302305
let result = try _readDataOfLength(count, untilEOF: true)
303306
return result.length == 0 ? nil : result.toData()
304307
}
305308

306309
@available(swift 5.0)
307310
public func write<T: DataProtocol>(contentsOf data: T) throws {
311+
guard self != FileHandle._nulldeviceFileHandle else { return }
312+
308313
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue) }
309314

310315
for region in data.regions {
@@ -322,6 +327,8 @@ open class FileHandle : NSObject, NSSecureCoding {
322327

323328
@available(swift 5.0)
324329
public func offset() throws -> UInt64 {
330+
guard self != FileHandle._nulldeviceFileHandle else { return 0 }
331+
325332
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadUnknown.rawValue) }
326333

327334
#if os(Windows)
@@ -340,6 +347,8 @@ open class FileHandle : NSObject, NSSecureCoding {
340347
@available(swift 5.0)
341348
@discardableResult
342349
public func seekToEnd() throws -> UInt64 {
350+
guard self != FileHandle._nulldeviceFileHandle else { return 0 }
351+
343352
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadUnknown.rawValue) }
344353

345354
#if os(Windows)
@@ -357,6 +366,8 @@ open class FileHandle : NSObject, NSSecureCoding {
357366

358367
@available(swift 5.0)
359368
public func seek(toOffset offset: UInt64) throws {
369+
guard self != FileHandle._nulldeviceFileHandle else { return }
370+
360371
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileReadUnknown.rawValue) }
361372

362373
#if os(Windows)
@@ -370,6 +381,8 @@ open class FileHandle : NSObject, NSSecureCoding {
370381

371382
@available(swift 5.0)
372383
public func truncate(toOffset offset: UInt64) throws {
384+
guard self != FileHandle._nulldeviceFileHandle else { return }
385+
373386
guard _isPlatformHandleValid else { throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue) }
374387

375388
#if os(Windows)
@@ -388,6 +401,8 @@ open class FileHandle : NSObject, NSSecureCoding {
388401

389402
@available(swift 5.0)
390403
public func synchronize() throws {
404+
guard self != FileHandle._nulldeviceFileHandle else { return }
405+
391406
#if os(Windows)
392407
guard FlushFileBuffers(_handle) != FALSE else {
393408
throw _NSErrorWithWindowsError(GetLastError(), reading: false)
@@ -399,18 +414,19 @@ open class FileHandle : NSObject, NSSecureCoding {
399414

400415
@available(swift 5.0)
401416
public func close() throws {
417+
guard self != FileHandle._nulldeviceFileHandle else { return }
418+
guard _isPlatformHandleValid else { return }
419+
402420
#if os(Windows)
403-
if _handle != INVALID_HANDLE_VALUE {
404-
guard CloseHandle(_handle) != FALSE else {
405-
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
406-
}
407-
_handle = INVALID_HANDLE_VALUE
421+
guard CloseHandle(_handle) != FALSE else {
422+
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
408423
}
424+
_handle = INVALID_HANDLE_VALUE
409425
#else
410-
if _fd >= 0 {
411-
guard _close(_fd) >= 0 else { throw _NSErrorWithErrno(errno, reading: true) }
412-
_fd = -1
426+
guard _close(_fd) >= 0 else {
427+
throw _NSErrorWithErrno(errno, reading: true)
413428
}
429+
_fd = -1
414430
#endif
415431
}
416432

0 commit comments

Comments
 (0)