safeio

Write output to disk atomically

LTS Haskell 9.14:0.0.4.0
Stackage Nightly 2017-11-23:0.0.4.0
Latest on Hackage:0.0.4.0
MIT licensed and maintained by Luis Pedro Coelho

Module documentation for 0.0.4.0

SafeIO: Haskell library for safe (atomic) IO

This is a simple module, which enables writing in atomic mode. It implements the following 4 step procedure:

  1. Open a temporary file in the same directory as the final output.
  2. Write to this temporary file.
  3. Close and sync the file.
  4. Atomically rename the file to its final destination.

Example

Direct use:

import System.IO.SafeWrite
...
main = do
    withOutputFile "output.txt" $ \hout -> do
        hPutStrLn hout "Hello World"

Through conduit:

import qualified Data.Conduit as C
import           Data.Conduit ((.|))
import           Data.Conduit.SafeWrite

main = C.runConduitRes $
    C.yield "Hello World" .| safeSinkFile "hello.txt"

In any case, only successful termination of the process will result in the output file being written. Early termination by throwing an exception will cause the temporary file to be removed and no output will be produced.

Author

Luis Pedro Coelho | Email | Twitter

Changes

Version 0.0.4.0 2017-09-07 by luispedro
* Add atomicConduitUseFile

Version 0.0.3.0 2017-07-29 by luispedro
* Fix issue with creating files in separate directories

Version 0.0.2.0 2017-05-30 by luispedro
* Add conduit version

Version 0.0.1.0 2017-05-29 by luispedro
* First version
comments powered byDisqus