{-# LINE 1 "System/Posix/Temp/ByteString.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Safe #-}
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 35 "System/Posix/Temp/ByteString.hsc" #-}
import System.Posix.IO
import System.Posix.Types
{-# LINE 42 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 50 "System/Posix/Temp/ByteString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemp"
c_mkstemp :: CString -> IO CInt
mkstemp :: ByteString -> IO (RawFilePath, Handle)
mkstemp :: ByteString -> IO (ByteString, Handle)
mkstemp ByteString
template' = do
let template :: ByteString
template = ByteString
template' ByteString -> ByteString -> ByteString
`B.append` (String -> ByteString
BC.pack String
"XXXXXX")
ByteString
-> (CString -> IO (ByteString, Handle)) -> IO (ByteString, Handle)
forall a. ByteString -> (CString -> IO a) -> IO a
withFilePath ByteString
template ((CString -> IO (ByteString, Handle)) -> IO (ByteString, Handle))
-> (CString -> IO (ByteString, Handle)) -> IO (ByteString, Handle)
forall a b. (a -> b) -> a -> b
$ \ CString
ptr -> do
CInt
fd <- String -> IO CInt -> IO CInt
forall a. (Eq a, Num a) => String -> IO a -> IO a
throwErrnoIfMinus1 String
"mkstemp" (CString -> IO CInt
c_mkstemp CString
ptr)
ByteString
name <- CString -> IO ByteString
peekFilePath CString
ptr
Handle
h <- Fd -> IO Handle
fdToHandle (CInt -> Fd
Fd CInt
fd)
(ByteString, Handle) -> IO (ByteString, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
name, Handle
h)
{-# LINE 71 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 73 "System/Posix/Temp/ByteString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemps"
c_mkstemps :: CString -> CInt -> IO CInt
{-# LINE 76 "System/Posix/Temp/ByteString.hsc" #-}
mkstemps :: ByteString -> ByteString -> IO (RawFilePath, Handle)
mkstemps :: ByteString -> ByteString -> IO (ByteString, Handle)
{-# LINE 84 "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 96 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 98 "System/Posix/Temp/ByteString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkdtemp"
c_mkdtemp :: CString -> IO CString
{-# LINE 101 "System/Posix/Temp/ByteString.hsc" #-}
mkdtemp :: ByteString -> IO RawFilePath
mkdtemp :: ByteString -> IO ByteString
mkdtemp ByteString
template' = do
let template :: ByteString
template = ByteString
template' ByteString -> ByteString -> ByteString
`B.append` (String -> ByteString
BC.pack String
"XXXXXX")
{-# LINE 112 "System/Posix/Temp/ByteString.hsc" #-}
withFilePath template $ \ ptr -> do
_ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
name <- peekFilePath ptr
return name
{-# LINE 121 "System/Posix/Temp/ByteString.hsc" #-}
{-# LINE 136 "System/Posix/Temp/ByteString.hsc" #-}