-
Notifications
You must be signed in to change notification settings - Fork 63
/
file.go
98 lines (80 loc) · 1.88 KB
/
file.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
package stream
import (
"fmt"
"io"
"math"
"os"
"github.com/criyle/go-judge/envexec"
"github.com/criyle/go-judge/filestore"
"github.com/criyle/go-judge/worker"
)
var (
_ worker.CmdFile = &fileStreamIn{}
_ worker.CmdFile = &fileStreamOut{}
)
type fileStreamIn struct {
index int
fd int
r io.ReadCloser
w *io.PipeWriter
tty *os.File
done chan struct{}
hasTTY bool
}
type fileStreamInReader struct {
*io.PipeReader
fi *fileStreamIn
}
func (f *fileStreamInReader) TTY(tty *os.File) {
f.fi.tty = tty
close(f.fi.done)
}
func newFileStreamIn(index, fd int, hasTTY bool) *fileStreamIn {
r, w := io.Pipe()
fi := &fileStreamIn{index: index, fd: fd, w: w, done: make(chan struct{}), hasTTY: hasTTY}
fi.r = &fileStreamInReader{r, fi}
return fi
}
func (f *fileStreamIn) GetTTY() *os.File {
if !f.hasTTY {
return nil
}
<-f.done
return f.tty
}
func (f *fileStreamIn) Write(b []byte) (int, error) {
return f.w.Write(b)
}
func (f *fileStreamIn) EnvFile(fs filestore.FileStore) (envexec.File, error) {
return envexec.NewFileReader(f.r, true), nil
}
func (f *fileStreamIn) String() string {
return fmt.Sprintf("fileStreamIn:(index:%d,fd:%d)", f.index, f.fd)
}
func (f *fileStreamIn) Close() error {
f.r.Close()
return f.w.Close()
}
type fileStreamOut struct {
index int
fd int
r *io.PipeReader
w *io.PipeWriter
}
func newFileStreamOut(index, fd int) *fileStreamOut {
r, w := io.Pipe()
return &fileStreamOut{index: index, fd: fd, r: r, w: w}
}
func (f *fileStreamOut) Read(b []byte) (int, error) {
return f.r.Read(b)
}
func (f *fileStreamOut) EnvFile(fs filestore.FileStore) (envexec.File, error) {
return envexec.NewFileWriter(f.w, envexec.Size(math.MaxInt32)), nil
}
func (f *fileStreamOut) String() string {
return fmt.Sprintf("fileStreamOut:(index:%d,fd:%d)", f.index, f.fd)
}
func (f *fileStreamOut) Close() error {
f.w.Close()
return f.r.Close()
}