handleToFd without closing of the file descriptor

Jeremy Shaw jeremy at n-heptane.com
Mon Jan 31 06:10:48 CET 2011


At line 206 of this file there is a withFd function that might suit your needs,

https://patch-tag.com/r/mae/sendfile/snapshot/current/content/pretty/src/Network/Socket/SendFile/Internal.hs

-- The Fd should not be used after the action returns because the
-- Handler may be garbage collected and than will cause the finalizer
-- to close the fd.
withFd :: Handle -> (Fd -> IO a) -> IO a
#ifdef __GLASGOW_HASKELL__
#if __GLASGOW_HASKELL__ >= 611
withFd h f = withHandle_ "withFd" h $ \ Handle__{..} -> do
  case cast haDevice of
    Nothing -> ioError (ioeSetErrorString (mkIOError IllegalOperation
                                           "withFd" (Just h) Nothing)
                        "handle is not a file descriptor")
    Just fd -> f (Fd (fromIntegral (FD.fdFD fd)))
#else
withFd h f =
    withHandle_ "withFd" h $ \ h_ ->
      f (Fd (fromIntegral (haFD h_)))
#endif
#endif

It uses GHC internals stuff, so it could easily break someday.

On Thu, Jan 27, 2011 at 2:00 PM, Volker Wysk <pf3 at volker-wysk.de> wrote:
> Hello
>
> I need to get the file descriptor, which is encapsulated inside a handle.
> handleToFd gets it, but the fd is closed. Is it possible to extract the fd
> from the handle, without modifying the handle?
>
> Greetings,
> Volker
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
>



More information about the Glasgow-haskell-users mailing list