()
| 560 | } |
| 561 | |
| 562 | func (b *backgroundWriter) run() { |
| 563 | state := 0 |
| 564 | for { |
| 565 | b.mu.Lock() |
| 566 | b.running = true |
| 567 | b.mu.Unlock() |
| 568 | select { |
| 569 | case s := <-b.stateCh: |
| 570 | state = s |
| 571 | default: |
| 572 | // |
| 573 | } |
| 574 | switch state { |
| 575 | case 1: |
| 576 | runtime.Gosched() |
| 577 | time.Sleep(time.Millisecond * 500) |
| 578 | continue |
| 579 | case 2: |
| 580 | return |
| 581 | } |
| 582 | |
| 583 | absPath, err := b.fs.cache.getPendingUpload(b.fs.Root(), time.Duration(b.fs.opt.TempWaitTime)) |
| 584 | if err != nil || absPath == "" || !b.fs.isRootInPath(absPath) { |
| 585 | time.Sleep(time.Second) |
| 586 | continue |
| 587 | } |
| 588 | |
| 589 | remote := b.fs.cleanRootFromPath(absPath) |
| 590 | b.notify(remote, BackgroundUploadStarted, nil) |
| 591 | fs.Infof(remote, "background upload: started upload") |
| 592 | err = operations.MoveFile(context.TODO(), b.fs.UnWrap(), b.fs.tempFs, remote, remote) |
| 593 | if err != nil { |
| 594 | b.notify(remote, BackgroundUploadError, err) |
| 595 | _ = b.fs.cache.rollbackPendingUpload(absPath) |
| 596 | fs.Errorf(remote, "background upload: %v", err) |
| 597 | continue |
| 598 | } |
| 599 | // clean empty dirs up to root |
| 600 | thisDir := cleanPath(path.Dir(remote)) |
| 601 | for thisDir != "" { |
| 602 | thisList, err := b.fs.tempFs.List(context.TODO(), thisDir) |
| 603 | if err != nil { |
| 604 | break |
| 605 | } |
| 606 | if len(thisList) > 0 { |
| 607 | break |
| 608 | } |
| 609 | err = b.fs.tempFs.Rmdir(context.TODO(), thisDir) |
| 610 | fs.Debugf(thisDir, "cleaned from temp path") |
| 611 | if err != nil { |
| 612 | break |
| 613 | } |
| 614 | thisDir = cleanPath(path.Dir(thisDir)) |
| 615 | } |
| 616 | fs.Infof(remote, "background upload: uploaded entry") |
| 617 | err = b.fs.cache.removePendingUpload(absPath) |
| 618 | if err != nil && !strings.Contains(err.Error(), "pending upload not found") { |
| 619 | fs.Errorf(remote, "background upload: %v", err) |
nothing calls this directly
no test coverage detected