From 2a633df603d35a4fd8b1c2637d2162e1bc664bd6 Mon Sep 17 00:00:00 2001 From: unclejack Date: Wed, 20 Aug 2014 23:50:42 +0300 Subject: [PATCH] pkg/ioutils: add ReaderErrWrapper to readers Docker-DCO-1.1-Signed-off-by: Cristian Staretu (github: unclejack) --- ioutils/readers.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/ioutils/readers.go b/ioutils/readers.go index f059bb1..22f46fb 100644 --- a/ioutils/readers.go +++ b/ioutils/readers.go @@ -22,6 +22,26 @@ func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser { } } +type readerErrWrapper struct { + reader io.Reader + closer func() +} + +func (r *readerErrWrapper) Read(p []byte) (int, error) { + n, err := r.reader.Read(p) + if err != nil { + r.closer() + } + return n, err +} + +func NewReaderErrWrapper(r io.Reader, closer func()) io.Reader { + return &readerErrWrapper{ + reader: r, + closer: closer, + } +} + type bufReader struct { sync.Mutex buf *bytes.Buffer