/
client.go
204 lines (180 loc) · 4.62 KB
/
client.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// The client binary owns the filesystem and runs on the host machine
// (which may be Mac, Windows, Linux, etc) and responds to the dockerd
// server (which is running FUSE, and always Linux).
package main
import (
"flag"
"log"
"net"
"os"
"path/filepath"
"sync"
"github.com/dotcloud/docker/vfuse"
"github.com/dotcloud/docker/vfuse/pb"
"code.google.com/p/goprotobuf/proto"
)
var (
root = flag.String("root", ".", "Directory to share.")
rw = flag.Bool("writable", true, "whether -root is writable")
addr = flag.String("addr", "localhost:4321", "dockerfs service address")
verbose = flag.Bool("verbose", false, "verbose debugging mode")
)
type Volume struct {
Root string
Writable bool
mu sync.Mutex
files map[uint64]*os.File
}
// Server is the server that runs on the client (filesystem host)
// server. It accepts FUSE proxy requests from the peer (the Linux
// dockerd running a real FUSE filesystem) and responds with the FUSE
// answers from vol.
type Server struct {
// TODO(bradfitz): perhaps rename some things here to avoid both
// the words "server" and "client".
vol *Volume
c *vfuse.Client
}
func NewVolume(root string, writable bool) *Volume {
return &Volume{
Root: root,
Writable: writable,
files: make(map[uint64]*os.File),
}
}
func vlogf(format string, args ...interface{}) {
if !*verbose {
return
}
log.Printf("server: "+format, args...)
}
func main() {
flag.Parse()
conn, err := net.Dial("tcp", *addr)
if err != nil {
log.Panic(err)
}
srv := &Server{
vol: NewVolume(".", *rw),
c: vfuse.NewClient(conn),
}
srv.Run()
}
func (s *Server) Run() {
for {
vlogf("client: reading packet...")
p, err := s.c.ReadPacket()
vlogf("client: read packet %v, %v", p, err)
if err != nil {
log.Fatalf("client: ReadPacket error: %v", err)
}
vlogf("client: got packet %+v %T", p.Header, p.Body)
var res proto.Message
switch m := p.Body.(type) {
case *pb.AttrRequest:
res, err = s.handleAttrRequest(m)
case *pb.ReaddirRequest:
res, err = s.handleReaddirRequest(m)
case *pb.ReadlinkRequest:
res, err = s.handleReadlinkRequest(m)
case *pb.ChmodRequest:
res, err = s.handleChmodRequest(m)
default:
log.Fatalf("client: unhandled request type %T", p.Body)
}
if err != nil {
log.Fatalf("Error handling %T: %v", p.Body, err)
}
err = s.c.WriteReply(p.ID, res)
if err != nil {
log.Fatal(err)
}
}
}
func mapError(err error) *pb.Error {
if err == nil {
return nil
}
if os.IsNotExist(err) {
return &pb.Error{NotExist: proto.Bool(true)}
}
// TODO: more specific types
return &pb.Error{Other: proto.String(err.Error())}
}
// mapMode maps from a Go os.FileMode to a Linux FUSE uint32 mode.
func mapMode(m os.FileMode) uint32 {
mode := uint32(m & 0777)
const (
S_IFBLK = 0x6000
S_IFCHR = 0x2000
S_IFDIR = 0x4000
S_IFIFO = 0x1000
S_IFLNK = 0xa000
S_IFREG = 0x8000
S_IFSOCK = 0xc000
)
if m.IsDir() {
mode |= S_IFDIR
} else if m.IsRegular() {
mode |= S_IFREG
} else if m&os.ModeSymlink != 0 {
mode |= S_IFLNK
}
// TODO: more
return mode
}
func (s *Server) handleAttrRequest(req *pb.AttrRequest) (proto.Message, error) {
fi, err := os.Lstat(filepath.Join(s.vol.Root, filepath.FromSlash(req.GetName())))
res := new(pb.AttrResponse)
if err != nil {
res.Err = mapError(err)
return res, nil
}
res.Attr = &pb.Attr{
Size: proto.Uint64(uint64(fi.Size())),
Mode: proto.Uint32(mapMode(fi.Mode())),
// TODO: more
}
return res, nil
}
func (s *Server) handleReaddirRequest(req *pb.ReaddirRequest) (proto.Message, error) {
f, err := os.Open(filepath.Join(s.vol.Root, filepath.FromSlash(req.GetName())))
res := new(pb.ReaddirResponse)
if err != nil {
res.Err = mapError(err)
return res, nil
}
defer f.Close()
all, err := f.Readdir(-1)
if err != nil {
res.Err = mapError(err)
return res, nil
}
res.Entry = make([]*pb.DirEntry, 0, len(all))
for _, fi := range all {
res.Entry = append(res.Entry, &pb.DirEntry{
Name: proto.String(fi.Name()),
Mode: proto.Uint32(mapMode(fi.Mode())),
})
}
return res, nil
}
func (s *Server) handleReadlinkRequest(req *pb.ReadlinkRequest) (proto.Message, error) {
target, err := os.Readlink(filepath.Join(s.vol.Root, filepath.FromSlash(req.GetName())))
res := new(pb.ReadlinkResponse)
if err != nil {
res.Err = mapError(err)
return res, nil
}
res.Target = &target
return res, nil
}
func (s *Server) handleChmodRequest(req *pb.ChmodRequest) (proto.Message, error) {
err := os.Chmod(filepath.Join(s.vol.Root, filepath.FromSlash(req.GetName())), os.FileMode(req.GetMode()))
res := new(pb.ChmodResponse)
if err != nil {
res.Err = mapError(err)
return res, nil
}
return res, nil
}