3 import jalview.bin.Cache;
6 import java.io.IOException;
8 public class BackupFiles
11 // labels for saved params in Cache and .jalview_properties
12 private static String NS = "BACKUPFILES";
14 public static String ENABLED = NS + "_ENABLED";
16 public static String SUFFIX = NS + "_SUFFIX";
18 public static String ROLL_MAX = NS + "_ROLL_MAX";
20 public static String SUFFIX_DIGITS = NS + "_SUFFIX_DIGITS";
22 protected static String NUM_PLACEHOLDER = "%n";
24 public static String REVERSE_ORDER = NS + "_REVERSE_ORDER";
26 private static String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS;
28 // file - File object to be backed up and then updated (written over)
31 // enabled - default flag as to whether to do the backup file roll (if not
32 // defined in preferences)
33 private boolean enabled;
35 // defaultSuffix - default template to use to append to basename of file
36 private String suffix;
38 // defaultMax - default max number of backup files
41 // defaultDigits - number of zero-led digits to use in the filename
44 // reverseOrder - set to true to make newest (latest) files lowest number
45 // (like rolled log files)
46 private boolean reverseOrder;
48 // temp saved file to become new saved file
49 private File tempFile;
51 public BackupFiles(String filename)
53 this(new File(filename));
56 // first time defaults for ENABLED, SUFFIX, ROLL_MAX, SUFFIX_DIGITS and
58 public BackupFiles(File file)
60 this(file, true, "-v" + NUM_PLACEHOLDER, 4, 3, false);
63 // set, get and rename temp file into place
64 public void setTempFile(File temp)
69 public File getTempFile()
74 public boolean renameTempFile()
76 return tempFile.renameTo(file);
79 public BackupFiles(File file, boolean defaultEnabled,
81 int defaultMax, int defaultDigits, boolean defaultReverseOrder)
84 this.enabled = Cache.getDefault(ENABLED, defaultEnabled);
85 this.suffix = Cache.getDefault(SUFFIX, defaultSuffix);
86 this.max = Cache.getDefault(ROLL_MAX, defaultMax);
87 this.digits = Cache.getDefault(SUFFIX_DIGITS, defaultDigits);
88 this.reverseOrder = Cache.getDefault(REVERSE_ORDER,
91 // create a temp file to save new data in
97 String tempfilename = file.getName();
98 File tempdir = file.getParentFile();
99 temp = File.createTempFile(tempfilename, ".tmp", tempdir);
103 temp = File.createTempFile(DEFAULT_TEMP_FILE, ".tmp");
106 } catch (IOException e)
109 "Could not create temp file to save into (IOException)");
110 } catch (Exception e)
112 System.out.println("Exception ctreating temp file for saving");
117 // roll the backupfiles
118 public boolean rollBackupFiles()
121 // file doesn't yet exist or backups are not enabled
122 if ((!file.exists()) || (!enabled) || (max < 1))
128 // split filename up to insert suffix template in the right place. template
129 // and backupMax can be set in .jalview_properties
134 dirFile = file.getParentFile();
135 dir = dirFile.getCanonicalPath();
136 } catch (Exception e)
139 "Could not get canonical path for file '" + file + "'");
142 String filename = file.getName();
143 String basename = filename;
144 String extension = "";
145 int dotcharpos = filename.lastIndexOf('.');
146 // don't split of filenames with the last '.' at the very beginning or
147 // very end of the filename
148 if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1))
150 basename = filename.substring(0, dotcharpos);
151 extension = filename.substring(dotcharpos); // NOTE this includes the '.'
155 // Create/move backups up one
156 String numString = null;
157 File lastfile = null;
161 // backup style numbering
162 for (int m = 0; m < max; m++)
165 numString = String.format("%0" + digits + "d", n);
166 String backupSuffix = suffix.replaceAll(NUM_PLACEHOLDER, numString);
167 String backupfilename = dir + File.separatorChar + basename
168 + backupSuffix + extension;
169 File backupfile_n = new File(backupfilename);
171 if (!backupfile_n.exists())
173 lastfile = backupfile_n;
178 { // Move the max backup to /tmp instead of deleting (Just In
180 String tmpfile = "tmp-" + backupfile_n.getName();
183 File tmpFile = File.createTempFile(tmpfile, ".tmp");
184 ret = ret && backupfile_n.renameTo(tmpFile);
185 } catch (IOException e)
188 "Could not create temp file '" + tmpfile + ".tmp'");
194 if (lastfile != null)
196 ret = ret && backupfile_n.renameTo(lastfile);
200 lastfile = backupfile_n;
203 // now actually backup the important file!
204 ret = ret && file.renameTo(lastfile);
208 // version style numbering (with earliest file deletion if max files
211 // find existing backup files
212 BackupFileFilter bff = new BackupFileFilter(basename, suffix, digits,
214 File[] backupFiles = dirFile.listFiles(bff);