When creating a file like this:
1. Create file
2. Write file
3. Close file
there is an opening before the file is written and closed where, if the
application were to crash (or not finish for any other reason), the file
would be left in an undefined state. Instead, this change updates it to
do this:
1. Create temp file
2. Write temp file
3. Close temp file
4. Move temp file to final path
This ensures that at step 3 we have a valid file, before we rename it.
We should never end up with a final file in an undefined state, but it is
possible to end up with a temp file that is an undefined state if we,
for instance, crash during step 2.