2014-08-12 12:10:43 -04:00
|
|
|
package ioutils
|
|
|
|
|
|
|
|
import (
|
2015-03-29 17:17:23 -04:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2014-08-12 12:10:43 -04:00
|
|
|
"io"
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2014-08-12 12:10:43 -04:00
|
|
|
)
|
|
|
|
|
2018-01-16 13:49:18 -05:00
|
|
|
// ReadCloserWrapper wraps an io.Reader, and implements an io.ReadCloser
|
|
|
|
// It calls the given callback function when closed. It should be constructed
|
|
|
|
// with NewReadCloserWrapper
|
|
|
|
type ReadCloserWrapper struct {
|
2014-08-12 12:10:43 -04:00
|
|
|
io.Reader
|
|
|
|
closer func() error
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:49:18 -05:00
|
|
|
// Close calls back the passed closer function
|
|
|
|
func (r *ReadCloserWrapper) Close() error {
|
2014-08-12 12:10:43 -04:00
|
|
|
return r.closer()
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NewReadCloserWrapper returns a new io.ReadCloser.
|
2014-08-12 12:10:43 -04:00
|
|
|
func NewReadCloserWrapper(r io.Reader, closer func() error) io.ReadCloser {
|
2018-01-16 13:49:18 -05:00
|
|
|
return &ReadCloserWrapper{
|
2014-08-12 12:10:43 -04:00
|
|
|
Reader: r,
|
|
|
|
closer: closer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 16:50:42 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NewReaderErrWrapper returns a new io.Reader.
|
2014-08-20 16:50:42 -04:00
|
|
|
func NewReaderErrWrapper(r io.Reader, closer func()) io.Reader {
|
|
|
|
return &readerErrWrapper{
|
|
|
|
reader: r,
|
|
|
|
closer: closer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// HashData returns the sha256 sum of src.
|
2015-03-29 17:17:23 -04:00
|
|
|
func HashData(src io.Reader) (string, error) {
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, src); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "sha256:" + hex.EncodeToString(h.Sum(nil)), nil
|
|
|
|
}
|
2015-05-17 05:07:48 -04:00
|
|
|
|
2016-05-07 21:36:10 -04:00
|
|
|
// OnEOFReader wraps an io.ReadCloser and a function
|
2015-08-07 18:24:18 -04:00
|
|
|
// the function will run at the end of file or close the file.
|
2015-05-17 05:07:48 -04:00
|
|
|
type OnEOFReader struct {
|
|
|
|
Rc io.ReadCloser
|
|
|
|
Fn func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OnEOFReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = r.Rc.Read(p)
|
|
|
|
if err == io.EOF {
|
|
|
|
r.runFunc()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Close closes the file and run the function.
|
2015-05-17 05:07:48 -04:00
|
|
|
func (r *OnEOFReader) Close() error {
|
|
|
|
err := r.Rc.Close()
|
|
|
|
r.runFunc()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OnEOFReader) runFunc() {
|
|
|
|
if fn := r.Fn; fn != nil {
|
|
|
|
fn()
|
|
|
|
r.Fn = nil
|
|
|
|
}
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
// cancelReadCloser wraps an io.ReadCloser with a context for cancelling read
|
|
|
|
// operations.
|
|
|
|
type cancelReadCloser struct {
|
|
|
|
cancel func()
|
|
|
|
pR *io.PipeReader // Stream to read from
|
|
|
|
pW *io.PipeWriter
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCancelReadCloser creates a wrapper that closes the ReadCloser when the
|
|
|
|
// context is cancelled. The returned io.ReadCloser must be closed when it is
|
|
|
|
// no longer needed.
|
|
|
|
func NewCancelReadCloser(ctx context.Context, in io.ReadCloser) io.ReadCloser {
|
|
|
|
pR, pW := io.Pipe()
|
|
|
|
|
|
|
|
// Create a context used to signal when the pipe is closed
|
|
|
|
doneCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
p := &cancelReadCloser{
|
|
|
|
cancel: cancel,
|
|
|
|
pR: pR,
|
|
|
|
pW: pW,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(pW, in)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// If the context was closed, p.closeWithError
|
|
|
|
// was already called. Calling it again would
|
|
|
|
// change the error that Read returns.
|
|
|
|
default:
|
|
|
|
p.closeWithError(err)
|
|
|
|
}
|
|
|
|
in.Close()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
p.closeWithError(ctx.Err())
|
|
|
|
case <-doneCtx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read wraps the Read method of the pipe that provides data from the wrapped
|
|
|
|
// ReadCloser.
|
|
|
|
func (p *cancelReadCloser) Read(buf []byte) (n int, err error) {
|
|
|
|
return p.pR.Read(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// closeWithError closes the wrapper and its underlying reader. It will
|
|
|
|
// cause future calls to Read to return err.
|
|
|
|
func (p *cancelReadCloser) closeWithError(err error) {
|
|
|
|
p.pW.CloseWithError(err)
|
|
|
|
p.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the wrapper its underlying reader. It will cause
|
|
|
|
// future calls to Read to return io.EOF.
|
|
|
|
func (p *cancelReadCloser) Close() error {
|
|
|
|
p.closeWithError(io.EOF)
|
|
|
|
return nil
|
|
|
|
}
|