forked from dullgiulio/sys-file-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.go
181 lines (168 loc) · 3.29 KB
/
scan.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
package main
import (
"fmt"
"io"
"log"
"os"
)
func sumBytes(bs []byte) int {
var t int
for _, b := range bs {
t += int(b)
}
return t
}
type file struct {
os.FileInfo
path string
}
func makeFile(f os.FileInfo, path string) file {
return file{f, path}
}
func (f *file) name() string {
return fmt.Sprintf("%s/%s", f.path, f.FileInfo.Name())
}
type indexer struct {
stash chan string
dirs chan string
wn chan int
out chan file
ws int
wi int
}
func newIndexer(ws, wi int) *indexer {
return &indexer{
// dirs scheduled to be scanned.
stash: make(chan string),
// dirs is for directories to scan from dispatcher to workers.
dirs: make(chan string),
// wn is for workers status.
wn: make(chan int),
// out is for all found files.
out: make(chan file),
// number of workers
ws: ws,
// unmber of this worker
wi: wi,
}
}
func (i *indexer) canProcess(f file) bool {
if i.ws < 2 {
return true
}
return sumBytes([]byte(f.name()))%i.ws == i.wi
}
func (i *indexer) sink() <-chan file {
return i.out
}
func (s *indexer) scan(root string, n int) {
for i := 0; i < n; i++ {
go s.worker(i)
}
// One worker will pick this up and stop until dispatch starts.
s.dirs <- root
s.dispatch(n)
}
func (i *indexer) worker(n int) {
for dir := range i.dirs {
i.wn <- n
i.readdir(dir)
i.wn <- n
}
}
func (s *indexer) dispatch(n int) {
workerActive := make(map[int]bool)
dirs := make([]string, 0)
for {
select {
// Append a directory to scan.
case dir := <-s.stash:
dirs = append(dirs, dir)
// A worker has started or stopped working.
case m := <-s.wn:
// Flip this worker between active and inactive
workerActive[m] = !workerActive[m]
}
freeWorkers := 0
for i := 0; i < n; i++ {
// Send one dir out per free worker.
// If no worker is free, send nothing and
// keep collecting dirs until a worker is free.
if !workerActive[i] {
freeWorkers++
}
}
// If there are more directories to scan,
// send out the first one and one worker will
// pick it up. The dirs chan shouldn't be buffered.
if len(dirs) == 0 && freeWorkers == n {
break
}
if len(dirs) == 0 {
continue
}
for i := 0; i < freeWorkers; i++ {
var dir string
dir, dirs = dirs[0], dirs[1:]
s.dirs <- dir
for m := range s.wn {
workerActive[m] = !workerActive[m]
if workerActive[m] {
break
}
}
if len(dirs) == 0 {
break
}
}
// If there are no pending dirs to scan,
// and no worker is still active, we have finished.
var cont bool
for i := 0; i < n; i++ {
if workerActive[i] {
cont = true
break
}
}
if !cont {
break
}
}
close(s.stash)
close(s.dirs)
close(s.out)
close(s.wn)
}
func (i *indexer) readdir(name string) {
dir, err := os.Open(name)
if err != nil {
log.Print(err)
return
}
defer dir.Close()
for {
fi, err := dir.Readdir(255)
if err != nil {
if err != io.EOF {
log.Print(err)
}
return
}
for j := range fi {
f := makeFile(fi[j], name)
if f.IsDir() {
i.stash <- f.name()
continue
}
// TODO: Handling of symlinks outside the root!
if f.Mode()&os.ModeSymlink != 0 {
log.Printf("Error: %s skipped. Symlinks are currently not supported!", name)
}
if f.Mode().IsRegular() {
if i.canProcess(f) {
i.out <- f
}
}
}
}
}