From 4c29c5b40db7b7d0ac1175e7ec1a3ef94936416a Mon Sep 17 00:00:00 2001 From: Ben Soares Date: Mon, 22 Oct 2018 17:10:38 +0100 Subject: [PATCH] JAL-3141 Taken the backing up code out of FileChooser into its own class for more generic usage, and extra data safe-guarding functionality --- src/jalview/io/BackupFiles.java | 85 ++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 761ab80..2d75746 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -152,45 +152,72 @@ public class BackupFiles // Create/move backups up one String numString = null; File lastfile = null; - for (int m = 0; m < max; m++) + + if (reverseOrder) { - 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()) + // backup style numbering + for (int m = 0; m < max; m++) { - 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 + int n = max - m; + 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()) { - File tmpFile = File.createTempFile(tmpfile, ".tmp"); - ret = ret && backupfile_n.renameTo(tmpFile); - } catch (IOException e) + 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 { - System.out.println( - "Could not create temp file '" + tmpfile + ".tmp'"); + // Just In Case + if (lastfile != null) + { + ret = ret && backupfile_n.renameTo(lastfile); + } } + + lastfile = backupfile_n; } - else + + } + else + { + // version style numbering (with file rolling though) + + // check if all backup files exist + int largest = 0; + for (int m = 0; m < max; m++) { - // Just In Case - if (lastfile != null) + int n = 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()) { - ret = ret && backupfile_n.renameTo(lastfile); + largest = n; } } - - lastfile = backupfile_n; + // MORE CODE HERE BEN! } // now actually backup the important file! -- 1.7.10.2