Added isValid method to Utils BackupHelper cleanup method uses now separated delete method (for logging)

delete method checks if file isn't going to be restored
This commit is contained in:
szymon 2020-08-07 00:03:38 +02:00
parent 5ed0511e7f
commit c43ae5b19c
3 changed files with 24 additions and 22 deletions

View File

@ -41,4 +41,5 @@ public class Statics {
public static final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true); public static final AtomicBoolean globalShutdownBackupFlag = new AtomicBoolean(true);
public static AwaitThread restoreAwaitThread; public static AwaitThread restoreAwaitThread;
public static File untouchableFile;
} }

View File

@ -151,6 +151,10 @@ public class Utilities {
return path; return path;
} }
public static boolean isValid(File f) {
return getFileExtension(f).isPresent() && getFileCreationTime(f).isPresent();
}
public static DateTimeFormatter getDateTimeFormatter() { public static DateTimeFormatter getDateTimeFormatter() {
return DateTimeFormatter.ofPattern(Statics.CONFIG.dateTimeFormat); return DateTimeFormatter.ofPattern(Statics.CONFIG.dateTimeFormat);
} }

View File

@ -68,15 +68,11 @@ public class BackupHelper {
Arrays.stream(root.listFiles()) Arrays.stream(root.listFiles())
.filter(BackupHelper::isFileOk) .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) .filter(f -> now.toEpochSecond(ZoneOffset.UTC) - Utilities.getFileCreationTime(f).get().toEpochSecond(ZoneOffset.UTC) > Statics.CONFIG.maxAge)
.forEach(f -> { .forEach(f -> {
if(f.delete()) { if(deleteFile(f, ctx))
Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName());
deletedFiles.getAndIncrement(); deletedFiles.getAndIncrement();
} else {
Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName());
}
}); });
} }
@ -85,19 +81,13 @@ public class BackupHelper {
Iterator<File> it = Arrays.stream(root.listFiles()) Iterator<File> it = Arrays.stream(root.listFiles())
.filter(BackupHelper::isFileOk) .filter(BackupHelper::isFileOk)
.filter(f -> Utilities.getFileCreationTime(f).isPresent()) .filter(Utilities::isValid)
.sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get())) .sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get()))
.iterator(); .iterator();
while(i > Statics.CONFIG.backupsToKeep && it.hasNext()) { while(i > Statics.CONFIG.backupsToKeep && it.hasNext()) {
File f = it.next(); if(deleteFile(it.next(), ctx))
if(f.delete()) {
Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName());
deletedFiles.getAndIncrement(); deletedFiles.getAndIncrement();
} else {
Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName());
}
i--; i--;
} }
@ -106,19 +96,13 @@ public class BackupHelper {
if (Statics.CONFIG.maxSize > 0 && FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize) { if (Statics.CONFIG.maxSize > 0 && FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize) {
Iterator<File> it = Arrays.stream(root.listFiles()) Iterator<File> it = Arrays.stream(root.listFiles())
.filter(BackupHelper::isFileOk) .filter(BackupHelper::isFileOk)
.filter(f -> Utilities.getFileCreationTime(f).isPresent()) .filter(Utilities::isValid)
.sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get())) .sorted(Comparator.comparing(f -> Utilities.getFileCreationTime(f).get()))
.iterator(); .iterator();
while(FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize && it.hasNext()) { while(FileUtils.sizeOfDirectory(root) / 1024 > Statics.CONFIG.maxSize && it.hasNext()) {
File f = it.next(); if(deleteFile(it.next(), ctx))
if(f.delete()) {
Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName());
deletedFiles.getAndIncrement(); deletedFiles.getAndIncrement();
} else {
Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName());
}
} }
} }
} }
@ -126,5 +110,18 @@ public class BackupHelper {
return deletedFiles.get(); 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(); } private static boolean isFileOk(File f) {return f.exists() && f.isFile(); }
} }