add a new writeFileAtomic() function
Summary:
Add a utility function to more safely set a file's contents by writing to a
temporary file first, then renaming the temporary file into place. On Linux
systems where renames are atomic, this ensures that the operation either
succeeds or that the old file state is left unchanged on failure.
Note that unlike most of the other APIs in FileUtil.h, I intentionally made
writeFileAtomic() throw an exception on failure. This is implemented using a
lower-level writeFileAtomicNoThrow() version. Callers who care about the
exception overhead can use the lower level version instead. (Exception
overhead should be relatively low compared to the file I/O operations, though,
so I suspect most users will prefer the throwing APIs.)
Reviewed By: yfeldblum
Differential Revision:
D4253964
fbshipit-source-id:
5301e2791b82c6f90b63bb509b0411841c266705