{-# LINE 1 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LINE 3 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LINE 7 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
module System.Posix.Temp.ByteString (
mkstemp, mkstemps, mkdtemp
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Foreign.C
import System.IO
import System.Posix.ByteString.FilePath
{-# LINE 39 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
import System.Posix.IO
import System.Posix.Types
foreign import capi unsafe "HsUnix.h mkstemp"
c_mkstemp :: CString -> IO CInt
mkstemp :: ByteString -> IO (RawFilePath, Handle)
mkstemp template' = do
let template = template' `B.append` (BC.pack "XXXXXX")
withFilePath template $ \ ptr -> do
fd <- throwErrnoIfMinus1 "mkstemp" (c_mkstemp ptr)
name <- peekFilePath ptr
h <- fdToHandle (Fd fd)
return (name, h)
{-# LINE 62 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemps"
c_mkstemps :: CString -> CInt -> IO CInt
{-# LINE 65 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
mkstemps :: ByteString -> ByteString -> IO (RawFilePath, Handle)
{-# LINE 73 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
mkstemps prefix suffix = do
let template = prefix `B.append` (BC.pack "XXXXXX") `B.append` suffix
lenOfsuf = (fromIntegral $ B.length suffix) :: CInt
withFilePath template $ \ ptr -> do
fd <- throwErrnoIfMinus1 "mkstemps" (c_mkstemps ptr lenOfsuf)
name <- peekFilePath ptr
h <- fdToHandle (Fd fd)
return (name, h)
{-# LINE 84 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 86 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkdtemp"
c_mkdtemp :: CString -> IO CString
{-# LINE 89 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
mkdtemp :: ByteString -> IO RawFilePath
mkdtemp template' = do
let template = template' `B.append` (BC.pack "XXXXXX")
{-# LINE 100 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
withFilePath template $ \ ptr -> do
_ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
name <- peekFilePath ptr
return name
{-# LINE 109 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 124 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}