From 3b6e48e7a9c663a8cee7fdff127cbfe9e47daf6a Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 13 Apr 2007 20:50:12 +0000 Subject: [PATCH] Reinstate the "move the old file out of the way then move the new file in place" Windows-friendly rename and try that before falling back to the brute force copy (which risks failing due to a full disk and leaving us with a corrupt file rather than just an out of date one). --- .../com/threerings/getdown/util/FileUtil.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/java/com/threerings/getdown/util/FileUtil.java b/src/java/com/threerings/getdown/util/FileUtil.java index 94e3220..56d46bb 100644 --- a/src/java/com/threerings/getdown/util/FileUtil.java +++ b/src/java/com/threerings/getdown/util/FileUtil.java @@ -49,14 +49,37 @@ public class FileUtil return true; } - // otherwise try copying the old data over the new + // fall back to trying to rename the old file out of the way, rename the new file into + // place and then delete the old file + if (dest.exists()) { + File temp = new File(dest.getPath() + "_old"); + if (temp.exists()) { + if (!temp.delete()) { + Log.warning("Failed to delete old intermediate file " + temp + "."); + // the subsequent code will probably fail + } + } + if (dest.renameTo(temp)) { + if (source.renameTo(dest)) { + if (temp.delete()) { + Log.warning("Failed to delete intermediate file " + temp + "."); + } + return true; + } + } + } + + // as a last resort, try copying the old data over the new FileInputStream fin = null; FileOutputStream fout = null; try { fin = new FileInputStream(source); fout = new FileOutputStream(dest); IOUtils.copy(fin, fout); - source.delete(); + if (!source.delete()) { + Log.warning("Failed to delete " + source + + " after brute force copy to " + dest + "."); + } return true; } catch (IOException ioe) {