2014-09-17 11:04:56 -04:00
|
|
|
package stdcopy
|
2013-09-11 14:35:09 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2014-07-24 16:37:44 -04:00
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2013-09-11 14:35:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-07-28 12:13:12 -04:00
|
|
|
stdWriterPrefixLen = 8
|
|
|
|
stdWriterFdIndex = 0
|
|
|
|
stdWriterSizeIndex = 4
|
2015-09-22 18:30:06 -04:00
|
|
|
|
|
|
|
startingBufLen = 32*1024 + stdWriterPrefixLen + 1
|
2013-09-11 14:35:09 -04:00
|
|
|
)
|
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
// StdType prefixes type and length to standard stream.
|
|
|
|
type StdType [stdWriterPrefixLen]byte
|
2013-09-11 14:35:09 -04:00
|
|
|
|
|
|
|
var (
|
2015-07-28 12:13:12 -04:00
|
|
|
// Stdin represents standard input stream type.
|
|
|
|
Stdin = StdType{0: 0}
|
|
|
|
// Stdout represents standard output stream type.
|
|
|
|
Stdout = StdType{0: 1}
|
|
|
|
// Stderr represents standard error steam type.
|
|
|
|
Stderr = StdType{0: 2}
|
2013-09-11 14:35:09 -04:00
|
|
|
)
|
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
// StdWriter is wrapper of io.Writer with extra customized info.
|
2013-09-11 14:35:09 -04:00
|
|
|
type StdWriter struct {
|
|
|
|
io.Writer
|
2013-09-26 18:59:02 -04:00
|
|
|
prefix StdType
|
|
|
|
sizeBuf []byte
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *StdWriter) Write(buf []byte) (n int, err error) {
|
2014-09-17 09:50:56 -04:00
|
|
|
var n1, n2 int
|
2013-09-11 14:35:09 -04:00
|
|
|
if w == nil || w.Writer == nil {
|
2015-06-24 03:00:14 -04:00
|
|
|
return 0, errors.New("Writer not instantiated")
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
2013-09-26 18:59:02 -04:00
|
|
|
binary.BigEndian.PutUint32(w.prefix[4:], uint32(len(buf)))
|
2014-09-17 09:50:56 -04:00
|
|
|
n1, err = w.Writer.Write(w.prefix[:])
|
|
|
|
if err != nil {
|
2015-07-28 12:13:12 -04:00
|
|
|
n = n1 - stdWriterPrefixLen
|
2014-09-17 09:50:56 -04:00
|
|
|
} else {
|
|
|
|
n2, err = w.Writer.Write(buf)
|
2015-07-28 12:13:12 -04:00
|
|
|
n = n1 + n2 - stdWriterPrefixLen
|
2014-09-17 09:50:56 -04:00
|
|
|
}
|
|
|
|
if n < 0 {
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
return
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 03:00:14 -04:00
|
|
|
// NewStdWriter instantiates a new Writer.
|
2013-10-16 13:58:53 -04: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 05:21:27 -04:00
|
|
|
// It can be stdcopy.Stdin, stdcopy.Stdout, stdcopy.Stderr.
|
2013-09-11 14:35:09 -04:00
|
|
|
func NewStdWriter(w io.Writer, t StdType) *StdWriter {
|
|
|
|
return &StdWriter{
|
2013-09-26 18:59:02 -04:00
|
|
|
Writer: w,
|
|
|
|
prefix: t,
|
|
|
|
sizeBuf: make([]byte, 4),
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
var errInvalidStdHeader = errors.New("Unrecognized input header")
|
2013-09-11 14:35:09 -04:00
|
|
|
|
|
|
|
// StdCopy is a modified version of io.Copy.
|
|
|
|
//
|
2013-10-16 13:58:53 -04:00
|
|
|
// 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`.
|
2013-09-11 14:35:09 -04:00
|
|
|
//
|
2013-10-16 13:58:53 -04:00
|
|
|
// 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.
|
2013-09-11 14:35:09 -04:00
|
|
|
//
|
2013-10-16 13:58:53 -04:00
|
|
|
// `written` will hold the total number of bytes written to `dstout` and `dsterr`.
|
2013-09-11 14:35:09 -04:00
|
|
|
func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
|
|
|
|
var (
|
2015-09-22 18:30:06 -04:00
|
|
|
buf = make([]byte, startingBufLen)
|
2013-09-11 14:35:09 -04: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 12:13:12 -04:00
|
|
|
for nr < stdWriterPrefixLen {
|
2013-09-11 14:35:09 -04:00
|
|
|
var nr2 int
|
|
|
|
nr2, er = src.Read(buf[nr:])
|
|
|
|
nr += nr2
|
2014-06-19 13:42:55 -04:00
|
|
|
if er == io.EOF {
|
2015-07-28 12:13:12 -04:00
|
|
|
if nr < stdWriterPrefixLen {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Corrupted prefix: %v", buf[:nr])
|
2014-06-19 13:42:55 -04:00
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error reading header: %s", er)
|
2014-06-19 13:42:55 -04:00
|
|
|
return 0, er
|
2014-06-09 08:28:32 -04:00
|
|
|
}
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the first byte to know where to write
|
2015-07-28 12:13:12 -04:00
|
|
|
switch buf[stdWriterFdIndex] {
|
2013-09-11 14:35:09 -04:00
|
|
|
case 0:
|
|
|
|
fallthrough
|
|
|
|
case 1:
|
|
|
|
// Write on stdout
|
|
|
|
out = dstout
|
|
|
|
case 2:
|
|
|
|
// Write on stderr
|
|
|
|
out = dsterr
|
|
|
|
default:
|
2015-07-28 12:13:12 -04:00
|
|
|
logrus.Debugf("Error selecting output fd: (%d)", buf[stdWriterFdIndex])
|
|
|
|
return 0, errInvalidStdHeader
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the size of the frame
|
2015-07-28 12:13:12 -04:00
|
|
|
frameSize = int(binary.BigEndian.Uint32(buf[stdWriterSizeIndex : stdWriterSizeIndex+4]))
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("framesize: %d", frameSize)
|
2013-09-11 14:35:09 -04:00
|
|
|
|
|
|
|
// Check if the buffer is big enough to read the frame.
|
|
|
|
// Extend it if necessary.
|
2015-07-28 12:13:12 -04: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)...)
|
2013-09-11 14:35:09 -04: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 12:13:12 -04:00
|
|
|
for nr < frameSize+stdWriterPrefixLen {
|
2013-09-11 14:35:09 -04:00
|
|
|
var nr2 int
|
|
|
|
nr2, er = src.Read(buf[nr:])
|
2014-06-19 13:42:55 -04:00
|
|
|
nr += nr2
|
2013-09-11 14:35:09 -04:00
|
|
|
if er == io.EOF {
|
2015-07-28 12:13:12 -04:00
|
|
|
if nr < frameSize+stdWriterPrefixLen {
|
|
|
|
logrus.Debugf("Corrupted frame: %v", buf[stdWriterPrefixLen:nr])
|
2014-06-19 13:42:55 -04:00
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
break
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
|
|
|
if er != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error reading frame: %s", er)
|
2013-09-11 14:35:09 -04:00
|
|
|
return 0, er
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the retrieved frame (without header)
|
2015-07-28 12:13:12 -04:00
|
|
|
nw, ew = out.Write(buf[stdWriterPrefixLen : frameSize+stdWriterPrefixLen])
|
2013-09-11 14:35:09 -04:00
|
|
|
if ew != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error writing frame: %s", ew)
|
2013-09-11 14:35:09 -04:00
|
|
|
return 0, ew
|
|
|
|
}
|
|
|
|
// If the frame has not been fully written: error
|
|
|
|
if nw != frameSize {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Error Short Write: (%d on %d)", nw, frameSize)
|
2013-09-11 14:35:09 -04:00
|
|
|
return 0, io.ErrShortWrite
|
|
|
|
}
|
2014-06-19 13:42:55 -04:00
|
|
|
written += int64(nw)
|
2013-09-11 14:35:09 -04:00
|
|
|
|
|
|
|
// Move the rest of the buffer to the beginning
|
2015-07-28 12:13:12 -04:00
|
|
|
copy(buf, buf[frameSize+stdWriterPrefixLen:])
|
2013-09-11 14:35:09 -04:00
|
|
|
// Move the index
|
2015-07-28 12:13:12 -04:00
|
|
|
nr -= frameSize + stdWriterPrefixLen
|
2013-09-11 14:35:09 -04:00
|
|
|
}
|
|
|
|
}
|