2021-09-12 14:15:40 +00:00
|
|
|
package backup
|
2021-01-26 19:52:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/archiver"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
|
|
|
"github.com/restic/restic/internal/ui/signals"
|
|
|
|
)
|
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
// A ProgressPrinter can print various progress messages.
|
|
|
|
// It must be safe to call its methods from concurrent goroutines.
|
2021-01-26 19:52:00 +00:00
|
|
|
type ProgressPrinter interface {
|
|
|
|
Update(total, processed Counter, errors uint, currentFiles map[string]struct{}, start time.Time, secs uint64)
|
2022-05-20 22:31:26 +00:00
|
|
|
Error(item string, err error) error
|
|
|
|
ScannerError(item string, err error) error
|
2021-01-26 19:52:00 +00:00
|
|
|
CompleteItem(messageType string, item string, previous, current *restic.Node, s archiver.ItemStats, d time.Duration)
|
|
|
|
ReportTotal(item string, start time.Time, s archiver.ScanStats)
|
2021-08-18 11:03:08 +00:00
|
|
|
Finish(snapshotID restic.ID, start time.Time, summary *Summary, dryRun bool)
|
2021-01-26 19:52:00 +00:00
|
|
|
Reset()
|
|
|
|
|
|
|
|
// ui.StdioWrapper
|
|
|
|
Stdout() io.WriteCloser
|
|
|
|
Stderr() io.WriteCloser
|
|
|
|
|
|
|
|
P(msg string, args ...interface{})
|
|
|
|
V(msg string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Counter struct {
|
|
|
|
Files, Dirs, Bytes uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type Summary struct {
|
|
|
|
Files, Dirs struct {
|
|
|
|
New uint
|
|
|
|
Changed uint
|
|
|
|
Unchanged uint
|
|
|
|
}
|
|
|
|
ProcessedBytes uint64
|
|
|
|
archiver.ItemStats
|
|
|
|
}
|
|
|
|
|
2021-08-17 23:27:22 +00:00
|
|
|
// Progress reports progress for the `backup` command.
|
2021-01-26 19:52:00 +00:00
|
|
|
type Progress struct {
|
2022-10-21 11:38:10 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
|
2022-10-16 10:01:27 +00:00
|
|
|
interval time.Duration
|
|
|
|
start time.Time
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
scanStarted, scanFinished bool
|
|
|
|
|
|
|
|
currentFiles map[string]struct{}
|
|
|
|
processed, total Counter
|
|
|
|
errors uint
|
|
|
|
|
|
|
|
closed chan struct{}
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-16 10:01:27 +00:00
|
|
|
summary Summary
|
2021-01-26 19:52:00 +00:00
|
|
|
printer ProgressPrinter
|
|
|
|
}
|
|
|
|
|
2022-10-16 10:01:27 +00:00
|
|
|
func NewProgress(printer ProgressPrinter, interval time.Duration) *Progress {
|
2021-01-26 19:52:00 +00:00
|
|
|
return &Progress{
|
2022-10-16 10:01:27 +00:00
|
|
|
interval: interval,
|
|
|
|
start: time.Now(),
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
currentFiles: make(map[string]struct{}),
|
|
|
|
closed: make(chan struct{}),
|
2021-01-26 19:52:00 +00:00
|
|
|
|
|
|
|
printer: printer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 23:27:22 +00:00
|
|
|
// Run regularly updates the status lines. It should be called in a separate
|
|
|
|
// goroutine.
|
2022-10-16 10:01:27 +00:00
|
|
|
func (p *Progress) Run(ctx context.Context) {
|
2021-01-26 19:52:00 +00:00
|
|
|
defer close(p.closed)
|
|
|
|
// Reset status when finished
|
|
|
|
defer p.printer.Reset()
|
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
var tick <-chan time.Time
|
|
|
|
if p.interval != 0 {
|
|
|
|
t := time.NewTicker(p.interval)
|
|
|
|
defer t.Stop()
|
|
|
|
tick = t.C
|
|
|
|
}
|
|
|
|
|
|
|
|
signalsCh := signals.GetProgressChannel()
|
|
|
|
|
2021-01-26 19:52:00 +00:00
|
|
|
for {
|
2022-10-21 11:38:10 +00:00
|
|
|
var now time.Time
|
2021-01-26 19:52:00 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-10-16 10:01:27 +00:00
|
|
|
return
|
2022-10-21 11:38:10 +00:00
|
|
|
case now = <-tick:
|
2021-01-26 19:52:00 +00:00
|
|
|
case <-signalsCh:
|
2022-10-21 11:38:10 +00:00
|
|
|
now = time.Now()
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2022-11-02 20:26:15 +00:00
|
|
|
if !p.scanStarted {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
var secondsRemaining uint64
|
|
|
|
if p.scanFinished {
|
|
|
|
secs := float64(now.Sub(p.start) / time.Second)
|
|
|
|
todo := float64(p.total.Bytes - p.processed.Bytes)
|
|
|
|
secondsRemaining = uint64(secs / float64(p.processed.Bytes) * todo)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.printer.Update(p.total, p.processed, p.errors, p.currentFiles, p.start, secondsRemaining)
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error is the error callback function for the archiver, it prints the error and returns nil.
|
2022-05-20 22:31:26 +00:00
|
|
|
func (p *Progress) Error(item string, err error) error {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.errors++
|
|
|
|
p.scanStarted = true
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
return p.printer.Error(item, err)
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartFile is called when a file is being processed by a worker.
|
|
|
|
func (p *Progress) StartFile(filename string) {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
p.currentFiles[filename] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Progress) addProcessed(c Counter) {
|
|
|
|
p.processed.Files += c.Files
|
|
|
|
p.processed.Dirs += c.Dirs
|
|
|
|
p.processed.Bytes += c.Bytes
|
|
|
|
p.scanStarted = true
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteBlob is called for all saved blobs for files.
|
2022-10-15 13:21:17 +00:00
|
|
|
func (p *Progress) CompleteBlob(bytes uint64) {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.addProcessed(Counter{Bytes: bytes})
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteItem is the status callback function for the archiver when a
|
|
|
|
// file/dir has been saved successfully.
|
|
|
|
func (p *Progress) CompleteItem(item string, previous, current *restic.Node, s archiver.ItemStats, d time.Duration) {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2021-01-26 19:52:00 +00:00
|
|
|
p.summary.ItemStats.Add(s)
|
|
|
|
|
|
|
|
// for the last item "/", current is nil
|
|
|
|
if current != nil {
|
|
|
|
p.summary.ProcessedBytes += current.Size
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
|
|
|
if current == nil {
|
|
|
|
// error occurred, tell the status display to remove the line
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
delete(p.currentFiles, item)
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch current.Type {
|
|
|
|
case "dir":
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.addProcessed(Counter{Dirs: 1})
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
switch {
|
|
|
|
case previous == nil:
|
2021-01-26 19:52:00 +00:00
|
|
|
p.printer.CompleteItem("dir new", item, previous, current, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2021-01-26 19:52:00 +00:00
|
|
|
p.summary.Dirs.New++
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
case previous.Equals(*current):
|
2021-01-26 19:52:00 +00:00
|
|
|
p.printer.CompleteItem("dir unchanged", item, previous, current, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2021-01-26 19:52:00 +00:00
|
|
|
p.summary.Dirs.Unchanged++
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
default:
|
2021-01-26 19:52:00 +00:00
|
|
|
p.printer.CompleteItem("dir modified", item, previous, current, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2021-01-26 19:52:00 +00:00
|
|
|
p.summary.Dirs.Changed++
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-16 10:01:27 +00:00
|
|
|
case "file":
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
p.addProcessed(Counter{Files: 1})
|
|
|
|
delete(p.currentFiles, item)
|
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
switch {
|
|
|
|
case previous == nil:
|
2021-01-26 19:52:00 +00:00
|
|
|
p.printer.CompleteItem("file new", item, previous, current, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2021-01-26 19:52:00 +00:00
|
|
|
p.summary.Files.New++
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
|
2022-10-21 11:38:10 +00:00
|
|
|
case previous.Equals(*current):
|
2021-01-26 19:52:00 +00:00
|
|
|
p.printer.CompleteItem("file unchanged", item, previous, current, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2021-01-26 19:52:00 +00:00
|
|
|
p.summary.Files.Unchanged++
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
default:
|
2021-01-26 19:52:00 +00:00
|
|
|
p.printer.CompleteItem("file modified", item, previous, current, s, d)
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
2021-01-26 19:52:00 +00:00
|
|
|
p.summary.Files.Changed++
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Unlock()
|
2021-01-26 19:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReportTotal sets the total stats up to now
|
|
|
|
func (p *Progress) ReportTotal(item string, s archiver.ScanStats) {
|
2022-10-21 11:38:10 +00:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
p.total = Counter{Files: uint64(s.Files), Dirs: uint64(s.Dirs), Bytes: s.Bytes}
|
2022-11-02 20:26:15 +00:00
|
|
|
p.scanStarted = true
|
2021-01-26 19:52:00 +00:00
|
|
|
|
|
|
|
if item == "" {
|
|
|
|
p.printer.ReportTotal(item, p.start, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish prints the finishing messages.
|
2022-10-16 10:01:27 +00:00
|
|
|
func (p *Progress) Finish(snapshotID restic.ID, dryrun bool) {
|
2021-08-17 23:27:22 +00:00
|
|
|
// wait for the status update goroutine to shut down
|
2021-01-26 19:52:00 +00:00
|
|
|
<-p.closed
|
2022-10-16 10:01:27 +00:00
|
|
|
p.printer.Finish(snapshotID, p.start, &p.summary, dryrun)
|
2021-08-18 11:03:08 +00:00
|
|
|
}
|