diff --git a/src/main/java/net/szum123321/textile_backup/Statics.java b/src/main/java/net/szum123321/textile_backup/Statics.java index 97ec15c..61d3838 100644 --- a/src/main/java/net/szum123321/textile_backup/Statics.java +++ b/src/main/java/net/szum123321/textile_backup/Statics.java @@ -41,4 +41,5 @@ public class Statics { public static final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true); public static AwaitThread restoreAwaitThread; + public static File untouchableFile; } diff --git a/src/main/java/net/szum123321/textile_backup/core/Utilities.java b/src/main/java/net/szum123321/textile_backup/core/Utilities.java index 46fd1c0..ffd38df 100644 --- a/src/main/java/net/szum123321/textile_backup/core/Utilities.java +++ b/src/main/java/net/szum123321/textile_backup/core/Utilities.java @@ -151,6 +151,10 @@ public class Utilities { return path; } + public static boolean isValid(File f) { + return getFileExtension(f).isPresent() && getFileCreationTime(f).isPresent(); + } + public static DateTimeFormatter getDateTimeFormatter() { return DateTimeFormatter.ofPattern(Statics.CONFIG.dateTimeFormat); } diff --git a/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java b/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java index 79cd87e..3577796 100644 --- a/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java +++ b/src/main/java/net/szum123321/textile_backup/core/create/BackupHelper.java @@ -68,15 +68,11 @@ public class BackupHelper { Arrays.stream(root.listFiles()) .filter(BackupHelper::isFileOk) - .filter(f -> Utilities.getFileCreationTime(f).isPresent()) // We check if we can get file's creation date so that the next line won't throw an exception + .filter(Utilities::isValid)// We check if we can get file's creation date so that the next line won't throw an exception .filter(f -> now.toEpochSecond(ZoneOffset.UTC) - Utilities.getFileCreationTime(f).get().toEpochSecond(ZoneOffset.UTC) > Statics.CONFIG.maxAge) .forEach(f -> { - if(f.delete()) { - Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName()); + if(deleteFile(f, ctx)) deletedFiles.getAndIncrement(); - } else { - Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName()); - } }); } @@ -85,19 +81,13 @@ public class BackupHelper { Iterator it = Arrays.stream(root.listFiles()) .filter(BackupHelper::isFileOk) - .filter(f -> Utilities.getFileCreationTime(f).isPresent()) + .filter(Utilities::isValid) .sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get())) .iterator(); while(i > Statics.CONFIG.backupsToKeep && it.hasNext()) { - File f = it.next(); - - if(f.delete()) { - Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName()); + if(deleteFile(it.next(), ctx)) deletedFiles.getAndIncrement(); - } else { - Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName()); - } i--; } @@ -106,19 +96,13 @@ public class BackupHelper { if (Statics.CONFIG.maxSize > 0 && FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize) { Iterator it = Arrays.stream(root.listFiles()) .filter(BackupHelper::isFileOk) - .filter(f -> Utilities.getFileCreationTime(f).isPresent()) + .filter(Utilities::isValid) .sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get())) .iterator(); while(FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize && it.hasNext()) { - File f = it.next(); - - if(f.delete()) { - Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName()); + if(deleteFile(it.next(), ctx)) deletedFiles.getAndIncrement(); - } else { - Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName()); - } } } } @@ -126,5 +110,18 @@ public class BackupHelper { return deletedFiles.get(); } + private static boolean deleteFile(File f, ServerCommandSource ctx) { + if(f != Statics.untouchableFile) { + if(f.delete()) { + Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName()); + return true; + } else { + Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName()); + } + } + + return false; + } + private static boolean isFileOk(File f) {return f.exists() && f.isFile(); } } \ No newline at end of file