• About
  • Snapshots
  • LTS
  • Nightly
  • FAQ
  • Blog

atomic-write

Atomically write to a file

http://github.com/stackbuilders/atomic-write

Version on this page:0.2.0.7
LTS Haskell 23.23:0.2.1.1
Stackage Nightly 2025-05-20:0.2.1.1
Latest on Hackage:0.2.1.1

See all snapshots atomic-write appears in

MIT licensed by Justin Leitgeb
Maintained by support@stackbuilders.com
This version can be pinned in stack with:atomic-write-0.2.0.7@sha256:3b626dfbc288cd070f1ac31b1c15ddd49822a923778ffe21f92b2116ffc72dc3,4584

Module documentation for 0.2.0.7

  • System
    • System.AtomicWrite
      • System.AtomicWrite.Writer
        • System.AtomicWrite.Writer.ByteString
          • System.AtomicWrite.Writer.ByteString.Binary
        • System.AtomicWrite.Writer.ByteStringBuilder
        • System.AtomicWrite.Writer.LazyByteString
          • System.AtomicWrite.Writer.LazyByteString.Binary
        • System.AtomicWrite.Writer.LazyText
          • System.AtomicWrite.Writer.LazyText.Binary
        • System.AtomicWrite.Writer.String
          • System.AtomicWrite.Writer.String.Binary
        • System.AtomicWrite.Writer.Text
          • System.AtomicWrite.Writer.Text.Binary
Depends on 7 packages(full list with versions):
base, bytestring, directory, filepath, temporary, text, unix-compat
Used by 1 package in lts-19.33(full list with versions):
dhall

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:

  • Ruby on Rails includes a similar method called atomic_write

  • Chef includes atomic update functionality

  • There is a python library for atomically updating a file

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

Stackage is a service provided by the Haskell Foundation │ Originally developed by FP Complete