diff options
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; + } + } |