module System.AtomicWrite.Writer.String (atomicWriteFile, atomicWithFile, atomicWriteFileWithMode, atomicWithFileAndMode) where
import System.AtomicWrite.Internal (closeAndRename, maybeSetFileMode,
tempFileFor)
import System.IO (Handle, hPutStr)
import System.Posix.Types (FileMode)
atomicWriteFile ::
FilePath
-> String
-> IO ()
atomicWriteFile = (. flip hPutStr) . atomicWithFile
atomicWriteFileWithMode ::
FileMode
-> FilePath
-> String
-> IO ()
atomicWriteFileWithMode mode = ( . flip hPutStr)
. atomicWithFileAndMode mode
atomicWithFile :: FilePath -> (Handle -> IO ()) -> IO ()
atomicWithFile = atomicWithFileAndMaybeMode Nothing
atomicWithFileAndMode :: FileMode
-> FilePath
-> (Handle -> IO ())
-> IO ()
atomicWithFileAndMode = atomicWithFileAndMaybeMode . Just
atomicWithFileAndMaybeMode :: Maybe FileMode
-> FilePath
-> (Handle -> IO ())
-> IO ()
atomicWithFileAndMaybeMode mmode path action =
tempFileFor path >>= \(tmpPath, h) -> action h
>> closeAndRename h tmpPath path
>> maybeSetFileMode path mmode