fix(persistence): atomic + owner-only settings/address-book writes
settings.json and addressbook.json were written in place with a bare ofstream — a crash or power loss mid-write truncated the file, and on the next launch the parse failure silently reset every preference (hidden/favorite addresses, labels, pool workers, language, theme, lite-server list) because the next save overwrote the corrupt file with defaults. Add Platform::writeFileAtomically() (temp file -> fsync -> atomic rename; dir fsync on POSIX, MoveFileEx on Windows; optional owner-only 0600) and route both saves through it. On a parse failure, quarantine the unreadable settings file to settings.json.corrupt-<ts> instead of clobbering it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -74,6 +74,26 @@ public:
|
||||
* @return true if successful or file didn't exist
|
||||
*/
|
||||
static bool deleteFile(const std::string& path);
|
||||
|
||||
/**
|
||||
* @brief Write a file atomically and durably.
|
||||
*
|
||||
* Writes @p content to `<path>.tmp`, flushes it to stable storage
|
||||
* (fsync / FlushFileBuffers), then atomically renames it over @p path
|
||||
* (POSIX rename / Win32 MoveFileEx with REPLACE_EXISTING). A crash or power
|
||||
* loss at any point leaves either the old file intact or the fully-written
|
||||
* new one — never a truncated/corrupt file. The parent directory is created
|
||||
* if missing, and on POSIX it is fsync'd so the rename itself is durable.
|
||||
*
|
||||
* @param path Destination file path.
|
||||
* @param content Bytes to write.
|
||||
* @param restrictPermissions When true, create the file owner-only (0600 on
|
||||
* POSIX) before the rename so it is never briefly world-readable.
|
||||
* @return true on success; on failure any pre-existing file is left untouched.
|
||||
*/
|
||||
static bool writeFileAtomically(const std::string& path,
|
||||
const std::string& content,
|
||||
bool restrictPermissions = false);
|
||||
|
||||
/**
|
||||
* @brief Get the directory containing the executable
|
||||
|
||||
Reference in New Issue
Block a user