/
pipe.go
175 lines (153 loc) · 3.4 KB
/
pipe.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package fs
import (
"io"
"os"
"time"
"github.com/hack-pad/hackpad/internal/interop"
)
func (f *FileDescriptors) Pipe() [2]FID {
r, w := newPipe(f.newFID)
f.addFileDescriptor(r)
f.addFileDescriptor(w)
r.Open(f.parentPID)
w.Open(f.parentPID)
return [2]FID{r.id, w.id}
}
func newPipe(newFID func() FID) (r, w *fileDescriptor) {
readerFID, writerFID := newFID(), newFID()
pipeC := newPipeChan(readerFID, writerFID)
rPipe := &namedPipe{pipeChan: pipeC, fid: readerFID}
r = newIrregularFileDescriptor(
readerFID,
rPipe.Name(),
&pipeReadOnly{rPipe},
os.ModeNamedPipe,
)
wPipe := &namedPipe{pipeChan: pipeC, fid: writerFID}
w = newIrregularFileDescriptor(
writerFID,
wPipe.Name(),
&pipeWriteOnly{wPipe},
os.ModeNamedPipe,
)
return
}
type pipeChan struct {
buf chan byte
done chan struct{}
reader, writer FID
}
func newPipeChan(reader, writer FID) *pipeChan {
const maxPipeBuffer = 32 << 10 // 32KiB
return &pipeChan{
buf: make(chan byte, maxPipeBuffer),
done: make(chan struct{}),
reader: reader,
writer: writer,
}
}
type pipeStat struct {
name string
size int64
mode os.FileMode
}
func (p pipeStat) Name() string { return p.name }
func (p pipeStat) Size() int64 { return p.size }
func (p pipeStat) Mode() os.FileMode { return p.mode }
func (p pipeStat) ModTime() time.Time { return time.Time{} }
func (p pipeStat) IsDir() bool { return false }
func (p pipeStat) Sys() interface{} { return nil }
func (p *pipeChan) Stat() (os.FileInfo, error) {
return &pipeStat{
name: "",
size: int64(len(p.buf)),
mode: os.ModeNamedPipe,
}, nil
}
func (p *pipeChan) Sync() error {
timer := time.NewTimer(time.Second)
defer timer.Stop()
select {
case <-p.done:
return nil
case <-timer.C:
return io.ErrNoProgress
}
}
func (p *pipeChan) Read(buf []byte) (n int, err error) {
for n < len(buf) {
// Read should always block if the pipe is not closed
b, ok := <-p.buf
if !ok {
err = io.EOF
return
}
buf[n] = b
n++
}
if n == 0 {
err = io.EOF
}
return
}
func (p *pipeChan) Write(buf []byte) (n int, err error) {
for _, b := range buf {
select {
case <-p.done:
// do not allow writes to a closed pipe
return 0, interop.BadFileNumber(p.writer)
case p.buf <- b:
n++
// no default case allowed, Write should always return immediately if the pipe buffer has space, otherwise it should block
}
}
if n < len(buf) {
err = io.ErrShortWrite
}
return
}
func (p *pipeChan) Close() error {
select {
case <-p.done:
return interop.BadFileNumber(p.writer)
default:
close(p.done)
close(p.buf)
return nil
}
}
type namedPipe struct {
*pipeChan
fid FID
}
func (n *namedPipe) Name() string {
return "pipe" + n.fid.String()
}
type pipeReadOnly struct {
*namedPipe
}
func (r *pipeReadOnly) ReadAt(buf []byte, off int64) (n int, err error) {
if off == 0 {
return r.Read(buf)
}
return 0, interop.ErrNotImplemented
}
func (r *pipeReadOnly) Write(buf []byte) (n int, err error) {
return 0, interop.ErrNotImplemented
}
func (r *pipeReadOnly) Close() error {
// only write side of pipe should close the buffer
return nil
}
type pipeWriteOnly struct {
*namedPipe
}
func (w *pipeWriteOnly) Read(buf []byte) (n int, err error) {
return 0, interop.ErrNotImplemented
}
func (w *pipeWriteOnly) WriteAt(buf []byte, off int64) (n int, err error) {
if off == 0 {
return w.Write(buf)
}
return 0, interop.ErrNotImplemented
}