atomic_files
File writes a crash cannot leave half-done.
create_exclusively is first-writer-wins, for files that define an identity
and must therefore be created exactly once -- the compute's key pair and
installation id. Two processes starting together on an empty
directory each generate a candidate; whichever creates the file first wins,
and the other discards its candidate and loads the file. Neither needs to
know the other exists.
replace_atomically is for a file that is rewritten in full, where a reader
must see either the old contents or the new ones and never a prefix of the
new. open(path, "w") cannot offer that: it truncates before anything is
written, so the whole write is a window in which the file is incomplete.
Module
Functions
create_exclusively
def create_exclusively(path: Path, data: bytes, *, mode: int = 384) ‑> bool:Create path holding data, but only if it does not already exist.
data is written in full to a private temporary file first, which is
then linked into path. The link is atomic against every other process
doing the same thing, so exactly one caller can ever succeed for a given
path -- and, unlike creating path itself with O_EXCL, a loser can
never observe path after it exists but before the winner's write has
landed.
Arguments
path: The file to create. Parent directories are created as needed.data: The bytes to write into the new file.mode: Permission bits for the new file. Defaults to owner-only, the right default for key material.
Returns
True if this call created the file. False if it already existed,
in which case nothing was written and data should be discarded in
favour of the file's contents.
is_group_or_world_accessible
def is_group_or_world_accessible(path: Path) ‑> bool:Whether path's mode lets accounts other than its owner reach it.
Use this before deciding to rewrite an existing file purely to tighten it: a file written by an earlier version of the code, at the process umask, keeps those bits for as long as nothing rewrites it.
Always False on Windows. Access there is governed by ACLs rather than
mode bits, and os.chmod only toggles the read-only flag, so st_mode
comes back 0o666 for any writable file however it was created. Answering
from that would send a caller into rewriting the file on every check,
forever, without changing anything.
replace_atomically
def replace_atomically(path: Path, data: bytes, *, mode: int = 384) ‑> None:Replace path's contents with data, atomically.
data is written to a temporary file in the same directory, flushed to
disk, and then moved onto path. A reader therefore sees either the
previous contents or the new ones, and a crash at any point leaves one or
the other rather than a prefix.
Prefer this to open(path, "w") for any file whose partial contents would
be misread as meaningful. The motivating case is a store that is loaded
with yaml.safe_load(...) or {}, where a truncated file parses as "no
entries" rather than failing, and the caller cannot tell the difference.
Arguments
path: The file to replace. Parent directories are created as needed.data: The bytes the file should hold afterwards.mode: Permission bits for the file. Defaults to owner-only; pass a wider mode for a file other accounts must read.