Skip to content

Commit 4e76279

Browse files
authored
Merge pull request #1877 from spevans/pr_filehandle_checks
2 parents 53ffe05 + 98ebe98 commit 4e76279

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

Foundation/FileHandle.swift

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,41 @@ import CoreFoundation
1212
open class FileHandle : NSObject, NSSecureCoding {
1313
#if os(Windows)
1414
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+
1525
#else
1626
private var _fd: Int32
17-
#endif
18-
private var _closeOnDealloc: Bool
1927

20-
@available(windows, unavailable,
21-
message: "cannot perform non-owning handle to fd conversion")
2228
open var fileDescriptor: Int32 {
23-
#if os(Windows)
24-
return -1
25-
#else
2629
return _fd
27-
#endif
2830
}
2931

32+
private func _checkFileHandle() {
33+
precondition(_fd >= 0, "Bad file descriptor")
34+
}
35+
#endif
36+
37+
private var _closeOnDealloc: Bool
38+
39+
3040
open var readabilityHandler: ((FileHandle) -> Void)? = {
3141
(FileHandle) -> Void in NSUnimplemented()
3242
}
43+
3344
open var writeabilityHandler: ((FileHandle) -> Void)? = {
3445
(FileHandle) -> Void in NSUnimplemented()
3546
}
3647

3748
open var availableData: Data {
49+
_checkFileHandle()
3850
do {
3951
let readResult = try _readDataOfLength(Int.max, untilEOF: false)
4052
return readResult.toData()
@@ -44,10 +56,12 @@ open class FileHandle : NSObject, NSSecureCoding {
4456
}
4557

4658
open func readDataToEndOfFile() -> Data {
59+
_checkFileHandle()
4760
return readData(ofLength: Int.max)
4861
}
4962

5063
open func readData(ofLength length: Int) -> Data {
64+
_checkFileHandle()
5165
do {
5266
let readResult = try _readDataOfLength(length, untilEOF: true)
5367
return readResult.toData()
@@ -58,7 +72,6 @@ open class FileHandle : NSObject, NSSecureCoding {
5872

5973
internal func _readDataOfLength(_ length: Int, untilEOF: Bool, options: NSData.ReadingOptions = []) throws -> NSData.NSDataReadResult {
6074
#if os(Windows)
61-
precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
6275

6376
if length == 0 && !untilEOF {
6477
// Nothing requested, return empty response
@@ -131,7 +144,6 @@ open class FileHandle : NSObject, NSSecureCoding {
131144
free(buffer)
132145
}
133146
#else
134-
precondition(_fd >= 0, "Bad file descriptor")
135147
if length == 0 && !untilEOF {
136148
// Nothing requested, return empty response
137149
return NSData.NSDataReadResult(bytes: nil, length: 0, deallocator: nil)
@@ -202,8 +214,8 @@ open class FileHandle : NSObject, NSSecureCoding {
202214
}
203215

204216
open func write(_ data: Data) {
217+
_checkFileHandle()
205218
#if os(Windows)
206-
precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
207219
data.enumerateBytes() { (bytes, range, stop) in
208220
do {
209221
try NSData.write(toHandle: self._handle, path: nil,
@@ -214,7 +226,6 @@ open class FileHandle : NSObject, NSSecureCoding {
214226
}
215227
}
216228
#else
217-
guard _fd >= 0 else { return }
218229
data.enumerateBytes() { (bytes, range, stop) in
219230
do {
220231
try NSData.write(toFileDescriptor: self._fd, path: nil, buf: UnsafeRawPointer(bytes.baseAddress!), length: bytes.count)
@@ -228,52 +239,49 @@ open class FileHandle : NSObject, NSSecureCoding {
228239
// TODO: Error handling.
229240

230241
open var offsetInFile: UInt64 {
242+
_checkFileHandle()
231243
#if os(Windows)
232-
precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
233244
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
234245
if SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0),
235246
&liPointer, DWORD(FILE_CURRENT)) == FALSE {
236247
fatalError("SetFilePointerEx failed")
237248
}
238249
return UInt64(liPointer.QuadPart)
239250
#else
240-
precondition(_fd >= 0, "Bad file descriptor")
241251
return UInt64(lseek(_fd, 0, SEEK_CUR))
242252
#endif
243253
}
244254

245255
@discardableResult
246256
open func seekToEndOfFile() -> UInt64 {
257+
_checkFileHandle()
247258
#if os(Windows)
248-
precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
249259
var liPointer: LARGE_INTEGER = LARGE_INTEGER(QuadPart: 0)
250260
if SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: 0),
251261
&liPointer, DWORD(FILE_END)) == FALSE {
252262
fatalError("SetFilePointerEx failed")
253263
}
254264
return UInt64(liPointer.QuadPart)
255265
#else
256-
precondition(_fd >= 0, "Bad file descriptor")
257266
return UInt64(lseek(_fd, 0, SEEK_END))
258267
#endif
259268
}
260269

261270
open func seek(toFileOffset offset: UInt64) {
271+
_checkFileHandle()
262272
#if os(Windows)
263-
precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
264273
if SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)),
265274
nil, DWORD(FILE_BEGIN)) == FALSE {
266275
fatalError("SetFilePointerEx failed")
267276
}
268277
#else
269-
precondition(_fd >= 0, "Bad file descriptor")
270278
lseek(_fd, off_t(offset), SEEK_SET)
271279
#endif
272280
}
273281

274282
open func truncateFile(atOffset offset: UInt64) {
283+
_checkFileHandle()
275284
#if os(Windows)
276-
precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
277285
if SetFilePointerEx(_handle, LARGE_INTEGER(QuadPart: LONGLONG(offset)),
278286
nil, DWORD(FILE_BEGIN)) == FALSE {
279287
fatalError("SetFilePointerEx failed")
@@ -282,20 +290,18 @@ open class FileHandle : NSObject, NSSecureCoding {
282290
fatalError("SetEndOfFile failed")
283291
}
284292
#else
285-
precondition(_fd >= 0, "Bad file descriptor")
286293
if lseek(_fd, off_t(offset), SEEK_SET) < 0 { fatalError("lseek() failed.") }
287294
if ftruncate(_fd, off_t(offset)) < 0 { fatalError("ftruncate() failed.") }
288295
#endif
289296
}
290297

291298
open func synchronizeFile() {
299+
_checkFileHandle()
292300
#if os(Windows)
293-
precondition(_handle != INVALID_HANDLE_VALUE, "invalid file handle")
294301
if FlushFileBuffers(_handle) == FALSE {
295302
fatalError("FlushFileBuffers failed: \(GetLastError())")
296303
}
297304
#else
298-
precondition(_fd >= 0, "Bad file descriptor")
299305
fsync(_fd)
300306
#endif
301307
}

0 commit comments

Comments
 (0)