-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathutil.go
More file actions
87 lines (76 loc) · 1.56 KB
/
Copy pathutil.go
File metadata and controls
87 lines (76 loc) · 1.56 KB
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
//go:build windows
// +build windows
package main
import (
"encoding/gob"
"io"
"sync"
"syscall"
)
type msg struct {
Name string
Exit int
Error string
Data []byte
}
// msgEncoder is a gob encoder that is safe for concurrent use, since the
// stdout/stderr writers and the main goroutine all encode to a single
// connection.
type msgEncoder struct {
mu sync.Mutex
enc *gob.Encoder
}
func newMsgEncoder(w io.Writer) *msgEncoder {
return &msgEncoder{enc: gob.NewEncoder(w)}
}
func (e *msgEncoder) Encode(v interface{}) error {
e.mu.Lock()
defer e.mu.Unlock()
return e.enc.Encode(v)
}
// msgWriter is an io.WriteCloser whose Close blocks until every byte written
// has been encoded, so callers can flush all output before sending a final
// message such as the exit code.
type msgWriter struct {
w *io.PipeWriter
done chan struct{}
once sync.Once
}
func (mw *msgWriter) Write(p []byte) (int, error) {
return mw.w.Write(p)
}
func (mw *msgWriter) Close() error {
err := mw.w.Close()
mw.once.Do(func() { <-mw.done })
return err
}
func msgWrite(enc *msgEncoder, typ string) io.WriteCloser {
r, w := io.Pipe()
done := make(chan struct{})
go func() {
defer close(done)
defer r.Close()
var b [4096]byte
for {
n, err := r.Read(b[:])
if err != nil {
break
}
err = enc.Encode(&msg{Name: typ, Data: b[:n]})
if err != nil {
break
}
}
}()
return &msgWriter{w: w, done: done}
}
func makeCmdLine(args []string) string {
var s string
for _, v := range args {
if s != "" {
s += " "
}
s += syscall.EscapeArg(v)
}
return s
}