{-# LINE 1 "libraries/unix/System/Posix/PosixPath/FilePath.hsc" #-}
{-# LANGUAGE CPP #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.PosixPath.FilePath
-- 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)
--
-- Internal stuff: support for ByteString FilePaths
--
-----------------------------------------------------------------------------

module System.Posix.PosixPath.FilePath (
     withFilePath, peekFilePath, peekFilePathLen,
     throwErrnoPathIfMinus1Retry,
     throwErrnoPathIfMinus1Retry_,
     throwErrnoPathIfNullRetry,
     throwErrnoPathIfRetry,
     throwErrnoPath,
     throwErrnoPathIf,
     throwErrnoPathIf_,
     throwErrnoPathIfNull,
     throwErrnoPathIfMinus1,
     throwErrnoPathIfMinus1_,
     throwErrnoTwoPathsIfMinus1_
  ) where

import Foreign hiding ( void )
import Foreign.C hiding (
     throwErrnoPath,
     throwErrnoPathIf,
     throwErrnoPathIf_,
     throwErrnoPathIfNull,
     throwErrnoPathIfMinus1,
     throwErrnoPathIfMinus1_ )

import System.OsPath.Types
import Control.Monad
import GHC.IO.Encoding.UTF8 ( mkUTF8 )
import GHC.IO.Encoding.Failure ( CodingFailureMode(..) )
import System.OsPath.Posix
import System.OsPath.Data.ByteString.Short
import Prelude hiding (FilePath)
import System.OsString.Internal.Types (PosixString(..))

{-# LINE 52 "libraries/unix/System/Posix/PosixPath/FilePath.hsc" #-}


withFilePath :: PosixPath -> (CString -> IO a) -> IO a
withFilePath :: forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath = ShortByteString -> (CString -> IO a) -> IO a
forall a. ShortByteString -> (CString -> IO a) -> IO a
useAsCString (ShortByteString -> (CString -> IO a) -> IO a)
-> (PosixPath -> ShortByteString)
-> PosixPath
-> (CString -> IO a)
-> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PosixPath -> ShortByteString
getPosixString

peekFilePath :: CString -> IO PosixPath
peekFilePath :: CString -> IO PosixPath
peekFilePath = (ShortByteString -> PosixPath)
-> IO ShortByteString -> IO PosixPath
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ShortByteString -> PosixPath
PosixString (IO ShortByteString -> IO PosixPath)
-> (CString -> IO ShortByteString) -> CString -> IO PosixPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CString -> IO ShortByteString
packCString

peekFilePathLen :: CStringLen -> IO PosixPath
peekFilePathLen :: CStringLen -> IO PosixPath
peekFilePathLen = (ShortByteString -> PosixPath)
-> IO ShortByteString -> IO PosixPath
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ShortByteString -> PosixPath
PosixString (IO ShortByteString -> IO PosixPath)
-> (CStringLen -> IO ShortByteString) -> CStringLen -> IO PosixPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CStringLen -> IO ShortByteString
packCStringLen


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

throwErrnoPathIfMinus1Retry_ :: (Eq a, Num a)
                             => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ :: forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
loc PosixPath
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 -> PosixPath -> IO a -> IO a
forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIfRetry (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== -a
1) String
loc PosixPath
path IO a
f

throwErrnoPathIfNullRetry :: String -> PosixPath -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNullRetry :: forall a. String -> PosixPath -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNullRetry String
loc PosixPath
path IO (Ptr a)
f =
  (Ptr a -> Bool) -> String -> PosixPath -> IO (Ptr a) -> IO (Ptr a)
forall a. (a -> Bool) -> String -> PosixPath -> 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 PosixPath
path IO (Ptr a)
f

throwErrnoPathIfRetry :: (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIfRetry :: forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIfRetry a -> Bool
pr String
loc PosixPath
rpath 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 -> PosixPath -> IO a -> IO a
forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIfRetry a -> Bool
pr String
loc PosixPath
rpath IO a
f
          else String -> PosixPath -> IO a
forall a. String -> PosixPath -> IO a
throwErrnoPath String
loc PosixPath
rpath
      else a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res

-- | as 'throwErrno', but exceptions include the given path when appropriate.
--
throwErrnoPath :: String -> PosixPath -> IO a
throwErrnoPath :: forall a. String -> PosixPath -> IO a
throwErrnoPath String
loc PosixPath
path =
  do
    Errno
errno <- IO Errno
getErrno
    IOError -> IO a
forall a. IOError -> IO a
ioError (String -> Errno -> Maybe Handle -> Maybe String -> IOError
errnoToIOError String
loc Errno
errno Maybe Handle
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just (PosixPath -> String
_toStr PosixPath
path)))

-- | as 'throwErrnoIf', but exceptions include the given path when
--   appropriate.
--
throwErrnoPathIf :: (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIf :: forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIf a -> Bool
cond String
loc PosixPath
path IO a
f =
  do
    a
res <- IO a
f
    if a -> Bool
cond a
res then String -> PosixPath -> IO a
forall a. String -> PosixPath -> IO a
throwErrnoPath String
loc PosixPath
path else a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
res

-- | as 'throwErrnoIf_', but exceptions include the given path when
--   appropriate.
--
throwErrnoPathIf_ :: (a -> Bool) -> String -> PosixPath -> IO a -> IO ()
throwErrnoPathIf_ :: forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO ()
throwErrnoPathIf_ a -> Bool
cond String
loc PosixPath
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 -> PosixPath -> IO a -> IO a
forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIf a -> Bool
cond String
loc PosixPath
path IO a
f

-- | as 'throwErrnoIfNull', but exceptions include the given path when
--   appropriate.
--
throwErrnoPathIfNull :: String -> PosixPath -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNull :: forall a. String -> PosixPath -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNull  = (Ptr a -> Bool) -> String -> PosixPath -> IO (Ptr a) -> IO (Ptr a)
forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIf (Ptr a -> Ptr a -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr a
forall a. Ptr a
nullPtr)

-- | as 'throwErrnoIfMinus1', but exceptions include the given path when
--   appropriate.
--
throwErrnoPathIfMinus1 :: (Eq a, Num a) => String -> PosixPath -> IO a -> IO a
throwErrnoPathIfMinus1 :: forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO a
throwErrnoPathIfMinus1 = (a -> Bool) -> String -> PosixPath -> IO a -> IO a
forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIf (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== -a
1)

-- | as 'throwErrnoIfMinus1_', but exceptions include the given path when
--   appropriate.
--
throwErrnoPathIfMinus1_ :: (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_ :: forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1_  = (a -> Bool) -> String -> PosixPath -> IO a -> IO ()
forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO ()
throwErrnoPathIf_ (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== -a
1)

-- | as 'throwErrnoTwoPathsIfMinus1_', but exceptions include two paths when appropriate.
--
throwErrnoTwoPathsIfMinus1_ :: (Eq a, Num a) => String -> PosixPath -> PosixPath -> IO a -> IO ()
throwErrnoTwoPathsIfMinus1_ :: forall a.
(Eq a, Num a) =>
String -> PosixPath -> PosixPath -> IO a -> IO ()
throwErrnoTwoPathsIfMinus1_  String
loc PosixPath
path1 PosixPath
path2 =
    String -> IO a -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ (String
loc String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" '" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> PosixPath -> String
_toStr PosixPath
path1 String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"' to '" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> PosixPath -> String
_toStr PosixPath
path2 String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"'")


_toStr :: PosixPath -> String
_toStr :: PosixPath -> String
_toStr PosixPath
fp = (EncodingException -> String)
-> (String -> String) -> Either EncodingException String -> String
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> String
forall a. HasCallStack => String -> a
error (String -> String)
-> (EncodingException -> String) -> EncodingException -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodingException -> String
forall a. Show a => a -> String
show) String -> String
forall a. a -> a
id (Either EncodingException String -> String)
-> Either EncodingException String -> String
forall a b. (a -> b) -> a -> b
$ TextEncoding -> PosixPath -> Either EncodingException String
decodeWith (CodingFailureMode -> TextEncoding
mkUTF8 CodingFailureMode
TransliterateCodingFailure) PosixPath
fp