mirror of
https://github.com/octoleo/restic.git
synced 2024-11-02 19:49:44 +00:00
Merge pull request #865 from restic/handle-empty-snapshots
Refuse to create empty snapshots
This commit is contained in:
commit
f5a55a81f7
@ -734,6 +734,21 @@ func (arch *Archiver) Snapshot(p *restic.Progress, paths, tags []string, hostnam
|
|||||||
return nil, restic.ID{}, err
|
return nil, restic.ID{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// receive the top-level tree
|
||||||
|
root := (<-resCh).(*restic.Node)
|
||||||
|
debug.Log("root node received: %v", root.Subtree.Str())
|
||||||
|
sn.Tree = root.Subtree
|
||||||
|
|
||||||
|
// load top-level tree again to see if it is empty
|
||||||
|
toptree, err := arch.repo.LoadTree(*root.Subtree)
|
||||||
|
if err != nil {
|
||||||
|
return nil, restic.ID{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(toptree.Nodes) == 0 {
|
||||||
|
return nil, restic.ID{}, errors.Fatal("no files/dirs saved, refusing to create empty snapshot")
|
||||||
|
}
|
||||||
|
|
||||||
// save index
|
// save index
|
||||||
err = arch.repo.SaveIndex()
|
err = arch.repo.SaveIndex()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -743,11 +758,6 @@ func (arch *Archiver) Snapshot(p *restic.Progress, paths, tags []string, hostnam
|
|||||||
|
|
||||||
debug.Log("saved indexes")
|
debug.Log("saved indexes")
|
||||||
|
|
||||||
// receive the top-level tree
|
|
||||||
root := (<-resCh).(*restic.Node)
|
|
||||||
debug.Log("root node received: %v", root.Subtree.Str())
|
|
||||||
sn.Tree = root.Subtree
|
|
||||||
|
|
||||||
// save snapshot
|
// save snapshot
|
||||||
id, err := arch.repo.SaveJSONUnpacked(restic.SnapshotFile, sn)
|
id, err := arch.repo.SaveJSONUnpacked(restic.SnapshotFile, sn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -294,3 +294,23 @@ func assertNoUnreferencedPacks(t *testing.T, chkr *checker.Checker) {
|
|||||||
OK(t, err)
|
OK(t, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestArchiveEmptySnapshot(t *testing.T) {
|
||||||
|
repo, cleanup := repository.TestRepository(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
arch := archiver.New(repo)
|
||||||
|
|
||||||
|
sn, id, err := arch.Snapshot(nil, []string{"file-does-not-exist-123123213123", "file2-does-not-exist-too-123123123"}, nil, "localhost", nil)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected error for empty snapshot, got nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !id.IsNull() {
|
||||||
|
t.Errorf("expected null ID for empty snapshot, got %v", id.Str())
|
||||||
|
}
|
||||||
|
|
||||||
|
if sn != nil {
|
||||||
|
t.Errorf("expected null snapshot for empty snapshot, got %v", sn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user