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

{-# LINE 2 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
{-# LANGUAGE Trustworthy #-}

{-# LINE 4 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Temp.ByteString
-- 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.ByteString (
        mkstemp, mkstemps, mkdtemp
    ) where


{-# LINE 24 "libraries/unix/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 36 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
import System.Posix.IO
import System.Posix.Types


{-# LINE 40 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
foreign import ccall unsafe "HsUnix.h __hscore_mkstemp"
  c_mkstemp :: CString -> IO CInt

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

-- | Make a unique filename and open it for reading\/writing. The returned
-- 'RawFilePath' 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 :: ByteString -> IO (RawFilePath, Handle)
mkstemp template' = do
  let template = template' `B.append` (BC.pack "XXXXXX")

{-# LINE 55 "libraries/unix/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 65 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}


{-# LINE 67 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
foreign import ccall unsafe "HsUnix.h __hscore_mkstemps"
  c_mkstemps :: CString -> CInt -> IO CInt

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

-- |'mkstemps' - make a unique filename with a given prefix and suffix 
-- and open it for reading\/writing (only safe on GHC & Hugs).
-- The returned 'RawFilePath' is the (possibly relative) path of
-- the created file, which contains  6 random characters in between
-- the prefix and suffix.
mkstemps :: ByteString -> ByteString -> IO (RawFilePath, Handle)

{-# LINE 78 "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 89 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}


{-# LINE 91 "libraries/unix/System/Posix/Temp/ByteString.hsc" #-}
foreign import ccall unsafe "HsUnix.h __hscore_mkdtemp"
  c_mkdtemp :: CString -> IO CString

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

-- | Make a unique directory. The returned 'RawFilePath' 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 aren't using GHC or Hugs then this function simply wraps mktemp and
-- so shouldn't be considered safe.
mkdtemp :: ByteString -> IO RawFilePath
mkdtemp template' = do
  let template = template' `B.append` (BC.pack "XXXXXX")

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

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


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