SafeFile: use atomic rename-with-overwrite, rather than non-atomic delete-then-rename (#9296)
This commit is contained in:
+2
-10
@@ -54,7 +54,7 @@ size_t SafeFile::write(const uint8_t *buffer, size_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Atomically close the file (deleting any old versions) and readback the contents to confirm the hash matches
|
* Atomically close the file (overwriting any old version) and readback the contents to confirm the hash matches
|
||||||
*
|
*
|
||||||
* @return false for failure
|
* @return false for failure
|
||||||
*/
|
*/
|
||||||
@@ -73,15 +73,7 @@ bool SafeFile::close()
|
|||||||
if (!testReadback())
|
if (!testReadback())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
{ // Scope for lock
|
// Rename or overwrite (atomic operation)
|
||||||
concurrency::LockGuard g(spiLock);
|
|
||||||
// brief window of risk here ;-)
|
|
||||||
if (fullAtomic && FSCom.exists(filename.c_str()) && !FSCom.remove(filename.c_str())) {
|
|
||||||
LOG_ERROR("Can't remove old pref file");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String filenameTmp = filename;
|
String filenameTmp = filename;
|
||||||
filenameTmp += ".tmp";
|
filenameTmp += ".tmp";
|
||||||
if (!renameFile(filenameTmp.c_str(), filename.c_str())) {
|
if (!renameFile(filenameTmp.c_str(), filename.c_str())) {
|
||||||
|
|||||||
Reference in New Issue
Block a user