Save YAML regions database to a temporary file before renaming.

This commit is contained in:
sk89q 2014-08-02 21:22:32 -07:00
parent 504fceceeb
commit 29f804b9f0

View File

@ -87,13 +87,13 @@ public YAMLDatabase(File file, Logger logger) throws ProtectionDatabaseException
} }
} }
private YAMLProcessor createYamlProcessor() { private YAMLProcessor createYamlProcessor(File file) {
return new YAMLProcessor(file, false, YAMLFormat.COMPACT); return new YAMLProcessor(file, false, YAMLFormat.COMPACT);
} }
@Override @Override
public void performLoad() throws ProtectionDatabaseException { public void performLoad() throws ProtectionDatabaseException {
YAMLProcessor config = createYamlProcessor(); YAMLProcessor config = createYamlProcessor(file);
try { try {
config.load(); config.load();
@ -260,8 +260,9 @@ private DefaultDomain parseDomain(YAMLNode node) {
} }
@Override @Override
protected void performSave() { protected void performSave() throws ProtectionDatabaseException {
YAMLProcessor config = createYamlProcessor(); File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
YAMLProcessor config = createYamlProcessor(tempFile);
config.clear(); config.clear();
@ -317,6 +318,11 @@ protected void performSave() {
"# REMEMBER TO KEEP PERIODICAL BACKUPS.\r\n" + "# REMEMBER TO KEEP PERIODICAL BACKUPS.\r\n" +
"#"); "#");
config.save(); config.save();
file.delete();
if (!tempFile.renameTo(file)) {
throw new ProtectionDatabaseException("Failed to rename temporary regions file to " + file.getAbsolutePath());
}
} }
private Map<String, Object> getFlagData(ProtectedRegion region) { private Map<String, Object> getFlagData(ProtectedRegion region) {