{-# LINE 1 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 2 "System/Posix/Temp/ByteString.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LINE 6 "System/Posix/Temp/ByteString.hsc" #-}
module System.Posix.Temp.ByteString (
mkstemp, mkstemps, mkdtemp
) where
{-# LINE 26 "System/Posix/Temp/ByteString.hsc" #-}
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 38 "System/Posix/Temp/ByteString.hsc" #-}
import System.Posix.IO
import System.Posix.Types
{-# LINE 42 "System/Posix/Temp/ByteString.hsc" #-}
foreign import ccall unsafe "HsUnix.h __hscore_mkstemp"
c_mkstemp :: CString -> IO CInt
{-# LINE 45 "System/Posix/Temp/ByteString.hsc" #-}
mkstemp :: ByteString -> IO (RawFilePath, Handle)
mkstemp template' = do
let template = template' `B.append` (BC.pack "XXXXXX")
{-# LINE 57 "System/Posix/Temp/ByteString.hsc" #-}
withFilePath template $ \ ptr -> do
fd <- throwErrnoIfMinus1 "mkstemp" (c_mkstemp ptr)
name <- peekFilePath ptr
h <- fdToHandle (Fd fd)
return (name, h)
{-# LINE 67 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 69 "System/Posix/Temp/ByteString.hsc" #-}
foreign import ccall unsafe "HsUnix.h __hscore_mkstemps"
c_mkstemps :: CString -> CInt -> IO CInt
{-# LINE 72 "System/Posix/Temp/ByteString.hsc" #-}
mkstemps :: ByteString -> ByteString -> IO (RawFilePath, Handle)
{-# LINE 80 "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 91 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 93 "System/Posix/Temp/ByteString.hsc" #-}
foreign import ccall unsafe "HsUnix.h __hscore_mkdtemp"
c_mkdtemp :: CString -> IO CString
{-# LINE 96 "System/Posix/Temp/ByteString.hsc" #-}
mkdtemp :: ByteString -> IO RawFilePath
mkdtemp template' = do
let template = template' `B.append` (BC.pack "XXXXXX")
{-# LINE 107 "System/Posix/Temp/ByteString.hsc" #-}
withFilePath template $ \ ptr -> do
_ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
name <- peekFilePath ptr
return name
{-# LINE 116 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 131 "System/Posix/Temp/ByteString.hsc" #-}