in src/main/java/com/spotify/sparkey/Util.java [270:307]
static void renameFile(File srcFile, File destFile) throws IOException {
if (!srcFile.exists()) {
throw new FileNotFoundException(srcFile.getPath());
}
if (srcFile.equals(destFile)) {
return;
}
if (!destFile.exists()) {
rename(srcFile, destFile);
return;
}
File backupFile = new File(destFile.getParent(), destFile.getName() + "-backup" + UUID.randomUUID().toString());
if (backupFile.exists()) {
// Edge case, but let's be safe.
throw new IOException("Expected duplicate temporary backup file: " + backupFile.getPath());
}
rename(destFile, backupFile);
if (destFile.exists()) {
// This is strange, it should be renamed by now. But let's be safe.
throw new IOException("Unexpected file still existing: " + destFile.getPath() + ", should have been renamed to: " + backupFile.getPath());
}
try {
rename(srcFile, destFile);
if (!backupFile.delete()) {
log.warn("Could not delete backup file: " + backupFile.getPath());
}
} catch (IOException e) {
// roll back failed rename
try {
rename(backupFile, destFile);
} catch (IOException e2) {
log.warn("Failed to roll back failed rename - file still exists: {}", backupFile);
}
throw new IOException("Could not rename " + srcFile.getPath() + " to " + destFile.getPath(), e);
}
}