summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-21 01:50:08 +0800
committerKeuin <[email protected]>2021-01-21 01:50:08 +0800
commitac3b5e1476dedcefb723f19bd0fdd9a22fcb16e9 (patch)
tree75d0994f3f7aa37c3d240933f0f2d179d318e597 /src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java
parent82e3986045ac7eaca6aaa290fb2283fd6c6c901a (diff)
parent7a5297de3467b1069fdf5e4a1b2aaf510ca35663 (diff)
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/FilesystemUtil.java38
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;
+ }
+
}