- POSIX：stdio.h標準ヘッダ rename() 関数を利用する。
- Windows：windows.hヘッダ ReplaceFile() 関数が最有力候補。ただしUndocumentedな保証にすぎず、データ部のみがアトミック更新との噂。
This rename() function is equivalent for regular files to that defined by the ISO C standard. Its inclusion here expands that definition to include actions on directories and specifies behavior when the new parameter names a file that already exists. That specification requires that the action of the function be atomic.
Applications updating a single file with "document-like" dataAlternatives to using Transactional NTFS
Many applications which deal with "document-like" data tend to load the entire document into memory, operate on it, and then write it back out to save the changes. The needed atomicity here is that the changes either are completely applied or not applied at all, as an inconsistent state would render the file corrupt. A common approach is to write the document to a new file, then replace the original file with the new one. One method to do this is with the ReplaceFile API.
Microsoft Research発行のTechnical Report (PDF)"To BLOB or Not To BLOB: Large Object Storage in a Database or a Filesystem?"より一部引用。
2.2. Safe writes
(snip) Under UNIX, rename() is guaranteed to atomically overwrite the old version of the file. Under Windows, the ReplaceFile() call is used to atomically replace one file with another.
Microsoft intern here. I had this problem, so I asked a guy who worked on NTFS. The part where data is moved is atomic, so while it can be interrupted while the file attributes are being modified, the part where data itself is moved is atomic. - zneak Jul 17 '14 at 22:13