-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsrv.go
87 lines (76 loc) · 1.31 KB
/
srv.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
//go:build !plan9
// +build !plan9
package go9p
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"sync"
"9fans.net/go/plan9/client"
"github.com/fhs/mux9p"
)
type readWriteCloser interface {
io.Reader
io.Writer
Close()
}
type pipe struct {
sync.Mutex
in <-chan []byte
inbs []byte
out chan<- []byte
}
func (p *pipe) Read(b []byte) (n int, err error) {
if len(p.inbs) > 0 {
copied := copy(b, p.inbs)
p.inbs = p.inbs[copied:]
return copied, nil
}
bs := <-p.in
if bs == nil {
return 0, fmt.Errorf("pipe closed.")
}
copied := copy(b, bs)
if copied < len(bs) {
p.inbs = bs[copied:]
}
return copied, nil
}
func (p *pipe) Write(b []byte) (n int, err error) {
p.out <- b
return len(b), nil
}
func (p *pipe) Close() {
close(p.out)
}
func newPipePair() (*pipe, *pipe) {
c1 := make(chan []byte, 100)
c2 := make(chan []byte, 100)
p1 := &pipe{
in: c1,
out: c2,
}
p2 := &pipe{
in: c2,
out: c1,
}
return p1, p2
}
func postfd(name string) (readWriteCloser, *os.File, error) {
ns := client.Namespace()
if err := os.MkdirAll(ns, 0700); err != nil {
return nil, nil, err
}
name = filepath.Join(ns, name)
f1, f2 := newPipePair()
go func() {
err := mux9p.Listen("unix", name, f2, &mux9p.Config{})
if err != nil {
log.Printf("Error: %v", err)
}
f2.Close()
}()
return f1, nil, nil
}