{-# OPTIONS_HADDOCK hide #-} module System.Posix.Handle.Internals ( withHandleFd ) where #ifdef __GLASGOW_HASKELL__ #if __GLASGOW_HASKELL__ >= 611 import GHC.IO.Exception (IOErrorType(..)) import GHC.IO.FD (fdFD) import Data.Dynamic (cast) import System.IO.Error import GHC.IO.Handle.Types (Handle__(..)) import GHC.IO.Handle.Internals (withHandle_) #else import GHC.IOBase (haFD) import GHC.Handle (withHandle_) #endif #endif import System.IO import System.Posix.Types withHandleFd :: String -> Handle -> (Fd -> IO a) -> IO a #ifdef __GLASGOW_HASKELL__ #if __GLASGOW_HASKELL__ >= 611 withHandleFd s h f = withHandle_ s h $ \(Handle__{haDevice=dev}) -> case cast dev of Nothing -> ioError (ioeSetErrorString (mkIOError IllegalOperation s (Just h) Nothing) "handle is not a file descriptor") Just fd -> f (Fd (fromIntegral (fdFD fd))) #else withHandleFd s h f = withHandle_ s h $ \h_ -> f (Fd (fromIntegral (haFD h_))) #endif #endif