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);
}
@Override
public void performLoad() throws ProtectionDatabaseException {
YAMLProcessor config = createYamlProcessor();
YAMLProcessor config = createYamlProcessor(file);
try {
config.load();
@ -260,8 +260,9 @@ private DefaultDomain parseDomain(YAMLNode node) {
}
@Override
protected void performSave() {
YAMLProcessor config = createYamlProcessor();
protected void performSave() throws ProtectionDatabaseException {
File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
YAMLProcessor config = createYamlProcessor(tempFile);
config.clear();
@ -317,6 +318,11 @@ protected void performSave() {
"# REMEMBER TO KEEP PERIODICAL BACKUPS.\r\n" +
"#");
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) {