From 972c19a95f8ba393a6d037cf03d9d9b3bb07b693 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Fri, 4 Jan 2013 17:54:17 +0100 Subject: [PATCH] Use canonical path to the wallet destination file. Resolves issue 265. --- core/src/main/java/com/google/bitcoin/core/Utils.java | 4 ++++ core/src/main/java/com/google/bitcoin/core/Wallet.java | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Utils.java b/core/src/main/java/com/google/bitcoin/core/Utils.java index 0b254848..0ac6274b 100644 --- a/core/src/main/java/com/google/bitcoin/core/Utils.java +++ b/core/src/main/java/com/google/bitcoin/core/Utils.java @@ -444,4 +444,8 @@ public class Utils { } return decode; } + + public static boolean isWindows() { + return System.getProperty("os.name").toLowerCase().indexOf("win") >= 0; + } } diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index d09bca50..57812fbc 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -253,11 +253,13 @@ public class Wallet implements Serializable, BlockChainListener { stream.getFD().sync(); stream.close(); stream = null; - if (!temp.renameTo(destFile)) { + if (Utils.isWindows()) { // Work around an issue on Windows whereby you can't rename over existing files. - if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { - if (destFile.delete() && temp.renameTo(destFile)) return; // else fall through. - } + File canonical = destFile.getCanonicalFile(); + if (canonical.delete() && temp.renameTo(canonical)) + return; // else fall through. + throw new IOException("Failed to rename " + temp + " to " + canonical); + } else if (!temp.renameTo(destFile)) { throw new IOException("Failed to rename " + temp + " to " + destFile); } if (destFile.equals(autosaveToFile)) {