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 :: RawFilePath -> IO RawFilePath
readFile RawFilePath
path = forall r. RawFilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile RawFilePath
path IOMode
ReadMode Handle -> IO RawFilePath
hGetContents
writeFile :: RawFilePath -> ByteString -> IO ()
writeFile :: RawFilePath -> RawFilePath -> IO ()
writeFile RawFilePath
path RawFilePath
content = forall r. RawFilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile RawFilePath
path IOMode
WriteMode (Handle -> RawFilePath -> IO ()
`hPut` RawFilePath
content)
appendFile :: RawFilePath -> ByteString -> IO ()
appendFile :: RawFilePath -> RawFilePath -> IO ()
appendFile RawFilePath
path RawFilePath
content = forall r. RawFilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile RawFilePath
path IOMode
AppendMode (Handle -> RawFilePath -> IO ()
`hPut` RawFilePath
content)
withFile :: RawFilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile :: forall r. RawFilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile RawFilePath
path IOMode
ioMode = forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (IO Fd
open forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Fd -> IO Handle
fdToHandle) Handle -> IO ()
hClose
where
#if MIN_VERSION_unix(2,8,0)
open = case ioMode of
ReadMode -> openFd path ReadOnly $ defaultFlags Nothing
WriteMode -> createFile path stdFileMode
AppendMode -> openFd path WriteOnly $ appendFlags $ Just stdFileMode
ReadWriteMode -> openFd path ReadWrite $ defaultFlags $ Just stdFileMode
defaultFlags creat = OpenFileFlags
{ System.Posix.ByteString.append = False
, creat = creat
, exclusive = False
, noctty = True
, nonBlock = False
, trunc = False
, nofollow = False
, cloexec = False
, directory = False
, sync = False
}
appendFlags creat = (defaultFlags creat) { System.Posix.ByteString.append = True }
#else
open :: IO Fd
open = case IOMode
ioMode of
IOMode
ReadMode -> RawFilePath -> OpenMode -> Maybe FileMode -> OpenFileFlags -> IO Fd
openFd RawFilePath
path OpenMode
ReadOnly forall a. Maybe a
Nothing OpenFileFlags
defaultFlags
IOMode
WriteMode -> RawFilePath -> FileMode -> IO Fd
createFile RawFilePath
path FileMode
stdFileMode
IOMode
AppendMode -> RawFilePath -> OpenMode -> Maybe FileMode -> OpenFileFlags -> IO Fd
openFd RawFilePath
path OpenMode
WriteOnly (forall a. a -> Maybe a
Just FileMode
stdFileMode) OpenFileFlags
appendFlags
IOMode
ReadWriteMode -> RawFilePath -> OpenMode -> Maybe FileMode -> OpenFileFlags -> IO Fd
openFd RawFilePath
path OpenMode
ReadWrite (forall a. a -> Maybe a
Just FileMode
stdFileMode) OpenFileFlags
defaultFlags
defaultFlags :: OpenFileFlags
defaultFlags = OpenFileFlags
{ append :: Bool
System.Posix.ByteString.append = Bool
False
, exclusive :: Bool
exclusive = Bool
False
, noctty :: Bool
noctty = Bool
True
, nonBlock :: Bool
nonBlock = Bool
False
, trunc :: Bool
trunc = Bool
False
}
appendFlags :: OpenFileFlags
appendFlags = OpenFileFlags
defaultFlags { append :: Bool
System.Posix.ByteString.append = Bool
True }
#endif