{-# LANGUAGE Safe #-}
-----------------------------------------------------------------------------
-- |
-- 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 =
  forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry (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 =
  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry (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 =
  forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry (forall a. Eq a => a -> a -> Bool
== 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 forall a. Eq a => a -> a -> Bool
== Errno
eINTR
          then forall a. (a -> Bool) -> String -> String -> IO a -> IO a
throwErrnoPathIfRetry a -> Bool
pr String
loc String
path IO a
f
          else forall a. String -> String -> IO a
throwErrnoPath String
loc String
path
      else forall (m :: * -> *) a. Monad m => a -> m a
return a
res