Delete the wallet file before renaming its replacement on Windows. Resolves issue 200.

This commit is contained in:
Mike Hearn 2012-06-08 17:10:35 +02:00
parent e0ca3f4a7e
commit 4e4abf8a54

View file

@ -186,22 +186,26 @@ public class Wallet implements Serializable {
*/ */
public synchronized void saveToFile(File f) throws IOException { public synchronized void saveToFile(File f) throws IOException {
FileOutputStream stream = null; FileOutputStream stream = null;
File temp = null; File temp;
try { try {
File directory = f.getAbsoluteFile().getParentFile(); File directory = f.getAbsoluteFile().getParentFile();
temp = File.createTempFile("wallet", null, directory); temp = File.createTempFile("wallet", null, directory);
stream = new FileOutputStream(temp); stream = new FileOutputStream(temp);
saveToFileStream(stream); saveToFileStream(stream);
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide
// to not write through to physical media for at least a few seconds, but this is the best we can do.
stream.flush();
stream.getFD().sync();
if (!temp.renameTo(f)) {
// Work around an issue on Windows whereby you can't rename over existing files.
if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) {
if (f.delete() && temp.renameTo(f)) return; // else fall through.
}
throw new IOException("Failed to rename " + temp + " to " + f);
}
} finally { } finally {
if (stream != null) { if (stream != null) {
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide
// to not write through to physical media for at least a few seconds, but this is the best we can do.
stream.flush();
stream.getFD().sync();
stream.close(); stream.close();
if (!temp.renameTo(f)) {
throw new IOException("Failed to rename " + temp + " to " + f);
}
} }
} }
} }