+ // need to delete some files to keep number of backups to designated
+ // max
+ int numToDelete = bfTreeMap.size() - max + 1;
+ // the "replacement" file is the latest backup file being kept (it's
+ // not replacing though)
+ File replacementFile = numToDelete < backupFiles.length
+ ? backupFiles[numToDelete]
+ : null;
+ for (int i = 0; i < numToDelete; i++)
+ {
+ // check the deletion files for modification time of the last
+ // backupfile being saved
+ File fileToBeDeleted = backupFiles[i];
+ boolean delete = true;
+
+ boolean newer = false;
+ if (replacementFile != null)
+ {
+ long fileToBeDeletedLMT = fileToBeDeleted.lastModified();
+ long replacementFileLMT = replacementFile != null
+ ? replacementFile.lastModified()
+ : Long.MAX_VALUE;
+ if (fileToBeDeletedLMT > replacementFileLMT)
+ {
+ String fileToBeDeletedLMTString = sdf
+ .format(fileToBeDeletedLMT);
+ String replacementFileLMTString = sdf
+ .format(replacementFileLMT);
+
+ System.out
+ .println("WARNING! I am set to delete backupfile '"
+ + fileToBeDeleted.getName()
+ + "' has modification time "
+ + fileToBeDeletedLMTString
+ + " which is newer than the oldest backupfile being kept '"
+ + replacementFile.getName()
+ + "' with modification time "
+ + replacementFileLMTString);
+
+ delete = confirmNewerDeleteFile(fileToBeDeleted,
+ replacementFile, false);
+ if (delete)
+ {
+ // User has confirmed delete -- no need to add it to the list
+ fileToBeDeleted.delete();
+ delete = false;
+ }
+ else
+ {
+ // keeping file, nothing to do!
+ }
+ }
+ }
+ if (delete)
+ {
+ addDeleteFile(fileToBeDeleted);
+ }
+
+ }
+
+ }
+
+ nextIndexNum = bfTreeMap.lastKey() + 1;
+ }
+ }
+
+ // Let's make the new backup file!! yay, got there at last!
+ String latestBackupFilename = dir + File.separatorChar
+ + BackupFilenameParts.getBackupFilename(nextIndexNum, basename,
+ suffix, digits);
+ ret |= file.renameTo(new File(latestBackupFilename));
+
+ if (tidyUp)
+ {
+ tidyUpFiles();
+ }
+
+ return ret;
+ }
+
+ private static File nextTempFile(String filename, File dirFile)
+ throws IOException
+ {
+ File temp = null;
+ COUNT: for (int i = 1; i < 1000; i++)
+ {
+ File trythis = new File(dirFile,
+ filename + '~' + Integer.toString(i));
+ if (!trythis.exists())
+ {
+ temp = trythis;
+ break COUNT;
+ }
+
+ }
+ if (temp == null)
+ {
+ temp = File.createTempFile(filename, TEMP_FILE_EXT, dirFile);
+ }
+ return temp;
+ }
+
+ private void tidyUpFiles()
+ {
+ deleteOldFiles();
+ }
+
+ private static boolean confirmNewerDeleteFile(File fileToBeDeleted,
+ File replacementFile, boolean replace)
+ {
+ StringBuilder messageSB = new StringBuilder();
+
+ File ftbd = fileToBeDeleted;
+ String ftbdLMT = sdf.format(ftbd.lastModified());
+ String ftbdSize = Long.toString(ftbd.length());
+
+ File rf = replacementFile;
+ String rfLMT = sdf.format(rf.lastModified());
+ String rfSize = Long.toString(rf.length());
+
+ int confirmButton = JvOptionPane.NO_OPTION;
+ if (replace)
+ {
+ File saveFile = null;
+ try
+ {
+ saveFile = nextTempFile(ftbd.getName(), ftbd.getParentFile());
+ } catch (Exception e)
+ {
+ System.out.println(
+ "Error when confirming to keep backup file newer than other backup files.");
+ e.printStackTrace();
+ }
+ messageSB.append(MessageManager.formatMessage(
+ "label.newerdelete_replacement_line", new String[]
+ { ftbd.getName(), rf.getName(), ftbdLMT, rfLMT, ftbdSize,
+ rfSize }));
+ messageSB.append("\n\n");
+ messageSB.append(MessageManager.formatMessage(
+ "label.confirm_deletion_or_rename", new String[]
+ { ftbd.getName(), saveFile.getName() }));
+ String[] options = new String[] {
+ MessageManager.getString("label.delete"),
+ MessageManager.getString("label.rename") };
+
+ confirmButton = JvOptionPane.showOptionDialog(Desktop.desktop,
+ messageSB.toString(),
+ MessageManager.getString("label.backupfiles_confirm_delete"),
+ JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE,
+ null, options, options[0]);
+ }
+ else
+ {
+ messageSB.append(MessageManager
+ .formatMessage("label.newerdelete_line", new String[]
+ { ftbd.getName(), rf.getName(), ftbdLMT, rfLMT, ftbdSize,
+ rfSize }));
+ messageSB.append("\n\n");
+ messageSB.append(MessageManager
+ .formatMessage("label.confirm_deletion", new String[]
+ { ftbd.getName() }));
+ String[] options = new String[] {
+ MessageManager.getString("label.delete"),
+ MessageManager.getString("label.keep") };
+
+ confirmButton = JvOptionPane.showOptionDialog(Desktop.desktop,
+ messageSB.toString(),
+ MessageManager.getString("label.backupfiles_confirm_delete"),
+ JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE,
+ null, options, options[0]);
+ }
+
+
+ // return should be TRUE if file is to be deleted
+ return (confirmButton == JvOptionPane.YES_OPTION);
+ }
+
+ private void deleteOldFiles()
+ {
+ if (deleteFiles != null && !deleteFiles.isEmpty())
+ {
+ boolean doDelete = false;
+ StringBuilder messageSB = null;
+ if (confirmDelete && deleteFiles.size() > 0)
+ {
+ messageSB = new StringBuilder();
+ messageSB.append(MessageManager
+ .getString("label.backupfiles_confirm_delete_old_files"));
+ for (int i = 0; i < deleteFiles.size(); i++)
+ {
+ File df = deleteFiles.get(i);
+ messageSB.append("\n");
+ messageSB.append(df.getName());
+ messageSB.append(" ");
+ messageSB.append(MessageManager.formatMessage("label.file_info",
+ new String[]
+ { sdf.format(df.lastModified()),
+ Long.toString(df.length()) }));
+ }
+
+ int confirmButton = JvOptionPane.showConfirmDialog(Desktop.desktop,
+ messageSB.toString(),
+ MessageManager
+ .getString("label.backupfiles_confirm_delete"),
+ JvOptionPane.YES_NO_OPTION, JvOptionPane.WARNING_MESSAGE);
+
+ doDelete = (confirmButton == JvOptionPane.YES_OPTION);
+ }
+ else
+ {
+ doDelete = true;
+ }
+
+ if (doDelete)
+ {
+ for (int i = 0; i < deleteFiles.size(); i++)
+ {
+ File fileToDelete = deleteFiles.get(i);
+ fileToDelete.delete();
+ System.out.println("DELETING '" + fileToDelete.getName() + "'");
+ }
+ }
+
+ }
+
+ deleteFiles.clear();
+ }
+
+ private TreeMap<Integer, File> sortBackupFilesAsTreeMap(
+ File[] backupFiles,
+ String basename)
+ {
+ // sort the backup files (based on integer found in the suffix) using a
+ // precomputed Hashmap for speed
+ Map<Integer, File> bfHashMap = new HashMap<>();
+ for (int i = 0; i < backupFiles.length; i++)
+ {
+ File f = backupFiles[i];
+ BackupFilenameParts bfp = new BackupFilenameParts(f, basename, suffix,
+ digits);
+ bfHashMap.put(bfp.indexNum(), f);
+ }
+ TreeMap<Integer, File> bfTreeMap = new TreeMap<>();
+ bfTreeMap.putAll(bfHashMap);
+ return bfTreeMap;
+ }
+
+ public boolean rollBackupsAndRenameTempFile()
+ {
+ boolean write = this.getWriteSuccess();
+
+ boolean roll = false;
+ boolean rename = false;
+ if (write)
+ {
+ roll = this.rollBackupFiles(false);
+ rename = this.renameTempFile();
+ }
+
+ /*
+ * Not sure that this confirmation is desirable. By this stage the new file is
+ * already written successfully, but something (e.g. disk full) has happened while
+ * trying to roll the backup files, and most likely the filename needed will already
+ * be vacant so renaming the temp file is nearly always correct!
+ */
+ boolean okay = roll && rename;
+ if (!okay)
+ {
+ StringBuilder messageSB = new StringBuilder();
+ messageSB.append(MessageManager.getString( "label.backupfiles_confirm_save_file_backupfiles_roll_wrong"));
+ if (rename)
+ {
+ if (messageSB.length() > 0)