Skip to main content

Module atomic_write

Module atomic_write 

Source
Expand description

Atomic JSON file writes shared by every on-disk config the project maintains (preferences.json, hardware.json, per-dashboard files under dashboards/).

The pattern is the same every time:

  1. mkdir -p the parent.
  2. Open a per-process-unique sibling .tmp.<pid>.<counter> with O_CREAT|O_EXCL so two writers can’t trample each other.
  3. Write the serialized body, then fsync(2) so the bytes hit stable storage before the rename swaps the inode.
  4. rename(2) onto the final path. On rename failure clean up the tmp sibling so it doesn’t accumulate.

Living in linsight-core means the daemon’s NicknameStore, the GUI’s PreferencesModel, and the dashboards storage all share one tested implementation — diverging behavior (e.g., one of them missing the fsync, another missing the cleanup) caused minor bugs before this lift.

Functions§

atomic_write_json
Serialize value as pretty JSON and write it to path atomically. Errors propagate as std::io::Error (serialization errors are wrapped via Error::other). Use ? to surface them; the caller decides whether to log / retry / abort.