-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
262 lines (225 loc) · 5 KB
/
pipeline.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package megasd
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"github.com/bodgit/megasd/metadata"
)
func containsCue(dir string) (bool, error) {
d, err := os.Open(dir)
if err != nil {
return false, err
}
defer d.Close()
info, err := d.Stat()
if err != nil {
return false, err
}
if !info.IsDir() {
return false, errors.New("not a directory")
}
files, err := d.Readdirnames(0)
if err != nil {
return false, err
}
for _, file := range files {
if filepath.Ext(file) == ".cue" {
return true, nil
}
}
return false, nil
}
func (m *MegaSD) findDirectories(ctx context.Context, base string) (<-chan string, <-chan error, error) {
out := make(chan string)
errc := make(chan error, 1)
go func() {
defer close(out)
defer close(errc)
errc <- filepath.Walk(base, func(dir string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore any hidden files or directories, otherwise we end up fighting with things like Spotlight, etc.
if info.Name()[0] == '.' {
if info.Mode().IsDir() {
return filepath.SkipDir
}
return nil
}
// Ignore anything that isn't a directory
if !info.Mode().IsDir() {
return nil
}
select {
case out <- dir:
case <-ctx.Done():
return errors.New("walk cancelled")
}
return nil
})
}()
return out, errc, nil
}
func (m *MegaSD) fileWorker(dir, file string, db *metadata.DB) error {
var crc string
var err error
switch filepath.Ext(file) {
case ".bin":
// For any .bin file, if there is a .cue file in the same directory, assume it's a CD track rather than a ROM image
hasCue, err := containsCue(filepath.Dir(file))
if err != nil {
return err
}
if hasCue {
return nil
}
fallthrough
case ".32x", ".md", ".sg", ".sms":
// Check files are in the "top" directory
if filepath.Dir(file) != dir {
return nil
}
crc, err = crcFile(file)
if err != nil {
return err
}
screenshot, err := m.db.FindScreenshotByCRC(crc)
if err != nil {
return err
}
if screenshot != nil {
return db.Set(metadata.CRCFilename(strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))), screenshot)
}
case ".cue":
if filepath.Dir(filepath.Dir(file)) != dir {
return nil
}
crc, err = crcCueFile(file)
if err != nil {
return err
}
screenshot, err := m.db.FindScreenshotByCRC(crc)
if err != nil {
return err
}
if screenshot != nil {
return db.Set(metadata.CRCFilename(filepath.Base(filepath.Dir(file))), screenshot)
}
default:
return nil
}
m.logger.Printf("No match for \"%s\", with CRC \"%s\"\n", file, crc)
return nil
}
func writeMetadata(dir string, db *metadata.DB) error {
b, err := db.MarshalBinary()
if err != nil {
return err
}
f, err := os.Create(filepath.Join(dir, metadata.Filename))
if err != nil {
return err
}
defer f.Close()
if _, err = f.Write(b); err != nil {
return err
}
return nil
}
func (m *MegaSD) directoryWorker(ctx context.Context, in <-chan string) (<-chan error, error) {
errc := make(chan error, 1)
go func() {
defer close(errc)
for dir := range in {
db := metadata.New()
if err := filepath.Walk(dir, func(file string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore any hidden files or directories, otherwise we end up fighting with things like Spotlight, etc.
if info.Name()[0] == '.' {
if info.Mode().IsDir() {
return filepath.SkipDir
}
return nil
}
// Ignore anything that isn't a normal file
if !info.Mode().IsRegular() {
return nil
}
// Ignore any file greater than 16 MB
if info.Size() > 16<<(10*2) {
return nil
}
if err := m.fileWorker(dir, file, db); err != nil {
return err
}
return nil
}); err != nil {
errc <- err
return
}
if db.Length() > 0 {
if err := writeMetadata(dir, db); err != nil {
errc <- err
return
}
}
}
}()
return errc, nil
}
func waitForPipeline(errs ...<-chan error) error {
errc := mergeErrors(errs...)
for err := range errc {
if err != nil {
return err
}
}
return nil
}
func mergeErrors(cs ...<-chan error) <-chan error {
var wg sync.WaitGroup
out := make(chan error, len(cs))
wg.Add(len(cs))
for _, c := range cs {
go func(c <-chan error) {
for n := range c {
out <- n
}
wg.Done()
}(c)
}
go func() {
wg.Wait()
close(out)
}()
return out
}
// Scan traverses the given directory and creates a metadata in any
// sub-directory that contains matching images
func (m *MegaSD) Scan(path string) error {
dir, err := filepath.Abs(path)
if err != nil {
return err
}
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
var errcList []<-chan error
dirs, errc, err := m.findDirectories(ctx, dir)
if err != nil {
return err
}
errcList = append(errcList, errc)
for i := 0; i < 10; i++ {
errc, err := m.directoryWorker(ctx, dirs)
if err != nil {
return err
}
errcList = append(errcList, errc)
}
return waitForPipeline(errcList...)
}