{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.Internal.IO.Handle.FD
-- Copyright   :  (c) The University of Glasgow, 1994-2008
-- License     :  see libraries/base/LICENSE
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  internal
-- Portability :  non-portable
--
-- Handle operations implemented by file descriptors (FDs)
--
-- @since base-4.2.0.0
--
-----------------------------------------------------------------------------

module GHC.Internal.IO.Handle.FD (
  stdin, stdout, stderr,
  openFile, withFile,
  openBinaryFile, withBinaryFile,
  openFileBlocking, withFileBlocking,
  mkHandleFromFD, fdToHandle, fdToHandle', handleToFd
 ) where

import GHC.Internal.Base
import GHC.Internal.Show
import GHC.Internal.Control.Exception (try)
import GHC.Internal.Data.Maybe
import GHC.Internal.Data.Either (either)
import GHC.Internal.Data.Typeable
import GHC.Internal.Foreign.C.Types
import GHC.Internal.MVar
import GHC.Internal.IO
import GHC.Internal.IO.Encoding
import GHC.Internal.IO.Device as IODevice
import GHC.Internal.IO.Exception
import GHC.Internal.IO.IOMode
import GHC.Internal.IO.Handle.Types
import GHC.Internal.IO.Handle.Internals
import qualified GHC.Internal.IO.FD as FD
import qualified GHC.Internal.System.Posix.Internals as Posix

-- ---------------------------------------------------------------------------
-- Standard Handles

-- Three handles are allocated during program initialisation.  The first
-- two manage input or output from the Haskell program's standard input
-- or output channel respectively.  The third manages output to the
-- standard error channel. These handles are initially open.

-- | A handle managing input from the Haskell program's standard input channel.
stdin :: Handle
{-# NOINLINE stdin #-}
stdin :: Handle
stdin = IO Handle -> Handle
forall a. IO a -> a
unsafePerformIO (IO Handle -> Handle) -> IO Handle -> Handle
forall a b. (a -> b) -> a -> b
$ do
   -- ToDo: acquire lock
   FD -> IO ()
setBinaryMode FD
FD.stdin
   enc <- IO TextEncoding
getLocaleEncoding
   mkHandle FD.stdin "<stdin>" ReadHandle True (Just enc)
                nativeNewlineMode{-translate newlines-}
                (Just stdHandleFinalizer) Nothing

-- | A handle managing output to the Haskell program's standard output channel.
stdout :: Handle
{-# NOINLINE stdout #-}
stdout :: Handle
stdout = IO Handle -> Handle
forall a. IO a -> a
unsafePerformIO (IO Handle -> Handle) -> IO Handle -> Handle
forall a b. (a -> b) -> a -> b
$ do
   -- ToDo: acquire lock
   FD -> IO ()
setBinaryMode FD
FD.stdout
   enc <- IO TextEncoding
getLocaleEncoding
   mkHandle FD.stdout "<stdout>" WriteHandle True (Just enc)
                nativeNewlineMode{-translate newlines-}
                (Just stdHandleFinalizer) Nothing

-- | A handle managing output to the Haskell program's standard error channel.
stderr :: Handle
{-# NOINLINE stderr #-}
stderr :: Handle
stderr = IO Handle -> Handle
forall a. IO a -> a
unsafePerformIO (IO Handle -> Handle) -> IO Handle -> Handle
forall a b. (a -> b) -> a -> b
$ do
    -- ToDo: acquire lock
   FD -> IO ()
setBinaryMode FD
FD.stderr
   enc <- IO TextEncoding
getLocaleEncoding
   mkHandle FD.stderr "<stderr>" WriteHandle False{-stderr is unbuffered-}
                (Just enc)
                nativeNewlineMode{-translate newlines-}
                (Just stdHandleFinalizer) Nothing

stdHandleFinalizer :: FilePath -> MVar Handle__ -> IO ()
stdHandleFinalizer :: HandleFinalizer
stdHandleFinalizer FilePath
fp MVar Handle__
m = do
  h_ <- MVar Handle__ -> IO Handle__
forall a. MVar a -> IO a
takeMVar MVar Handle__
m
  flushWriteBuffer h_
  case haType h_ of
      HandleType
ClosedHandle -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      HandleType
_other       -> Handle__ -> IO ()
closeTextCodecs Handle__
h_
  putMVar m (ioe_finalizedHandle fp)

-- We have to put the FDs into binary mode on Windows to avoid the newline
-- translation that the CRT IO library does.
setBinaryMode :: FD.FD -> IO ()
#if defined(mingw32_HOST_OS)
setBinaryMode fd = do _ <- setmode (FD.fdFD fd) True
                      return ()
#else
setBinaryMode :: FD -> IO ()
setBinaryMode FD
_ = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
#endif

#if defined(mingw32_HOST_OS)
foreign import ccall unsafe "__hscore_setmode"
  setmode :: CInt -> Bool -> IO CInt
#endif

-- ---------------------------------------------------------------------------
-- Opening and Closing Files

addFilePathToIOError :: String -> FilePath -> IOException -> IOException
addFilePathToIOError :: FilePath -> FilePath -> IOException -> IOException
addFilePathToIOError FilePath
fun FilePath
fp IOException
ioe
  = IOException
ioe{ ioe_location = fun, ioe_filename = Just fp }

-- | Computation 'openFile' @file mode@ allocates and returns a new, open
-- handle to manage the file @file@.  It manages input if @mode@
-- is 'ReadMode', output if @mode@ is 'WriteMode' or 'AppendMode',
-- and both input and output if mode is 'ReadWriteMode'.
--
-- If the file does not exist and it is opened for output, it should be
-- created as a new file.  If @mode@ is 'WriteMode' and the file
-- already exists, then it should be truncated to zero length.
-- Some operating systems delete empty files, so there is no guarantee
-- that the file will exist following an 'openFile' with @mode@
-- 'WriteMode' unless it is subsequently written to successfully.
-- The handle is positioned at the end of the file if @mode@ is
-- 'AppendMode', and otherwise at the beginning (in which case its
-- internal position is 0).
-- The initial buffer mode is implementation-dependent.
--
-- This operation may fail with:
--
--  * 'GHC.Internal.System.IO.Error.isAlreadyInUseError' if the file is already open and
--    cannot be reopened;
--
--  * 'GHC.Internal.System.IO.Error.isDoesNotExistError' if the file does not exist or
--    (on POSIX systems) is a FIFO without a reader and 'WriteMode' was
--    requested; or
--
--  * 'GHC.Internal.System.IO.Error.isPermissionError' if the user does not have permission
--     to open the file.
--
-- On POSIX systems, 'openFile' is an /interruptible operation/ as
-- described in "Control.Exception".
--
-- Note: if you will be working with files containing binary data, you'll want to
-- be using 'openBinaryFile'.
openFile :: FilePath -> IOMode -> IO Handle
openFile :: FilePath -> IOMode -> IO Handle
openFile FilePath
fp IOMode
im =
  IO Handle -> (IOException -> IO Handle) -> IO Handle
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catchException
    (FilePath -> IOMode -> Bool -> Bool -> IO Handle
openFile' FilePath
fp IOMode
im Bool
dEFAULT_OPEN_IN_BINARY_MODE Bool
True)
    (\IOException
e -> IOException -> IO Handle
forall a. IOException -> IO a
ioError (FilePath -> FilePath -> IOException -> IOException
addFilePathToIOError FilePath
"openFile" FilePath
fp IOException
e))

-- | @'withFile' name mode act@ opens a file like 'openFile' and passes
-- the resulting handle to the computation @act@.  The handle will be
-- closed on exit from 'withFile', whether by normal termination or by
-- raising an exception.  If closing the handle raises an exception, then
-- this exception will be raised by 'withFile' rather than any exception
-- raised by @act@.
--
-- @since base-4.16.0.0
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile :: forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile FilePath
fp IOMode
im Handle -> IO r
act = do
  -- Only annotate when setup or teardown of withFile' raised the exception
  IO (Either IOException r)
-> (IOException -> IO (Either IOException r))
-> IO (Either IOException r)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catchException
    (FilePath
-> IOMode
-> Bool
-> Bool
-> (Handle -> IO (Either IOException r))
-> IO (Either IOException r)
forall r.
FilePath -> IOMode -> Bool -> Bool -> (Handle -> IO r) -> IO r
withFile' FilePath
fp IOMode
im Bool
dEFAULT_OPEN_IN_BINARY_MODE Bool
True (IO r -> IO (Either IOException r)
forall e a. Exception e => IO a -> IO (Either e a)
try (IO r -> IO (Either IOException r))
-> (Handle -> IO r) -> Handle -> IO (Either IOException r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO r
act))
    (\IOException
e -> IOException -> IO (Either IOException r)
forall a. IOException -> IO a
ioError (FilePath -> FilePath -> IOException -> IOException
addFilePathToIOError FilePath
"withFile" FilePath
fp IOException
e))
    IO (Either IOException r) -> (Either IOException r -> IO r) -> IO r
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (IOException -> IO r)
-> (r -> IO r) -> Either IOException r -> IO r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either IOException -> IO r
forall a. IOException -> IO a
ioError r -> IO r
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Like 'openFile', but opens the file in ordinary blocking mode.
-- This can be useful for opening a FIFO for writing: if we open in
-- non-blocking mode then the open will fail if there are no readers,
-- whereas a blocking open will block until a reader appear.
--
-- Note: when blocking happens, an OS thread becomes tied up with the
-- processing, so the program must have at least another OS thread if
-- it wants to unblock itself. By corollary, a non-threaded runtime
-- will need a process-external trigger in order to become unblocked.
--
-- On POSIX systems, 'openFileBlocking' is an /interruptible operation/ as
-- described in "Control.Exception".
--
-- @since base-4.4.0.0
openFileBlocking :: FilePath -> IOMode -> IO Handle
openFileBlocking :: FilePath -> IOMode -> IO Handle
openFileBlocking FilePath
fp IOMode
im =
  IO Handle -> (IOException -> IO Handle) -> IO Handle
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catchException
    (FilePath -> IOMode -> Bool -> Bool -> IO Handle
openFile' FilePath
fp IOMode
im Bool
dEFAULT_OPEN_IN_BINARY_MODE Bool
False)
    (\IOException
e -> IOException -> IO Handle
forall a. IOException -> IO a
ioError (FilePath -> FilePath -> IOException -> IOException
addFilePathToIOError FilePath
"openFileBlocking" FilePath
fp IOException
e))

-- | @'withFileBlocking' name mode act@ opens a file like 'openFileBlocking'
-- and passes the resulting handle to the computation @act@.  The handle will
-- be closed on exit from 'withFileBlocking', whether by normal termination or
-- by raising an exception.  If closing the handle raises an exception, then
-- this exception will be raised by 'withFile' rather than any exception raised
-- by @act@.
--
-- @since base-4.16.0.0
withFileBlocking :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFileBlocking :: forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFileBlocking FilePath
fp IOMode
im Handle -> IO r
act = do
  -- Only annotate when setup or teardown of withFile' raised the exception
  IO (Either IOException r)
-> (IOException -> IO (Either IOException r))
-> IO (Either IOException r)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catchException
    (FilePath
-> IOMode
-> Bool
-> Bool
-> (Handle -> IO (Either IOException r))
-> IO (Either IOException r)
forall r.
FilePath -> IOMode -> Bool -> Bool -> (Handle -> IO r) -> IO r
withFile' FilePath
fp IOMode
im Bool
dEFAULT_OPEN_IN_BINARY_MODE Bool
False (IO r -> IO (Either IOException r)
forall e a. Exception e => IO a -> IO (Either e a)
try (IO r -> IO (Either IOException r))
-> (Handle -> IO r) -> Handle -> IO (Either IOException r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO r
act))
    (\IOException
e -> IOException -> IO (Either IOException r)
forall a. IOException -> IO a
ioError (FilePath -> FilePath -> IOException -> IOException
addFilePathToIOError FilePath
"withFileBlocking" FilePath
fp IOException
e))
    IO (Either IOException r) -> (Either IOException r -> IO r) -> IO r
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (IOException -> IO r)
-> (r -> IO r) -> Either IOException r -> IO r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either IOException -> IO r
forall a. IOException -> IO a
ioError r -> IO r
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Like 'openFile', but open the file in binary mode.
-- On Windows, reading a file in text mode (which is the default)
-- will translate CRLF to LF, and writing will translate LF to CRLF.
-- This is usually what you want with text files.  With binary files
-- this is undesirable; also, as usual under Microsoft operating systems,
-- text mode treats control-Z as EOF.  Binary mode turns off all special
-- treatment of end-of-line and end-of-file characters.
-- (See also 'GHC.Internal.System.IO.hSetBinaryMode'.)

-- On POSIX systems, 'openBinaryFile' is an /interruptible operation/ as
-- described in "Control.Exception".
openBinaryFile :: FilePath -> IOMode -> IO Handle
openBinaryFile :: FilePath -> IOMode -> IO Handle
openBinaryFile FilePath
fp IOMode
m =
  IO Handle -> (IOException -> IO Handle) -> IO Handle
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catchException
    (FilePath -> IOMode -> Bool -> Bool -> IO Handle
openFile' FilePath
fp IOMode
m Bool
True Bool
True)
    (\IOException
e -> IOException -> IO Handle
forall a. IOException -> IO a
ioError (FilePath -> FilePath -> IOException -> IOException
addFilePathToIOError FilePath
"openBinaryFile" FilePath
fp IOException
e))

-- | A version of `openBinaryFile` that takes an action to perform
-- with the handle. If an exception occurs in the action, then
-- the file will be closed automatically. The action /should/
-- close the file when finished with it so the file does not remain
-- open until the garbage collector collects the handle.
--
-- @since base-4.16.0.0
withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile :: forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile FilePath
fp IOMode
im Handle -> IO r
act =
  -- Only annotate when setup or teardown of withFile' raised the exception
  IO (Either IOException r)
-> (IOException -> IO (Either IOException r))
-> IO (Either IOException r)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catchException
    (FilePath
-> IOMode
-> Bool
-> Bool
-> (Handle -> IO (Either IOException r))
-> IO (Either IOException r)
forall r.
FilePath -> IOMode -> Bool -> Bool -> (Handle -> IO r) -> IO r
withFile' FilePath
fp IOMode
im Bool
True Bool
True (IO r -> IO (Either IOException r)
forall e a. Exception e => IO a -> IO (Either e a)
try (IO r -> IO (Either IOException r))
-> (Handle -> IO r) -> Handle -> IO (Either IOException r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO r
act))
    (\IOException
e -> IOException -> IO (Either IOException r)
forall a. IOException -> IO a
ioError (FilePath -> FilePath -> IOException -> IOException
addFilePathToIOError FilePath
"withBinaryFile" FilePath
fp IOException
e))
    IO (Either IOException r) -> (Either IOException r -> IO r) -> IO r
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (IOException -> IO r)
-> (r -> IO r) -> Either IOException r -> IO r
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either IOException -> IO r
forall a. IOException -> IO a
ioError r -> IO r
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Open a file and perform an action with it. If the action throws an
-- exception, then the file will be closed. If the last argument is 'True',
-- then the file will be closed on successful completion as well. We use this to
-- implement both the `withFile` family of functions (via `withFile'`) and the
-- `openFile` family (via `openFile'`).
withOpenFile' :: String -> IOMode -> Bool -> Bool -> (Handle -> IO r) -> Bool -> IO r
withOpenFile' :: forall r.
FilePath
-> IOMode -> Bool -> Bool -> (Handle -> IO r) -> Bool -> IO r
withOpenFile' FilePath
filepath IOMode
iomode Bool
binary Bool
non_blocking Handle -> IO r
act Bool
close_finally =
  -- first open the file to get an FD
  FilePath
-> IOMode
-> Bool
-> (FD -> IODeviceType -> IO Handle)
-> ((forall x. IO x -> IO x) -> Handle -> IO r)
-> IO r
forall r s.
FilePath
-> IOMode
-> Bool
-> (FD -> IODeviceType -> IO r)
-> ((forall x. IO x -> IO x) -> r -> IO s)
-> IO s
FD.openFileWith FilePath
filepath IOMode
iomode Bool
non_blocking (\FD
fd IODeviceType
fd_type -> do

      mb_codec <- if Bool
binary then Maybe TextEncoding -> IO (Maybe TextEncoding)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe TextEncoding
forall a. Maybe a
Nothing else (TextEncoding -> Maybe TextEncoding)
-> IO TextEncoding -> IO (Maybe TextEncoding)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TextEncoding -> Maybe TextEncoding
forall a. a -> Maybe a
Just IO TextEncoding
getLocaleEncoding

      -- Then use it to make a Handle. If this fails, openFileWith
      -- will take care of closing the file.
      mkHandleFromFDNoFinalizer fd fd_type filepath iomode
                       False {- do not *set* non-blocking mode -}
                       mb_codec)

    -- Add a finalizer to the handle. This is done under a mask,
    -- so there are no asynchronous exceptions, and (satisfying
    -- the conditions of openFileWith), addHandleFinalizer
    -- cannot throw a synchronous exception.
    (\forall x. IO x -> IO x
restore Handle
hndl -> do
        Handle -> HandleFinalizer -> IO ()
addHandleFinalizer Handle
hndl HandleFinalizer
handleFinalizer
        r <- IO r -> IO r
forall x. IO x -> IO x
restore (Handle -> IO r
act Handle
hndl) IO r -> IO () -> IO r
forall a b. IO a -> IO b -> IO a
`onException` Handle -> IO ()
hClose_impl Handle
hndl
        when close_finally $ hClose_impl hndl
        pure r
        )

        -- ASSERT: if we just created the file, then fdToHandle' won't fail
        -- (so we don't need to worry about removing the newly created file
        --  in the event of an error).

-- | Open a file and perform an action with it. When the action
-- completes or throws/receives an exception, the file will be closed.
withFile' :: String -> IOMode -> Bool -> Bool -> (Handle -> IO r) -> IO r
withFile' :: forall r.
FilePath -> IOMode -> Bool -> Bool -> (Handle -> IO r) -> IO r
withFile' FilePath
filepath IOMode
iomode Bool
binary Bool
non_blocking Handle -> IO r
act =
  FilePath
-> IOMode -> Bool -> Bool -> (Handle -> IO r) -> Bool -> IO r
forall r.
FilePath
-> IOMode -> Bool -> Bool -> (Handle -> IO r) -> Bool -> IO r
withOpenFile' FilePath
filepath IOMode
iomode Bool
binary Bool
non_blocking Handle -> IO r
act Bool
True

openFile' :: String -> IOMode -> Bool -> Bool -> IO Handle
openFile' :: FilePath -> IOMode -> Bool -> Bool -> IO Handle
openFile' FilePath
filepath IOMode
iomode Bool
binary Bool
non_blocking =
  FilePath
-> IOMode
-> Bool
-> Bool
-> (Handle -> IO Handle)
-> Bool
-> IO Handle
forall r.
FilePath
-> IOMode -> Bool -> Bool -> (Handle -> IO r) -> Bool -> IO r
withOpenFile' FilePath
filepath IOMode
iomode Bool
binary Bool
non_blocking Handle -> IO Handle
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False

-- ---------------------------------------------------------------------------
-- Converting file descriptors from/to Handles

mkHandleFromFDNoFinalizer
   :: FD.FD
   -> IODeviceType
   -> FilePath  -- a string describing this file descriptor (e.g. the filename)
   -> IOMode
   -> Bool      --  *set* non-blocking mode on the FD
   -> Maybe TextEncoding
   -> IO Handle

mkHandleFromFDNoFinalizer :: FD
-> IODeviceType
-> FilePath
-> IOMode
-> Bool
-> Maybe TextEncoding
-> IO Handle
mkHandleFromFDNoFinalizer FD
fd0 IODeviceType
fd_type FilePath
filepath IOMode
iomode Bool
set_non_blocking Maybe TextEncoding
mb_codec
  = do
#if !defined(mingw32_HOST_OS)
    -- turn on non-blocking mode
    fd <- if Bool
set_non_blocking
             then FD -> Bool -> IO FD
FD.setNonBlockingMode FD
fd0 Bool
True
             else FD -> IO FD
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return FD
fd0
#else
    let _ = set_non_blocking -- warning suppression
    fd <- return fd0
#endif

    let nl | Maybe TextEncoding -> Bool
forall a. Maybe a -> Bool
isJust Maybe TextEncoding
mb_codec = NewlineMode
nativeNewlineMode
           | Bool
otherwise       = NewlineMode
noNewlineTranslation

    case fd_type of
        IODeviceType
Directory ->
           IOException -> IO Handle
forall a. HasCallStack => IOException -> IO a
ioException (Maybe Handle
-> IOErrorType
-> FilePath
-> FilePath
-> Maybe CInt
-> Maybe FilePath
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
InappropriateType FilePath
"openFile"
                           FilePath
"is a directory" Maybe CInt
forall a. Maybe a
Nothing Maybe FilePath
forall a. Maybe a
Nothing)

        IODeviceType
Stream
           -- only *Streams* can be DuplexHandles.  Other read/write
           -- Handles must share a buffer.
           | IOMode
ReadWriteMode <- IOMode
iomode ->
                FD -> FilePath -> Maybe TextEncoding -> NewlineMode -> IO Handle
forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev -> FilePath -> Maybe TextEncoding -> NewlineMode -> IO Handle
mkDuplexHandleNoFinalizer FD
fd FilePath
filepath Maybe TextEncoding
mb_codec NewlineMode
nl

        IODeviceType
_other ->
           FD
-> FilePath
-> IOMode
-> Maybe TextEncoding
-> NewlineMode
-> IO Handle
forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> FilePath
-> IOMode
-> Maybe TextEncoding
-> NewlineMode
-> IO Handle
mkFileHandleNoFinalizer FD
fd FilePath
filepath IOMode
iomode Maybe TextEncoding
mb_codec NewlineMode
nl

mkHandleFromFD
   :: FD.FD
   -> IODeviceType
   -> FilePath  -- a string describing this file descriptor (e.g. the filename)
   -> IOMode
   -> Bool      --  *set* non-blocking mode on the FD
   -> Maybe TextEncoding
   -> IO Handle
mkHandleFromFD :: FD
-> IODeviceType
-> FilePath
-> IOMode
-> Bool
-> Maybe TextEncoding
-> IO Handle
mkHandleFromFD FD
fd0 IODeviceType
fd_type FilePath
filepath IOMode
iomode Bool
set_non_blocking Maybe TextEncoding
mb_codec = do
  h <- FD
-> IODeviceType
-> FilePath
-> IOMode
-> Bool
-> Maybe TextEncoding
-> IO Handle
mkHandleFromFDNoFinalizer FD
fd0 IODeviceType
fd_type FilePath
filepath IOMode
iomode
                                 Bool
set_non_blocking Maybe TextEncoding
mb_codec
  addHandleFinalizer h handleFinalizer
  pure h

-- | Old API kept to avoid breaking clients
fdToHandle' :: CInt
            -> Maybe IODeviceType
            -> Bool -- is_socket on Win, non-blocking on Unix
            -> FilePath
            -> IOMode
            -> Bool -- binary
            -> IO Handle
fdToHandle' :: CInt
-> Maybe IODeviceType
-> Bool
-> FilePath
-> IOMode
-> Bool
-> IO Handle
fdToHandle' CInt
fdint Maybe IODeviceType
mb_type Bool
is_socket FilePath
filepath IOMode
iomode Bool
binary = do
  let mb_stat :: Maybe (IODeviceType, CDev, CIno)
mb_stat = case Maybe IODeviceType
mb_type of
                        Maybe IODeviceType
Nothing          -> Maybe (IODeviceType, CDev, CIno)
forall a. Maybe a
Nothing
                          -- mkFD will do the stat:
                        Just IODeviceType
RegularFile -> Maybe (IODeviceType, CDev, CIno)
forall a. Maybe a
Nothing
                          -- no stat required for streams etc.:
                        Just IODeviceType
other       -> (IODeviceType, CDev, CIno) -> Maybe (IODeviceType, CDev, CIno)
forall a. a -> Maybe a
Just (IODeviceType
other,CDev
0,CIno
0)
  (fd,fd_type) <- CInt
-> IOMode
-> Maybe (IODeviceType, CDev, CIno)
-> Bool
-> Bool
-> IO (FD, IODeviceType)
FD.mkFD CInt
fdint IOMode
iomode Maybe (IODeviceType, CDev, CIno)
mb_stat
                       Bool
is_socket
                       Bool
is_socket
  enc <- if binary then return Nothing else fmap Just getLocaleEncoding
  mkHandleFromFD fd fd_type filepath iomode is_socket enc


-- | Turn an existing file descriptor into a Handle.  This is used by
-- various external libraries to make Handles.
--
-- Makes a binary Handle.  This is for historical reasons; it should
-- probably be a text Handle with the default encoding and newline
-- translation instead.
fdToHandle :: Posix.FD -> IO Handle
fdToHandle :: CInt -> IO Handle
fdToHandle CInt
fdint = do
   iomode <- CInt -> IO IOMode
Posix.fdGetMode CInt
fdint
   (fd,fd_type) <- FD.mkFD fdint iomode Nothing
            False{-is_socket-}
              -- NB. the is_socket flag is False, meaning that:
              --  on Windows we're guessing this is not a socket (XXX)
            False{-is_nonblock-}
              -- file descriptors that we get from external sources are
              -- not put into non-blocking mode, because that would affect
              -- other users of the file descriptor
   let fd_str = FilePath
"<file descriptor: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FD -> FilePath
forall a. Show a => a -> FilePath
show FD
fd FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
">"
   mkHandleFromFD fd fd_type fd_str iomode False{-non-block-}
                  Nothing -- bin mode

-- | Turn an existing Handle into a file descriptor. This function throws an
-- IOError if the Handle does not reference a file descriptor.
--
-- @since base-4.10.0.0
handleToFd :: Handle -> IO FD.FD
handleToFd :: Handle -> IO FD
handleToFd Handle
h = case Handle
h of
  FileHandle FilePath
_ MVar Handle__
mv -> do
    Handle__{haDevice = dev} <- MVar Handle__ -> IO Handle__
forall a. MVar a -> IO a
readMVar MVar Handle__
mv
    case cast dev of
      Just FD
fd -> FD -> IO FD
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return FD
fd
      Maybe FD
Nothing -> FilePath -> IO FD
forall {a}. FilePath -> IO a
throwErr FilePath
"not a file descriptor"
  DuplexHandle{} -> FilePath -> IO FD
forall {a}. FilePath -> IO a
throwErr FilePath
"not a file handle"
  where
    throwErr :: FilePath -> IO a
throwErr FilePath
msg = IOException -> IO a
forall a. HasCallStack => IOException -> IO a
ioException (IOException -> IO a) -> IOException -> IO a
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> FilePath
-> FilePath
-> Maybe CInt
-> Maybe FilePath
-> IOException
IOError (Handle -> Maybe Handle
forall a. a -> Maybe a
Just Handle
h)
      IOErrorType
InappropriateType FilePath
"handleToFd" FilePath
msg Maybe CInt
forall a. Maybe a
Nothing Maybe FilePath
forall a. Maybe a
Nothing


-- ---------------------------------------------------------------------------
-- Are files opened by default in text or binary mode, if the user doesn't
-- specify?

dEFAULT_OPEN_IN_BINARY_MODE :: Bool
dEFAULT_OPEN_IN_BINARY_MODE :: Bool
dEFAULT_OPEN_IN_BINARY_MODE = Bool
False