Editor patterns for file modification
These are patterns that editors might use when writing changes to a file after editing it.
- Truncate-and-rewrite.
open(path, O_WRONLY|O_TRUNC),write(...),close(). - Modify-in-place. Same as above but without truncating. Same considerations
- Write-tempfile-then-rename.
open("path.tmp.XXX", O_WRONLY|O_CREAT),write(...),fsync(),close(),rename("path.tmp.XXX", "path")(e.g. vim) - Backup-rename-write.
rename("path", "path~"),open("path", O_WRONLY|O_CREAT),write(...),close(), optionallyunlink("path~")(e.g. vim with backupfile=no) - macOS-specific atomic replace.
renamex_np()withRENAME_SWAP, orFSReplaceObjectfrom the higher-level APIs. TextEdit, some Apple apps - In-place mmap edits.: (especially anything Electron-based with SQLite, or scientific data tools)
- Lock files and dotfiles. Many editors create
.path.lock,.~lock.path#,.path.swp,path.swo, etc.