-
Notifications
You must be signed in to change notification settings - Fork 0
/
walkfs.go
285 lines (259 loc) · 7.29 KB
/
walkfs.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
package walkfs
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
// Namespace imports
. "github.com/djthorpe/go-errors"
"github.com/hashicorp/go-multierror"
// Packages
)
////////////////////////////////////////////////////////////////////////////////
// TYPES
type VisitFunc func(context.Context, string, string, fs.FileInfo) error
type WalkFS struct {
sync.Mutex
inext map[string]bool
exext map[string]bool
expath map[string]bool
exname map[string]bool
count int
visitfn VisitFunc
}
////////////////////////////////////////////////////////////////////////////////
// GLOBALS
const (
pathSeparator = string(os.PathSeparator)
)
////////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
// Create a new walkfs with a given visitor function, which is used for
// touching each visited file and folder
func New(fn VisitFunc) *WalkFS {
walkfs := new(WalkFS)
walkfs.inext = make(map[string]bool)
walkfs.exext = make(map[string]bool)
walkfs.expath = make(map[string]bool)
walkfs.exname = make(map[string]bool)
walkfs.visitfn = fn
return walkfs
}
////////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Count the number of files and folders visited
func (walkfs *WalkFS) Count() int {
return walkfs.count
}
// Include adds a file extension inclusion to the indexer.
// Path exclusions are case-sensitive, file extension exclusions are not.
// If no inclusions are added, all files are visited
func (walkfs *WalkFS) Include(ext string) error {
ext = strings.TrimSpace(ext)
ext = strings.ToUpper("." + strings.TrimPrefix(ext, "."))
if ext != "." {
walkfs.inext[ext] = true
} else {
return ErrBadParameter.Withf("invalid inclusion: %q", ext)
}
// Return success
return nil
}
// Exclude adds a path or file extension exclusion to the indexer.
// If it begins with a '.' then a file extension exlusion is added,
// If it begins with a '/' then a path extension exclusion is added.
// Path and name exclusions are case-sensitive, file extension exclusions are not.
func (walkfs *WalkFS) Exclude(v string) error {
v = strings.TrimSpace(v)
if strings.HasPrefix(v, ".") && v != "." {
v = strings.ToUpper(v)
walkfs.exext[v] = true
} else if strings.HasPrefix(v, pathSeparator) && v != pathSeparator {
v = pathSeparator + strings.Trim(v, pathSeparator)
walkfs.expath[v] = true
} else if !strings.Contains(v, pathSeparator) && v != "" {
walkfs.exname[v] = true
} else {
return ErrBadParameter.Withf("invalid exclusion: %q", v)
}
// Return success
return nil
}
// Walk will walk a file or folder and visit the function for each
func (walkfs *WalkFS) Walk(ctx context.Context, path string) error {
walkfs.Mutex.Lock()
defer walkfs.Mutex.Unlock()
walkfs.count = 0
if abspath, err := filepath.Abs(path); err != nil {
return ErrNotFound.With(path)
} else {
path = abspath
}
if stat, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
return ErrNotFound.With(path)
} else if err != nil {
return err
} else if stat.IsDir() {
if err := walkfs.walk(ctx, path); err != nil {
return err
}
} else if stat.Mode().IsRegular() {
if err := walkfs.visit(ctx, "", path, stat); err != nil {
return err
}
} else {
return ErrNotFound.With(path)
}
// Return success
return nil
}
// ShouldVisit returns true if a path or file should be visited based
// on exclusions or else returns false
func (walkfs *WalkFS) ShouldVisit(relpath string, info fs.FileInfo) bool {
if !walkfs.shouldVisit(info) {
return false
}
if walkfs.shouldExcludePath(relpath) {
return false
}
if info.Mode().IsRegular() && walkfs.shouldExcludeFile(info) {
return false
}
return true
}
////////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
func (walkfs *WalkFS) walk(ctx context.Context, abspath string) error {
// Walk filesystem
var result error
err := filepath.WalkDir(abspath, func(path string, file fs.DirEntry, err error) error {
// Bail out on context error
if ctx.Err() != nil {
return ctx.Err()
} else if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return err
}
// Ignore hidden files and folders
if strings.HasPrefix(file.Name(), ".") {
if file.IsDir() {
return filepath.SkipDir
}
return nil
}
// Process files which can be read
if relpath, err := filepath.Rel(abspath, path); err == nil {
if info, err := file.Info(); err == nil {
if err := walkfs.visit(ctx, abspath, relpath, info); err != nil {
if errors.Is(filepath.SkipDir, err) {
return filepath.SkipDir
} else {
result = multierror.Append(result, err)
}
}
return nil
}
}
// Return any context error
return ctx.Err()
})
// Return errors unless they are cancel/timeout
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return result
} else if err != nil {
return multierror.Append(result, err)
} else {
return nil
}
}
func (walkfs *WalkFS) visit(ctx context.Context, abspath, relpath string, info fs.FileInfo) error {
walkfs.count++
if !walkfs.ShouldVisit(relpath, info) {
if info.IsDir() {
return filepath.SkipDir
} else {
return nil
}
} else if walkfs.visitfn != nil {
return walkfs.visitfn(ctx, abspath, relpath, info)
} else {
return nil
}
}
// shouldVisit returns true if the given directory entry should be visited
func (walkfs *WalkFS) shouldVisit(info fs.FileInfo) bool {
// Include all files if no inclusions are specified
if len(walkfs.inext) == 0 {
return true
}
// Should visit all folders
if info.Mode().IsDir() {
return true
}
// Ignore anything which isn't a regular file
if !info.Mode().IsRegular() {
return false
}
ext := strings.ToUpper(filepath.Ext(info.Name()))
if _, exists := walkfs.inext[ext]; exists {
return true
} else {
return false
}
}
// shouldExcludePath returns true if the given relative path should be excluded
func (walkfs *WalkFS) shouldExcludePath(relpath string) bool {
// Exclude any paths which have a .<folder> as part of their path
if relpath != "." {
for _, path := range strings.Split(relpath, pathSeparator) {
if strings.HasPrefix(path, ".") {
return true
}
}
}
// Include all files if no inclusions are specified
if len(walkfs.expath) == 0 {
return false
}
// Exclude by path prefix
relpath = pathSeparator + strings.Trim(relpath, pathSeparator) + pathSeparator
for path := range walkfs.expath {
if strings.HasPrefix(relpath, path) {
return true
}
}
// Exclude by extension
if len(walkfs.exext) == 0 {
return false
}
ext := strings.ToUpper(filepath.Ext(relpath))
if _, exists := walkfs.exext[ext]; exists {
return true
} else {
return false
}
}
// shouldExcludeFile returns true if the given file should not be visited
// based on file extension
func (walkfs *WalkFS) shouldExcludeFile(info fs.FileInfo) bool {
// Ignore anything which isn't a regular file
if !info.Mode().IsRegular() {
return false
}
// Include all files if no inclusions are specified
if len(walkfs.exext) > 0 {
ext := strings.ToUpper(filepath.Ext(info.Name()))
if _, exists := walkfs.exext[ext]; exists {
return true
}
}
if len(walkfs.exname) > 0 {
if _, exists := walkfs.exname[info.Name()]; exists {
return true
}
}
// Return false - no exclusions
return false
}