-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplan.go
262 lines (230 loc) · 5.42 KB
/
plan.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
// Copyright (c) 2016, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package lackey
import (
"errors"
"fmt"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/Jeffail/tunny"
"github.com/goulash/osutil"
)
type Planner struct {
IgnoreData bool
DataExcept map[string]bool
DeleteBefore bool
TranscodeAll bool
Concurrent int
DownscaleCover bool
CoverSource string
CoverTarget string
op Operator
src *Database
dst *Database
pool *tunny.WorkPool
errs chan error
wg sync.WaitGroup
quit error
}
func NewPlanner(src, dst *Database, op Operator) *Planner {
return &Planner{
DataExcept: make(map[string]bool),
Concurrent: runtime.NumCPU(),
op: op,
src: src,
dst: dst,
}
}
func (p *Planner) Plan() error {
var err error
if p.src == nil || p.dst == nil || p.op == nil {
return errors.New("planner contains nil fields")
}
src := p.src.Root()
if !src.IsDir() {
return errors.New("src must be a directory")
}
dst := p.dst.Root()
if !dst.IsDir() {
return errors.New("dst must be a directory")
}
p.pool, err = tunny.CreatePoolGeneric(p.Concurrent).Open()
if err != nil {
return err
}
defer p.pool.Close()
p.errs = make(chan error, 1)
go func() {
for e := range p.errs {
err := p.op.Warn(e)
if err != nil {
p.quit = err
break
}
}
}()
err = p.planDir(src, dst)
p.wg.Wait()
return err
}
func (p *Planner) planDir(src, dst *Entry) error {
// We know that both src and dst are directories, or dst doesn't exist.
if dst != nil && p.DeleteBefore {
// Delete extra files on destination first, if dst exists.
expect := make(map[string]bool)
for _, e := range src.Children() {
expect[p.dkey(e)] = true
}
for _, e := range dst.Children() {
if !expect[e.Key()] {
p.remove(e)
}
}
} else {
// Create the directory if it doesn't exist.
path := p.dpath(src.Key())
ex, err := osutil.DirExists(path)
if err != nil {
return err
}
if !ex {
err := p.op.CreateDir(p.dpath(src.Key()))
if err != nil {
return err
}
}
}
// Sync source to destination
for _, s := range src.Children() {
// Check for errors from the workers
if p.quit != nil {
return p.quit
}
d := p.dst.Get(p.dkey(s))
// Eliminate the possibility of a mismatch
if d != nil && (s.IsDir() != d.IsDir() || s.IsMusic() != d.IsMusic()) {
err := p.remove(d)
if err != nil {
return err
}
d = nil
}
var err error
if s.IsDir() {
err = p.planDir(s, d)
} else {
err = p.planFile(s, d)
}
if err != nil {
err = p.op.Warn(err)
if err != nil {
return err
}
}
}
return nil
}
// planFile synchronizes src to dst, which may be nil.
func (p *Planner) planFile(src, dst *Entry) error {
path := p.dpath(p.dkey(src))
// Check our assumption, that dst is the data for path:
if dst != nil && path != p.dpath(dst.Key()) {
fmt.Printf("warn: destination path %q != stat data from %q", path, p.dpath(dst.Key()))
}
if src.IsMusic() {
switch p.op.Which(src, dst) {
case SkipAudio:
return p.op.Ok(path)
case CopyAudio:
return p.op.CopyFile(src.AbsPath(), path)
case TranscodeAudio:
p.wg.Add(1)
p.pool.SendWorkAsync(func() {
err := p.op.Transcode(src.AbsPath(), path, src)
if err != nil {
p.errs <- err
}
p.wg.Done()
}, nil)
return nil
case UpdateAudio:
p.wg.Add(1)
p.pool.SendWorkAsync(func() {
err := p.op.Update(src.AbsPath(), path, dst)
if err != nil {
p.errs <- err
}
p.wg.Done()
}, nil)
return nil
case IgnoreAudio:
return p.op.Ignore(path)
default:
panic("unknown audio operation")
}
} else if src.IsIgnored() {
return p.op.Ignore(path)
} else {
if p.IgnoreData != p.DataExcept[src.Filename()] && src.Filename() != p.CoverSource {
// We land in here when:
// - Ignore all data (true) and there is no exception (false)
// - Do not ignore all data (false) and there is an exception (true)
return p.op.Ignore(path)
}
if dst != nil && dst.FileInfo().ModTime().After(src.FileInfo().ModTime()) {
return p.op.Ok(path)
}
if p.DownscaleCover && src.Filename() == p.CoverSource {
return p.op.DownscaleCover(src.AbsPath(), path)
}
return p.op.CopyFile(src.AbsPath(), path)
}
}
// dpath returns the absolute destination path, given the key.
func (p *Planner) dpath(key string) string {
return filepath.Join(p.dst.Path(), key)
}
// dkey returns the destination key, which also takes into account whether the
// file should be transcoded or not.
func (p *Planner) dkey(src *Entry) string {
if src.Filename() == p.CoverSource && p.CoverSource != p.CoverTarget {
if p.CoverTarget == "" {
return src.Key()
}
return strings.Replace(src.Key(), p.CoverSource, p.CoverTarget, 1)
}
if !src.IsMusic() {
return src.Key()
}
ext := p.op.WhichExt(src)
key := src.Key()
_, oxt := src.FilenameExt()
return key[:len(key)-len(oxt)] + ext // this might not work
}
func (p *Planner) pathWithExt(src *Entry, ext string) string {
path := filepath.Join(p.dst.Path(), src.RelPath())
if ext == "" {
return path
}
_, oxt := src.FilenameExt()
return path[:len(path)-len(oxt)] + ext // this might not work
}
func (p *Planner) remove(dst *Entry) error {
//debug
if dst.parent == nil {
panic("why?")
}
var err error
if dst.IsDir() {
err = p.op.RemoveDir(dst.AbsPath())
} else {
err = p.op.RemoveFile(dst.AbsPath())
}
if err != nil {
return p.op.Warn(err)
}
return nil
}