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 public static String REVERSE_ORDER = NS + "_REVERSE_ORDER";
24 private static String DEFAULT_TEMP_FILE = "jalview_temp_file_" + NS;
26 // file - File object to be backed up and then updated (written over)
29 // enabled - default flag as to whether to do the backup file roll (if not
30 // defined in preferences)
31 private boolean enabled = true;
33 // defaultSuffix - default template to use to append to basename of file
34 private String suffix = "-v%n";
36 // defaultMax - default max number of backup files
39 // defaultDigits - number of zero-led digits to use in the filename
40 private int digits = 2;
42 // reverseOrder - set to true to make newest (latest) files lowest number
43 // (like rolled log files)
44 private boolean reverseOrder = false;
46 // temp saved file to become new saved file
47 private File tempFile;
49 public BackupFiles(String filename)
51 this(new File(filename));
54 // first time defaults for ENABLED, SUFFIX, ROLL_MAX, SUFFIX_DIGITS and
56 public BackupFiles(File file)
58 this(file, true, "-v%n", 4, 2, false);
61 // set, get and rename temp file into place
62 public void setTempFile(File temp)
67 public File getTempFile()
72 public boolean renameTempFile()
74 return tempFile.renameTo(file);
77 protected BackupFiles(File file, boolean defaultEnabled,
79 int defaultMax, int defaultDigits, boolean defaultReverseOrder)
82 this.enabled = Cache.getDefault(ENABLED, defaultEnabled);
83 this.suffix = Cache.getDefault(SUFFIX, defaultSuffix);
84 this.max = Cache.getDefault(ROLL_MAX, defaultMax);
85 this.digits = Cache.getDefault(SUFFIX_DIGITS, defaultDigits);
86 this.reverseOrder = Cache.getDefault(REVERSE_ORDER,
89 // create a temp file to save new data in
95 String tempfilename = file.getName();
96 File tempdir = file.getParentFile();
97 temp = File.createTempFile(tempfilename, ".tmp", tempdir);
101 temp = File.createTempFile(DEFAULT_TEMP_FILE, ".tmp");
104 } catch (IOException e)
107 "Could not create temp file to save into (IOException)");
108 } catch (Exception e)
110 System.out.println("Exception ctreating temp file for saving");
115 // roll the backupfiles
116 public boolean rollBackupFiles()
119 // file doesn't yet exist or backups are not enabled
120 if ((!file.exists()) || (!enabled) || (max < 1))
126 // split filename up to insert suffix template in the right place. template
127 // and backupMax can be set in .jalview_properties
131 File dirFile = file.getParentFile();
132 dir = dirFile.getCanonicalPath();
133 } catch (Exception e)
136 "Could not get canonical path for file '" + file + "'");
139 String filename = file.getName();
140 String basename = filename;
141 String extension = "";
142 int dotcharpos = filename.lastIndexOf('.');
143 // don't split of filenames with the last '.' at the very beginning or
144 // very end of the filename
145 if ((dotcharpos > 0) && (dotcharpos < filename.length() - 1))
147 basename = filename.substring(0, dotcharpos);
148 extension = filename.substring(dotcharpos); // NOTE this includes the '.'
152 // Create/move backups up one
153 String numString = null;
154 File lastfile = null;
158 // backup style numbering
159 for (int m = 0; m < max; m++)
162 numString = String.format("%0" + digits + "d", n);
163 String backupSuffix = suffix.replaceAll("%n", numString);
164 String backupfilename = dir + File.separatorChar + basename
165 + backupSuffix + extension;
166 File backupfile_n = new File(backupfilename);
168 if (!backupfile_n.exists())
170 lastfile = backupfile_n;
175 { // Move the max backup to /tmp instead of deleting (Just In
177 String tmpfile = "tmp-" + backupfile_n.getName();
180 File tmpFile = File.createTempFile(tmpfile, ".tmp");
181 ret = ret && backupfile_n.renameTo(tmpFile);
182 } catch (IOException e)
185 "Could not create temp file '" + tmpfile + ".tmp'");
191 if (lastfile != null)
193 ret = ret && backupfile_n.renameTo(lastfile);
197 lastfile = backupfile_n;
203 // version style numbering (with file rolling though)
205 // check if all backup files exist
207 for (int m = 0; m < max; m++)
210 numString = String.format("%0" + digits + "d", n);
211 String backupSuffix = suffix.replaceAll("%n", numString);
212 String backupfilename = dir + File.separatorChar + basename
213 + backupSuffix + extension;
214 File backupfile_n = new File(backupfilename);
215 if (backupfile_n.exists())
220 // MORE CODE HERE BEN!
223 // now actually backup the important file!
224 ret = ret && file.renameTo(lastfile);