This is a hopefully more robust way of doing atomic writing of a file.
Previously, we wrote a new temporary file on each write, and renamed it
over the target file on success. This is technically neat, but
constantly recreating the file causes problems with some network
filesystems, and with sync tools like Dropbox.
The new approach copies the old file contents to a temporary file,
overwrites the target using standard open() and write() calls, and then
removes the temporary file. In case of a failure during writing, the
temporary file is renamed over the target, which should preserve the old
data.
This way, we're only using a new inode in case of a write failure, which
is hopefully rare, instead of on successful writes.