module System.Posix.Handle.Internals (
withHandleFd
) where
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_)
import System.IO
import System.Posix.Types
withHandleFd :: String -> Handle -> (Fd -> IO a) -> IO a
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)))