fix problem with temp file on win8

This commit is contained in:
Manfred Karrer 2014-07-09 14:21:09 +02:00
parent 99d00500de
commit 7ef9481876
2 changed files with 16 additions and 24 deletions

View file

@ -221,9 +221,11 @@ public class Storage
try try
{ {
final File tempFile = FileUtil.getTempFile("temp_" + prefix); final File tempFile = FileUtil.getTempFile("temp_" + prefix);
try (final FileOutputStream fileOutputStream = new FileOutputStream(tempFile); try (final FileOutputStream fileOutputStream = new FileOutputStream(tempFile))
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream))
{ {
// don't use closeable resource in try for the ObjectOutputStream as it produces problems on Windows 8
// -> rename of temp file fails
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(serializable); objectOutputStream.writeObject(serializable);
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide // Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide

View file

@ -4,7 +4,6 @@ import com.google.bitcoin.core.Utils;
import io.bitsquare.BitSquare; import io.bitsquare.BitSquare;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -19,16 +18,9 @@ public class FileUtil
} }
public static File getTempFile(String prefix) throws IOException public static File getTempFile(String prefix) throws IOException
{
if (Utils.isWindows())
{
return getFile("temp_" + prefix, ".tmp");
}
else
{ {
return File.createTempFile("temp_" + prefix, null, StorageDirectory.getStorageDirectory()); return File.createTempFile("temp_" + prefix, null, StorageDirectory.getStorageDirectory());
} }
}
public static String getApplicationFileName() public static String getApplicationFileName()
{ {
@ -67,23 +59,21 @@ public class FileUtil
{ {
if (Utils.isWindows()) if (Utils.isWindows())
{ {
// renameTo fails on win 8 // Work around an issue on Windows whereby you can't rename over existing files.
String canonicalPath = file.getCanonicalPath(); final File canonical = file.getCanonicalFile();
file.delete(); if (canonical.exists() && !canonical.delete())
final File canonicalFile = new File(canonicalPath);
Files.copy(tempFile.toPath(), canonicalFile.toPath());
if (tempFile.exists() && !tempFile.delete())
{ {
log.error("Cannot delete temp file."); throw new IOException("Failed to delete canonical file for replacement with save");
}
if (!tempFile.renameTo(canonical))
{
throw new IOException("Failed to rename " + tempFile + " to " + canonical);
} }
} }
else else if (!tempFile.renameTo(file))
{
if (!tempFile.renameTo(file))
{ {
throw new IOException("Failed to rename " + tempFile + " to " + file); throw new IOException("Failed to rename " + tempFile + " to " + file);
} }
}
} }
} }