-
Notifications
You must be signed in to change notification settings - Fork 2
/
node.go
353 lines (303 loc) · 6.85 KB
/
node.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
package ramfs
import (
"sync"
"time"
"9fans.net/go/plan9"
)
var errPerm = perror("permission denied")
type node struct {
mu sync.RWMutex
fs *FS
file buffer
dir *plan9.Dir
parent *node
children map[string]*node
open bool // used for OEXCL
orclose bool
}
func newNode(fs *FS, name, uid, gid string, perm plan9.Perm, path uint64, b buffer) *node {
now := uint32(time.Now().Unix())
n := &node{
fs: fs,
dir: &plan9.Dir{
Qid: plan9.Qid{
Type: uint8(perm >> 24),
Vers: uint32(0),
Path: path,
},
Mode: perm, // permission
Atime: now, // last read time
Mtime: now, // last write time
Length: uint64(0), // node length
Name: name, // last element of path
Uid: uid, // owner name
Gid: gid, // group name
Muid: uid, // last modifier name
},
}
if perm&plan9.DMDIR != 0 {
n.children = make(map[string]*node)
} else {
n.file = b
}
return n
}
func (n *node) Create(uid, name string, mode uint8, perm plan9.Perm) (*node, error) {
if name == "." || name == ".." {
return nil, perror("illegal name")
}
if perm&plan9.DMDIR != 0 {
perm = (perm &^ 0777) | (n.dir.Mode & 0777)
} else {
perm = (perm &^ 0666) | (n.dir.Mode & 0666)
}
n.mu.Lock()
if n.dir.Mode&plan9.DMDIR == 0 {
n.mu.Unlock()
return nil, perror("not a directory")
}
if n.dir.Mode&plan9.DMEXCL != 0 && n.open {
n.mu.Unlock()
return nil, perror("exclusive use file already open")
}
path, err := n.fs.newPath()
if err != nil {
n.mu.Unlock()
return nil, err
}
node := newNode(n.fs, name, uid, n.dir.Gid, perm, path, newFile(BLOCKSIZE))
node.parent = n
if f, found := n.children[name]; found {
n.mu.Unlock()
if err := f.Open(mode); err != nil {
return nil, err
}
return f, nil
}
n.children[name] = node
n.mu.Unlock()
return node, nil
}
func (n *node) Open(mode uint8) error {
n.mu.Lock()
defer n.mu.Unlock()
if n.dir.Mode&plan9.DMEXCL != 0 && n.open {
return perror("exclusive use file already open")
}
if n.dir.Mode&plan9.DMEXCL != 0 && !n.open {
n.open = true
}
if mode&plan9.ORCLOSE != 0 {
n.orclose = true
}
return nil
}
func (n *node) Close() error {
n.mu.Lock()
defer n.mu.Unlock()
if n.dir.Mode&plan9.DMEXCL != 0 && n.open {
n.open = false
}
if n.dir.Mode&plan9.DMDIR == 0 {
n.file.Close()
}
if n.orclose {
return n.remove()
}
return nil
}
func (n *node) remove() error {
if n.dir.Mode&plan9.DMDIR != 0 && len(n.children) != 0 {
return perror("directory not empty")
}
parent := n.parent
parent.mu.Lock()
name := n.dir.Name
if _, found := parent.children[name]; !found {
parent.mu.Unlock()
return perror("file does not exist")
}
delete(parent.children, name)
parent.mu.Unlock()
n.fs.delPath(n.dir.Qid.Path)
return nil
}
func (n *node) Remove() error {
n.mu.Lock()
defer n.mu.Unlock()
return n.remove()
}
func (n *node) WriteAt(p []byte, offset int64) (int, error) {
n.mu.Lock()
defer n.mu.Unlock()
if n.dir.Mode&plan9.DMDIR != 0 {
return 0, perror("is a directory")
}
if n.dir.Mode&plan9.DMAPPEND != 0 {
n := n.file.Len()
if n > uint64(1<<63-1) { // TODO
return 0, perror("offset overflow")
}
offset = int64(n)
}
m, err := n.file.WriteAt(p, offset)
if err != nil {
return 0, err
}
now := uint32(time.Now().Unix())
n.dir.Atime = now
n.dir.Mtime = now
n.dir.Length = n.file.Len()
if n.dir.Mode&plan9.DMTMP == 0 {
n.dir.Qid.Vers++
}
return m, nil
}
func (n *node) ReadAt(p []byte, offset int64) (int, error) {
n.mu.Lock()
defer n.mu.Unlock()
if n.dir.Mode&plan9.DMDIR != 0 {
return 0, perror("is a directory")
}
m, err := n.file.ReadAt(p, offset)
if err != nil {
return 0, err
}
n.dir.Atime = uint32(time.Now().Unix())
return m, nil
}
func (n *node) Readdir() ([]byte, error) {
n.mu.RLock()
defer n.mu.RUnlock()
if n.dir.Mode&plan9.DMDIR == 0 {
return nil, perror("not a directory")
}
var data []byte
for _, f := range n.children {
buf, err := f.dir.Bytes()
if err != nil {
return nil, err
}
data = append(data, buf...)
}
return data, nil
}
func (n *node) Stat() *plan9.Dir {
n.mu.RLock()
defer n.mu.RUnlock()
return n.dir
}
func (n *node) Wstat(uname string, dir *plan9.Dir) error {
// To change mode, must be owner or group leader. Because of lack of
// group file, leader=>group itself.
if dir.Mode != 0xFFFFFFFF && dir.Mode != n.dir.Mode {
if uname != n.dir.Uid && uname != n.dir.Gid {
return perror("not owner")
}
}
// To change name, must have write permission in parent and name must
// be unique.
parent := n.parent
if dir.Name != "" && dir.Name != n.dir.Name {
if !parent.HasPerm(uname, plan9.DMWRITE) {
return errPerm
}
parent.mu.Lock()
if _, found := parent.children[dir.Name]; found {
parent.mu.Unlock()
return perror("file exists")
}
parent.mu.Unlock()
}
// To change group, must be owner and member of new group
if dir.Gid != "" && dir.Gid != n.dir.Gid {
fgroup, err := n.fs.group.Get(n.dir.Gid)
if err != nil {
panic(err) // can't happen
}
if _, found := fgroup.Member[uname]; !found {
return perror("not owner")
}
}
// all ok; do it
if dir.Mode != 0xFFFFFFFF && dir.Mode != n.dir.Mode {
if dir.Mode&plan9.DMDIR != 0 {
n.dir.Mode = (dir.Mode &^ 0777) | (n.dir.Mode & 0777)
} else {
n.dir.Mode = (dir.Mode &^ 0666) | (n.dir.Mode & 0666)
}
}
if dir.Name != "" && dir.Name != n.dir.Name {
parent.mu.Lock()
delete(parent.children, n.dir.Name)
n.mu.Lock()
n.dir.Name = dir.Name
n.mu.Unlock()
parent.children[dir.Name] = n
parent.mu.Unlock()
}
if dir.Gid != "" && dir.Gid != n.dir.Gid {
n.mu.Lock()
n.dir.Gid = dir.Gid
n.mu.Unlock()
}
return nil
}
func (n *node) HasPerm(uname string, perm plan9.Perm) bool {
other := plan9.Perm(7)
perm &= other
// other
fperm := n.dir.Mode & other
if uname == "none" && (fperm&perm) == perm {
return true
}
if _, err := n.fs.group.Get(uname); err == nil {
// user
if n.dir.Uid == uname {
user := plan9.Perm(6)
fperm |= (n.dir.Mode >> user) & other
}
if (fperm & perm) == perm {
return true
}
// group
fgroup, err := n.fs.group.Get(n.dir.Gid)
if err != nil {
panic(err) // can't happen
}
if _, found := fgroup.Member[uname]; found {
group := plan9.Perm(3)
fperm |= (n.dir.Mode >> group) & other
}
return (fperm & perm) == perm
}
return false
}
type walkFunc func(root *node, path []string) error
func walk(root *node, path []string, fn walkFunc) error {
if len(path) == 0 {
return nil
}
node := root
name, path := path[0], path[1:]
if name == ".." {
node = node.parent
} else {
n, found := node.children[name]
if found {
node = n
} else {
return perror("file does not exist")
}
}
stat := node.Stat()
if (stat.Type & plan9.QTDIR) > 0 {
if (stat.Mode & plan9.DMEXEC) > 0 {
return errPerm
}
}
if err := fn(node, path); err != nil {
return err
}
return walk(node, path, fn)
}