diff options
author | Keuin <[email protected]> | 2021-01-13 17:47:20 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-13 17:47:20 +0800 |
commit | e0c6a21fe9bfb01237fd145064f0af309879a9fb (patch) | |
tree | 321955f96030213c2bc8c3ec350961a81b60edee /src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java | |
parent | 2bc659d8f95a97d0514491e48ed9c66828a4e308 (diff) |
Incremental backup now works (tested, but not thoroughly)
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java index 7f74725..f245cff 100644 --- a/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java +++ b/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java @@ -1,6 +1,9 @@ package com.keuin.kbackupfabric.util; import java.io.File; +import java.io.IOException; + +import static org.apache.commons.io.FileUtils.forceDelete; public class FilesystemUtil { @@ -22,12 +25,43 @@ public class FilesystemUtil { public static long getFileSizeBytes(String filePath) { long fileSize = -1; - try{ + try { File backupZipFile = new File(filePath); fileSize = backupZipFile.length(); - } catch (SecurityException ignored){ + } catch (SecurityException ignored) { } return fileSize; } + public static boolean forceDeleteDirectory(File levelDirFile) throws IOException { + int failedCounter = 0; + final int MAX_RETRY_TIMES = 20; + IOException exception = null; + while (failedCounter < MAX_RETRY_TIMES) { + System.gc(); + if (!levelDirFile.delete() && levelDirFile.exists()) { + System.gc(); + try { + forceDelete(levelDirFile); // Try to force delete. + } catch (IOException e) { + exception = e; + } + } + if (!levelDirFile.exists()) + break; + ++failedCounter; + try { + Thread.sleep(500); + } catch (InterruptedException ignored) { + } + } + if (exception != null) + throw exception; + if (levelDirFile.exists()) { + PrintUtil.error(String.format("Cannot restore: failed to delete old level %s .", levelDirFile.getName())); + return false; + } + return true; + } + } |