mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-05 02:19:46 +01:00
Saved YML files then move for reliability reasons
Instead of saving on top of a YML file, we now save to a tmp file and if it is saved successfully, then it is moved on top of the old file in an atomic move. This helps avoid data loss if the file write fails.
This commit is contained in:
parent
c03c1e3ecc
commit
8353d266a5
@ -92,7 +92,15 @@ public class FlatFileDatabaseConnecter implements DatabaseConnecter {
|
|||||||
tableFolder.mkdirs();
|
tableFolder.mkdirs();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
yamlConfig.save(file);
|
// Approach is save to temp file (saving is not necessarily atomic), then move file atomically
|
||||||
|
// This has best chance of no file corruption
|
||||||
|
File tmpFile = File.createTempFile(tableName, ".tmp", tableFolder);
|
||||||
|
yamlConfig.save(tmpFile);
|
||||||
|
if (tmpFile.exists()) {
|
||||||
|
Files.move(tmpFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
|
||||||
|
} else {
|
||||||
|
throw new Exception();
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
plugin.logError("Could not save yaml file to database " + tableName + " " + fileName + " " + e.getMessage());
|
plugin.logError("Could not save yaml file to database " + tableName + " " + fileName + " " + e.getMessage());
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user