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"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type readCloserWrapper struct {
|
|
|
|
io.Reader
|
|
|
|
closer func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *readCloserWrapper) Close() error {
|
|
|
|
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 {
|
|
|
|
return &readCloserWrapper{
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 09:10:36 -05:00
|
|
|
// bufReader allows the underlying reader to continue to produce
|
|
|
|
// output by pre-emptively reading from the wrapped reader.
|
|
|
|
// This is achieved by buffering this data in bufReader's
|
|
|
|
// expanding buffer.
|
2014-08-12 12:10:43 -04:00
|
|
|
type bufReader struct {
|
|
|
|
sync.Mutex
|
2015-09-09 12:48:34 -04:00
|
|
|
buf io.ReadWriter
|
|
|
|
reader io.Reader
|
|
|
|
err error
|
|
|
|
wait sync.Cond
|
|
|
|
drainBuf []byte
|
2014-08-12 12:10:43 -04:00
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NewBufReader returns a new bufReader.
|
|
|
|
func NewBufReader(r io.Reader) io.ReadCloser {
|
2014-08-12 12:10:43 -04:00
|
|
|
reader := &bufReader{
|
2015-09-09 12:48:34 -04:00
|
|
|
buf: NewBytesPipe(nil),
|
|
|
|
reader: r,
|
|
|
|
drainBuf: make([]byte, 1024),
|
2014-08-19 12:58:07 -04:00
|
|
|
}
|
|
|
|
reader.wait.L = &reader.Mutex
|
|
|
|
go reader.drain()
|
|
|
|
return reader
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NewBufReaderWithDrainbufAndBuffer returns a BufReader with drainBuffer and buffer.
|
2015-09-09 12:48:34 -04:00
|
|
|
func NewBufReaderWithDrainbufAndBuffer(r io.Reader, drainBuffer []byte, buffer io.ReadWriter) io.ReadCloser {
|
2014-08-19 12:58:07 -04:00
|
|
|
reader := &bufReader{
|
|
|
|
buf: buffer,
|
|
|
|
drainBuf: drainBuffer,
|
|
|
|
reader: r,
|
2014-08-12 12:10:43 -04:00
|
|
|
}
|
|
|
|
reader.wait.L = &reader.Mutex
|
|
|
|
go reader.drain()
|
|
|
|
return reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bufReader) drain() {
|
|
|
|
for {
|
2015-09-24 20:13:38 -04:00
|
|
|
//Call to scheduler is made to yield from this goroutine.
|
|
|
|
//This avoids goroutine looping here when n=0,err=nil, fixes code hangs when run with GCC Go.
|
|
|
|
callSchedulerIfNecessary()
|
2014-08-19 12:58:07 -04:00
|
|
|
n, err := r.reader.Read(r.drainBuf)
|
2014-08-12 12:10:43 -04:00
|
|
|
r.Lock()
|
|
|
|
if err != nil {
|
|
|
|
r.err = err
|
|
|
|
} else {
|
2015-09-09 12:48:34 -04:00
|
|
|
if n == 0 {
|
|
|
|
// nothing written, no need to signal
|
|
|
|
r.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.buf.Write(r.drainBuf[:n])
|
2014-08-12 12:10:43 -04:00
|
|
|
}
|
|
|
|
r.wait.Signal()
|
|
|
|
r.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bufReader) Read(p []byte) (n int, err error) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
for {
|
|
|
|
n, err = r.buf.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
if r.err != nil {
|
|
|
|
return 0, r.err
|
|
|
|
}
|
|
|
|
r.wait.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Close closes the bufReader
|
2014-08-12 12:10:43 -04:00
|
|
|
func (r *bufReader) Close() error {
|
|
|
|
closer, ok := r.reader.(io.ReadCloser)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return closer.Close()
|
|
|
|
}
|
2015-03-29 17:17:23 -04:00
|
|
|
|
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
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// OnEOFReader wraps a 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
|
|
|
|
}
|
|
|
|
}
|