restorer: rewrite partially matching hard-linked files in full (#22086)

This commit is contained in:
Younes Beriane authored and GitHub committed 2026-11-15 18:40:11 +00:00
1 parent 516c8d32e0
commit c4f9e50609
3 files changed
+70 -1

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
Bugfix: Restore partially matching hard-linked files correctly
When restoring over an existing file that had multiple hard links and whose
data blobs only partly matched the snapshot, restic replaced the file by
an empty one but still skipped writing the blobs it believed were already
present. The restored file then contained zero bytes in place of those
blobs. Restic now rewrites the whole file in this case.
https://github.com/restic/restic/issues/22085
https://github.com/restic/restic/pull/22086
+8 -1
View File
@@ -773,5 +773,12 @@ func (res *Restorer) verifyFile(ctx context.Context, target string, node *data.N
offset += int64(length)
}
return &fileState{matches, sizeMatches}, buf, nil
state := &fileState{matches, sizeMatches}
if state.NeedsRestore() && fs.ExtendedStat(fi).Links > 1 {
// createFile replaces files with multiple hard links by an empty file,
// so none of the existing blobs can be reused
state.blobMatches = make([]bool, len(node.Content))
}
return state, buf, nil
}
+52
View File
@@ -7,6 +7,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
@@ -136,3 +137,54 @@ func TestRestorePermissions(t *testing.T) {
rtest.Equals(t, fs.FileMode(0o600), fi.Mode().Perm(), "unexpected permissions")
}
}
func TestRestorerOverwriteHardlinkedPartial(t *testing.T) {
baseTime := time.Now().Add(-time.Hour)
newTime := time.Now()
parts := []string{"aaaa", "bbbb", "cccc", "dddd"}
changed := []string{"aaaa", "bbbb", "XXXX", "dddd"}
baseSnapshot := Snapshot{
Nodes: map[string]Node{
"foo": File{DataParts: parts, ModTime: baseTime},
},
}
overwriteSnapshot := Snapshot{
Nodes: map[string]Node{
"foo": File{DataParts: changed, ModTime: newTime},
},
}
repo := repository.TestRepository(t)
tempdir := filepath.Join(rtest.TempDir(t), "target")
ctx := t.Context()
sn, _ := saveSnapshot(t, repo, baseSnapshot, noopGetGenericAttributes)
_, err := NewRestorer(repo, sn, Options{}).RestoreTo(ctx, tempdir)
rtest.OK(t, err)
// add a second hard link to the restored file, like `cp -al` does
target := filepath.Join(tempdir, "foo")
linked := filepath.Join(rtest.TempDir(t), "linked")
rtest.OK(t, os.Link(target, linked))
sn, _ = saveSnapshot(t, repo, overwriteSnapshot, noopGetGenericAttributes)
for _, overwrite := range []OverwriteBehavior{OverwriteIfChanged, OverwriteAlways} {
_, err = NewRestorer(repo, sn, Options{Overwrite: overwrite}).RestoreTo(ctx, tempdir)
rtest.OK(t, err)
buf, err := os.ReadFile(target)
rtest.OK(t, err)
rtest.Equals(t, strings.Join(changed, ""), string(buf))
// the other hard link must be left untouched
buf, err = os.ReadFile(linked)
rtest.OK(t, err)
rtest.Equals(t, strings.Join(parts, ""), string(buf))
// restore the old file content and link again for the next round
rtest.OK(t, os.Remove(target))
rtest.OK(t, os.Link(linked, target))
}
}