if (!file.renameTo(backupFile)) {
try {
content.close();
} catch (IOException e) {
}
throw new LowlevelStorageException(true, "failed to rename with "
+ ".bak extension " + getPath(file));
}
boolean needToRevert = false;
String err = null;
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
boolean fileCopySuccessful = FileUtils.copy(content, out);
if(!fileCopySuccessful) {
needToRevert = true;
err = "failed to write content to file " + file.getPath();
}
} catch (IOException e) {
needToRevert = true;
err = "failed to write content to file " + file.getPath();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
logger.warn("Could not close file for writing "
+ file.getPath(), e);
}
}
try {
content.close();
} catch (IOException e) {
logger.warn("Could not close content stream for reading", e);
}
}
if (needToRevert) {
if (backupFile.renameTo(file)) {
err += ", so reverted to original";
} else {
err += ", AND failed to revert to original from .bak!";
}
throw new LowlevelStorageException(true, err);
} else {
if (!backupFile.delete()) {
logger.warn("Could not delete backup file {}",
backupFile.getPath());
}