File renaming should be the last operation of an atomic write

That way there is just no possible race conditions.

cc @wisq
If you're going to use `File.rename` (which you should, because `FileUtils.mv` is not atomic) you'll need to ensure that `temp_dir` is somewhere on the same filesystem.  (Generally, the best place is the same directory you're creating the file in.)