mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-13 01:16:46 -04:00
config save: write config to temp file and copy it to original
This commit is contained in:
parent
8b22faf696
commit
d0e246e8b4
@ -172,12 +172,14 @@ public class Configuration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void saveToFile(String filename) {
|
public void saveToFile(String filename) {
|
||||||
|
|
||||||
Thread thread = new Thread(() -> {
|
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();
|
Yaml yaml = new Yaml();
|
||||||
FileWriter writer;
|
FileWriter writer;
|
||||||
try {
|
try {
|
||||||
writer = new FileWriter(Config.homeDir + "config/" + filename);
|
writer = new FileWriter(tempFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return;
|
return;
|
||||||
@ -185,7 +187,11 @@ public class Configuration {
|
|||||||
synchronized (config) {
|
synchronized (config) {
|
||||||
yaml.dump(config, writer);
|
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.setName("IO-Thread");
|
||||||
thread.start();
|
thread.start();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user