Skip to content

Commit 3e6884b

Browse files
committed
Windows platform compatibility added for internal method _readBytes(into:lenght) in FileHandle
1 parent 690d266 commit 3e6884b

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Foundation/FileHandle.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,20 @@ open class FileHandle : NSObject, NSSecureCoding {
344344
}
345345

346346
internal func _readBytes(into buffer: UnsafeMutablePointer<UInt8>, length: Int) throws -> Int {
347+
#if os(Windows)
348+
var BytesRead: DWORD = 0
349+
let BytesToRead: DWORD = DWORD(length)
350+
if ReadFile(_handle, buffer, BytesToRead, &BytesRead, nil) == FALSE {
351+
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
352+
}
353+
return Int(BytesRead)
354+
#else
347355
let amtRead = _read(_fd, buffer, length)
348356
if amtRead < 0 {
349357
throw _NSErrorWithErrno(errno, reading: true)
350358
}
351359
return amtRead
360+
#endif
352361
}
353362

354363
internal func _writeBytes(buf: UnsafeRawPointer, length: Int) throws {
@@ -438,11 +447,7 @@ open class FileHandle : NSObject, NSSecureCoding {
438447
let fd = _CFOpenFileWithMode(fileSystemRepresentation, flags, mode_t(createMode))
439448
guard fd > 0 else { return nil }
440449

441-
#if os(Windows)
442-
self.init(handle: HANDLE(bitPattern: _get_osfhandle(fd))!, closeOnDealloc: true)
443-
#else
444450
self.init(fileDescriptor: fd, closeOnDealloc: true)
445-
#endif
446451
}
447452

448453
deinit {

Foundation/FileManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,7 @@ open class FileManager : NSObject {
17291729
buffer1.deallocate()
17301730
buffer2.deallocate()
17311731
}
1732-
1732+
17331733
var bytesLeft = size
17341734
while bytesLeft > 0 {
17351735
let bytesToRead = Int(min(Int64(bufSize), bytesLeft))

0 commit comments

Comments
 (0)