diff --git a/changelog/unreleased/issue-22085 b/changelog/unreleased/issue-22085 new file mode 100644 index 000000000..bdf2a0963 --- /dev/null +++ b/changelog/unreleased/issue-22085 @@ -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 diff --git a/internal/restorer/restorer.go b/internal/restorer/restorer.go index 99119b747..ac8509e6f 100644 --- a/internal/restorer/restorer.go +++ b/internal/restorer/restorer.go @@ -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 } diff --git a/internal/restorer/restorer_unix_test.go b/internal/restorer/restorer_unix_test.go index 641b89aaf..d7435520b 100644 --- a/internal/restorer/restorer_unix_test.go +++ b/internal/restorer/restorer_unix_test.go @@ -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)) + } +}