restore: move nil pointer check into restoreui

This commit is contained in:
Michael Eischer 2024-05-31 11:07:53 +02:00
parent 30320a249a
commit 607daeed4f
3 changed files with 18 additions and 30 deletions

View File

@ -211,9 +211,7 @@ func (r *fileRestorer) restoreEmptyFileAt(location string) error {
return err return err
} }
if r.progress != nil { r.progress.AddProgress(location, 0, 0)
r.progress.AddProgress(location, 0, 0)
}
return nil return nil
} }
@ -361,11 +359,7 @@ func (r *fileRestorer) downloadBlobs(ctx context.Context, packID restic.ID,
createSize = file.size createSize = file.size
} }
writeErr := r.filesWriter.writeToFile(r.targetPath(file.location), blobData, offset, createSize, file.sparse) writeErr := r.filesWriter.writeToFile(r.targetPath(file.location), blobData, offset, createSize, file.sparse)
r.progress.AddProgress(file.location, uint64(len(blobData)), uint64(file.size))
if r.progress != nil {
r.progress.AddProgress(file.location, uint64(len(blobData)), uint64(file.size))
}
return writeErr return writeErr
} }
err := r.sanitizeError(file, writeToFile()) err := r.sanitizeError(file, writeToFile())

View File

@ -170,10 +170,7 @@ func (res *Restorer) restoreNodeTo(ctx context.Context, node *restic.Node, targe
return err return err
} }
if res.progress != nil { res.progress.AddProgress(location, 0, 0)
res.progress.AddProgress(location, 0, 0)
}
return res.restoreNodeMetadataTo(node, target, location) return res.restoreNodeMetadataTo(node, target, location)
} }
@ -195,9 +192,7 @@ func (res *Restorer) restoreHardlinkAt(node *restic.Node, target, path, location
return errors.WithStack(err) 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 // TODO investigate if hardlinks have separate metadata on any supported system
return res.restoreNodeMetadataTo(node, path, location) 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{ _, err = res.traverseTree(ctx, dst, string(filepath.Separator), *res.sn.Tree, treeVisitor{
enterDir: func(_ *restic.Node, target, location string) error { enterDir: func(_ *restic.Node, target, location string) error {
debug.Log("first pass, enterDir: mkdir %q, leaveDir should restore metadata", location) 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 // create dir with default permissions
// #leaveDir restores dir metadata after visiting all children // #leaveDir restores dir metadata after visiting all children
return fs.MkdirAll(target, 0700) return fs.MkdirAll(target, 0700)
@ -243,27 +236,20 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error {
} }
if node.Type != "file" { if node.Type != "file" {
if res.progress != nil { res.progress.AddFile(0)
res.progress.AddFile(0)
}
return nil return nil
} }
if node.Links > 1 { if node.Links > 1 {
if idx.Has(node.Inode, node.DeviceID) { if idx.Has(node.Inode, node.DeviceID) {
if res.progress != nil { // a hardlinked file does not increase the restore size
// a hardlinked file does not increase the restore size res.progress.AddFile(0)
res.progress.AddFile(0)
}
return nil return nil
} }
idx.Add(node.Inode, node.DeviceID, location) 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)) filerestorer.addFile(location, node.Content, int64(node.Size))
return nil 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 { leaveDir: func(node *restic.Node, target, location string) error {
err := res.restoreNodeMetadataTo(node, target, location) err := res.restoreNodeMetadataTo(node, target, location)
if err == nil && res.progress != nil { if err == nil {
res.progress.AddProgress(location, 0, 0) res.progress.AddProgress(location, 0, 0)
} }
return err return err

View File

@ -59,6 +59,10 @@ func (p *Progress) update(runtime time.Duration, final bool) {
// AddFile starts tracking a new file with the given size // AddFile starts tracking a new file with the given size
func (p *Progress) AddFile(size uint64) { func (p *Progress) AddFile(size uint64) {
if p == nil {
return
}
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()
@ -68,6 +72,10 @@ func (p *Progress) AddFile(size uint64) {
// AddProgress accumulates the number of bytes written for a file // AddProgress accumulates the number of bytes written for a file
func (p *Progress) AddProgress(name string, bytesWrittenPortion uint64, bytesTotal uint64) { func (p *Progress) AddProgress(name string, bytesWrittenPortion uint64, bytesTotal uint64) {
if p == nil {
return
}
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()