-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdir.go
148 lines (126 loc) · 3.15 KB
/
dir.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
// Copyright ©2016 The ev3go 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 sisyphus
import (
"context"
"os"
"path/filepath"
"strings"
"sync"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
)
// Dir is a directory node.
type Dir struct {
mu sync.Mutex
name string
attr
files map[string]Node
fs *FileSystem
}
var (
_ Node = (*Dir)(nil)
_ fs.Node = (*Dir)(nil)
_ fs.HandleReadDirAller = (*Dir)(nil)
_ fs.NodeStringLookuper = (*Dir)(nil)
)
// NewDir returns a new Dir with the given name and file mode.
func NewDir(name string, mode os.FileMode) (*Dir, error) {
if name != "/" && strings.Contains(name, string(filepath.Separator)) {
return nil, ErrBadName
}
return &Dir{
name: name,
attr: attr{
mode: os.ModeDir | mode&^(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket),
},
files: make(map[string]Node),
}, nil
}
// MustNewDir returns a new Dir with the given name and file mode. It
// will panic if name contains a filepath separator unless name is "/".
func MustNewDir(name string, mode os.FileMode) *Dir {
d, err := NewDir(name, mode)
if err != nil {
panic(err)
}
return d
}
// Own sets the uid and gid of the directory.
func (d *Dir) Own(uid, gid uint32) *Dir {
d.uid = uid
d.gid = gid
d.mtime = d.fs.now()
return d
}
// With adds nodes to the dirctory. If with is used the FileSystem Sync method
// should be called when all nodes have been added.
func (d *Dir) With(nodes ...Node) Node {
for _, n := range nodes {
d.files[n.Name()] = n
}
return d
}
// Name returns the name of the directory.
func (d *Dir) Name() string { return d.name }
// SetSys sets the directory's containing file system.
func (d *Dir) SetSys(filesys *FileSystem) {
d.mu.Lock()
d.fs = filesys
var now time.Time
if filesys != nil {
now = filesys.now()
}
d.ctime = now
d.atime = now
d.mtime = now
d.mu.Unlock()
}
// Sys returns the directory's containing filesystem.
func (d *Dir) Sys() *FileSystem {
d.mu.Lock()
defer d.mu.Unlock()
return d.fs
}
// Invalidate invalidates the kernel cache of the directory.
func (d *Dir) Invalidate() error {
d.mu.Lock()
defer d.mu.Unlock()
return d.fs.Invalidate(d)
}
// Attr satisfies the bazil.org/fuse/fs.Node interface.
func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
d.mu.Lock()
defer d.mu.Unlock()
copyAttr(a, d.attr)
return nil
}
// ReadDirAll satisfies the bazil.org/fuse/HandleReadDirAller.Node interface.
func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
d.mu.Lock()
defer d.mu.Unlock()
files := make([]fuse.Dirent, 0, len(d.files))
var attr fuse.Attr
for name, f := range d.files {
err := f.Attr(ctx, &attr)
if err != nil {
return files, err
}
files = append(files, fuse.Dirent{Inode: attr.Inode, Name: name})
}
d.atime = d.fs.now()
return files, nil
}
// Lookup satisfies the bazil.org/fuse/NodeStringLookuper.Node interface.
func (d *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
d.mu.Lock()
defer d.mu.Unlock()
n, ok := d.files[name]
d.atime = d.fs.now()
if !ok {
return nil, fuse.ENOENT
}
return n, nil
}