Debian Codegraph @

tree_nonrecursive.go

raw symbols SCIP
1 // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
2 // Use of this source code is governed by the MIT license that can be
3 // found in the LICENSE file.
4
5 package notify
6
7 import "sync"
8
9 // nonrecursiveTree TODO(rjeczalik)
10 type nonrecursiveTree struct {
11 rw sync.RWMutex // protects root
12 root root
13 w watcher
14 c chan EventInfo
15 rec chan EventInfo
16 }
17
18 // newNonrecursiveTree TODO(rjeczalik)
19 func newNonrecursiveTree(w watcher, c, rec chan EventInfo) *nonrecursiveTree {
20 if rec == nil {
21 rec = make(chan EventInfo, buffer)
22 }
23 t := &nonrecursiveTree{
24 root: root{nd: newnode("")},
25 w: w,
26 c: c,
27 rec: rec,
28 }
29 go t.dispatch(c)
30 go t.internal(rec)
31 return t
32 }
33
34 // dispatch TODO(rjeczalik)
35 func (t *nonrecursiveTree) dispatch(c <-chan EventInfo) {
36 for ei := range c {
37 dbgprintf("dispatching %v on %q", ei.Event(), ei.Path())
38 go func(ei EventInfo) {
39 var nd node
40 var isrec bool
41 dir, base := split(ei.Path())
42 fn := func(it node, isbase bool) error {
43 isrec = isrec || it.Watch.IsRecursive()
44 if isbase {
45 nd = it
46 } else {
47 it.Watch.Dispatch(ei, recursive)
48 }
49 return nil
50 }
51 t.rw.RLock()
52 // Notify recursive watchpoints found on the path.
53 if err := t.root.WalkPath(dir, fn); err != nil {
54 dbgprint("dispatch did not reach leaf:", err)
55 t.rw.RUnlock()
56 return
57 }
58 // Notify parent watchpoint.
59 nd.Watch.Dispatch(ei, 0)
60 isrec = isrec || nd.Watch.IsRecursive()
61 // If leaf watchpoint exists, notify it.
62 if nd, ok := nd.Child[base]; ok {
63 isrec = isrec || nd.Watch.IsRecursive()
64 nd.Watch.Dispatch(ei, 0)
65 }
66 t.rw.RUnlock()
67 // If the event describes newly leaf directory created within
68 if !isrec || ei.Event()&(Create|Remove) == 0 {
69 return
70 }
71 if ok, err := ei.(isDirer).isDir(); !ok || err != nil {
72 return
73 }
74 t.rec <- ei
75 }(ei)
76 }
77 }
78
79 // internal TODO(rjeczalik)
80 func (t *nonrecursiveTree) internal(rec <-chan EventInfo) {
81 for ei := range rec {
82 t.rw.Lock()
83 if ei.Event() == Remove {
84 nd, err := t.root.Get(ei.Path())
85 if err != nil {
86 t.rw.Unlock()
87 continue
88 }
89 t.walkWatchpoint(nd, func(_ Event, nd node) error {
90 t.w.Unwatch(nd.Name)
91 return nil
92 })
93 t.root.Del(ei.Path())
94 t.rw.Unlock()
95 continue
96 }
97 var nd node
98 var eset = internal
99 t.root.WalkPath(ei.Path(), func(it node, _ bool) error {
100 if e := it.Watch[t.rec]; e != 0 && e > eset {
101 eset = e
102 }
103 nd = it
104 return nil
105 })
106 if eset == internal {
107 t.rw.Unlock()
108 continue
109 }
110 if ei.Path() != nd.Name {
111 nd = nd.Add(ei.Path())
112 }
113 err := nd.AddDir(t.recFunc(eset), nil)
114 t.rw.Unlock()
115 if err != nil {
116 dbgprintf("internal(%p) error: %v", rec, err)
117 }
118 }
119 }
120
121 // watchAdd TODO(rjeczalik)
122 func (t *nonrecursiveTree) watchAdd(nd node, c chan<- EventInfo, e Event) eventDiff {
123 if e&recursive != 0 {
124 diff := nd.Watch.Add(t.rec, e|Create|omit)
125 nd.Watch.Add(c, e)
126 return diff
127 }
128 return nd.Watch.Add(c, e)
129 }
130
131 // watchDelMin TODO(rjeczalik)
132 func (t *nonrecursiveTree) watchDelMin(min Event, nd node, c chan<- EventInfo, e Event) eventDiff {
133 old, ok := nd.Watch[t.rec]
134 if ok {
135 nd.Watch[t.rec] = min
136 }
137 diff := nd.Watch.Del(c, e)
138 if ok {
139 switch old &^= diff[0] &^ diff[1]; {
140 case old|internal == internal:
141 delete(nd.Watch, t.rec)
142 if set, ok := nd.Watch[nil]; ok && len(nd.Watch) == 1 && set == 0 {
143 delete(nd.Watch, nil)
144 }
145 default:
146 nd.Watch.Add(t.rec, old|Create)
147 switch {
148 case diff == none:
149 case diff[1]|Create == diff[0]:
150 diff = none
151 default:
152 diff[1] |= Create
153 }
154 }
155 }
156 return diff
157 }
158
159 // watchDel TODO(rjeczalik)
160 func (t *nonrecursiveTree) watchDel(nd node, c chan<- EventInfo, e Event) eventDiff {
161 return t.watchDelMin(0, nd, c, e)
162 }
163
164 // Watch TODO(rjeczalik)
165 func (t *nonrecursiveTree) Watch(path string, c chan<- EventInfo,
166 doNotWatch DoNotWatchFn, events ...Event) error {
167 if c == nil {
168 panic("notify: Watch using nil channel")
169 }
170 // Expanding with empty event set is a nop.
171 if len(events) == 0 {
172 return nil
173 }
174 path, isrec, err := cleanpath(path)
175 if err != nil {
176 return err
177 }
178 eset := joinevents(events)
179 t.rw.Lock()
180 defer t.rw.Unlock()
181 nd := t.root.Add(path)
182 if isrec {
183 return t.watchrec(nd, c, eset|recursive, doNotWatch)
184 }
185 return t.watch(nd, c, eset)
186 }
187
188 func (t *nonrecursiveTree) watch(nd node, c chan<- EventInfo, e Event) (err error) {
189 diff := nd.Watch.Add(c, e)
190 switch {
191 case diff == none:
192 return nil
193 case diff[1] == 0:
194 // TODO(rjeczalik): cleanup this panic after implementation is stable
195 panic("eset is empty: " + nd.Name)
196 case diff[0] == 0:
197 err = t.w.Watch(nd.Name, diff[1])
198 default:
199 err = t.w.Rewatch(nd.Name, diff[0], diff[1])
200 }
201 if err != nil {
202 nd.Watch.Del(c, diff.Event())
203 return err
204 }
205 return nil
206 }
207
208 func (t *nonrecursiveTree) recFunc(e Event) walkFunc {
209 return func(nd node) (err error) {
210 switch diff := nd.Watch.Add(t.rec, e|omit|Create); {
211 case diff == none:
212 case diff[1] == 0:
213 // TODO(rjeczalik): cleanup this panic after implementation is stable
214 panic("eset is empty: " + nd.Name)
215 case diff[0] == 0:
216 err = t.w.Watch(nd.Name, diff[1])
217 default:
218 err = t.w.Rewatch(nd.Name, diff[0], diff[1])
219 }
220 return
221 }
222 }
223
224 func (t *nonrecursiveTree) watchrec(nd node, c chan<- EventInfo, e Event,
225 doNotWatch DoNotWatchFn) error {
226 var traverse func(walkFunc, DoNotWatchFn) error
227 // Non-recursive tree listens on Create event for every recursive
228 // watchpoint in order to automagically set a watch for every
229 // created directory.
230 switch diff := nd.Watch.dryAdd(t.rec, e|Create); {
231 case diff == none:
232 t.watchAdd(nd, c, e)
233 nd.Watch.Add(t.rec, e|omit|Create)
234 return nil
235 case diff[1] == 0:
236 // TODO(rjeczalik): cleanup this panic after implementation is stable
237 panic("eset is empty: " + nd.Name)
238 case diff[0] == 0:
239 // TODO(rjeczalik): BFS into directories and skip subtree as soon as first
240 // recursive watchpoint is encountered.
241 traverse = nd.AddDir
242 default:
243 traverse = nd.Walk
244 }
245 // TODO(rjeczalik): account every path that failed to be (re)watched
246 // and retry.
247 if err := traverse(t.recFunc(e), doNotWatch); err != nil {
248 return err
249 }
250 t.watchAdd(nd, c, e)
251 return nil
252 }
253
254 type walkWatchpointFunc func(Event, node) error
255
256 func (t *nonrecursiveTree) walkWatchpoint(nd node, fn walkWatchpointFunc) error {
257 type minode struct {
258 min Event
259 nd node
260 }
261 mnd := minode{nd: nd}
262 stack := []minode{mnd}
263 Traverse:
264 for n := len(stack); n != 0; n = len(stack) {
265 mnd, stack = stack[n-1], stack[:n-1]
266 // There must be no recursive watchpoints if the node has no watchpoints
267 // itself (every node in subtree rooted at recursive watchpoints must
268 // have at least nil (total) and t.rec watchpoints).
269 if len(mnd.nd.Watch) != 0 {
270 switch err := fn(mnd.min, mnd.nd); err {
271 case nil:
272 case errSkip:
273 continue Traverse
274 default:
275 return err
276 }
277 }
278 for _, nd := range mnd.nd.Child {
279 stack = append(stack, minode{mnd.nd.Watch[t.rec], nd})
280 }
281 }
282 return nil
283 }
284
285 // Stop TODO(rjeczalik)
286 func (t *nonrecursiveTree) Stop(c chan<- EventInfo) {
287 fn := func(min Event, nd node) error {
288 // TODO(rjeczalik): aggregate watcher errors and retry; in worst case
289 // forward to the user.
290 switch diff := t.watchDelMin(min, nd, c, all); {
291 case diff == none:
292 return nil
293 case diff[1] == 0:
294 t.w.Unwatch(nd.Name)
295 default:
296 t.w.Rewatch(nd.Name, diff[0], diff[1])
297 }
298 return nil
299 }
300 t.rw.Lock()
301 err := t.walkWatchpoint(t.root.nd, fn) // TODO(rjeczalik): store max root per c
302 t.rw.Unlock()
303 dbgprintf("Stop(%p) error: %v\n", c, err)
304 }
305
306 // Close TODO(rjeczalik)
307 func (t *nonrecursiveTree) Close() error {
308 err := t.w.Close()
309 close(t.c)
310 return err
311 }