-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.go
More file actions
161 lines (153 loc) · 4.42 KB
/
index.go
File metadata and controls
161 lines (153 loc) · 4.42 KB
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
package main
import (
"context"
"fmt"
"io"
"io/fs"
"os"
"path"
"strings"
"sync"
"sync/atomic"
"github.com/hekmon/liveterm"
"golang.org/x/sync/semaphore"
)
const (
specialFileModes = os.ModeSymlink | os.ModeDevice | os.ModeNamedPipe | os.ModeSocket | os.ModeCharDevice | os.ModeIrregular
)
type FileInfos struct {
FullPath string
Infos fs.FileInfo
Children FileInfosList
ChildrenAccess *sync.Mutex
}
type FileInfosList []*FileInfos
func (list FileInfosList) String() string {
fullPaths := make([]string, len(list))
for index, candidate := range list {
fullPaths[index] = candidate.FullPath
}
return "'" + strings.Join(fullPaths, "', '") + "'"
}
func index(pathA, pathB string, tokenPool *semaphore.Weighted) (pathATree, pathBTree *FileInfos, nbAFiles int64, errorCount int) {
// Prepare to launch goroutines
var workers sync.WaitGroup
errChan := make(chan error)
scannedA := new(atomic.Int64)
scannedB := new(atomic.Int64)
// Prepare live term
var stdout, stderr io.Writer
err := liveterm.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to start liveterm: %s\n", err)
stdout = os.Stdout
stderr = os.Stderr
} else {
defer liveterm.Stop(true)
stdout = liveterm.Bypass()
stderr = stdout
liveterm.SetSingleLineUpdateFx(func() string {
return fmt.Sprintf("Indexing: %d regular files found on '%s' & %d regular files found on '%s'\n",
scannedA.Load(), pathA, scannedB.Load(), pathB)
})
}
// error logger
errorsDone := make(chan any)
go func() {
for err := range errChan {
fmt.Fprintf(stderr, "ERROR: %s\n", err)
errorCount++
}
close(errorsDone)
}()
// Launch the path A walker
fakeAParent := FileInfos{
Children: make(FileInfosList, 0, 1),
ChildrenAccess: new(sync.Mutex),
}
_ = tokenPool.Acquire(context.Background(), 1) // no err check as error can only come from expired context
workers.Add(1)
go func() {
indexChild(pathA, &fakeAParent, tokenPool, &workers, scannedA, errChan)
tokenPool.Release(1)
workers.Done()
}()
// Launch the path B walker
fakeBParent := FileInfos{
Children: make(FileInfosList, 0, 1),
ChildrenAccess: new(sync.Mutex),
}
_ = tokenPool.Acquire(context.Background(), 1) // no err check as error can only come from expired context
workers.Add(1)
go func() {
indexChild(pathB, &fakeBParent, tokenPool, &workers, scannedB, errChan)
tokenPool.Release(1)
workers.Done()
}()
// Wait for walkers to return
workers.Wait()
// Stop utilities goroutines
close(errChan)
// Return both trees root
pathATree = fakeAParent.Children[0]
pathBTree = fakeBParent.Children[0]
// Print results
<-errorsDone
totalA := scannedA.Load()
totalB := scannedB.Load()
total := totalA + totalB
fmt.Fprintf(stdout, "Indexing done: %d total files found\n\t%d on %s\n\t%d on %s\n", total, totalA, pathA, totalB, pathB)
nbAFiles = totalA
return
}
func indexChild(pathScan string, parent *FileInfos, tokenPool *semaphore.Weighted, waitGroup *sync.WaitGroup, scanned *atomic.Int64, errChan chan<- error) {
var err error
self := FileInfos{
FullPath: pathScan,
}
// get infos
if self.Infos, err = os.Lstat(pathScan); err != nil {
errChan <- fmt.Errorf("failed to stat '%s': %w: this path won't be included in the scan", pathScan, err)
return
}
// if special file: skip
if self.Infos.Mode()&specialFileModes != 0 {
return
}
// if dir, scan it too
if self.Infos.IsDir() {
// list dir
var entries []fs.DirEntry
if entries, err = os.ReadDir(pathScan); err != nil {
errChan <- fmt.Errorf("failed to list directory '%s': %w: this path won't be included in the scan", pathScan, err)
return
}
// process list
self.Children = make(FileInfosList, 0, len(entries))
self.ChildrenAccess = new(sync.Mutex)
for _, entry := range entries {
// can we launch it concurrently ?
if tokenPool.TryAcquire(1) {
waitGroup.Add(1)
go func(localEntry fs.DirEntry) {
indexChild(path.Join(pathScan, localEntry.Name()), &self, tokenPool, waitGroup, scanned, errChan)
tokenPool.Release(1)
waitGroup.Done()
}(entry)
} else {
indexChild(path.Join(pathScan, entry.Name()), &self, tokenPool, waitGroup, scanned, errChan)
}
}
} else {
// regular file, update stats
scanned.Add(1)
}
// register to parent when possible in a non weighted goroutine
waitGroup.Add(1)
go func() {
parent.ChildrenAccess.Lock()
parent.Children = append(parent.Children, &self)
parent.ChildrenAccess.Unlock()
waitGroup.Done()
}()
}