2022-10-28 15:44:34 +00:00
|
|
|
package restore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/ui/progress"
|
|
|
|
)
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
type State struct {
|
|
|
|
FilesFinished uint64
|
|
|
|
FilesTotal uint64
|
2024-05-31 12:12:06 +00:00
|
|
|
FilesSkipped uint64
|
2024-05-31 11:43:57 +00:00
|
|
|
AllBytesWritten uint64
|
|
|
|
AllBytesTotal uint64
|
2024-05-31 12:12:06 +00:00
|
|
|
AllBytesSkipped uint64
|
2024-05-31 11:43:57 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:44:34 +00:00
|
|
|
type Progress struct {
|
|
|
|
updater progress.Updater
|
|
|
|
m sync.Mutex
|
|
|
|
|
|
|
|
progressInfoMap map[string]progressInfoEntry
|
2024-05-31 11:43:57 +00:00
|
|
|
s State
|
2022-10-28 15:44:34 +00:00
|
|
|
started time.Time
|
|
|
|
|
|
|
|
printer ProgressPrinter
|
|
|
|
}
|
|
|
|
|
|
|
|
type progressInfoEntry struct {
|
|
|
|
bytesWritten uint64
|
|
|
|
bytesTotal uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProgressPrinter interface {
|
2024-05-31 11:43:57 +00:00
|
|
|
Update(progress State, duration time.Duration)
|
2024-07-27 23:06:26 +00:00
|
|
|
Error(item string, err error) error
|
2024-05-31 18:38:51 +00:00
|
|
|
CompleteItem(action ItemAction, item string, size uint64)
|
2024-05-31 11:43:57 +00:00
|
|
|
Finish(progress State, duration time.Duration)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 18:38:51 +00:00
|
|
|
type ItemAction string
|
|
|
|
|
|
|
|
// Constants for the different CompleteItem actions.
|
|
|
|
const (
|
|
|
|
ActionDirRestored ItemAction = "dir restored"
|
|
|
|
ActionFileRestored ItemAction = "file restored"
|
|
|
|
ActionFileUpdated ItemAction = "file updated"
|
|
|
|
ActionFileUnchanged ItemAction = "file unchanged"
|
2024-07-20 10:46:28 +00:00
|
|
|
ActionOtherRestored ItemAction = "other restored"
|
2024-06-29 19:29:42 +00:00
|
|
|
ActionDeleted ItemAction = "deleted"
|
2024-05-31 18:38:51 +00:00
|
|
|
)
|
|
|
|
|
2022-10-28 15:44:34 +00:00
|
|
|
func NewProgress(printer ProgressPrinter, interval time.Duration) *Progress {
|
|
|
|
p := &Progress{
|
|
|
|
progressInfoMap: make(map[string]progressInfoEntry),
|
|
|
|
started: time.Now(),
|
|
|
|
printer: printer,
|
|
|
|
}
|
|
|
|
p.updater = *progress.NewUpdater(interval, p.update)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Progress) update(runtime time.Duration, final bool) {
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
if !final {
|
2024-05-31 11:43:57 +00:00
|
|
|
p.printer.Update(p.s, runtime)
|
2022-10-28 15:44:34 +00:00
|
|
|
} else {
|
2024-05-31 11:43:57 +00:00
|
|
|
p.printer.Finish(p.s, runtime)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddFile starts tracking a new file with the given size
|
|
|
|
func (p *Progress) AddFile(size uint64) {
|
2024-05-31 09:07:53 +00:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:44:34 +00:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
p.s.FilesTotal++
|
|
|
|
p.s.AllBytesTotal += size
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddProgress accumulates the number of bytes written for a file
|
2024-06-29 19:24:45 +00:00
|
|
|
func (p *Progress) AddProgress(name string, action ItemAction, bytesWrittenPortion uint64, bytesTotal uint64) {
|
2024-05-31 09:07:53 +00:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:44:34 +00:00
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
entry, exists := p.progressInfoMap[name]
|
|
|
|
if !exists {
|
|
|
|
entry.bytesTotal = bytesTotal
|
|
|
|
}
|
|
|
|
entry.bytesWritten += bytesWrittenPortion
|
|
|
|
p.progressInfoMap[name] = entry
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
p.s.AllBytesWritten += bytesWrittenPortion
|
2022-10-28 15:44:34 +00:00
|
|
|
if entry.bytesWritten == entry.bytesTotal {
|
|
|
|
delete(p.progressInfoMap, name)
|
2024-05-31 11:43:57 +00:00
|
|
|
p.s.FilesFinished++
|
2024-05-31 18:38:51 +00:00
|
|
|
|
|
|
|
p.printer.CompleteItem(action, name, bytesTotal)
|
2022-10-28 15:44:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 18:38:51 +00:00
|
|
|
func (p *Progress) AddSkippedFile(name string, size uint64) {
|
2024-05-31 12:12:06 +00:00
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
p.s.FilesSkipped++
|
|
|
|
p.s.AllBytesSkipped += size
|
2024-05-31 18:38:51 +00:00
|
|
|
|
|
|
|
p.printer.CompleteItem(ActionFileUnchanged, name, size)
|
2024-05-31 12:12:06 +00:00
|
|
|
}
|
|
|
|
|
2024-06-29 19:29:42 +00:00
|
|
|
func (p *Progress) ReportDeletedFile(name string) {
|
|
|
|
if p == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
p.printer.CompleteItem(ActionDeleted, name, 0)
|
|
|
|
}
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
func (p *Progress) Error(item string, err error) error {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p.m.Lock()
|
|
|
|
defer p.m.Unlock()
|
|
|
|
|
|
|
|
return p.printer.Error(item, err)
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:44:34 +00:00
|
|
|
func (p *Progress) Finish() {
|
|
|
|
p.updater.Done()
|
|
|
|
}
|