Skip to content

Commit

Permalink
Fix max_folder_size not working for smaller worlds (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts authored Jan 3, 2025
1 parent abb4b83 commit d38e849
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/serverutils/command/CmdBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CmdBackupGetSize(String s) {
public void processCommand(ICommandSender sender, String[] args) {
String sizeW = FileUtils.getSizeString(sender.getEntityWorld().getSaveHandler().getWorldDirectory());
String sizeT = FileUtils.getSizeString(BackupTask.BACKUP_FOLDER);
sender.addChatMessage(ServerUtilities.lang(sender, "cmd.backup_not_running", sizeW, sizeT));
sender.addChatMessage(ServerUtilities.lang(sender, "cmd.backup_size", sizeW, sizeT));
}
}
}
16 changes: 8 additions & 8 deletions src/main/java/serverutils/task/backup/BackupTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,22 @@ public static void clearOldBackups() {
file -> backups.delete_custom_name_backups || BACKUP_NAME_PATTERN.matcher(file.getName()).matches())
.sorted(Comparator.comparingLong(File::lastModified)).collect(Collectors.toList());

int maxGb = backups.max_folder_size;
if (maxGb > 0) {
long currentSize = backupFiles.stream().mapToLong(file -> FileUtils.getSize(file, SizeUnit.GB)).sum();
if (currentSize <= maxGb) return;
deleteOldBackups(backupFiles, currentSize, maxGb);
long maxSize = backups.max_folder_size * SizeUnit.GB.getSize();
if (maxSize > 0) {
long currentSize = backupFiles.stream().mapToLong(FileUtils::getSize).sum();
if (currentSize <= maxSize) return;
deleteOldBackups(backupFiles, currentSize, maxSize);

} else if (backupFiles.size() > backups.backups_to_keep) {
deleteExcessBackups(backupFiles);
}
}

private static void deleteOldBackups(List<File> backupFiles, long currentSize, int maxGb) {
private static void deleteOldBackups(List<File> backupFiles, long currentSize, long maxSize) {
int deleted = 0;
for (File file : backupFiles) {
if (currentSize <= maxGb) break;
currentSize -= FileUtils.getSize(file, SizeUnit.GB);
if (currentSize <= maxSize) break;
currentSize -= FileUtils.getSize(file);
ServerUtilities.LOGGER.info("Deleting old backup: {}", file.getPath());
FileUtils.delete(file);
deleted++;
Expand Down

0 comments on commit d38e849

Please # to comment.