-
Notifications
You must be signed in to change notification settings - Fork 0
/
walk.go
148 lines (126 loc) · 3.28 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
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sync"
"syscall"
)
func openAndInfo(path string, d fs.DirEntry, err error, rootDev deviceID) (bool, *os.File, fs.FileInfo, error) {
// Excluded check must come first, because it can be use to skip
// directories that would otherwise cause errors.
if isExcluded(path) {
if d.IsDir() {
return false, nil, nil, fs.SkipDir
}
return false, nil, nil, nil
}
if err != nil {
return false, nil, nil, err
}
if d.IsDir() || !d.Type().IsRegular() {
return false, nil, nil, nil
}
// It is important that we obtain fs.FileInfo at this point, before
// reading any of the file contents, because the file could be modified
// while we do so. See the comment on ChecksumV1.ModTimeUsec for more
// details.
info, err := d.Info()
if err != nil {
return true, nil, nil, err
}
fd, err := os.Open(path)
if err != nil {
return true, nil, nil, err
}
if options.oneFilesystem && rootDev != getDevice(info) {
fd.Close()
return false, nil, nil, fs.SkipDir
}
return true, fd, info, nil
}
type deviceID uint64
func getDevice(info fs.FileInfo) deviceID {
return deviceID(info.Sys().(*syscall.Stat_t).Dev)
}
func getDeviceForPath(path string) deviceID {
fi, err := os.Stat(path)
if err != nil {
// Doesn't matter, because we'll get an error during WalkDir.
return 0
}
return getDevice(fi)
}
type walkFn func(fd *os.File, info fs.FileInfo, p *Progress) error
type walkItem struct {
fd *os.File
info fs.FileInfo
p *Progress
}
func walk(roots []string, fn walkFn) error {
rootDev := deviceID(0)
p := NewProgress(options.isTTY)
defer p.Stop()
// Launch the workers.
wg := sync.WaitGroup{}
workC := make(chan walkItem)
workerErrs := make(chan error, options.parallel)
for i := 0; i < options.parallel; i++ {
wg.Add(1)
go worker(&wg, workC, fn, workerErrs)
}
// Helper function used by filepath.WalkDir to send items to the workers.
wfn := func(path string, d fs.DirEntry, err error) error {
// On each iteration, check if any of the workers had an error.
// If so, return it, which stops the walk immediately.
if werr, ok := hasErr(workerErrs); ok {
return werr
}
// Open the file one by one, because as part of doing so, the function
// will return fs.SkipDir as needed, so we can't parallelize it.
ok, fd, info, err := openAndInfo(path, d, err, rootDev)
if !ok || err != nil {
return err
}
// Send the work to the workers. They will close the fd.
workC <- walkItem{fd, info, p}
return nil
}
var err error
for _, root := range roots {
rootDev = getDeviceForPath(root)
err = filepath.WalkDir(root, wfn)
if err != nil {
break
}
}
close(workC)
wg.Wait()
// Check for any errors in the last iterations.
if werr, ok := hasErr(workerErrs); err == nil && ok {
err = werr
}
if p.corrupted > 0 && err == nil {
err = fmt.Errorf("detected %d corrupted files", p.corrupted)
}
return err
}
func worker(wg *sync.WaitGroup, c chan walkItem, fn walkFn, errc chan error) {
defer wg.Done()
for item := range c {
err := fn(item.fd, item.info, item.p)
item.fd.Close()
if err != nil {
errc <- fmt.Errorf("error in %q: %w", item.fd.Name(), err)
}
}
}
func hasErr(errc chan error) (error, bool) {
select {
case err := <-errc:
return err, true
default:
return nil, false
}
}