Replaces file with content so readers see the old content or the new, never a partial file:
the content goes to a temporary file in the same directory, which then takes the file's place
with a single rename. An existing file keeps its permissions. A crash or error leaves the
original intact and removes the temporary file.
With expected set, the file is re-read immediately before the swap and the write is refused if
another writer got there first. For expected: null the swap is exact (link fails if the file
exists). For existing files the check narrows the race to the instant between the re-read and
the rename but does not close it against another process; use withFileLock to
serialize writers within this one.
Replaces
filewithcontentso readers see the old content or the new, never a partial file: the content goes to a temporary file in the same directory, which then takes the file's place with a singlerename. An existing file keeps its permissions. A crash or error leaves the original intact and removes the temporary file.With
expectedset, the file is re-read immediately before the swap and the write is refused if another writer got there first. Forexpected: nullthe swap is exact (linkfails if the file exists). For existing files the check narrows the race to the instant between the re-read and therenamebut does not close it against another process; use withFileLock to serialize writers within this one.