diff --git a/src/duplicacy_snapshotmanager.go b/src/duplicacy_snapshotmanager.go index 0f873be..553a1fc 100644 --- a/src/duplicacy_snapshotmanager.go +++ b/src/duplicacy_snapshotmanager.go @@ -2184,13 +2184,9 @@ func (manager *SnapshotManager) pruneSnapshotsExhaustive(referencedFossils map[s if _, found := referencedChunks[chunk]; found { manager.resurrectChunk(chunkDir+file, chunk) } else { - err := manager.storage.DeleteFile(0, chunkDir+file) - if err != nil { - LOG_WARN("FOSSIL_DELETE", "Failed to remove the unreferenced fossil %s: %v", file, err) - } else { - LOG_DEBUG("FOSSIL_DELETE", "Deleted unreferenced fossil %s", file) - fmt.Fprintf(logFile, "Deleted unreferenced fossil %s\n", file) - } + collection.AddFossil(chunkDir + file) + LOG_DEBUG("FOSSIL_FIND", "Found unreferenced fossil %s", file) + fmt.Fprintf(logFile, "Found unreferenced fossil %s\n", file) } } diff --git a/src/duplicacy_snapshotmanager_test.go b/src/duplicacy_snapshotmanager_test.go index 99309c4..d64965f 100644 --- a/src/duplicacy_snapshotmanager_test.go +++ b/src/duplicacy_snapshotmanager_test.go @@ -502,3 +502,36 @@ func TestPruneWithRetentionPolicyAndTag(t *testing.T) { snapshotManager.PruneSnapshots("vm1@host1", "vm1@host1", []int{}, []string{"manual"}, []string{"0:7"}, false, true, []string{}, false, false, false) checkTestSnapshots(snapshotManager, 22, 0) } + +// Test that an unreferenced fossil shouldn't be removed as it may be the result of another prune job in-progress. +func TestPruneWithFossils(t *testing.T) { + setTestingT(t) + + testDir := path.Join(os.TempDir(), "duplicacy_test", "snapshot_test") + + snapshotManager := createTestSnapshotManager(testDir) + + chunkSize := 1024 + chunkHash1 := uploadRandomChunk(snapshotManager, chunkSize) + chunkHash2 := uploadRandomChunk(snapshotManager, chunkSize) + chunkHash3 := uploadRandomChunk(snapshotManager, chunkSize) + // Create an unreferenced fossil + snapshotManager.storage.UploadFile(0, "chunks/113b6a2350dcfd836829c47304dd330fa6b58b93dd7ac696c6b7b913e6868662.fsl", []byte("this is a test fossil")) + + now := time.Now().Unix() + day := int64(24 * 3600) + t.Logf("Creating 2 snapshots") + createTestSnapshot(snapshotManager, "vm1@host1", 1, now-3*day-3600, now-3*day-60, []string{chunkHash1, chunkHash2}, "tag") + createTestSnapshot(snapshotManager, "vm1@host1", 2, now-2*day-3600, now-2*day-60, []string{chunkHash2, chunkHash3}, "tag") + checkTestSnapshots(snapshotManager, 2, 1) + + t.Logf("Prune without removing any snapshots but with --exhaustive") + // The unreferenced fossil shouldn't be removed + snapshotManager.PruneSnapshots("vm1@host1", "vm1@host1", []int{}, []string{}, []string{}, true, false, []string{}, false, false, false) + checkTestSnapshots(snapshotManager, 2, 1) + + t.Logf("Prune without removing any snapshots but with --exclusive") + // Now the unreferenced fossil should be removed + snapshotManager.PruneSnapshots("vm1@host1", "vm1@host1", []int{}, []string{}, []string{}, false, true, []string{}, false, false, false) + checkTestSnapshots(snapshotManager, 2, 0) +}