forked from odeke-em/drive
/
push.go
556 lines (453 loc) · 12.7 KB
/
push.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drive
import (
"fmt"
"os"
"os/signal"
gopath "path"
"path/filepath"
"sort"
"strings"
"sync"
"time"
"github.com/odeke-em/drive/config"
"github.com/odeke-em/semalim"
)
var mkdirAllMu = sync.Mutex{}
// Pushes to remote if local path exists and in a gd context. If path is a
// directory, it recursively pushes to the remote if there are local changes.
// It doesn't check if there are local changes if isForce is set.
func (g *Commands) Push() error {
g.rem.encrypter = g.opts.Encrypter
g.rem.decrypter = g.opts.Decrypter
defer g.clearMountPoints()
var cl []*Change
g.log.Logln("Resolving...")
spin := g.playabler()
spin.play()
// To Ensure mount points are cleared in the event of external exceptions
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
go func() {
_ = <-c
spin.stop()
g.clearMountPoints()
os.Exit(1)
}()
clashes := []*Change{}
rootAbsPath := g.context.AbsPathOf("")
destAbsPath := g.context.AbsPathOf(g.opts.Destination)
remoteDestRelPath, err := filepath.Rel(rootAbsPath, destAbsPath)
if err != nil {
return err
}
for _, relToRootPath := range g.opts.Sources {
fsAbsPath := g.context.AbsPathOf(relToRootPath)
// Join this relative path to that of the remote relative path of the destination.
relToDestPath := remotePathJoin(remoteDestRelPath, relToRootPath)
ccl, cclashes, cErr := g.changeListResolve(relToDestPath, fsAbsPath, true)
clashes = append(clashes, cclashes...)
if cErr != nil && cErr != ErrClashesDetected {
spin.stop()
return cErr
}
if len(ccl) > 0 {
cl = append(cl, ccl...)
}
}
mount := g.opts.Mount
if mount != nil {
for _, mt := range mount.Points {
ccl, cclashes, cerr := g.changeListResolve(mt.Name, mt.MountPath, true)
if cerr == nil {
cl = append(cl, ccl...)
}
clashes = append(clashes, cclashes...)
}
}
if len(clashes) >= 1 {
if g.opts.FixClashes {
err := autoRenameClashes(g, clashes)
if err == nil {
g.log.Logln(MsgClashesFixedNowRetry)
}
return err
}
warnClashesPersist(g.log, clashes)
return ErrClashesDetected
}
spin.stop()
nonConflictsPtr, conflictsPtr := g.resolveConflicts(cl, true)
if conflictsPtr != nil {
warnConflictsPersist(g.log, *conflictsPtr)
return unresolvedConflictsErr(fmt.Errorf("conflicts have prevented a push operation"))
}
nonConflicts := *nonConflictsPtr
pushSize, modSize := reduceToSize(cl, SelectDest|SelectSrc)
// Compensate for deletions and modifications
pushSize -= modSize
// Warn about (near) quota exhaustion
quotaStatus, quotaErr := g.QuotaStatus(pushSize)
if quotaErr != nil {
return quotaErr
}
unSafe := false
switch quotaStatus {
case AlmostExceeded:
g.log.LogErrln("\033[92mAlmost exceeding your drive quota\033[00m")
case Exceeded:
g.log.LogErrln("\033[91mThis change will exceed your drive quota\033[00m")
unSafe = true
}
if unSafe {
unSafeQuotaMsg := fmt.Sprintf("projected size: (%d) %s\n", pushSize, prettyBytes(pushSize))
if !g.opts.canPrompt() {
return cannotPromptErr(fmt.Errorf("quota: noPrompt is set yet for quota %s", unSafeQuotaMsg))
}
g.log.LogErrf(" %s", unSafeQuotaMsg)
if status := promptForChanges(); !accepted(status) {
return status.Error()
}
}
clArg := changeListArg{
logy: g.log,
changes: nonConflicts,
noPrompt: !g.opts.canPrompt(),
noClobber: g.opts.NoClobber,
canPreview: g.opts.canPreview(),
}
status, opMap := printChangeList(&clArg)
if !accepted(status) {
return status.Error()
}
return g.playPushChanges(nonConflicts, opMap)
}
func (g *Commands) PushPiped() error {
g.rem.encrypter = g.opts.Encrypter
g.rem.decrypter = g.opts.Decrypter
// Cannot push asynchronously because the push order must be maintained
for _, relToRootPath := range g.opts.Sources {
rem, resErr := g.rem.FindByPath(relToRootPath)
if resErr != nil && resErr != ErrPathNotExists {
return resErr
}
if rem != nil && !g.opts.Force {
return overwriteAttemptedErr(fmt.Errorf("%s already exists remotely, use `%s` to override this behaviour.\n", relToRootPath, ForceKey))
}
if hasExportLinks(rem) {
return googleDocNonExportErr(fmt.Errorf("'%s' is a GoogleDoc/Sheet document cannot be pushed to raw.\n", relToRootPath))
}
base := filepath.Base(relToRootPath)
local := fauxLocalFile(base)
if rem == nil {
rem = local
}
parentPath := g.parentPather(relToRootPath)
parent, pErr := g.rem.FindByPath(parentPath)
if pErr != nil {
spin := g.playabler()
spin.play()
parent, pErr = g.remoteMkdirAll(parentPath)
spin.stop()
if pErr != nil || parent == nil {
g.log.LogErrf("%s: %v\n", relToRootPath, pErr)
return pErr
}
}
fauxSrc := DupFile(rem)
if fauxSrc != nil {
fauxSrc.ModTime = time.Now()
}
args := &upsertOpt{
parentId: parent.Id,
fsAbsPath: relToRootPath,
src: fauxSrc,
dest: rem,
mask: g.opts.TypeMask,
nonStatable: true,
ignoreChecksum: g.opts.IgnoreChecksum,
retryCount: g.opts.ExponentialBackoffRetryCount,
}
rem, _, rErr := g.rem.upsertByComparison(os.Stdin, args)
if rErr != nil {
g.log.LogErrf("%s: %v\n", relToRootPath, rErr)
return rErr
}
if rem == nil {
continue
}
index := rem.ToIndex()
wErr := g.context.SerializeIndex(index)
// TODO: Should indexing errors be reported?
if wErr != nil {
g.log.LogErrf("serializeIndex %s: %v\n", rem.Name, wErr)
}
}
return nil
}
func (g *Commands) deserializeIndex(identifier string) *config.Index {
index, err := g.context.DeserializeIndex(identifier)
if err != nil {
return nil
}
return index
}
func (g *Commands) playPushChanges(cl []*Change, opMap *map[Operation]sizeCounter) (err error) {
if opMap == nil {
result := opChangeCount(cl)
opMap = &result
}
totalSize := int64(0)
ops := *opMap
for op, counter := range ops {
totalSize += counter.sizeByOperation(op)
}
g.taskStart(totalSize)
defer close(g.rem.progressChan)
go func() {
for n := range g.rem.progressChan {
g.taskAdd(int64(n))
}
}()
type workPair struct {
fn func(*Change) error
arg *Change
}
n := maxProcs()
sort.Sort(ByPrecedence(cl))
jobsChan := make(chan semalim.Job)
go func() {
defer close(jobsChan)
throttle := time.Tick(time.Duration(1e9 / n))
for i, c := range cl {
if c == nil {
g.log.LogErrf("BUGON:: push: nil change found for change index %d\n", i)
continue
}
fn := remoteOpToChangerTranslator(g, c)
if fn == nil {
g.log.LogErrf("push: cannot find operator for %v", c.Op())
continue
}
cjs := changeJobSt{
change: c,
fn: fn,
verb: "Push",
throttle: throttle,
}
dofner := cjs.changeJober(g)
jobsChan <- jobSt{id: uint64(i), do: dofner}
}
}()
results := semalim.Run(jobsChan, uint64(n))
for result := range results {
res, resErr := result.Value(), result.Err()
if resErr != nil {
err = reComposeError(err, fmt.Sprintf("push: %s err: %v\n", res, resErr))
}
}
g.taskFinish()
return err
}
func (g *Commands) pathSplitter(absPath string) (dir, base string) {
p := strings.Split(absPath, "/")
pLen := len(p)
base = p[pLen-1]
p = append([]string{"/"}, p[:pLen-1]...)
dir = gopath.Join(p...)
return
}
func (g *Commands) parentPather(absPath string) string {
dir, _ := g.pathSplitter(absPath)
return dir
}
func (g *Commands) remoteMod(change *Change) (err error) {
if change.Dest == nil && change.Src == nil {
err = illogicalStateErr(fmt.Errorf("bug on: both dest and src cannot be nil"))
g.log.LogErrln(err)
return err
}
absPath := g.context.AbsPathOf(change.Path)
if change.Src != nil && change.Src.IsDir {
needsMkdirAll := change.Dest == nil || change.Src.Id == ""
if needsMkdirAll {
if destFile, _ := g.remoteMkdirAll(change.Path); destFile != nil {
change.Src.Id = destFile.Id
}
}
}
if change.Dest != nil && change.Src != nil && change.Src.Id == "" {
change.Src.Id = change.Dest.Id // TODO: bad hack
}
var parent *File
parentPath := g.parentPather(change.Path)
parent, err = g.remoteMkdirAll(parentPath)
if err != nil {
g.log.LogErrf("remoteMod/remoteMkdirAll: `%s` got %v\n", parentPath, err)
return err
}
if parent == nil {
err = errCannotMkdirAll(parentPath)
g.log.LogErrln(err)
return
}
args := &upsertOpt{
parentId: parent.Id,
fsAbsPath: absPath,
src: change.Src,
dest: change.Dest,
mask: g.opts.TypeMask,
ignoreChecksum: g.opts.IgnoreChecksum,
debug: g.opts.Verbose && g.opts.canPreview(),
retryCount: g.opts.ExponentialBackoffRetryCount,
}
coercedMimeKey, ok := g.coercedMimeKey()
if ok {
args.mimeKey = coercedMimeKey
} else if args.src != nil && !args.src.IsDir { // Infer it from the extension
args.mimeKey = filepath.Ext(args.src.Name)
}
rem, err := g.rem.UpsertByComparison(args)
if err != nil {
g.log.LogErrf("%s: %v\n", change.Path, err)
return
}
if rem == nil {
return
}
index := rem.ToIndex()
wErr := g.context.SerializeIndex(index)
// TODO: Should indexing errors be reported?
if wErr != nil {
g.log.LogErrf("serializeIndex %s: %v\n", rem.Name, wErr)
}
return
}
func (g *Commands) remoteAdd(change *Change) error {
return g.remoteMod(change)
}
func (g *Commands) remoteUntrash(change *Change) error {
target := change.Src
defer func() {
g.taskAdd(target.Size)
}()
if err := g.rem.Untrash(target.Id); err != nil {
return err
}
index := target.ToIndex()
wErr := g.context.SerializeIndex(index)
// TODO: Should indexing errors be reported?
if wErr != nil {
g.log.LogErrf("serializeIndex %s: %v\n", target.Name, wErr)
}
return nil
}
func remoteRemover(g *Commands, change *Change, fn func(string) error) error {
defer func() {
g.taskAdd(change.Dest.Size)
}()
if err := fn(change.Dest.Id); err != nil {
return err
}
if change.Dest.IsDir {
mkdirAllMu.Lock()
g.mkdirAllCache.Remove(change.Path)
mkdirAllMu.Unlock()
}
index := change.Dest.ToIndex()
err := g.context.RemoveIndex(index, g.context.AbsPathOf(""))
if err != nil {
if change.Src != nil {
g.log.LogErrf("%s \"%s\": remove indexfile %v\n", change.Path, change.Dest.Id, err)
}
}
return err
}
func (g *Commands) remoteTrash(change *Change) error {
return remoteRemover(g, change, g.rem.Trash)
}
func (g *Commands) remoteDelete(change *Change) error {
return remoteRemover(g, change, g.rem.Delete)
}
func (g *Commands) remoteMkdirAll(d string) (*File, error) {
mkdirAllMu.Lock()
cachedValue, ok := g.mkdirAllCache.Get(d)
if ok && cachedValue != nil {
castF, castOk := cachedValue.Value().(*File)
// g.log.Logln("CacheHit", d, castF, castOk)
if castOk && castF != nil {
mkdirAllMu.Unlock()
return castF, nil
}
}
// Try the lookup one last time in case a coroutine raced us to it.
retrFile, retryErr := g.rem.FindByPath(d)
if retryErr != nil && retryErr != ErrPathNotExists {
mkdirAllMu.Unlock()
return retrFile, retryErr
}
if retrFile != nil {
mkdirAllMu.Unlock()
return retrFile, nil
}
parDirPath, last := remotePathSplit(d)
parent, parentErr := g.rem.FindByPath(parDirPath)
if parentErr != nil && parentErr != ErrPathNotExists {
mkdirAllMu.Unlock()
return parent, parentErr
}
mkdirAllMu.Unlock()
if parent == nil {
parent, parentErr = g.remoteMkdirAll(parDirPath)
if parentErr != nil || parent == nil {
return parent, parentErr
}
}
mkdirAllMu.Lock()
defer mkdirAllMu.Unlock()
g.mkdirAllCache.Put(parDirPath, newExpirableCacheValue(parent))
remoteFile := &File{
IsDir: true,
Name: last,
ModTime: time.Now(),
}
args := upsertOpt{
parentId: parent.Id,
src: remoteFile,
debug: g.opts.Verbose && g.opts.canPreview(),
retryCount: g.opts.ExponentialBackoffRetryCount,
}
cur, curErr := g.rem.UpsertByComparison(&args)
if curErr != nil {
return cur, curErr
}
if cur == nil {
return cur, ErrPathNotExists
}
index := cur.ToIndex()
wErr := g.context.SerializeIndex(index)
// TODO: Should indexing errors be reported?
if wErr != nil {
g.log.LogErrf("serializeIndex %s: %v\n", cur.Name, wErr)
}
g.mkdirAllCache.Put(d, newExpirableCacheValue(cur))
return cur, nil
}
func namedPipe(mode os.FileMode) bool {
return (mode & os.ModeNamedPipe) != 0
}
func symlink(mode os.FileMode) bool {
return (mode & os.ModeSymlink) != 0
}