2014-09-17 15:04:56 +00:00
|
|
|
package stdcopy
|
|
|
|
|
|
|
|
import (
|
2016-03-21 03:25:11 +00:00
|
|
|
"bytes"
|
2014-09-17 15:04:56 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2016-02-25 23:22:19 +00:00
|
|
|
"fmt"
|
2014-09-17 15:04:56 +00:00
|
|
|
"io"
|
2016-03-21 03:25:11 +00:00
|
|
|
"sync"
|
2014-09-17 15:04:56 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-09-17 15:04:56 +00:00
|
|
|
)
|
|
|
|
|
2016-02-25 23:22:19 +00:00
|
|
|
// StdType is the type of standard stream
|
|
|
|
// a writer can multiplex to.
|
|
|
|
type StdType byte
|
|
|
|
|
2014-09-17 15:04:56 +00:00
|
|
|
const (
|
2016-02-25 23:22:19 +00:00
|
|
|
// Stdin represents standard input stream type.
|
|
|
|
Stdin StdType = iota
|
|
|
|
// Stdout represents standard output stream type.
|
|
|
|
Stdout
|
|
|
|
// Stderr represents standard error steam type.
|
|
|
|
Stderr
|
|
|
|
|
2015-07-28 16:13:12 +00:00
|
|
|
stdWriterPrefixLen = 8
|
|
|
|
stdWriterFdIndex = 0
|
|
|
|
stdWriterSizeIndex = 4
|
2015-09-22 22:30:06 +00:00
|
|
|
|
|
|
|
startingBufLen = 32*1024 + stdWriterPrefixLen + 1
|
2014-09-17 15:04:56 +00:00
|
|
|
)
|
|
|
|
|
2016-03-21 03:25:11 +00:00
|
|
|
var bufPool = &sync.Pool{New: func() interface{} { return bytes.NewBuffer(nil) }}
|
|
|
|
|
2016-02-25 23:22:19 +00:00
|
|
|
// stdWriter is wrapper of io.Writer with extra customized info.
|
|
|
|
type stdWriter struct {
|
2014-09-17 15:04:56 +00:00
|
|
|
io.Writer
|
2016-02-25 23:22:19 +00:00
|
|
|
prefix byte
|
2014-09-17 15:04:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-25 23:22:19 +00:00
|
|
|
// Write sends the buffer to the underneath writer.
|
2016-03-21 03:25:11 +00:00
|
|
|
// It inserts the prefix header before the buffer,
|
2016-02-25 23:22:19 +00:00
|
|
|
// so stdcopy.StdCopy knows where to multiplex the output.
|
|
|
|
// It makes stdWriter to implement io.Writer.
|
2016-03-21 03:25:11 +00:00
|
|
|
func (w *stdWriter) Write(p []byte) (n int, err error) {
|
2014-09-17 15:04:56 +00:00
|
|
|
if w == nil || w.Writer == nil {
|
2015-06-24 07:00:14 +00:00
|
|
|
return 0, errors.New("Writer not instantiated")
|
2014-09-17 15:04:56 +00:00
|
|
|
}
|
2016-03-21 03:25:11 +00:00
|
|
|
if p == nil {
|
2016-02-25 23:22:19 +00:00
|
|
|
return 0, nil
|
2014-09-17 13:50:56 +00:00
|
|
|
}
|
2016-02-25 23:22:19 +00:00
|
|
|
|
|
|
|
header := [stdWriterPrefixLen]byte{stdWriterFdIndex: w.prefix}
|
2016-03-21 03:25:11 +00:00
|
|
|
binary.BigEndian.PutUint32(header[stdWriterSizeIndex:], uint32(len(p)))
|
|
|
|
buf := bufPool.Get().(*bytes.Buffer)
|
|
|
|
buf.Write(header[:])
|
|
|
|
buf.Write(p)
|
2016-02-25 23:22:19 +00:00
|
|
|
|
2016-03-21 03:25:11 +00:00
|
|
|
n, err = w.Writer.Write(buf.Bytes())
|
2016-02-25 23:22:19 +00:00
|
|
|
n -= stdWriterPrefixLen
|
2014-09-17 13:50:56 +00:00
|
|
|
if n < 0 {
|
|
|
|
n = 0
|
|
|
|
}
|
2016-03-21 03:25:11 +00:00
|
|
|
|
|
|
|
buf.Reset()
|
|
|
|
bufPool.Put(buf)
|
2014-09-17 13:50:56 +00:00
|
|
|
return
|
2014-09-17 15:04:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 07:00:14 +00:00
|
|
|
// NewStdWriter instantiates a new Writer.
|
2014-09-17 15:04:56 +00:00
|
|
|
// Everything written to it will be encapsulated using a custom format,
|
|
|
|
// and written to the underlying `w` stream.
|
|
|
|
// This allows multiple write streams (e.g. stdout and stderr) to be muxed into a single connection.
|
|
|
|
// `t` indicates the id of the stream to encapsulate.
|
2015-04-13 09:21:27 +00:00
|
|
|
// It can be stdcopy.Stdin, stdcopy.Stdout, stdcopy.Stderr.
|
2016-02-25 23:22:19 +00:00
|
|
|
func NewStdWriter(w io.Writer, t StdType) io.Writer {
|
|
|
|
return &stdWriter{
|
|
|
|
Writer: w,
|
|
|
|
prefix: byte(t),
|
2014-09-17 15:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StdCopy is a modified version of io.Copy.
|
|
|
|
//
|
|
|
|
// StdCopy will demultiplex `src`, assuming that it contains two streams,
|
|
|
|
// previously multiplexed together using a StdWriter instance.
|
|
|
|
// As it reads from `src`, StdCopy will write to `dstout` and `dsterr`.
|
|
|
|
//
|
|
|
|
// StdCopy will read until it hits EOF on `src`. It will then return a nil error.
|
|
|
|
// In other words: if `err` is non nil, it indicates a real underlying error.
|
|
|
|
//
|
|
|
|
// `written` will hold the total number of bytes written to `dstout` and `dsterr`.
|
|
|
|
func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
|
|
|
|
var (
|
2015-09-22 22:30:06 +00:00
|
|
|
buf = make([]byte, startingBufLen)
|
2014-09-17 15:04:56 +00:00
|
|
|
bufLen = len(buf)
|
|
|
|
nr, nw int
|
|
|
|
er, ew error
|
|
|
|
out io.Writer
|
|
|
|
frameSize int
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Make sure we have at least a full header
|
2015-07-28 16:13:12 +00:00
|
|
|
for nr < stdWriterPrefixLen {
|
2014-09-17 15:04:56 +00:00
|
|
|
var nr2 int
|
|
|
|
nr2, er = src.Read(buf[nr:])
|
|
|
|
nr += nr2
|
|
|
|
if er == io.EOF {
|
2015-07-28 16:13:12 +00:00
|
|
|
if nr < stdWriterPrefixLen {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Corrupted prefix: %v", buf[:nr])
|
2014-09-17 15:04:56 +00:00
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error reading header: %s", er)
|
2014-09-17 15:04:56 +00:00
|
|
|
return 0, er
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the first byte to know where to write
|
2016-02-25 23:22:19 +00:00
|
|
|
switch StdType(buf[stdWriterFdIndex]) {
|
|
|
|
case Stdin:
|
2014-09-17 15:04:56 +00:00
|
|
|
fallthrough
|
2016-02-25 23:22:19 +00:00
|
|
|
case Stdout:
|
2014-09-17 15:04:56 +00:00
|
|
|
// Write on stdout
|
|
|
|
out = dstout
|
2016-02-25 23:22:19 +00:00
|
|
|
case Stderr:
|
2014-09-17 15:04:56 +00:00
|
|
|
// Write on stderr
|
|
|
|
out = dsterr
|
|
|
|
default:
|
2015-07-28 16:13:12 +00:00
|
|
|
logrus.Debugf("Error selecting output fd: (%d)", buf[stdWriterFdIndex])
|
2016-02-25 23:22:19 +00:00
|
|
|
return 0, fmt.Errorf("Unrecognized input header: %d", buf[stdWriterFdIndex])
|
2014-09-17 15:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the size of the frame
|
2015-07-28 16:13:12 +00:00
|
|
|
frameSize = int(binary.BigEndian.Uint32(buf[stdWriterSizeIndex : stdWriterSizeIndex+4]))
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("framesize: %d", frameSize)
|
2014-09-17 15:04:56 +00:00
|
|
|
|
|
|
|
// Check if the buffer is big enough to read the frame.
|
|
|
|
// Extend it if necessary.
|
2015-07-28 16:13:12 +00:00
|
|
|
if frameSize+stdWriterPrefixLen > bufLen {
|
|
|
|
logrus.Debugf("Extending buffer cap by %d (was %d)", frameSize+stdWriterPrefixLen-bufLen+1, len(buf))
|
|
|
|
buf = append(buf, make([]byte, frameSize+stdWriterPrefixLen-bufLen+1)...)
|
2014-09-17 15:04:56 +00:00
|
|
|
bufLen = len(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// While the amount of bytes read is less than the size of the frame + header, we keep reading
|
2015-07-28 16:13:12 +00:00
|
|
|
for nr < frameSize+stdWriterPrefixLen {
|
2014-09-17 15:04:56 +00:00
|
|
|
var nr2 int
|
|
|
|
nr2, er = src.Read(buf[nr:])
|
|
|
|
nr += nr2
|
|
|
|
if er == io.EOF {
|
2015-07-28 16:13:12 +00:00
|
|
|
if nr < frameSize+stdWriterPrefixLen {
|
|
|
|
logrus.Debugf("Corrupted frame: %v", buf[stdWriterPrefixLen:nr])
|
2014-09-17 15:04:56 +00:00
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error reading frame: %s", er)
|
2014-09-17 15:04:56 +00:00
|
|
|
return 0, er
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the retrieved frame (without header)
|
2015-07-28 16:13:12 +00:00
|
|
|
nw, ew = out.Write(buf[stdWriterPrefixLen : frameSize+stdWriterPrefixLen])
|
2014-09-17 15:04:56 +00:00
|
|
|
if ew != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error writing frame: %s", ew)
|
2014-09-17 15:04:56 +00:00
|
|
|
return 0, ew
|
|
|
|
}
|
|
|
|
// If the frame has not been fully written: error
|
|
|
|
if nw != frameSize {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error Short Write: (%d on %d)", nw, frameSize)
|
2014-09-17 15:04:56 +00:00
|
|
|
return 0, io.ErrShortWrite
|
|
|
|
}
|
|
|
|
written += int64(nw)
|
|
|
|
|
|
|
|
// Move the rest of the buffer to the beginning
|
2015-07-28 16:13:12 +00:00
|
|
|
copy(buf, buf[frameSize+stdWriterPrefixLen:])
|
2014-09-17 15:04:56 +00:00
|
|
|
// Move the index
|
2015-07-28 16:13:12 +00:00
|
|
|
nr -= frameSize + stdWriterPrefixLen
|
2014-09-17 15:04:56 +00:00
|
|
|
}
|
|
|
|
}
|