From: "Enrico Weigelt, metux IT consult" <enrico.weig...@gr13.net>
--- src/net/sf/freecol/client/FreeColClient.java | 38 +++++++++++++---- .../sf/freecol/common/io/FreeColDirectories.java | 49 +++------------------- src/net/sf/freecol/common/util/Utils.java | 22 ++++------ 3 files changed, 44 insertions(+), 65 deletions(-) diff --git a/src/net/sf/freecol/client/FreeColClient.java b/src/net/sf/freecol/client/FreeColClient.java index 6b295df85bf..6463aead100 100644 --- a/src/net/sf/freecol/client/FreeColClient.java +++ b/src/net/sf/freecol/client/FreeColClient.java @@ -64,6 +64,7 @@ import net.sf.freecol.common.networking.MessageHandler; import net.sf.freecol.common.networking.ServerAPI; import net.sf.freecol.common.resources.ResourceManager; import net.sf.freecol.common.resources.ResourceMapping; +import net.sf.freecol.common.util.Utils; import static net.sf.freecol.common.util.CollectionUtils.*; import net.sf.freecol.server.FreeColServer; import net.sf.freecol.server.FreeColServer.ServerState; @@ -993,19 +994,40 @@ public final class FreeColClient { } /** + * Remove out of date autosaves. This only removed generic autosaves, not + * the last-turn autosave, which can be useful for continuing the game on + * the next play-session. + */ + public void removeOutdatedAutosaves() { + final ClientOptions co = getClientOptions(); + final int validDays = co.getInteger(ClientOptions.AUTOSAVE_VALIDITY); + if (validDays <= 0L) return; + final long validMS = 1000L * 24L * 60L * 60L * validDays; // days to ms + final long timeNow = System.currentTimeMillis(); + final String prefix = co.getText(ClientOptions.AUTO_SAVE_PREFIX); + final String last_suffix = co.getText(ClientOptions.LAST_TURN_NAME) + + FreeColDirectories.FREECOL_SAVE_SUFFIX; + final String prev_suffix = co.getText(ClientOptions.BEFORE_LAST_TURN_NAME) + + FreeColDirectories.FREECOL_SAVE_SUFFIX; + final File asd = FreeColDirectories.getAutosaveDirectory(); + + for (String n : asd.list()) { + // skip last and previous savegame + if (n.startsWith(prefix) && !n.endsWith(last_suffix) && !n.endsWith(prev_suffix)) { + File f = new File(asd, n); + if (f.lastModified() + validMS < timeNow) + Utils.deleteFile(f); + } + } + } + + /** * Quits the application without any questions. */ public void quit() { stopServer(); - final ClientOptions co = getClientOptions(); - List<String> excludeSuffixes = new ArrayList<>(2); - excludeSuffixes.add(co.getText(ClientOptions.LAST_TURN_NAME)); - excludeSuffixes.add(co.getText(ClientOptions.BEFORE_LAST_TURN_NAME)); - FreeColDirectories.removeOutdatedAutosaves( - co.getText(ClientOptions.AUTO_SAVE_PREFIX), - excludeSuffixes, - co.getInteger(ClientOptions.AUTOSAVE_VALIDITY)); + removeOutdatedAutosaves(); // Exit int ret = 0; diff --git a/src/net/sf/freecol/common/io/FreeColDirectories.java b/src/net/sf/freecol/common/io/FreeColDirectories.java index 7fe01ca17f4..901d06c5fe6 100644 --- a/src/net/sf/freecol/common/io/FreeColDirectories.java +++ b/src/net/sf/freecol/common/io/FreeColDirectories.java @@ -709,49 +709,7 @@ public class FreeColDirectories { return new File(getAutosaveDirectory(), name); } - /** - * Get the autosave files. - * - * @param prefix The autosave file prefix. - * @param pred A {@code Predicate} to select files with. - * @return A list of of autosaved {@code File}s. - */ - private static List<File> getAutosaveFiles(String prefix, - Predicate<File> pred) { - final String suffix = "." + FreeCol.FREECOL_SAVE_EXTENSION; - final File asd = getAutosaveDirectory(); - final Predicate<File> fullPred = pred.and(f -> - f.getName().startsWith(prefix) && f.getName().endsWith(suffix)); - return (asd == null) ? Collections.emptyList() - : collectFiles(asd, fullPred); - } - - /** - * Remove out of date autosaves. This only removed generic autosaves, not - * the last-turn autosave, which can be useful for continuing the game on - * the next play-session. - * - * @param prefix The autosave file prefix. - * @param excludeSuffixes Only files not ending with any of these prefixes - * will be removed. - * @param validDays Only files older than this amount of days will be removed. - */ - public static void removeOutdatedAutosaves(String prefix, - List<String> excludeSuffixes, - long validDays) { - if (validDays <= 0L) return; - final long validMS = 1000L * 24L * 60L * 60L * validDays; // days to ms - final long timeNow = System.currentTimeMillis(); - final Predicate<File> outdatedPred = f -> - f.lastModified() + validMS < timeNow; - - final String extension = "." + FreeCol.FREECOL_SAVE_EXTENSION; - final Predicate<File> suffixPred = f -> - excludeSuffixes.stream().noneMatch( - excludeSuffix -> f.getName().endsWith(excludeSuffix + extension)); - - Utils.deleteFiles(getAutosaveFiles(prefix, outdatedPred.and(suffixPred))); - } + public static final String FREECOL_SAVE_SUFFIX = "." + FreeCol.FREECOL_SAVE_EXTENSION; /** * Remove all autosave files. @@ -759,7 +717,10 @@ public class FreeColDirectories { * @param prefix The autosave file prefix. */ public static void removeAutosaves(String prefix) { - Utils.deleteFiles(getAutosaveFiles(prefix, alwaysTrue())); + File asd = getAutosaveDirectory(); + for (String n : asd.list()) + if (n.startsWith(prefix)) + Utils.deleteFile(new File(asd, n)); } /** diff --git a/src/net/sf/freecol/common/util/Utils.java b/src/net/sf/freecol/common/util/Utils.java index 28d0dcfa98b..d2290e9dc1a 100644 --- a/src/net/sf/freecol/common/util/Utils.java +++ b/src/net/sf/freecol/common/util/Utils.java @@ -36,7 +36,6 @@ import java.io.UnsupportedEncodingException; import java.io.Writer; import java.nio.CharBuffer; -import java.util.List; import java.util.Random; import java.util.logging.Level; import java.util.logging.Logger; @@ -293,20 +292,17 @@ public class Utils { } /** - * Delete a list of files. + * Delete a file. * - * @param files The list of {@code File}s to delete. + * @param files The {@code File}s to delete. */ - public static void deleteFiles(List<File> files) { - for (File f : files) { - try { - if (!f.delete()) { - logger.warning("Failed to delete: " + f.getPath()); - } - } catch (SecurityException ex) { - logger.log(Level.WARNING, "Exception deleting: " - + f.getPath(), ex); - } + public static void deleteFile(File f) { + try { + if (!f.delete()) + logger.warning("Failed to delete: " + f.getPath()); + } catch (SecurityException ex) { + logger.log(Level.WARNING, "Exception deleting: " + + f.getPath(), ex); } } -- 2.11.0.rc0.7.gbe5a750 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ Freecol-developers mailing list Freecol-developers@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/freecol-developers