restorer: HardlinkIndex: improve API, combine Has() and Value()

This commit is contained in:
Ivan Shapovalov 2026-05-24 19:38:52 +03:00
parent feb2f22552
commit a9ef7a45c3
3 changed files with 11 additions and 7 deletions

View file

@ -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.
func (idx *HardlinkIndex[T]) Value(inode uint64, device uint64) T {
// 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.

View file

@ -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)

View file

@ -450,9 +450,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
}