-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap.go
79 lines (70 loc) · 1.49 KB
/
map.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
package sandboxfs
import (
"bufio"
"encoding/json"
"fmt"
"log"
"time"
)
type mapRequest struct {
Mapping string
Target string
Writable bool
}
func (sfs *Sandboxfs) mapHandler(requests <-chan []byte) {
for {
select {
case mr := <-requests:
// write mapping to the sandboxfs stdin
fmt.Fprintf(
sfs.stdin,
`[{"Map": %s}]`,
string(mr),
)
fmt.Fprint(sfs.stdin, "\n\n")
case <-sfs.ctx.Done():
return
}
}
}
func (sfs *Sandboxfs) stdoutHandler() {
scanner := bufio.NewScanner(sfs.stdout)
for scanner.Scan() {
line := scanner.Text()
if line == "Done" {
// sandboxfs completed a map request
sfs.outstandingRequests.Done()
} else {
// sandboxfs said something that we don't understand
log.Println("[sandboxfs]", line)
}
}
}
// Map sends a mapping request to sandboxfs.
func (sfs *Sandboxfs) Map(mapping, target string, writable bool) error {
buf, err := json.Marshal(&mapRequest{
Mapping: mapping,
Target: target,
Writable: writable,
})
if err != nil {
return fmt.Errorf("marshaling MapRequest: %v", err)
}
sfs.outstandingRequests.Add(1)
sfs.requests <- buf
return nil
}
// WaitUntilReady waits until all map requests have been processed.
func (sfs *Sandboxfs) WaitUntilReady(timeout time.Duration) error {
ready := make(chan bool)
go func() {
sfs.outstandingRequests.Wait()
close(ready)
}()
select {
case <-ready:
return nil
case <-time.After(timeout):
return fmt.Errorf("timed out waiting for sandboxfs to become ready")
}
}