-
Notifications
You must be signed in to change notification settings - Fork 458
/
walker.go
207 lines (170 loc) · 4.4 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
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package archiver
import (
"context"
"os"
"path/filepath"
"strings"
)
// FileItem is unit of work for archive.
type FileItem struct {
FullPath string
RelPath string
FileInfo os.FileInfo
Link string
Error error
}
// FileType is a file type.
type FileType int
// File types.
const (
RegularFileType FileType = iota
DirectoryFileType
SymlinkFileType
)
type walkerOptions struct {
skipRoot bool
maxRecurseDepth int
fnmatchPatterns []string
types map[FileType]struct{}
}
// WalkerOption configures Walker.
type WalkerOption func(*walkerOptions)
// WithSkipRoot skips root path if it's a directory.
func WithSkipRoot() WalkerOption {
return func(o *walkerOptions) {
o.skipRoot = true
}
}
// WithMaxRecurseDepth controls maximum recursion depth while walking file tree.
//
// Value of -1 disables depth control.
func WithMaxRecurseDepth(maxDepth int) WalkerOption {
return func(o *walkerOptions) {
o.maxRecurseDepth = maxDepth
}
}
// WithFnmatchPatterns filters results to match the patterns.
//
// Default is not to do any filtering.
func WithFnmatchPatterns(patterns ...string) WalkerOption {
return func(o *walkerOptions) {
o.fnmatchPatterns = append(o.fnmatchPatterns, patterns...)
}
}
// WithFileTypes filters results by file types.
//
// Default is not to do any filtering.
func WithFileTypes(fileType ...FileType) WalkerOption {
return func(o *walkerOptions) {
o.types = make(map[FileType]struct{}, len(fileType))
for _, t := range fileType {
o.types[t] = struct{}{}
}
}
}
// Walker provides a channel of file info/paths for archival.
//
//nolint:gocyclo,cyclop
func Walker(ctx context.Context, rootPath string, options ...WalkerOption) (<-chan FileItem, error) {
var opts walkerOptions
opts.maxRecurseDepth = -1
for _, o := range options {
o(&opts)
}
info, err := os.Lstat(rootPath)
if err != nil {
return nil, err
}
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
rootPath, err = filepath.EvalSymlinks(rootPath)
if err != nil {
return nil, err
}
}
ch := make(chan FileItem)
go func() {
defer close(ch)
err := filepath.Walk(rootPath, func(path string, fileInfo os.FileInfo, walkErr error) error {
item := FileItem{
FullPath: path,
FileInfo: fileInfo,
Error: walkErr,
}
if path == rootPath && !fileInfo.IsDir() {
// only one file
item.RelPath = filepath.Base(path)
} else if item.Error == nil {
item.RelPath, item.Error = filepath.Rel(rootPath, path)
}
// TODO: refactor all those `if item.Error == nil &&` conditions
if item.Error == nil && len(opts.types) > 0 {
var matches bool
for t := range opts.types {
switch t {
case RegularFileType:
matches = fileInfo.Mode()&os.ModeType == 0
case DirectoryFileType:
matches = fileInfo.Mode()&os.ModeDir != 0
case SymlinkFileType:
matches = fileInfo.Mode()&os.ModeSymlink != 0
}
if matches {
break
}
}
if !matches {
return nil
}
}
if item.Error == nil && path == rootPath && opts.skipRoot && fileInfo.IsDir() {
// skip containing directory
return nil
}
if item.Error == nil && fileInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
item.Link, item.Error = os.Readlink(path)
}
if item.Error == nil && len(opts.fnmatchPatterns) > 0 {
var matches bool
for _, pattern := range opts.fnmatchPatterns {
if matches, _ = filepath.Match(pattern, item.RelPath); matches { //nolint:errcheck
break
}
}
if !matches {
return nil
}
}
select {
case <-ctx.Done():
return ctx.Err()
case ch <- item:
}
if item.Error == nil && fileInfo.IsDir() && atMaxDepth(opts.maxRecurseDepth, rootPath, path) {
return filepath.SkipDir
}
return nil
})
if err != nil {
select {
case <-ctx.Done():
case ch <- FileItem{Error: err}:
}
}
}()
return ch, nil
}
// OSPathSeparator is the string version of the os.PathSeparator.
const OSPathSeparator = string(os.PathSeparator)
func atMaxDepth(max int, root, cur string) bool {
if max < 0 {
return false
}
if root == cur {
// always recurse the root directory
return false
}
return (strings.Count(cur, OSPathSeparator) - strings.Count(root, OSPathSeparator)) >= max
}