-
Notifications
You must be signed in to change notification settings - Fork 0
/
attach.go
54 lines (48 loc) · 861 Bytes
/
attach.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package oci
import (
"io"
"github.com/sirupsen/logrus"
)
/* Sync with stdpipe_t in conmon.c */
const (
AttachPipeStdin = 1
AttachPipeStdout = 2
AttachPipeStderr = 3
)
func redirectResponseToOutputStreams(outputStream, errorStream io.WriteCloser, conn io.Reader) error {
var err error
buf := make([]byte, BufSize+1)
for {
nr, er := conn.Read(buf)
if nr > 0 {
var dst io.Writer
switch buf[0] {
case AttachPipeStdout:
dst = outputStream
case AttachPipeStderr:
dst = errorStream
default:
logrus.Debugf("Got unexpected attach type %+d", buf[0])
}
if dst != nil {
nw, ew := dst.Write(buf[1:nr])
if ew != nil {
err = ew
break
}
if nr != nw+1 {
err = io.ErrShortWrite
break
}
}
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
return err
}