X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fjalview%2Fio%2FBackupFiles.java;h=2039d3c5b11ce650057c61081802da093ccd9b14;hb=11c0c1e2426619039052264ce0f2b06a78cd9194;hp=1a0f72d7b01a3a482fe1634261c47e6baaefdde8;hpb=6947b78af06682b463f408085577e5519cc2bdb9;p=jalview.git diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 1a0f72d..2039d3c 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -33,6 +33,7 @@ import java.util.Map; import java.util.TreeMap; import jalview.bin.Cache; +import jalview.bin.Console; import jalview.gui.Desktop; import jalview.gui.JvOptionPane; import jalview.util.MessageManager; @@ -131,30 +132,47 @@ public class BackupFiles { String tempfilename = file.getName(); File tempdir = file.getParentFile(); + Console.trace( + "BACKUPFILES [file!=null] attempting to create temp file for " + + tempfilename + " in dir " + tempdir); temp = File.createTempFile(tempfilename, TEMP_FILE_EXT + newTempFileSuffix, tempdir); + Console.debug( + "BACKUPFILES using temp file " + temp.getAbsolutePath()); } else { + Console.trace( + "BACKUPFILES [file==null] attempting to create default temp file " + + DEFAULT_TEMP_FILE + " with extension " + + TEMP_FILE_EXT); temp = File.createTempFile(DEFAULT_TEMP_FILE, TEMP_FILE_EXT); } } catch (IOException e) { - Cache.log.error( - "Could not create temp file to save into (IOException)"); + Console.error("Could not create temp file to save to (IOException)"); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); } catch (Exception e) { - Cache.log.error("Exception ctreating temp file for saving"); + Console.error("Exception creating temp file for saving"); + Console.debug(Cache.getStackTraceString(e)); } this.setTempFile(temp); } public static void classInit() { - setEnabled(Cache.getDefault(ENABLED, true)); + Console.initLogger(); + Console.trace("BACKUPFILES classInit"); + boolean e = Cache.getDefault(ENABLED, !Platform.isJS()); + setEnabled(e); + Console.trace("BACKUPFILES " + (e ? "enabled" : "disabled")); BackupFilesPresetEntry bfpe = BackupFilesPresetEntry .getSavedBackupEntry(); + Console.trace("BACKUPFILES preset scheme " + bfpe.toString()); setConfirmDelete(bfpe.confirmDelete); + Console.trace("BACKUPFILES confirm delete " + bfpe.confirmDelete); } public static void setEnabled(boolean flag) @@ -198,9 +216,9 @@ public class BackupFiles path = this.getTempFile().getCanonicalPath(); } catch (IOException e) { - Cache.log.error( - "IOException when getting Canonical Path of temp file '" - + this.getTempFile().getName() + "'"); + Console.error("IOException when getting Canonical Path of temp file '" + + this.getTempFile().getName() + "'"); + Console.debug(Cache.getStackTraceString(e)); } return path; } @@ -236,7 +254,7 @@ public class BackupFiles || suffix.length() == 0) { // nothing to do - Cache.log.debug("BACKUPFILES rollBackupFiles nothing to do." + ", " + Console.debug("BACKUPFILES rollBackupFiles nothing to do." + ", " + "filename: " + (file != null ? file.getName() : "null") + ", " + "file exists: " + file.exists() + ", " + "enabled: " + enabled + ", " + "max: " + max + ", " + "suffix: '" + suffix @@ -244,22 +262,26 @@ public class BackupFiles return true; } + Console.trace("BACKUPFILES rollBackupFiles starting"); + String dir = ""; File dirFile; try { dirFile = file.getParentFile(); dir = dirFile.getCanonicalPath(); - Cache.log.debug("BACKUPFILES: dir: " + dir); + Console.trace("BACKUPFILES dir: " + dir); } catch (Exception e) { - Cache.log.error( - "Could not get canonical path for file '" + file + "'"); + Console.error("Could not get canonical path for file '" + file + "'"); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); return false; } String filename = file.getName(); String basename = filename; + Console.trace("BACKUPFILES filename is " + filename); boolean ret = true; // Create/move backups up one @@ -271,11 +293,12 @@ public class BackupFiles File[] backupFiles = dirFile.listFiles(bff); int nextIndexNum = 0; - Cache.log - .debug("BACKUPFILES backupFiles.length: " + backupFiles.length); + Console.trace("BACKUPFILES backupFiles.length: " + backupFiles.length); if (backupFiles.length == 0) { // No other backup files. Just need to move existing file to backupfile_1 + Console.trace( + "BACKUPFILES no existing backup files, setting index to 1"); nextIndexNum = 1; } else @@ -288,6 +311,7 @@ public class BackupFiles if (reverseOrder) { // backup style numbering + Console.trace("BACKUPFILES rolling files in reverse order"); int tempMax = noMax ? -1 : max; // noMax == true means no limits @@ -319,6 +343,7 @@ public class BackupFiles // no "oldest" file to delete previousFile = backupfile_n; fileToBeDeleted = null; + Console.trace("BACKUPFILES No oldest file to delete"); continue; } @@ -329,6 +354,10 @@ public class BackupFiles File replacementFile = backupfile_n; long fileToBeDeletedLMT = fileToBeDeleted.lastModified(); long replacementFileLMT = replacementFile.lastModified(); + Console.trace("BACKUPFILES fileToBeDeleted is " + + fileToBeDeleted.getAbsolutePath()); + Console.trace("BACKUPFILES replacementFile is " + + backupfile_n.getAbsolutePath()); try { @@ -341,7 +370,7 @@ public class BackupFiles .format(fileToBeDeletedLMT); String replacementFileLMTString = sdf .format(replacementFileLMT); - Cache.log.warn("WARNING! I am set to delete backupfile " + Console.warn("WARNING! I am set to delete backupfile " + fileToBeDeleted.getName() + " has modification time " + fileToBeDeletedLMTString @@ -352,6 +381,11 @@ public class BackupFiles boolean delete = confirmNewerDeleteFile(fileToBeDeleted, replacementFile, true); + Console.trace("BACKUPFILES " + + (delete ? "confirmed" : "not") + " deleting file " + + fileToBeDeleted.getAbsolutePath() + + " which is newer than " + + replacementFile.getAbsolutePath()); if (delete) { @@ -360,21 +394,27 @@ public class BackupFiles } else { + Console.debug("BACKUPFILES moving " + + fileToBeDeleted.getAbsolutePath() + " to " + + oldestTempFile.getAbsolutePath()); moveFileToFile(fileToBeDeleted, oldestTempFile); } } else { + Console.debug("BACKUPFILES going to move " + + fileToBeDeleted.getAbsolutePath() + " to " + + oldestTempFile.getAbsolutePath()); moveFileToFile(fileToBeDeleted, oldestTempFile); addDeleteFile(oldestTempFile); } } catch (Exception e) { - Cache.log.error( + Console.error( "Error occurred, probably making new temp file for '" + fileToBeDeleted.getName() + "'"); - Cache.log.error(e.getStackTrace()); + Console.error(Cache.getStackTraceString(e)); } // reset @@ -389,7 +429,9 @@ public class BackupFiles { if (previousFile != null) { - ret = ret && moveFileToFile(backupfile_n, previousFile); + // using boolean '&' instead of '&&' as don't want moveFileToFile + // attempt to be conditional (short-circuit) + ret = ret & moveFileToFile(backupfile_n, previousFile); } } @@ -414,13 +456,13 @@ public class BackupFiles } bfsb.append(backupFiles[i].getName()); } - Cache.log.debug("BACKUPFILES backupFiles: " + bfsb.toString()); + Console.trace("BACKUPFILES backupFiles: " + bfsb.toString()); // noMax == true means keep all backup files if ((!noMax) && bfTreeMap.size() >= max) { - Cache.log.debug("BACKUPFILES noMax: " + noMax + ", " + "max: " - + max + ", " + "bfTreeMap.size(): " + bfTreeMap.size()); + Console.trace("BACKUPFILES noMax: " + noMax + ", " + "max: " + max + + ", " + "bfTreeMap.size(): " + bfTreeMap.size()); // need to delete some files to keep number of backups to designated // max. // Note that if the suffix is not numbered then do not delete any @@ -429,7 +471,7 @@ public class BackupFiles int numToDelete = suffix.indexOf(NUM_PLACEHOLDER) > -1 ? bfTreeMap.size() - max + 1 : 0; - Cache.log.debug("BACKUPFILES numToDelete: " + numToDelete); + Console.trace("BACKUPFILES numToDelete: " + numToDelete); // the "replacement" file is the latest backup file being kept (it's // not replacing though) File replacementFile = numToDelete < backupFiles.length @@ -442,7 +484,7 @@ public class BackupFiles File fileToBeDeleted = backupFiles[i]; boolean delete = true; - Cache.log.debug( + Console.trace( "BACKUPFILES fileToBeDeleted: " + fileToBeDeleted); boolean newer = false; @@ -459,7 +501,7 @@ public class BackupFiles String replacementFileLMTString = sdf .format(replacementFileLMT); - Cache.log.warn("WARNING! I am set to delete backupfile '" + Console.warn("WARNING! I am set to delete backupfile '" + fileToBeDeleted.getName() + "' has modification time " + fileToBeDeletedLMTString @@ -474,14 +516,14 @@ public class BackupFiles { // User has confirmed delete -- no need to add it to the list fileToBeDeleted.delete(); - Cache.log.debug("BACKUPFILES deleting fileToBeDeleted: " + Console.debug("BACKUPFILES deleting fileToBeDeleted: " + fileToBeDeleted); delete = false; } else { // keeping file, nothing to do! - Cache.log.debug("BACKUPFILES keeping fileToBeDeleted: " + Console.debug("BACKUPFILES keeping fileToBeDeleted: " + fileToBeDeleted); } } @@ -489,7 +531,7 @@ public class BackupFiles if (delete) { addDeleteFile(fileToBeDeleted); - Cache.log.debug("BACKUPFILES addDeleteFile(fileToBeDelted): " + Console.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): " + fileToBeDeleted); } @@ -505,15 +547,17 @@ public class BackupFiles String latestBackupFilename = dir + File.separatorChar + BackupFilenameParts.getBackupFilename(nextIndexNum, basename, suffix, digits); - Cache.log.debug("BACKUPFILES Moving old file [" + file + Console.trace("BACKUPFILES Moving old file [" + file + "] to latestBackupFilename [" + latestBackupFilename + "]"); - ret |= moveFileToFile(file, new File(latestBackupFilename)); - Cache.log.debug("BACKUPFILES moving " + latestBackupFilename + " to " - + file + " was " + (ret ? "" : "NOT ") + "successful"); - + // using boolean '&' instead of '&&' as don't want moveFileToFile attempt to + // be conditional (short-circuit) + ret = ret & moveFileToFile(file, new File(latestBackupFilename)); + Console.debug( + "BACKUPFILES moving " + file + " to " + latestBackupFilename + + " was " + (ret ? "" : "NOT ") + "successful"); if (tidyUp) { - Cache.log.debug("BACKUPFILES tidying up files"); + Console.debug("BACKUPFILES tidying up files"); tidyUpFiles(); } @@ -569,7 +613,7 @@ public class BackupFiles saveFile = nextTempFile(ftbd.getName(), ftbd.getParentFile()); } catch (Exception e) { - Cache.log.error( + Console.error( "Error when confirming to keep backup file newer than other backup files."); e.printStackTrace(); } @@ -679,10 +723,10 @@ public class BackupFiles for (int i = 0; i < deleteFiles.size(); i++) { File fileToDelete = deleteFiles.get(i); - Cache.log.debug( - "BACKUPFILES deleting fileToDelete:" + fileToDelete); + Console.trace("BACKUPFILES about to delete fileToDelete:" + + fileToDelete); fileToDelete.delete(); - Cache.log.warn("deleting '" + fileToDelete.getName() + "'"); + Console.warn("deleted '" + fileToDelete.getName() + "'"); } } @@ -730,7 +774,6 @@ public class BackupFiles boolean okay = roll && rename; if (!okay) { - boolean yesno = false; StringBuilder messageSB = new StringBuilder(); messageSB.append(MessageManager.getString( "label.backupfiles_confirm_save_file_backupfiles_roll_wrong")); @@ -755,6 +798,12 @@ public class BackupFiles "label.backupfiles_confirm_save_new_saved_file_not_ok")); // "The new saved file might not be okay." } + if (messageSB.length() > 0) + { + messageSB.append("\n"); + } + messageSB + .append(MessageManager.getString("label.continue_operation")); int confirmButton = Platform.isHeadless() ? JvOptionPane.OK_OPTION : JvOptionPane.showConfirmDialog(Desktop.desktop, @@ -786,8 +835,7 @@ public class BackupFiles dirFile = file.getParentFile(); } catch (Exception e) { - Cache.log.error( - "Could not get canonical path for file '" + file + "'"); + Console.error("Could not get canonical path for file '" + file + "'"); return new TreeMap<>(); } @@ -828,10 +876,15 @@ public class BackupFiles int pos = deleteFiles.indexOf(fileToBeDeleted); if (pos > -1) { + Console.debug("BACKUPFILES not adding file " + + fileToBeDeleted.getAbsolutePath() + + " to the delete list (already at index" + pos + ")"); return true; } else { + Console.debug("BACKUPFILES adding file " + + fileToBeDeleted.getAbsolutePath() + " to the delete list"); deleteFiles.add(fileToBeDeleted); } return ret; @@ -839,25 +892,31 @@ public class BackupFiles public static boolean moveFileToFile(File oldFile, File newFile) { + Console.initLogger(); boolean ret = false; Path oldPath = Paths.get(oldFile.getAbsolutePath()); Path newPath = Paths.get(newFile.getAbsolutePath()); try { // delete destination file - not usually necessary but Just In Case... + Console.trace("BACKUPFILES deleting " + newFile.getAbsolutePath()); newFile.delete(); + Console.trace("BACKUPFILES moving " + oldFile.getAbsolutePath() + + " to " + newFile.getAbsolutePath()); Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING); ret = true; + Console.trace("BACKUPFILES move seems to have succeeded"); } catch (IOException e) { - Cache.log.warn("Could not move file '" + oldPath.toString() + "' to '" + Console.warn("Could not move file '" + oldPath.toString() + "' to '" + newPath.toString() + "'"); - Cache.log.error(e.getStackTrace()); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); ret = false; } catch (Exception e) { - Cache.log.error(e.getMessage()); - Cache.log.error(e.getStackTrace()); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); ret = false; } return ret;