atomic-write: Atomically write to a file

[ library, mit, system ] [ Propose Tags ]

Atomically write to a file on POSIX-compliant systems while preserving permissions.

On most Unix systems, mv is an atomic operation. This makes it simple to write to a file atomically just by using the mv operation. However, this will destroy the permissions on the original file. This library does the following to preserve permissions while atomically writing to a file:

• If an original file exists, take those permissions and apply them to the temp file before mving the file into place.

• If the original file does not exist, create a following with default permissions (based on the currently-active umask).

This way, when the file is mv'ed into place, the permissions will be the ones held by the original file.

This library is based on similar implementations found in common libraries in Ruby and Python:

To use atomic-write, import the module corresponding to the type you wish to write atomically, e.g., to write a (strict) ByteString atomically:

import System.AtomicWrite.Writer.ByteString

Then you can use the atomicWriteFile function that accepts a FilePath and a ByteString, e.g.:

atomicWriteFile myFilePath myByteString