mirror of
https://github.com/restic/restic.git
synced 2025-03-09 00:00:02 +01:00
restorer: HardlinkIndex: improve API, combine Has() and Value()
This commit is contained in:
parent
051597b7b6
commit
98b51357b0
3 changed files with 10 additions and 6 deletions
|
@ -44,11 +44,13 @@ func (idx *HardlinkIndex[T]) Add(inode uint64, device uint64, value T) {
|
|||
}
|
||||
}
|
||||
|
||||
// Value looks up the associated data for a given inode.
|
||||
// Value looks up the associated data for a given inode, and returns that data
|
||||
// plus a flag indicating whether the inode exists in the index.
|
||||
func (idx *HardlinkIndex[T]) Value(inode uint64, device uint64) (T, bool) {
|
||||
idx.m.Lock()
|
||||
defer idx.m.Unlock()
|
||||
return idx.Index[HardlinkKey{inode, device}]
|
||||
v, ok := idx.Index[HardlinkKey{inode, device}]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// Remove removes an inode from the index.
|
||||
|
|
|
@ -15,10 +15,12 @@ func TestHardLinks(t *testing.T) {
|
|||
idx.Add(1, 2, "inode1-file1-on-device2")
|
||||
idx.Add(2, 3, "inode2-file2-on-device3")
|
||||
|
||||
sresult := idx.Value(1, 2)
|
||||
sresult, ok := idx.Value(1, 2)
|
||||
rtest.Equals(t, ok, true)
|
||||
rtest.Equals(t, sresult, "inode1-file1-on-device2")
|
||||
|
||||
sresult = idx.Value(2, 3)
|
||||
sresult, ok = idx.Value(2, 3)
|
||||
rtest.Equals(t, ok, true)
|
||||
rtest.Equals(t, sresult, "inode2-file2-on-device3")
|
||||
|
||||
bresult := idx.Has(1, 2)
|
||||
|
|
|
@ -445,9 +445,9 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) (uint64, error)
|
|||
return err
|
||||
}
|
||||
|
||||
if idx.Has(node.Inode, node.DeviceID) && idx.Value(node.Inode, node.DeviceID) != location {
|
||||
if orig, hasOrig := idx.Value(node.Inode, node.DeviceID); hasOrig && orig != location {
|
||||
_, err := res.withOverwriteCheck(ctx, node, target, location, true, nil, func(_ bool, _ *fileState) error {
|
||||
return res.restoreHardlinkAt(node, filerestorer.targetPath(idx.Value(node.Inode, node.DeviceID)), target, location)
|
||||
return res.restoreHardlinkAt(node, filerestorer.targetPath(orig), target, location)
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue