-
Notifications
You must be signed in to change notification settings - Fork 0
/
walker.go
115 lines (92 loc) · 2.54 KB
/
walker.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
package walker
import (
"context"
"io/fs"
"os"
"sync"
)
// Info represents and entry located by fs.WalkDir
// Path is always relative to base path supplied to Walker
// for example if Walk is called with path == "/tmp"
// the returned entries will not contain "/tmp" in Path field.
type Info struct {
Path string
DirEntry fs.DirEntry
}
// Walker crawls the specified path using fs.WalkDir and sends all encountered
// items as Info to the channel returned by Walk method.
type Walker interface {
// Walk runs fs.WalkDir under the hood and returns chan of Info.
// All items found by fs.WalkDir are returned as is. Basically
// Walk is a custom fs.WalkDirFunc and a wrapper around WalkDir intended
// to send result to a channel. It also computes lazily waiting for reader
// to pull results from the channel as opposed to fs.WalkDir (fire and forget,
// then wait for all recursive calls to finish).
Walk(ctx context.Context, path string, funcs ...FilterFunc) <-chan Info
// Err explains why chan returned by Walk was closed. If context was
// cancelled or context deadline exceeded or whatewer error the
// WalkDirFunc function encountered it is returned by this method.
// Err will return nil if chan Info has not yet been closed.
Err() error
}
func New() Walker {
return newWalker()
}
func (w *walker) Err() error {
return walkerErr(w)
}
func (w *walker) Walk(ctx context.Context, path string, funcs ...FilterFunc) (<-chan Info) {
return walkerWalk(ctx, w, path, funcs...)
}
// here comes the implementation
type walker struct {
err error
mu sync.Mutex
}
func newWalker() Walker {
return &walker{}
}
func walkerWalk(ctx context.Context, w *walker, path string, funcs ...FilterFunc) (<-chan Info) {
out := make(chan Info)
go func(c chan Info) {
defer close(c)
err := fs.WalkDir(os.DirFS(path), ".", newWalkDirFunc(ctx, c))
w.mu.Lock()
w.err = err
w.mu.Unlock()
}(out)
if len(funcs) > 0 {
out = filter(out, funcs...)
}
return out
}
func walkerErr(w *walker) error {
var err error
w.mu.Lock()
err = w.err
w.mu.Unlock()
return err
}
func newWalkDirFunc(ctx context.Context, infoCh chan Info) fs.WalkDirFunc {
return func(p string, d fs.DirEntry, err error) error {
if err != nil {
// if stat/io error occurs then
// fs.WalkDir calls WalkDirFunc with
// this stat error
// nothing to do here
// but return, since d is nil in this case.
return err
}
info := Info {
Path: p,
DirEntry: d,
}
select {
case <- ctx.Done():
err = ctx.Err()
case infoCh <- info:
err = nil
}
return err
}
}