https://github.com/luispedro/safeio
Haskell Library for safe (atomic) IO
https://github.com/luispedro/safeio
haskell io
Last synced: 6 months ago
JSON representation
Haskell Library for safe (atomic) IO
- Host: GitHub
- URL: https://github.com/luispedro/safeio
- Owner: luispedro
- License: other
- Created: 2017-05-29T14:20:48.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-06-26T15:04:14.000Z (over 2 years ago)
- Last Synced: 2024-04-25T23:31:19.074Z (over 1 year ago)
- Topics: haskell, io
- Language: Haskell
- Size: 22.5 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog
- License: COPYING
Awesome Lists containing this project
README
# SafeIO: Haskell library for safe (atomic) IO
[](https://hackage.haskell.org/package/safeio)
[](http://packdeps.haskellers.com/feed?needle=safeio)
[](http://stackage.org/lts/package/safeio)
[](https://travis-ci.com/luispedro/safeio)
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](https://www.stackage.org/package/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](mailto:luis@luispedro.org) | [Twitter](https://twitter.com/luispedrocoelho)