module Data.ByteString.RawFilePath
( module Data.ByteString
, RawFilePath
, readFile
, writeFile
, appendFile
, withFile
) where
import Prelude hiding (readFile, writeFile, appendFile)
import Control.Exception (bracket)
import System.IO (IOMode(..), Handle, hClose)
import System.Posix.ByteString
import Data.ByteString hiding (readFile, writeFile, appendFile)
readFile :: RawFilePath -> IO ByteString
readFile path = withFile path ReadMode hGetContents
writeFile :: RawFilePath -> ByteString -> IO ()
writeFile path content = withFile path WriteMode (`hPut` content)
appendFile :: RawFilePath -> ByteString -> IO ()
appendFile path content = withFile path AppendMode (`hPut` content)
withFile :: RawFilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile path ioMode = bracket (open >>= fdToHandle) hClose
where
open = case ioMode of
ReadMode -> openFd path ReadOnly Nothing defaultFlags
WriteMode -> createFile path stdFileMode
AppendMode -> openFd path WriteOnly (Just stdFileMode) appendFlags
ReadWriteMode -> openFd path ReadWrite (Just stdFileMode) defaultFlags
defaultFlags = OpenFileFlags
{ System.Posix.ByteString.append = False
, exclusive = False
, noctty = True
, nonBlock = False
, trunc = False
}
appendFlags = defaultFlags { System.Posix.ByteString.append = True }