From 4835d0f37daf2ac2f2768a92ff6f5ad17ca6dd07 Mon Sep 17 00:00:00 2001 From: taoneill Date: Wed, 20 Jun 2012 22:56:08 -0400 Subject: [PATCH] Startup warning when War files take over 100MB Temp files tend to accumulate rapidly if some zonemakers are particularly trigger happy with /savezone. Added a warning. Should probably add a real limit on number of oldversions that can be saved. --- war/src/main/java/com/tommytony/war/War.java | 9 +++++ .../tommytony/war/utility/SizeCounter.java | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 war/src/main/java/com/tommytony/war/utility/SizeCounter.java diff --git a/war/src/main/java/com/tommytony/war/War.java b/war/src/main/java/com/tommytony/war/War.java index a607266..3255c51 100644 --- a/war/src/main/java/com/tommytony/war/War.java +++ b/war/src/main/java/com/tommytony/war/War.java @@ -52,6 +52,7 @@ import com.tommytony.war.structure.HubLobbyMaterials; import com.tommytony.war.structure.ZoneLobby; import com.tommytony.war.utility.ChatFixUtil; import com.tommytony.war.utility.PlayerState; +import com.tommytony.war.utility.SizeCounter; import com.tommytony.war.utility.WarLogFormatter; /** @@ -264,6 +265,14 @@ public class War extends JavaPlugin { } catch (IOException e) { this.getLogger().log(Level.WARNING, "Failed to create War log file"); } + + // Size check + long datSize = SizeCounter.getFileOrDirectorySize(new File(this.getDataFolder() + "/dat/")) / 1024 / 1024; + long tempSize = SizeCounter.getFileOrDirectorySize(new File(this.getDataFolder() + "/temp/")) / 1024 / 1024; + + if (datSize + tempSize > 100) { + this.log("War data files are taking " + datSize + "MB and its temp files " + tempSize + "MB. Consider permanently deleting old warzone versions and backups in /plugins/War/temp/.", Level.WARNING); + } this.log("War v" + this.desc.getVersion() + " is on.", Level.INFO); } diff --git a/war/src/main/java/com/tommytony/war/utility/SizeCounter.java b/war/src/main/java/com/tommytony/war/utility/SizeCounter.java new file mode 100644 index 0000000..b2f1337 --- /dev/null +++ b/war/src/main/java/com/tommytony/war/utility/SizeCounter.java @@ -0,0 +1,33 @@ +package com.tommytony.war.utility; + +import java.io.File; +import java.io.FileFilter; + +// Credit: http://sanjaal.com/java/48/java-utilities/calculating-folder-size/ +public class SizeCounter implements FileFilter +{ + private long total = 0; + + public SizeCounter(){}; + + public boolean accept(File pathname) { + if (pathname.isFile()) { + total += pathname.length(); + } else { + pathname.listFiles(this); + } + + return false; + } + + public long getTotal() + { + return total; + } + + public static long getFileOrDirectorySize(File file) { + SizeCounter counter = new SizeCounter(); + file.listFiles(counter); + return counter.getTotal(); + } +} \ No newline at end of file