Hall of FameDan Luu201522 min readintermediate
Files are hard
Summary
The post explains why desktop file‑based apps often corrupt data: filesystem write ordering and crash‑consistency are subtle, and default journaling modes don’t guarantee atomic updates without explicit fsync barriers.
- Use an undo‑log plus fsync on the log, the data file, and the parent directory to get crash‑consistent updates on ext3/ext4 regardless of data mode.
- The default ext3 "ordered" mode can reorder writes; you must add explicit fsync calls to enforce ordering.
- Filesystem semantics differ (ext3, ext4, btrfs, XFS); assuming atomicity or ordering leads to bugs in many apps (git, LevelDB, HDFS).
- On macOS you need fcntl(F_FULLFSYNC) for a true flush; Linux fsync may be a no‑op on some disks or kernels.
Anyone building reliable storage‑oriented software or desktop apps must understand filesystem ordering and crash‑consistency to avoid silent data corruption.
6/10