From 43d48aa979a1e316af5ba0445412fe8926ce983b Mon Sep 17 00:00:00 2001 From: Keuin Date: Mon, 25 Jan 2021 02:07:54 +0800 Subject: Perform clean-up after deleting a backup. --- .../manager/IncrementalBackupStorageManager.java | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric/backup/incremental/manager') diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index ad7287f..0a99bf0 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; import static org.apache.commons.io.FileUtils.forceDelete; @@ -76,32 +77,38 @@ public class IncrementalBackupStorageManager { /** * Delete all files in the specific collection, from the storage base. * - * @param collection the collection containing files to be deleted. - * @param collectionBasePath the collection base path. + * @param collection the collection containing files to be deleted. + * @return files deleted * @throws IOException I/O error. */ - public void deleteObjectCollection(ObjectCollection2 collection, File collectionBasePath) throws IOException { - deleteObjectCollection(collection, collectionBasePath, Collections.emptySet()); + public int deleteObjectCollection(ObjectCollection2 collection) throws IOException { + return deleteObjectCollection(collection, Collections.emptySet()); } /** * Delete a collection from the storage base, optionally preserving files used by other backups. * * @param collection the collection containing files to be deleted. - * @param collectionBasePath the collection base path. * @param otherExistingCollections other collections (not to be deleted) in this base. Files exist in these collections will not be deleted. + * @return files deleted */ - public void deleteObjectCollection(ObjectCollection2 collection, File collectionBasePath, - Iterable otherExistingCollections) { + public int deleteObjectCollection(ObjectCollection2 collection, + Iterable otherExistingCollections) { Iterator iter = new ObjectCollectionIterator(collection); Set unusedElementSet = new HashSet<>(); iter.forEachRemaining(unusedElementSet::add); otherExistingCollections.forEach(col -> new ObjectCollectionIterator(col).forEachRemaining(unusedElementSet::remove)); + AtomicInteger deleteCount = new AtomicInteger(); unusedElementSet.forEach(ele -> { File file = new File(backupStorageBase.toFile(), ele.getIdentifier().getIdentification()); - if (!file.delete()) - LOGGER.warning("Failed to delete unused file " + file.getName()); + if (file.exists()) { + if (file.delete()) + deleteCount.incrementAndGet(); + else + LOGGER.warning("Failed to delete unused file " + file.getName()); + } }); + return deleteCount.get(); } /** -- cgit v1.2.3