Replaces the contents of file_name with data in a safe manner.
First write to a temp file and then rename. Since POSIX renames are atomic, the file is unlikely to be corrupted by competing writes.
We create the tempfile on the same device to ensure that it can be renamed.