{-# LINE 1 "System/Posix/Temp/PosixString.hsc" #-}
{-# LANGUAGE CApiFFI #-}
module System.Posix.Temp.PosixString (
mkstemp, mkstemps, mkdtemp
) where
import qualified System.OsPath.Data.ByteString.Short as BC
import Data.Word
import Foreign.C
import System.OsPath.Types
import System.IO
import System.Posix.PosixPath.FilePath
import System.OsString.Internal.Types (PosixString(..))
{-# LINE 35 "System/Posix/Temp/PosixString.hsc" #-}
import System.Posix.IO.PosixString
import System.Posix.Types
{-# LINE 42 "System/Posix/Temp/PosixString.hsc" #-}
{-# LINE 50 "System/Posix/Temp/PosixString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemp"
c_mkstemp :: CString -> IO CInt
mkstemp :: PosixString -> IO (PosixPath, Handle)
mkstemp :: PosixString -> IO (PosixString, Handle)
mkstemp (PosixString ShortByteString
template') = do
let template :: PosixString
template = ShortByteString -> PosixString
PosixString (ShortByteString -> PosixString) -> ShortByteString -> PosixString
forall a b. (a -> b) -> a -> b
$ ShortByteString
template' ShortByteString -> ShortByteString -> ShortByteString
`BC.append` ([Word8] -> ShortByteString
BC.pack [Word8
_X,Word8
_X,Word8
_X,Word8
_X,Word8
_X,Word8
_X])
PosixString
-> (CString -> IO (PosixString, Handle))
-> IO (PosixString, Handle)
forall a. PosixString -> (CString -> IO a) -> IO a
withFilePath PosixString
template ((CString -> IO (PosixString, Handle)) -> IO (PosixString, Handle))
-> (CString -> IO (PosixString, Handle))
-> IO (PosixString, 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)
PosixString
name <- CString -> IO PosixString
peekFilePath CString
ptr
Handle
h <- Fd -> IO Handle
fdToHandle (CInt -> Fd
Fd CInt
fd)
(PosixString, Handle) -> IO (PosixString, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (PosixString
name, Handle
h)
{-# LINE 71 "System/Posix/Temp/PosixString.hsc" #-}
{-# LINE 73 "System/Posix/Temp/PosixString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemps"
c_mkstemps :: CString -> CInt -> IO CInt
{-# LINE 76 "System/Posix/Temp/PosixString.hsc" #-}
mkstemps :: PosixString -> PosixString -> IO (PosixPath, Handle)
mkstemps :: PosixString -> PosixString -> IO (PosixString, Handle)
{-# LINE 84 "System/Posix/Temp/PosixString.hsc" #-}
mkstemps (PosixString prefix) (PosixString suffix) = do
let template = PosixString $ prefix `BC.append` (BC.pack [_X,_X,_X,_X,_X,_X]) `BC.append` suffix
lenOfsuf = (fromIntegral $ BC.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 95 "System/Posix/Temp/PosixString.hsc" #-}
{-# LINE 97 "System/Posix/Temp/PosixString.hsc" #-}
foreign import capi unsafe "HsUnix.h mkdtemp"
c_mkdtemp :: CString -> IO CString
{-# LINE 100 "System/Posix/Temp/PosixString.hsc" #-}
mkdtemp :: PosixString -> IO PosixPath
mkdtemp :: PosixString -> IO PosixString
mkdtemp (PosixString ShortByteString
template') = do
let template :: PosixString
template = ShortByteString -> PosixString
PosixString (ShortByteString -> PosixString) -> ShortByteString -> PosixString
forall a b. (a -> b) -> a -> b
$ ShortByteString
template' ShortByteString -> ShortByteString -> ShortByteString
`BC.append` ([Word8] -> ShortByteString
BC.pack [Word8
_X,Word8
_X,Word8
_X,Word8
_X,Word8
_X,Word8
_X])
{-# LINE 111 "System/Posix/Temp/PosixString.hsc" #-}
withFilePath template $ \ ptr -> do
_ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
name <- peekFilePath ptr
return name
{-# LINE 120 "System/Posix/Temp/PosixString.hsc" #-}
{-# LINE 135 "System/Posix/Temp/PosixString.hsc" #-}
_X :: Word8
_X :: Word8
_X = Word8
0x58