From: Ben Soares Date: Mon, 22 Oct 2018 16:03:15 +0000 (+0100) Subject: JAL-3141 Taken the backing up code out of FileChooser into its own class for more... X-Git-Tag: Release_2_11_0~17^2~97^2~52 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=5b4f0b77909420cfb2fe2a4b9b9724abdacf025d;p=jalview.git JAL-3141 Taken the backing up code out of FileChooser into its own class for more generic usage, and extra data safe-guarding functionality --- diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java new file mode 100644 index 0000000..761ab80 --- /dev/null +++ b/src/jalview/io/BackupFiles.java @@ -0,0 +1,202 @@ +package jalview.io; + +import jalview.bin.Cache; + +import java.io.File; +import java.io.IOException; + +public class BackupFiles +{ + + // labels for saved params in Cache and .jalview_properties + private static String NS = "BACKUPFILES"; + + public static String ENABLED = NS + "_ENABLED"; + + public static String SUFFIX = NS + "_SUFFIX"; + + public static String ROLL_MAX = NS + "_ROLL_MAX"; + + public static String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS"; + + public static String REVERSE_ORDER = NS + "_REVERSE_ORDER"; + + private static String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS; + + // file - File object to be backed up and then updated (written over) + private File file; + + // enabled - default flag as to whether to do the backup file roll (if not + // defined in preferences) + private boolean enabled = true; + + // defaultSuffix - default template to use to append to basename of file + private String suffix = "-v%n"; + + // defaultMax - default max number of backup files + private int max = 4; + + // defaultDigits - number of zero-led digits to use in the filename + private int digits = 2; + + // reverseOrder - set to true to make newest (latest) files lowest number + // (like rolled log files) + private boolean reverseOrder = false; + + // temp saved file to become new saved file + private File tempFile; + + public BackupFiles(String filename) + { + this(new File(filename)); + } + + // first time defaults for ENABLED, SUFFIX, ROLL_MAX, SUFFIX_DIGITS and + // REVERSE_ORDER + public BackupFiles(File file) + { + this(file, true, "-v%n", 4, 2, false); + } + + // set, get and rename temp file into place + public void setTempFile(File temp) + { + this.tempFile = temp; + } + + public File getTempFile() + { + return tempFile; + } + + public boolean renameTempFile() + { + return tempFile.renameTo(file); + } + + protected BackupFiles(File file, boolean defaultEnabled, + String defaultSuffix, + int defaultMax, int defaultDigits, boolean defaultReverseOrder) + { + this.file = file; + this.enabled = Cache.getDefault(ENABLED, defaultEnabled); + this.suffix = Cache.getDefault(SUFFIX, defaultSuffix); + this.max = Cache.getDefault(ROLL_MAX, defaultMax); + this.digits = Cache.getDefault(SUFFIX_DIGITS, defaultDigits); + this.reverseOrder = Cache.getDefault(REVERSE_ORDER, + defaultReverseOrder); + + // create a temp file to save new data in + File temp; + try + { + if (file != null) + { + String tempfilename = file.getName(); + File tempdir = file.getParentFile(); + temp = File.createTempFile(tempfilename, ".tmp", tempdir); + } + else + { + temp = File.createTempFile(DEFAULT_TEMP_FILE, ".tmp"); + setTempFile(temp); + } + } catch (IOException e) + { + System.out.println( + "Could not create temp file to save into (IOException)"); + } catch (Exception e) + { + System.out.println("Exception ctreating temp file for saving"); + } + + } + + // roll the backupfiles + public boolean rollBackupFiles() + { + + // file doesn't yet exist or backups are not enabled + if ((!file.exists()) || (!enabled) || (max < 1)) + { + // nothing to do + return true; + } + + // split filename up to insert suffix template in the right place. template + // and backupMax can be set in .jalview_properties + String dir = ""; + try + { + File dirFile = file.getParentFile(); + dir = dirFile.getCanonicalPath(); + } catch (Exception e) + { + System.out.println( + "Could not get canonical path for file '" + file + "'"); + return false; + } + String filename = file.getName(); + String basename = filename; + String extension = ""; + int dotcharpos = filename.lastIndexOf('.'); + // don't split of filenames with the last '.' at the very beginning or + // very end of the filename + if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1)) + { + basename = filename.substring(0, dotcharpos); + extension = filename.substring(dotcharpos); // NOTE this includes the '.' + } + + boolean ret = true; + // Create/move backups up one + String numString = null; + File lastfile = null; + for (int m = 0; m < max; m++) + { + int n = reverseOrder ? max - m : m + 1; + numString = String.format("%0" + digits + "d", n); + String backupSuffix = suffix.replaceAll("%n", numString); + String backupfilename = dir + File.separatorChar + basename + + backupSuffix + extension; + File backupfile_n = new File(backupfilename); + + if (!backupfile_n.exists()) + { + lastfile = backupfile_n; + continue; + } + + if (m == 0) + { // Move the max backup to /tmp instead of deleting (Just In + // Case) + String tmpfile = "tmp-" + backupfile_n.getName(); + try + { + File tmpFile = File.createTempFile(tmpfile, ".tmp"); + ret = ret && backupfile_n.renameTo(tmpFile); + } catch (IOException e) + { + System.out.println( + "Could not create temp file '" + tmpfile + ".tmp'"); + } + } + else + { + // Just In Case + if (lastfile != null) + { + ret = ret && backupfile_n.renameTo(lastfile); + } + } + + lastfile = backupfile_n; + } + + // now actually backup the important file! + ret = ret && file.renameTo(lastfile); + + return ret; + } + +} diff --git a/src/jalview/io/JalviewFileChooser.java b/src/jalview/io/JalviewFileChooser.java index 052cf68..90a3cc0 100755 --- a/src/jalview/io/JalviewFileChooser.java +++ b/src/jalview/io/JalviewFileChooser.java @@ -33,7 +33,6 @@ import java.awt.HeadlessException; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; @@ -352,114 +351,7 @@ public class JalviewFileChooser extends JFileChooser ret = JalviewFileChooser.CANCEL_OPTION; } - rollBackupFiles(ourselectedFile); - } - - return ret; - } - - // attempts to roll backup files for this file (before overwriting). Returns - // true if it rolled all the files it was supposed to, false otherwise. - public static boolean rollBackupFiles(String filename) - { - File file = new File(filename); - return rollBackupFiles(file); - } - - public static boolean rollBackupFiles(File file) - { - return rollBackupFiles(file, true, "-bak-%n", 4); - } - - public static boolean rollBackupFiles(File file, boolean defaultDo, - String defaultSuffix, int defaultMax) - { - - if (!(file.exists() - && jalview.bin.Cache.getDefault("BACKUP_FILES", - defaultDo))) - { - // nothing to do - return true; - } - - // split filename up to insert suffix template in the right place. template - // and backupMax can be set in .jalview_properties - String backupSuffixTemplate = jalview.bin.Cache - .getDefault("BACKUP_SUFFIX", defaultSuffix); - int backupMax = jalview.bin.Cache.getDefault("BACKUP_ROLL_MAX", - defaultMax); - String dir = ""; - try - { - File dirFile = file.getParentFile(); - dir = dirFile.getCanonicalPath(); - } catch (Exception e) - { - System.out.println( - "Could not get canonical path for file '" + file + "'"); - return false; - } - String filename = file.getName(); - String basename = filename; - String extension = ""; - int dotchar = filename.lastIndexOf('.'); - // don't split of filenames with the last '.' at the very beginning or - // very end of the filename - if ((dotchar > 0) && (dotchar < filename.length() - 1)) - { - basename = filename.substring(0, dotchar); - extension = filename.substring(dotchar); // NOTE this includes the '.' - } - - boolean ret = true; - if (backupMax >= 1) - { - // Create/move backups up one - String numString = null; - File lastfile = null; - for (int n = backupMax; n > 0; n--) - { - numString = String.format("%02d", n); - String backupSuffix = backupSuffixTemplate.replaceAll("%n", - numString); - String backupfilename = dir + File.separatorChar + basename - + backupSuffix + extension; - File backupfile_n = new File(backupfilename); - - if (! backupfile_n.exists()) { - lastfile = backupfile_n; - continue; - } - - if (n == backupMax) - { // Move the max backup to /tmp instead of deleting (Just In - // Case) - String tmpfile = "tmp-" + backupfile_n.getName(); - try - { - File tmpFile = File.createTempFile(tmpfile, ".tmp"); - ret = ret && backupfile_n.renameTo(tmpFile); - } catch (IOException e) - { - System.out.println( - "Could not create temp file '" + tmpfile + ".tmp'"); - } - } - else - { - // Just In Case - if (lastfile != null) - { - ret = ret && backupfile_n.renameTo(lastfile); - } - } - - lastfile = backupfile_n; - } - - // now actually backup the important file! - ret = ret && file.renameTo(lastfile); + // not happening here now rollBackupFiles(ourselectedFile); } return ret;