config save: write config to temp file and copy it to original

This commit is contained in:
Bixilon 2020-09-01 20:43:44 +02:00
parent 8b22faf696
commit d0e246e8b4
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -172,12 +172,14 @@ public class Configuration {
}
public void saveToFile(String filename) {
Thread thread = new Thread(() -> {
// write config to temp file, delete original config, rename temp file to original file to avoid conflicts if minosoft gets closed while saving the config
File tempFile = new File(Config.homeDir + "config/" + filename + ".tmp");
File file = new File(Config.homeDir + "config/" + filename);
Yaml yaml = new Yaml();
FileWriter writer;
try {
writer = new FileWriter(Config.homeDir + "config/" + filename);
writer = new FileWriter(tempFile);
} catch (IOException e) {
e.printStackTrace();
return;
@ -185,7 +187,11 @@ public class Configuration {
synchronized (config) {
yaml.dump(config, writer);
}
Log.verbose(String.format("Configuration saved to file %s", filename));
if (!file.delete() || !tempFile.renameTo(file)) {
Log.fatal("An error occurred while saving the config file");
} else {
Log.verbose(String.format("Configuration saved to file %s", filename));
}
});
thread.setName("IO-Thread");
thread.start();