{-# LINE 1 "src-unix/Lukko/Internal/FD.hsc" #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE InterruptibleFFI #-}
{-# LANGUAGE Trustworthy #-}
module Lukko.Internal.FD (
FD (..),
fdOpen,
fdClose,
handleToFd,
) where
import Data.Bits ((.|.))
import Foreign.C.Error (throwErrnoIfMinus1Retry)
import Foreign.C.Types
import Foreign.C.String (CString, withCString)
import System.IO (Handle)
import System.Posix.Types (CMode (..))
import qualified GHC.IO.FD as GHC (FD (..))
import Lukko.Internal.HandleToFD (ghcHandleToFd)
newtype FD = FD CInt
foreign import ccall interruptible "open"
c_open :: CString -> CInt -> CMode -> IO CInt
foreign import ccall interruptible "close"
c_close :: CInt -> IO CInt
fdOpen :: FilePath -> IO FD
fdOpen fp = withCString fp $ \cfp -> do
fd <- throwErrnoIfMinus1Retry "open" $ c_open cfp flags mode
return (FD fd)
where
flags = 2 .|. 64
{-# LINE 47 "src-unix/Lukko/Internal/FD.hsc" #-}
mode = CMode 0o666
fdClose :: FD -> IO ()
fdClose (FD fd) = do
ret <- throwErrnoIfMinus1Retry "close" $ c_close fd
return ()
handleToFd :: Handle -> IO FD
handleToFd h = do
GHC.FD {GHC.fdFD = fd} <- ghcHandleToFd h
return (FD fd)