-
Notifications
You must be signed in to change notification settings - Fork 22
/
srv_file.go
561 lines (477 loc) · 11 KB
/
srv_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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright 2009 The Go9p Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package go9p
import (
"log"
"sync"
"time"
)
// The FStatOp interface provides a single operation (Stat) that will be
// called before a file stat is sent back to the client. If implemented,
// the operation should update the data in the srvFile struct.
type FStatOp interface {
Stat(fid *FFid) error
}
// The FWstatOp interface provides a single operation (Wstat) that will be
// called when the client requests the srvFile metadata to be modified. If
// implemented, the operation will be called when Twstat message is received.
// If not implemented, "permission denied" error will be sent back. If the
// operation returns an Error, the error is send back to the client.
type FWstatOp interface {
Wstat(*FFid, *Dir) error
}
// If the FReadOp interface is implemented, the Read operation will be called
// to read from the file. If not implemented, "permission denied" error will
// be send back. The operation returns the number of bytes read, or the
// error occured while reading.
type FReadOp interface {
Read(fid *FFid, buf []byte, offset uint64) (int, error)
}
// If the FWriteOp interface is implemented, the Write operation will be called
// to write to the file. If not implemented, "permission denied" error will
// be send back. The operation returns the number of bytes written, or the
// error occured while writing.
type FWriteOp interface {
Write(fid *FFid, data []byte, offset uint64) (int, error)
}
// If the FCreateOp interface is implemented, the Create operation will be called
// when the client attempts to create a file in the srvFile implementing the interface.
// If not implemented, "permission denied" error will be send back. If successful,
// the operation should call (*File)Add() to add the created file to the directory.
// The operation returns the created file, or the error occured while creating it.
type FCreateOp interface {
Create(fid *FFid, name string, perm uint32) (*srvFile, error)
}
// If the FRemoveOp interface is implemented, the Remove operation will be called
// when the client attempts to create a file in the srvFile implementing the interface.
// If not implemented, "permission denied" error will be send back.
// The operation returns nil if successful, or the error that occured while removing
// the file.
type FRemoveOp interface {
Remove(*FFid) error
}
type FOpenOp interface {
Open(fid *FFid, mode uint8) error
}
type FClunkOp interface {
Clunk(fid *FFid) error
}
type FDestroyOp interface {
FidDestroy(fid *FFid)
}
type FFlags int
const (
Fremoved FFlags = 1 << iota
)
// The srvFile type represents a file (or directory) served by the file server.
type srvFile struct {
sync.Mutex
Dir
flags FFlags
Parent *srvFile // parent
next, prev *srvFile // siblings, guarded by parent.Lock
cfirst, clast *srvFile // children (if directory)
ops interface{}
}
type FFid struct {
F *srvFile
Fid *SrvFid
dirs []*srvFile // used for readdir
dirents []byte // serialized version of dirs
}
// The Fsrv can be used to create file servers that serve
// simple trees of synthetic files.
type Fsrv struct {
Srv
Root *srvFile
}
var lock sync.Mutex
var qnext uint64
var Eexist = &Error{"file already exists", EEXIST}
var Enoent = &Error{"file not found", ENOENT}
var Enotempty = &Error{"directory not empty", EPERM}
// Creates a file server with root as root directory
func NewsrvFileSrv(root *srvFile) *Fsrv {
srv := new(Fsrv)
srv.Root = root
root.Parent = root // make sure we can .. in root
return srv
}
// Initializes the fields of a file and add it to a directory.
// Returns nil if successful, or an error.
func (f *srvFile) Add(dir *srvFile, name string, uid User, gid Group, mode uint32, ops interface{}) error {
lock.Lock()
qpath := qnext
qnext++
lock.Unlock()
f.Qid.Type = uint8(mode >> 24)
f.Qid.Version = 0
f.Qid.Path = qpath
f.Mode = mode
f.Atime = uint32(time.Now().Unix())
f.Mtime = f.Atime
f.Length = 0
f.Name = name
if uid != nil {
f.Uid = uid.Name()
f.Uidnum = uint32(uid.Id())
} else {
f.Uid = "none"
f.Uidnum = NOUID
}
if gid != nil {
f.Gid = gid.Name()
f.Gidnum = uint32(gid.Id())
} else {
f.Gid = "none"
f.Gidnum = NOUID
}
f.Muid = ""
f.Muidnum = NOUID
f.Ext = ""
if dir != nil {
f.Parent = dir
dir.Lock()
for p := dir.cfirst; p != nil; p = p.next {
if name == p.Name {
dir.Unlock()
return Eexist
}
}
if dir.clast != nil {
dir.clast.next = f
} else {
dir.cfirst = f
}
f.prev = dir.clast
f.next = nil
dir.clast = f
dir.Unlock()
} else {
f.Parent = f
}
f.ops = ops
return nil
}
// Removes a file from its parent directory.
func (f *srvFile) Remove() {
f.Lock()
if (f.flags & Fremoved) != 0 {
f.Unlock()
return
}
f.flags |= Fremoved
f.Unlock()
p := f.Parent
p.Lock()
if f.next != nil {
f.next.prev = f.prev
} else {
p.clast = f.prev
}
if f.prev != nil {
f.prev.next = f.next
} else {
p.cfirst = f.next
}
f.next = nil
f.prev = nil
p.Unlock()
}
func (f *srvFile) Rename(name string) error {
p := f.Parent
p.Lock()
defer p.Unlock()
for c := p.cfirst; c != nil; c = c.next {
if name == c.Name {
return Eexist
}
}
f.Name = name
return nil
}
// Looks for a file in a directory. Returns nil if the file is not found.
func (p *srvFile) Find(name string) *srvFile {
var f *srvFile
p.Lock()
for f = p.cfirst; f != nil; f = f.next {
if name == f.Name {
break
}
}
p.Unlock()
return f
}
// Checks if the specified user has permission to perform
// certain operation on a file. Perm contains one or more
// of DMREAD, DMWRITE, and DMEXEC.
func (f *srvFile) CheckPerm(user User, perm uint32) bool {
if user == nil {
return false
}
perm &= 7
/* other permissions */
fperm := f.Mode & 7
if (fperm & perm) == perm {
return true
}
/* user permissions */
if f.Uid == user.Name() || f.Uidnum == uint32(user.Id()) {
fperm |= (f.Mode >> 6) & 7
}
if (fperm & perm) == perm {
return true
}
/* group permissions */
groups := user.Groups()
if groups != nil && len(groups) > 0 {
for i := 0; i < len(groups); i++ {
if f.Gid == groups[i].Name() || f.Gidnum == uint32(groups[i].Id()) {
fperm |= (f.Mode >> 3) & 7
break
}
}
}
if (fperm & perm) == perm {
return true
}
return false
}
func (s *Fsrv) Attach(req *SrvReq) {
fid := new(FFid)
fid.F = s.Root
fid.Fid = req.Fid
req.Fid.Aux = fid
req.RespondRattach(&s.Root.Qid)
}
func (*Fsrv) Walk(req *SrvReq) {
fid := req.Fid.Aux.(*FFid)
tc := req.Tc
if req.Newfid.Aux == nil {
nfid := new(FFid)
nfid.Fid = req.Newfid
req.Newfid.Aux = nfid
}
nfid := req.Newfid.Aux.(*FFid)
wqids := make([]Qid, len(tc.Wname))
i := 0
f := fid.F
for ; i < len(tc.Wname); i++ {
if tc.Wname[i] == ".." {
// handle dotdot
f = f.Parent
wqids[i] = f.Qid
continue
}
if (wqids[i].Type & QTDIR) > 0 {
if !f.CheckPerm(req.Fid.User, DMEXEC) {
break
}
}
p := f.Find(tc.Wname[i])
if p == nil {
break
}
f = p
wqids[i] = f.Qid
}
if len(tc.Wname) > 0 && i == 0 {
req.RespondError(Enoent)
return
}
nfid.F = f
req.RespondRwalk(wqids[0:i])
}
func mode2Perm(mode uint8) uint32 {
var perm uint32 = 0
switch mode & 3 {
case OREAD:
perm = DMREAD
case OWRITE:
perm = DMWRITE
case ORDWR:
perm = DMREAD | DMWRITE
}
if (mode & OTRUNC) != 0 {
perm |= DMWRITE
}
return perm
}
func (*Fsrv) Open(req *SrvReq) {
fid := req.Fid.Aux.(*FFid)
tc := req.Tc
if !fid.F.CheckPerm(req.Fid.User, mode2Perm(tc.Mode)) {
req.RespondError(Eperm)
return
}
if op, ok := (fid.F.ops).(FOpenOp); ok {
err := op.Open(fid, tc.Mode)
if err != nil {
req.RespondError(err)
return
}
}
req.RespondRopen(&fid.F.Qid, 0)
}
func (*Fsrv) Create(req *SrvReq) {
fid := req.Fid.Aux.(*FFid)
tc := req.Tc
dir := fid.F
if !dir.CheckPerm(req.Fid.User, DMWRITE) {
req.RespondError(Eperm)
return
}
if cop, ok := (dir.ops).(FCreateOp); ok {
f, err := cop.Create(fid, tc.Name, tc.Perm)
if err != nil {
req.RespondError(err)
} else {
fid.F = f
req.RespondRcreate(&fid.F.Qid, 0)
}
} else {
req.RespondError(Eperm)
}
}
func (*Fsrv) Read(req *SrvReq) {
var n int
var err error
fid := req.Fid.Aux.(*FFid)
f := fid.F
tc := req.Tc
rc := req.Rc
InitRread(rc, tc.Count)
if f.Mode&DMDIR != 0 {
// Get all the directory entries and
// serialize them all into an output buffer.
// This greatly simplifies the directory read.
if tc.Offset == 0 {
var g *srvFile
fid.dirents = nil
f.Lock()
for n, g = 0, f.cfirst; g != nil; n, g = n+1, g.next {
}
fid.dirs = make([]*srvFile, n)
for n, g = 0, f.cfirst; g != nil; n, g = n+1, g.next {
fid.dirs[n] = g
fid.dirents = append(fid.dirents,
PackDir(&g.Dir, req.Conn.Dotu)...)
}
f.Unlock()
}
switch {
case tc.Offset > uint64(len(fid.dirents)):
n = 0
case len(fid.dirents[tc.Offset:]) > int(tc.Size):
n = int(tc.Size)
default:
n = len(fid.dirents[tc.Offset:])
}
copy(rc.Data, fid.dirents[tc.Offset:int(tc.Offset)+1+n])
} else {
// file
if rop, ok := f.ops.(FReadOp); ok {
n, err = rop.Read(fid, rc.Data, tc.Offset)
if err != nil {
req.RespondError(err)
return
}
} else {
req.RespondError(Eperm)
return
}
}
SetRreadCount(rc, uint32(n))
req.Respond()
}
func (*Fsrv) Write(req *SrvReq) {
fid := req.Fid.Aux.(*FFid)
f := fid.F
tc := req.Tc
if wop, ok := (f.ops).(FWriteOp); ok {
n, err := wop.Write(fid, tc.Data, tc.Offset)
if err != nil {
req.RespondError(err)
} else {
req.RespondRwrite(uint32(n))
}
} else {
req.RespondError(Eperm)
}
}
func (*Fsrv) Clunk(req *SrvReq) {
fid := req.Fid.Aux.(*FFid)
if op, ok := (fid.F.ops).(FClunkOp); ok {
err := op.Clunk(fid)
if err != nil {
req.RespondError(err)
}
}
req.RespondRclunk()
}
func (*Fsrv) Remove(req *SrvReq) {
fid := req.Fid.Aux.(*FFid)
f := fid.F
f.Lock()
if f.cfirst != nil {
f.Unlock()
req.RespondError(Enotempty)
return
}
f.Unlock()
if rop, ok := (f.ops).(FRemoveOp); ok {
err := rop.Remove(fid)
if err != nil {
req.RespondError(err)
} else {
f.Remove()
req.RespondRremove()
}
} else {
log.Println("remove not implemented")
req.RespondError(Eperm)
}
}
func (*Fsrv) Stat(req *SrvReq) {
fid := req.Fid.Aux.(*FFid)
f := fid.F
if sop, ok := (f.ops).(FStatOp); ok {
err := sop.Stat(fid)
if err != nil {
req.RespondError(err)
} else {
req.RespondRstat(&f.Dir)
}
} else {
req.RespondRstat(&f.Dir)
}
}
func (*Fsrv) Wstat(req *SrvReq) {
tc := req.Tc
fid := req.Fid.Aux.(*FFid)
f := fid.F
if wop, ok := (f.ops).(FWstatOp); ok {
err := wop.Wstat(fid, &tc.Dir)
if err != nil {
req.RespondError(err)
} else {
req.RespondRwstat()
}
} else {
req.RespondError(Eperm)
}
}
func (*Fsrv) FidDestroy(ffid *SrvFid) {
if ffid.Aux == nil {
return
}
fid := ffid.Aux.(*FFid)
f := fid.F
if f == nil {
return // otherwise errs in bad walks
}
if op, ok := (f.ops).(FDestroyOp); ok {
op.FidDestroy(fid)
}
}