2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-26 04:17:29 +00:00

Merge pull request #4918 from MichaelEischer/restore-dont-delete-target-if-file

restore: do not delete target if it is a file
This commit is contained in:
Michael Eischer 2024-07-12 21:05:04 +02:00 committed by GitHub
commit 6010c98b3f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 0 deletions

View File

@ -347,6 +347,14 @@ func (res *Restorer) RestoreTo(ctx context.Context, dst string) error {
}
}
if !res.opts.DryRun {
// ensure that the target directory exists and is actually a directory
// Using ensureDir is too aggressive here as it also removes unexpected files
if err := fs.MkdirAll(dst, 0700); err != nil {
return fmt.Errorf("cannot create target directory: %w", err)
}
}
idx := NewHardlinkIndex[string]()
filerestorer := newFileRestorer(dst, res.repo.LoadBlobsFromPack, res.repo.LookupBlob,
res.repo.Connections(), res.opts.Sparse, res.opts.Delete, res.opts.Progress)

View File

@ -1372,3 +1372,25 @@ func TestRestoreDelete(t *testing.T) {
})
}
}
func TestRestoreToFile(t *testing.T) {
snapshot := Snapshot{
Nodes: map[string]Node{
"foo": File{Data: "content: foo\n"},
},
}
repo := repository.TestRepository(t)
tempdir := filepath.Join(rtest.TempDir(t), "target")
// create a file in the place of the target directory
rtest.OK(t, os.WriteFile(tempdir, []byte{}, 0o700))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sn, _ := saveSnapshot(t, repo, snapshot, noopGetGenericAttributes)
res := NewRestorer(repo, sn, Options{})
err := res.RestoreTo(ctx, tempdir)
rtest.Assert(t, strings.Contains(err.Error(), "cannot create target directory"), "unexpected error %v", err)
}