-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalk.go
244 lines (213 loc) · 6.25 KB
/
walk.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
package main
import (
"io/fs"
"iter"
"os"
"path/filepath"
)
// DirIter efficiently scans a filesystems directory
// file tree. The BatchSize is used to limit how
// many directory entries (files and sub-directories)
// are read at once, allowing efficiency on very
// flat, broad directories with large numbers of files
// (like S3).
//
// It uses the new Go iter approach to provide iter.Pull2
// usable iterators.
//
// MaxDepth and FollowSymlinks are available as options --
// they apply only to the FilesOnly iteration.
type DirIter struct {
// BatchSize is how many directory entries we read at once,
// to keep memory use low.
BatchSize int
// FollowSymlinks can result in results that must
// be de-duplicated if multiple symlink paths give
// the same file.
FollowSymlinks bool
// MaxDepth restricts how deeply we walk into the
// filesystem tree. Resolving a symlink only counts
// as one depth level, even if it involved
// chasing multiple symlinks to their target.
MaxDepth int
}
// NewDirIter creates a new DirIter.
func NewDirIter() *DirIter {
return &DirIter{
BatchSize: 100,
}
}
// DirsDepthFirstLeafOnly walks the filesystem from root.
// It returns only the deepest (leaf) directories.
// These suffice to re-create the directory structure.
func (di *DirIter) DirsDepthFirstLeafOnly(root string) iter.Seq2[string, bool] {
return func(yield func(string, bool) bool) {
// Helper function for recursive traversal
var visit func(path string) bool
visit = func(path string) bool {
dir, err := os.Open(path)
if err != nil {
return yield(path, false)
}
defer dir.Close()
hasSubdirs := false
for {
entries, err := dir.ReadDir(di.BatchSize)
// Process entries in directory order
for _, entry := range entries {
if entry.IsDir() {
hasSubdirs = true
// Recurse immediately when we find a directory
if !visit(filepath.Join(path, entry.Name())) {
return false
}
}
}
if err != nil || len(entries) < di.BatchSize {
break
}
}
// If this is a leaf directory, yield it
if !hasSubdirs {
return yield(path, true)
}
return true
}
// Start the recursion
visit(root)
}
}
// FilesOnly returns only files, skipping directories. This does
// return symlinks as files too, if di.FollowSymlinks is false.
// If di.FollowSymlinks is true, and a symlink links to a
// directory, the recursion will follow the symlink down
// that directory tree. Note that this can result in
// returning the same file multiple times if there
// are multiple paths throught symlinks to the same file.
// It is the user's responsibility to deduplicate the
// returned paths if need be when using FollowSymlinks true.
// Resolving a symlink through multiple other symlinks
// will only count as one depth level for MaxDepth stopping.
func (di *DirIter) FilesOnly(root string) iter.Seq2[string, bool] {
return func(yield func(string, bool) bool) {
// Helper function for recursive traversal
var visit func(path string, depth int) bool
visit = func(path string, depth int) bool {
//vv("top of visit, path = '%v'; depth = %v", path, depth)
if di.MaxDepth > 0 && depth >= di.MaxDepth {
return true // true lets cousins also get to max depth.
}
dir, err := os.Open(path)
if err != nil {
return yield(path, false)
}
defer dir.Close()
for {
entries, err := dir.ReadDir(di.BatchSize)
// Process entries in directory order
for _, entry := range entries {
//vv("entry = '%#v'; entry.Type()&fs.ModeSymlink = %v", entry, entry.Type()&fs.ModeSymlink)
if entry.Type()&fs.ModeSymlink != 0 {
//vv("see symlink : '%v'", entry.Name())
resolveMe := filepath.Join(path, entry.Name())
if !di.FollowSymlinks {
//target, err := os.Readlink(resolveMe)
//if err != nil {
// // allow dangling links to not stop the walk.
// continue
//}
//vv("unfollowed symlink : '%v'", entry.Name())
if !yield(resolveMe, true) {
return false
}
continue
}
//vv("have symlink '%v'", resolveMe)
target, err := filepath.EvalSymlinks(resolveMe)
if err != nil {
return false
}
//vv("resolveMe:'%v' -> target:'%v'", resolveMe, target)
fi, err := os.Stat(target)
if err != nil {
return false
}
//entry = fs.FileInfoToDirEntry(fi)
//vv("target entry = '%v'; entry.IsDir() = '%v'", fi.Name(), fi.IsDir())
// we cannot use the below, because path may
// no longer be the right prefix if the symlink
// went .. or elsewhere.
if fi.IsDir() {
// Recurse immediately when we find a directory
if !visit(target, depth+1) {
return false
}
} else {
if !yield(target, true) {
return false
}
}
continue
}
if entry.IsDir() {
// Recurse immediately when we find a directory
if !visit(filepath.Join(path, entry.Name()), depth+1) {
return false
}
} else {
if !yield(filepath.Join(path, entry.Name()), true) {
return false
}
}
}
if err != nil || len(entries) < di.BatchSize {
break
}
}
return true
}
// Start the recursion
visit(root, 0)
}
}
// AllDirsOnlyDirs returns all subdirectories of root.
// It does return any files.
func (di *DirIter) AllDirsOnlyDirs(root string) iter.Seq2[string, bool] {
return func(yield func(string, bool) bool) {
// Helper function for recursive traversal
var visit func(path string) bool
visit = func(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return yield(path, false)
}
if !fi.IsDir() {
return false
}
dir, err := os.Open(path)
if err != nil {
return yield(path, false)
}
defer dir.Close()
for {
entries, err := dir.ReadDir(di.BatchSize)
// Process entries in directory order
for _, entry := range entries {
if entry.IsDir() {
// Recurse immediately when we find a directory
if !visit(filepath.Join(path, entry.Name())) {
return false
}
}
}
if err != nil || len(entries) < di.BatchSize {
break
}
}
// we are a directory, yield ourselves.
return yield(path, true)
} // end of visit
// Start the recursion
visit(root)
}
}