{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 709
{-# LANGUAGE Safe #-}
#else
{-# LANGUAGE Trustworthy #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Error
-- Copyright   :  (c) The University of Glasgow 2002
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  non-portable (requires POSIX)
--
-- POSIX error support
--
-----------------------------------------------------------------------------

module System.Posix.Error (
        throwErrnoPath,
        throwErrnoPathIf,
        throwErrnoPathIf_,
        throwErrnoPathIfRetry,
        throwErrnoPathIfNull,
        throwErrnoPathIfNullRetry,
        throwErrnoPathIfMinus1,
        throwErrnoPathIfMinus1_,
        throwErrnoPathIfMinus1Retry,
        throwErrnoPathIfMinus1Retry_
  ) where

import Foreign hiding (void)
import Foreign.C
import Control.Monad

throwErrnoPathIfMinus1Retry :: (Eq a, Num a)
                            => String -> FilePath -> IO a -> IO a
throwErrnoPathIfMinus1Retry :: forall a. (Eq a, Num a) => String -> String -> IO a -> IO a
throwErrnoPathIfMinus1Retry String
loc String
path IO a
f =
  (a -> Bool) -> String -> String -> IO a -> IO a
forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== -a
1) String
loc String
path IO a
f

throwErrnoPathIfMinus1Retry_ :: (Eq a, Num a)
                             => String -> FilePath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ :: forall a. (Eq a, Num a) => String -> String -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
loc String
path IO a
f =
  IO a -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO a -> IO ()) -> IO a -> IO ()
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> String -> String -> IO a -> IO a
forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== -a
1) String
loc String
path IO a
f

throwErrnoPathIfNullRetry :: String -> FilePath -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNullRetry :: forall a. String -> String -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNullRetry String
loc String
path IO (Ptr a)
f =
  (Ptr a -> Bool) -> String -> String -> IO (Ptr a) -> IO (Ptr a)
forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry (Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
forall a. Ptr a
nullPtr) String
loc String
path IO (Ptr a)
f

throwErrnoPathIfRetry :: (a -> Bool) -> String -> FilePath -> IO a -> IO a
throwErrnoPathIfRetry :: forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry a -> Bool
pr String
loc String
path IO a
f =
  do
    a
res <- IO a
f
    if a -> Bool
pr a
res
      then do
        Errno
err <- IO Errno
getErrno
        if Errno
err Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eINTR
          then (a -> Bool) -> String -> String -> IO a -> IO a
forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry a -> Bool
pr String
loc String
path IO a
f
          else String -> String -> IO a
forall a. String -> String -> IO a
throwErrnoPath String
loc String
path
      else a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res