From 607daeed4ff670a3ab61b4fc8638e88f383d1ecc Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 31 May 2024 11:07:53 +0200 Subject: [PATCH] restore: move nil pointer check into restoreui --- internal/restorer/filerestorer.go | 10 ++-------- internal/restorer/restorer.go | 30 ++++++++---------------------- internal/ui/restore/progress.go | 8 ++++++++ 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/internal/restorer/filerestorer.go b/internal/restorer/filerestorer.go index f3a68c58a..49f5f7af8 100644 --- a/internal/restorer/filerestorer.go +++ b/internal/restorer/filerestorer.go @@ -211,9 +211,7 @@ func (r *fileRestorer) restoreEmptyFileAt(location string) error { return err } - if r.progress != nil { - r.progress.AddProgress(location, 0, 0) - } + r.progress.AddProgress(location, 0, 0) return nil } @@ -361,11 +359,7 @@ func (r *fileRestorer) downloadBlobs(ctx context.Context, packID restic.ID, createSize = file.size } writeErr := r.filesWriter.writeToFile(r.targetPath(file.location), blobData, offset, createSize, file.sparse) - - if r.progress != nil { - r.progress.AddProgress(file.location, uint64(len(blobData)), uint64(file.size)) - } - + r.progress.AddProgress(file.location, uint64(len(blobData)), uint64(file.size)) return writeErr } err := r.sanitizeError(file, writeToFile()) diff --git a/internal/restorer/restorer.go b/internal/restorer/restorer.go index f691c4cae..12ce84d5c 100644 --- a/internal/restorer/restorer.go +++ b/internal/restorer/restorer.go @@ -170,10 +170,7 @@ func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, targe return err } - if res.progress != nil { - res.progress.AddProgress(location, 0, 0) - } - + res.progress.AddProgress(location, 0, 0) return res.restoreNodeMetadataTo(node, target, location) } @@ -195,9 +192,7 @@ func (res *Restorer) restoreHardlinkAt(node *restic.Node, target, path, location return errors.WithStack(err) } - if res.progress != nil { - res.progress.AddProgress(location, 0, 0) - } + res.progress.AddProgress(location, 0, 0) // TODO investigate if hardlinks have separate metadata on any supported system return res.restoreNodeMetadataTo(node, path, location) @@ -225,9 +220,7 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error { _, err = res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{ enterDir: func(_ *restic.Node, target, location string) error { debug.Log("first pass, enterDir: mkdir %q, leaveDir should restore metadata", location) - if res.progress != nil { - res.progress.AddFile(0) - } + res.progress.AddFile(0) // create dir with default permissions // #leaveDir restores dir metadata after visiting all children return fs.MkdirAll(target, 0700) @@ -243,27 +236,20 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error { } if node.Type != "file" { - if res.progress != nil { - res.progress.AddFile(0) - } + res.progress.AddFile(0) return nil } if node.Links > 1 { if idx.Has(node.Inode, node.DeviceID) { - if res.progress != nil { - // a hardlinked file does not increase the restore size - res.progress.AddFile(0) - } + // a hardlinked file does not increase the restore size + res.progress.AddFile(0) return nil } idx.Add(node.Inode, node.DeviceID, location) } - if res.progress != nil { - res.progress.AddFile(node.Size) - } - + res.progress.AddFile(node.Size) filerestorer.addFile(location, node.Content, int64(node.Size)) return nil @@ -296,7 +282,7 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error { }, leaveDir: func(node *restic.Node, target, location string) error { err := res.restoreNodeMetadataTo(node, target, location) - if err == nil && res.progress != nil { + if err == nil { res.progress.AddProgress(location, 0, 0) } return err diff --git a/internal/ui/restore/progress.go b/internal/ui/restore/progress.go index f2bd5d38b..0e120b6a6 100644 --- a/internal/ui/restore/progress.go +++ b/internal/ui/restore/progress.go @@ -59,6 +59,10 @@ func (p *Progress) update(runtime time.Duration, final bool) { // AddFile starts tracking a new file with the given size func (p *Progress) AddFile(size uint64) { + if p == nil { + return + } + p.m.Lock() defer p.m.Unlock() @@ -68,6 +72,10 @@ func (p *Progress) AddFile(size uint64) { // AddProgress accumulates the number of bytes written for a file func (p *Progress) AddProgress(name string, bytesWrittenPortion uint64, bytesTotal uint64) { + if p == nil { + return + } + p.m.Lock() defer p.m.Unlock()