Please write temporary files to the same directory that the final file is in, that is the simplest way to drop the temp file onto the same filesystem. If the later move/rename crosses filesystems it isn't atomic since a full create/copy procedure is required.