-
Notifications
You must be signed in to change notification settings - Fork 129
/
node.go
275 lines (251 loc) · 5.35 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
// Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
package notify
import (
"errors"
"io/fs"
"os"
"path/filepath"
)
var errSkip = errors.New("notify: skip")
type walkPathFunc func(nd node, isbase bool) error
type walkFunc func(node) error
func errnotexist(name string) error {
return &os.PathError{
Op: "Node",
Path: name,
Err: os.ErrNotExist,
}
}
type node struct {
Name string
Watch watchpoint
Child map[string]node
}
func newnode(name string) node {
return node{
Name: name,
Watch: make(watchpoint),
Child: make(map[string]node),
}
}
func (nd node) addchild(name, base string) node {
child, ok := nd.Child[base]
if !ok {
child = newnode(name)
nd.Child[base] = child
}
return child
}
func (nd node) Add(name string) node {
i := indexrel(nd.Name, name)
if i == -1 {
return node{}
}
for j := indexSep(name[i:]); j != -1; j = indexSep(name[i:]) {
nd = nd.addchild(name[:i+j], name[i:i+j])
i += j + 1
}
return nd.addchild(name, name[i:])
}
func (nd node) AddDir(fn walkFunc) error {
stack := []node{nd}
Traverse:
for n := len(stack); n != 0; n = len(stack) {
nd, stack = stack[n-1], stack[:n-1]
switch err := fn(nd); err {
case nil:
case errSkip:
continue Traverse
default:
return &os.PathError{
Op: "error while traversing",
Path: nd.Name,
Err: err,
}
}
// TODO(rjeczalik): tolerate open failures - add failed names to
// AddDirError and notify users which names are not added to the tree.
fi, err := os.ReadDir(nd.Name)
if err != nil {
return err
}
for _, fi := range fi {
if fi.Type()&(fs.ModeSymlink|fs.ModeDir) == fs.ModeDir {
name := filepath.Join(nd.Name, fi.Name())
stack = append(stack, nd.addchild(name, name[len(nd.Name)+1:]))
}
}
}
return nil
}
func (nd node) Get(name string) (node, error) {
i := indexrel(nd.Name, name)
if i == -1 {
return node{}, errnotexist(name)
}
ok := false
for j := indexSep(name[i:]); j != -1; j = indexSep(name[i:]) {
if nd, ok = nd.Child[name[i:i+j]]; !ok {
return node{}, errnotexist(name)
}
i += j + 1
}
if nd, ok = nd.Child[name[i:]]; !ok {
return node{}, errnotexist(name)
}
return nd, nil
}
func (nd node) Del(name string) error {
i := indexrel(nd.Name, name)
if i == -1 {
return errnotexist(name)
}
stack := []node{nd}
ok := false
for j := indexSep(name[i:]); j != -1; j = indexSep(name[i:]) {
if nd, ok = nd.Child[name[i:i+j]]; !ok {
return errnotexist(name[:i+j])
}
stack = append(stack, nd)
i += j + 1
}
if _, ok = nd.Child[name[i:]]; !ok {
return errnotexist(name)
}
delete(nd.Child, name[i:])
for name, i = name[i:], len(stack); i != 0; name, i = base(nd.Name), i-1 {
nd = stack[i-1]
if nd := nd.Child[name]; len(nd.Watch) > 1 || len(nd.Child) != 0 {
break
} else {
nd.Child = nil
nd.Watch = nil
}
delete(nd.Child, name)
}
return nil
}
func (nd node) Walk(fn walkFunc) error {
stack := []node{nd}
Traverse:
for n := len(stack); n != 0; n = len(stack) {
nd, stack = stack[n-1], stack[:n-1]
switch err := fn(nd); err {
case nil:
case errSkip:
continue Traverse
default:
return err
}
for name, nd := range nd.Child {
if name == "" {
// Node storing inactive watchpoints has empty name, skip it
// form traversing. Root node has also an empty name, but it
// never has a parent node.
continue
}
stack = append(stack, nd)
}
}
return nil
}
func (nd node) WalkPath(name string, fn walkPathFunc) error {
i := indexrel(nd.Name, name)
if i == -1 {
return errnotexist(name)
}
ok := false
for j := indexSep(name[i:]); j != -1; j = indexSep(name[i:]) {
switch err := fn(nd, false); err {
case nil:
case errSkip:
return nil
default:
return err
}
if nd, ok = nd.Child[name[i:i+j]]; !ok {
return errnotexist(name[:i+j])
}
i += j + 1
}
switch err := fn(nd, false); err {
case nil:
case errSkip:
return nil
default:
return err
}
if nd, ok = nd.Child[name[i:]]; !ok {
return errnotexist(name)
}
switch err := fn(nd, true); err {
case nil, errSkip:
return nil
default:
return err
}
}
type root struct {
nd node
}
func (r root) addroot(name string) node {
if vol := filepath.VolumeName(name); vol != "" {
root, ok := r.nd.Child[vol]
if !ok {
root = r.nd.addchild(vol, vol)
}
return root
}
return r.nd
}
func (r root) root(name string) (node, error) {
if vol := filepath.VolumeName(name); vol != "" {
nd, ok := r.nd.Child[vol]
if !ok {
return node{}, errnotexist(name)
}
return nd, nil
}
return r.nd, nil
}
func (r root) Add(name string) node {
return r.addroot(name).Add(name)
}
func (r root) AddDir(dir string, fn walkFunc) error {
return r.Add(dir).AddDir(fn)
}
func (r root) Del(name string) error {
nd, err := r.root(name)
if err != nil {
return err
}
return nd.Del(name)
}
func (r root) Get(name string) (node, error) {
nd, err := r.root(name)
if err != nil {
return node{}, err
}
if nd.Name != name {
if nd, err = nd.Get(name); err != nil {
return node{}, err
}
}
return nd, nil
}
func (r root) Walk(name string, fn walkFunc) error {
nd, err := r.Get(name)
if err != nil {
return err
}
return nd.Walk(fn)
}
func (r root) WalkPath(name string, fn walkPathFunc) error {
nd, err := r.root(name)
if err != nil {
return err
}
return nd.WalkPath(name, fn)
}