forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iostream.go
91 lines (72 loc) · 1.58 KB
/
iostream.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2018 HyperHQ Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package virtcontainers
import (
"errors"
"io"
)
type iostream struct {
sandbox *Sandbox
container *Container
process string
closed bool
}
// io.WriteCloser
type stdinStream struct {
*iostream
}
// io.Reader
type stdoutStream struct {
*iostream
}
// io.Reader
type stderrStream struct {
*iostream
}
func newIOStream(s *Sandbox, c *Container, proc string) *iostream {
return &iostream{
sandbox: s,
container: c,
process: proc,
closed: false, // needed to workaround buggy structcheck
}
}
func (s *iostream) stdin() io.WriteCloser {
return &stdinStream{s}
}
func (s *iostream) stdout() io.Reader {
return &stdoutStream{s}
}
func (s *iostream) stderr() io.Reader {
return &stderrStream{s}
}
func (s *stdinStream) Write(data []byte) (n int, err error) {
if s.closed {
return 0, errors.New("stream closed")
}
return s.sandbox.agent.writeProcessStdin(s.container, s.process, data)
}
func (s *stdinStream) Close() error {
if s.closed {
return errors.New("stream closed")
}
err := s.sandbox.agent.closeProcessStdin(s.container, s.process)
if err == nil {
s.closed = true
}
return err
}
func (s *stdoutStream) Read(data []byte) (n int, err error) {
if s.closed {
return 0, errors.New("stream closed")
}
return s.sandbox.agent.readProcessStdout(s.container, s.process, data)
}
func (s *stderrStream) Read(data []byte) (n int, err error) {
if s.closed {
return 0, errors.New("stream closed")
}
return s.sandbox.agent.readProcessStderr(s.container, s.process, data)
}