{-# LINE 1 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Temp
-- Copyright   :  (c) Volker Stolz <vs@foldr.org>
--                    Deian Stefan <deian@cs.stanford.edu>
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org, vs@foldr.org, deian@cs.stanford.edu
-- Stability   :  provisional
-- Portability :  non-portable (requires POSIX)
--
-- POSIX temporary file and directory creation functions.
--
-----------------------------------------------------------------------------

module System.Posix.Temp (
        mkstemp, mkstemps, mkdtemp
    ) where



import Foreign.C
import System.IO

{-# LINE 29 "libraries/unix/System/Posix/Temp.hsc" #-}
import System.Posix.IO
import System.Posix.Types
import System.Posix.Internals (withFilePath, peekFilePath)


{-# LINE 37 "libraries/unix/System/Posix/Temp.hsc" #-}


{-# LINE 45 "libraries/unix/System/Posix/Temp.hsc" #-}

foreign import capi unsafe "HsUnix.h mkstemp"
  c_mkstemp :: CString -> IO CInt

-- | Make a unique filename and open it for reading\/writing. The returned
-- 'FilePath' is the (possibly relative) path of the created file, which is
-- padded with 6 random characters. The argument is the desired prefix of the
-- filepath of the temporary file to be created.
--
-- If you aren't using GHC or Hugs then this function simply wraps mktemp and
-- so shouldn't be considered safe.
mkstemp :: String -> IO (FilePath, Handle)
mkstemp :: String -> IO (String, Handle)
mkstemp String
template' = do
  let template :: String
template = String
template' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"XXXXXX"
  String -> (CString -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (CString -> IO a) -> IO a
withFilePath String
template ((CString -> IO (String, Handle)) -> IO (String, Handle))
-> (CString -> IO (String, Handle)) -> IO (String, 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)
    String
name <- CString -> IO String
peekFilePath CString
ptr
    Handle
h <- Fd -> IO Handle
fdToHandle (CInt -> Fd
Fd CInt
fd)
    (String, Handle) -> IO (String, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (String
name, Handle
h)


{-# LINE 66 "libraries/unix/System/Posix/Temp.hsc" #-}


{-# LINE 68 "libraries/unix/System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemps"
  c_mkstemps :: CString -> CInt -> IO CInt

{-# LINE 71 "libraries/unix/System/Posix/Temp.hsc" #-}

-- | Make a unique filename with a given prefix and suffix and open it for
-- reading\/writing. The returned 'FilePath' is the (possibly relative) path of
-- the created file, which contains  6 random characters in between the prefix
-- and suffix. The first argument is the desired prefix of the filepath of the
-- temporary file to be created. The second argument is the suffix of the
-- temporary file to be created.
--
-- If you are using as system that doesn't support the mkstemps glibc function
-- (supported in glibc > 2.11) then this function simply throws an error.
mkstemps :: String -> String -> IO (FilePath, Handle)

mkstemps :: String -> String -> IO (String, Handle)
{-# LINE 83 "libraries/unix/System/Posix/Temp.hsc" #-}
mkstemps prefix suffix = do
  let template = prefix ++ "XXXXXX" ++ suffix
      lenOfsuf = (fromIntegral $ 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 "libraries/unix/System/Posix/Temp.hsc" #-}


{-# LINE 97 "libraries/unix/System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkdtemp"
  c_mkdtemp :: CString -> IO CString

{-# LINE 100 "libraries/unix/System/Posix/Temp.hsc" #-}

-- | Make a unique directory. The returned 'FilePath' is the path of the
-- created directory, which is padded with 6 random characters. The argument is
-- the desired prefix of the filepath of the temporary directory to be created.
--
-- If you are using as system that doesn't support the mkdtemp glibc function
-- (supported in glibc > 2.1.91) then this function uses mktemp and so
-- shouldn't be considered safe.
mkdtemp :: String -> IO FilePath
mkdtemp :: String -> IO String
mkdtemp String
template' = do
  let template :: String
template = String
template' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"XXXXXX"

{-# LINE 112 "libraries/unix/System/Posix/Temp.hsc" #-}
  withFilePath template $ \ ptr -> do
    _ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
    name <- peekFilePath ptr
    return name

{-# LINE 121 "libraries/unix/System/Posix/Temp.hsc" #-}


{-# LINE 136 "libraries/unix/System/Posix/Temp.hsc" #-}