{-# LINE 1 "System/Posix/PosixPath/FilePath.hsc" #-}{-# LANGUAGE CPP #-}{-# LANGUAGE TypeApplications #-}{-# LANGUAGE PatternSynonyms #-}{-# LANGUAGE PackageImports #-}------------------------------------------------------------------------------- |-- 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-------------------------------------------------------------------------------moduleSystem.Posix.PosixPath.FilePath(withFilePath ,peekFilePath ,peekFilePathLen ,throwErrnoPathIfMinus1Retry ,throwErrnoPathIfMinus1Retry_ ,throwErrnoPathIfNullRetry ,throwErrnoPathIfRetry ,throwErrnoPath ,throwErrnoPathIf ,throwErrnoPathIf_ ,throwErrnoPathIfNull ,throwErrnoPathIfMinus1 ,throwErrnoPathIfMinus1_ ,throwErrnoTwoPathsIfMinus1_ )whereimportForeignhiding(void)importForeign.Chiding(throwErrnoPath,throwErrnoPathIf,throwErrnoPathIf_,throwErrnoPathIfNull,throwErrnoPathIfMinus1,throwErrnoPathIfMinus1_)importSystem.OsPath.TypesimportData.ByteString.Internal(c_strlen)importControl.MonadimportControl.ExceptionimportSystem.OsPath.PosixasPS{-# LINE 52 "System/Posix/PosixPath/FilePath.hsc" #-}import"filepath"System.OsPath.Data.ByteString.ShortasBSS{-# LINE 54 "System/Posix/PosixPath/FilePath.hsc" #-}importPreludehiding(FilePath)importSystem.OsString.Internal.Types(PosixString(..),patternPS)importGHC.IO.Exception{-# LINE 61 "System/Posix/PosixPath/FilePath.hsc" #-}withFilePath ::PosixPath->(CString->IOa )->IOa withFilePath :: forall a. PosixPath -> (CString -> IO a) -> IO a withFilePath PosixPath path =PosixPath -> (CString -> IO a) -> IO a forall a. PosixPath -> (CString -> IO a) -> IO a useAsCStringSafe PosixPath path peekFilePath ::CString->IOPosixPathpeekFilePath :: 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 fmapShortByteString -> 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 packCStringpeekFilePathLen ::CStringLen->IOPosixPathpeekFilePathLen :: 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 fmapShortByteString -> 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 packCStringLenthrowErrnoPathIfMinus1Retry ::(Eqa ,Numa )=>String->PosixPath->IOa ->IOa 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_ ::(Eqa ,Numa )=>String->PosixPath->IOa ->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(Ptra )->IO(Ptra )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->IOa ->IOa throwErrnoPathIfRetry :: forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a throwErrnoPathIfRetry a -> Bool pr String loc PosixPath rpath IO a f =doa res <-IO a f ifa -> Bool pr a res thendoErrno err <-IO Errno getErrnoifErrno err Errno -> Errno -> Bool forall a. Eq a => a -> a -> Bool ==Errno eINTRthen(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 elseString -> PosixPath -> IO a forall a. String -> PosixPath -> IO a throwErrnoPath String loc PosixPath rpath elsea -> IO a forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a returna res -- | as 'throwErrno', but exceptions include the given path when appropriate.--throwErrnoPath ::String->PosixPath->IOa throwErrnoPath :: forall a. String -> PosixPath -> IO a throwErrnoPath String loc PosixPath path =doErrno errno <-IO Errno getErrnoString path' <-(IOException -> String) -> (String -> String) -> Either IOException String -> String forall a c b. (a -> c) -> (b -> c) -> Either a b -> c either(String -> IOException -> String forall a b. a -> b -> a const(PosixPath -> String _toStr PosixPath path ))String -> String forall a. a -> a id(Either IOException String -> String) -> IO (Either IOException String) -> IO String forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$>forall e a. Exception e => IO a -> IO (Either e a) try@IOException(PosixPath -> IO String PS.decodeFSPosixPath path )IOException -> IO a forall a. IOException -> IO a ioError(String -> Errno -> Maybe Handle -> Maybe String -> IOException errnoToIOErrorString loc Errno errno Maybe Handle forall a. Maybe a Nothing(String -> Maybe String forall a. a -> Maybe a JustString path' ))-- | as 'throwErrnoIf', but exceptions include the given path when-- appropriate.--throwErrnoPathIf ::(a ->Bool)->String->PosixPath->IOa ->IOa throwErrnoPathIf :: forall a. (a -> Bool) -> String -> PosixPath -> IO a -> IO a throwErrnoPathIf a -> Bool cond String loc PosixPath path IO a f =doa res <-IO a f ifa -> Bool cond a res thenString -> PosixPath -> IO a forall a. String -> PosixPath -> IO a throwErrnoPath String loc PosixPath path elsea -> IO a forall a. a -> IO a forall (m :: * -> *) a. Monad m => a -> m a returna res -- | as 'throwErrnoIf_', but exceptions include the given path when-- appropriate.--throwErrnoPathIf_ ::(a ->Bool)->String->PosixPath->IOa ->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(Ptra )->IO(Ptra )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 ::(Eqa ,Numa )=>String->PosixPath->IOa ->IOa 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_ ::(Eqa ,Numa )=>String->PosixPath->IOa ->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_ ::(Eqa ,Numa )=>String->PosixPath->PosixPath->IOa ->IO()throwErrnoTwoPathsIfMinus1_ :: forall a. (Eq a, Num a) => String -> PosixPath -> PosixPath -> IO a -> IO () throwErrnoTwoPathsIfMinus1_ String loc PosixPath path1 PosixPath path2 IO a action =doString path1' <-(IOException -> String) -> (String -> String) -> Either IOException String -> String forall a c b. (a -> c) -> (b -> c) -> Either a b -> c either(String -> IOException -> String forall a b. a -> b -> a const(PosixPath -> String _toStr PosixPath path1 ))String -> String forall a. a -> a id(Either IOException String -> String) -> IO (Either IOException String) -> IO String forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$>forall e a. Exception e => IO a -> IO (Either e a) try@IOException(PosixPath -> IO String PS.decodeFSPosixPath path1 )String path2' <-(IOException -> String) -> (String -> String) -> Either IOException String -> String forall a c b. (a -> c) -> (b -> c) -> Either a b -> c either(String -> IOException -> String forall a b. a -> b -> a const(PosixPath -> String _toStr PosixPath path2 ))String -> String forall a. a -> a id(Either IOException String -> String) -> IO (Either IOException String) -> IO String forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$>forall e a. Exception e => IO a -> IO (Either e a) try@IOException(PosixPath -> IO String PS.decodeFSPosixPath 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 <>String path1' String -> String -> String forall a. Semigroup a => a -> a -> a <>String "' to '"String -> String -> String forall a. Semigroup a => a -> a -> a <>String path2' String -> String -> String forall a. Semigroup a => a -> a -> a <>String "'")IO a action _toStr ::PosixPath->String_toStr :: PosixPath -> String _toStr =(PosixChar -> Char) -> [PosixChar] -> String forall a b. (a -> b) -> [a] -> [b] forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b fmapPosixChar -> Char PS.toChar([PosixChar] -> String) -> (PosixPath -> [PosixChar]) -> PosixPath -> String forall b c a. (b -> c) -> (a -> b) -> a -> c .PosixPath -> [PosixChar] PS.unpack-- | Wrapper around 'useAsCString', checking the encoded 'FilePath' for internal NUL octets as these are-- disallowed in POSIX filepaths. See https://gitlab.haskell.org/ghc/ghc/-/issues/13660useAsCStringSafe ::PosixPath->(CString->IOa )->IOa useAsCStringSafe :: forall a. PosixPath -> (CString -> IO a) -> IO a useAsCStringSafe pp :: PosixPath pp @(PSShortByteString path )CString -> IO a f =ShortByteString -> (CString -> IO a) -> IO a forall a. ShortByteString -> (CString -> IO a) -> IO a useAsCStringShortByteString path ((CString -> IO a) -> IO a) -> (CString -> IO a) -> IO a forall a b. (a -> b) -> a -> b $\CString ptr ->doletlen :: Int len =ShortByteString -> Int BSS.lengthShortByteString path CSize clen <-CString -> IO CSize c_strlenCString ptr ifCSize clen CSize -> CSize -> Bool forall a. Eq a => a -> a -> Bool ==Int -> CSize forall a b. (Integral a, Num b) => a -> b fromIntegralInt len thenCString -> IO a f CString ptr elsedoString path' <-(IOException -> String) -> (String -> String) -> Either IOException String -> String forall a c b. (a -> c) -> (b -> c) -> Either a b -> c either(String -> IOException -> String forall a b. a -> b -> a const(PosixPath -> String _toStr PosixPath pp ))String -> String forall a. a -> a id(Either IOException String -> String) -> IO (Either IOException String) -> IO String forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b <$>forall e a. Exception e => IO a -> IO (Either e a) try@IOException(PosixPath -> IO String PS.decodeFSPosixPath pp )IOException -> IO a forall a. IOException -> IO a ioError(String -> IOException err String path' )whereerr :: String -> IOException err String path' =IOError{ioe_handle :: Maybe Handle ioe_handle =Maybe Handle forall a. Maybe a Nothing,ioe_type :: IOErrorType ioe_type =IOErrorType InvalidArgument,ioe_location :: String ioe_location =String "checkForInteriorNuls",ioe_description :: String ioe_description =String "POSIX filepaths must not contain internal NUL octets.",ioe_errno :: Maybe CInt ioe_errno =Maybe CInt forall a. Maybe a Nothing,ioe_filename :: Maybe String ioe_filename =String -> Maybe String forall a. a -> Maybe a JustString path' }