Add AtomicWriteFile to replace a file's contents in one step #17
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Writing a configuration file, a cache or a state file with
os.WriteFiletruncates the old contents first, so a crash or a concurrent reader in the
middle of the write sees a half-written or empty file. Writing to a temporary
file in the same directory and renaming it into place avoids that, and it is
short enough that everyone writes their own slightly wrong version.
Definition of done:
AtomicWriteFile(path string, data []byte, perm os.FileMode) errorwrites data to a temporary file beside path, flushes it, sets its mode to perm
and renames it over path, removing the temporary file if any step fails. It has
a doc comment and table-driven tests covering a new file, replacing an existing
file, an empty payload, the mode of the finished file, that no temporary files
are left behind, and a path in a directory that does not exist.
Model: opus-5