From 208cfba6a598a4b7d6932aed6040b65455490fd6 Mon Sep 17 00:00:00 2001 From: Gwen Mittertreiner Date: Tue, 25 Jun 2019 15:23:27 -0700 Subject: [PATCH] [Windows] Handle EOF with Pipes Windows indicates an EOF for a pipe by returning ERROR_BROKEN_PIPE as a result from ReadFile. --- Foundation/FileHandle.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Foundation/FileHandle.swift b/Foundation/FileHandle.swift index 713e2dbfd7..5c41f5eab8 100644 --- a/Foundation/FileHandle.swift +++ b/Foundation/FileHandle.swift @@ -266,8 +266,12 @@ open class FileHandle : NSObject { var BytesRead: DWORD = 0 if !ReadFile(_handle, buffer.advanced(by: total), BytesToRead, &BytesRead, nil) { + let err = GetLastError() + if err == ERROR_BROKEN_PIPE && untilEOF { + break + } free(buffer) - throw _NSErrorWithWindowsError(GetLastError(), reading: true) + throw _NSErrorWithWindowsError(err, reading: true) } total += Int(BytesRead) if BytesRead == 0 || !untilEOF {