{-# LINE 1 "libraries/unix/System/Posix/Directory/PosixPath.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE NondecreasingIndentation #-}
{-# LINE 24 "libraries/unix/System/Posix/Directory/PosixPath.hsc" #-}
module System.Posix.Directory.PosixPath (
createDirectory, removeDirectory,
Common.DirStream,
openDirStream,
readDirStream,
readDirStreamMaybe,
Common.rewindDirStream,
Common.closeDirStream,
Common.DirStreamOffset,
{-# LINE 38 "libraries/unix/System/Posix/Directory/PosixPath.hsc" #-}
Common.tellDirStream,
{-# LINE 40 "libraries/unix/System/Posix/Directory/PosixPath.hsc" #-}
{-# LINE 41 "libraries/unix/System/Posix/Directory/PosixPath.hsc" #-}
Common.seekDirStream,
{-# LINE 43 "libraries/unix/System/Posix/Directory/PosixPath.hsc" #-}
getWorkingDirectory,
changeWorkingDirectory,
Common.changeWorkingDirectoryFd,
) where
import Control.Monad ((>=>))
import Data.Maybe
import System.Posix.Types
import Foreign
import Foreign.C
import System.OsPath.Posix
import qualified System.Posix.Directory.Common as Common
import System.Posix.PosixPath.FilePath
createDirectory :: PosixPath -> FileMode -> IO ()
createDirectory :: PosixPath -> FileMode -> IO ()
createDirectory PosixPath
name FileMode
mode =
PosixPath -> (CString -> IO ()) -> IO ()
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> PosixPath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
"createDirectory" PosixPath
name (CString -> FileMode -> IO CInt
c_mkdir CString
s FileMode
mode)
foreign import ccall unsafe "mkdir"
c_mkdir :: CString -> CMode -> IO CInt
openDirStream :: PosixPath -> IO Common.DirStream
openDirStream :: PosixPath -> IO DirStream
openDirStream PosixPath
name =
PosixPath -> (CString -> IO DirStream) -> IO DirStream
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
name ((CString -> IO DirStream) -> IO DirStream)
-> (CString -> IO DirStream) -> IO DirStream
forall a b. (a -> b) -> a -> b
$ \CString
s -> do
Ptr CDir
dirp <- String -> PosixPath -> IO (Ptr CDir) -> IO (Ptr CDir)
forall a. String -> PosixPath -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNullRetry String
"openDirStream" PosixPath
name (IO (Ptr CDir) -> IO (Ptr CDir)) -> IO (Ptr CDir) -> IO (Ptr CDir)
forall a b. (a -> b) -> a -> b
$ CString -> IO (Ptr CDir)
c_opendir CString
s
DirStream -> IO DirStream
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr CDir -> DirStream
Common.DirStream Ptr CDir
dirp)
foreign import capi unsafe "HsUnix.h opendir"
c_opendir :: CString -> IO (Ptr Common.CDir)
readDirStream :: Common.DirStream -> IO PosixPath
readDirStream :: DirStream -> IO PosixPath
readDirStream = (Maybe PosixPath -> PosixPath)
-> IO (Maybe PosixPath) -> IO PosixPath
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PosixPath -> Maybe PosixPath -> PosixPath
forall a. a -> Maybe a -> a
fromMaybe PosixPath
forall a. Monoid a => a
mempty) (IO (Maybe PosixPath) -> IO PosixPath)
-> (DirStream -> IO (Maybe PosixPath)) -> DirStream -> IO PosixPath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DirStream -> IO (Maybe PosixPath)
readDirStreamMaybe
readDirStreamMaybe :: Common.DirStream -> IO (Maybe PosixPath)
readDirStreamMaybe :: DirStream -> IO (Maybe PosixPath)
readDirStreamMaybe = (DirEnt -> IO PosixPath) -> DirStream -> IO (Maybe PosixPath)
forall a. (DirEnt -> IO a) -> DirStream -> IO (Maybe a)
Common.readDirStreamWith
(DirEnt -> IO CString
Common.dirEntName (DirEnt -> IO CString)
-> (CString -> IO PosixPath) -> DirEnt -> IO PosixPath
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> CString -> IO PosixPath
peekFilePath)
getWorkingDirectory :: IO PosixPath
getWorkingDirectory :: IO PosixPath
getWorkingDirectory = Int -> IO PosixPath
go (Int
4096)
{-# LINE 109 "libraries/unix/System/Posix/Directory/PosixPath.hsc" #-}
where
go :: Int -> IO PosixPath
go Int
bytes = do
Maybe PosixPath
r <- Int -> (CString -> IO (Maybe PosixPath)) -> IO (Maybe PosixPath)
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
bytes ((CString -> IO (Maybe PosixPath)) -> IO (Maybe PosixPath))
-> (CString -> IO (Maybe PosixPath)) -> IO (Maybe PosixPath)
forall a b. (a -> b) -> a -> b
$ \CString
buf -> do
CString
buf' <- CString -> CSize -> IO CString
c_getcwd CString
buf (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bytes)
if CString
buf' CString -> CString -> Bool
forall a. Eq a => a -> a -> Bool
/= CString
forall a. Ptr a
nullPtr
then do PosixPath
s <- CString -> IO PosixPath
peekFilePath CString
buf
Maybe PosixPath -> IO (Maybe PosixPath)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (PosixPath -> Maybe PosixPath
forall a. a -> Maybe a
Just PosixPath
s)
else do Errno
errno <- IO Errno
getErrno
if Errno
errno Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eRANGE
then Maybe PosixPath -> IO (Maybe PosixPath)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe PosixPath
forall a. Maybe a
Nothing
else String -> IO (Maybe PosixPath)
forall a. String -> IO a
throwErrno String
"getWorkingDirectory"
IO PosixPath
-> (PosixPath -> IO PosixPath) -> Maybe PosixPath -> IO PosixPath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> IO PosixPath
go (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
bytes)) PosixPath -> IO PosixPath
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe PosixPath
r
foreign import ccall unsafe "getcwd"
c_getcwd :: Ptr CChar -> CSize -> IO (Ptr CChar)
changeWorkingDirectory :: PosixPath -> IO ()
changeWorkingDirectory :: PosixPath -> IO ()
changeWorkingDirectory PosixPath
path =
PosixPath -> (CString -> IO ()) -> IO ()
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
path ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> PosixPath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
"changeWorkingDirectory" PosixPath
path (CString -> IO CInt
c_chdir CString
s)
foreign import ccall unsafe "chdir"
c_chdir :: CString -> IO CInt
removeDirectory :: PosixPath -> IO ()
removeDirectory :: PosixPath -> IO ()
removeDirectory PosixPath
path =
PosixPath -> (CString -> IO ()) -> IO ()
forall a. PosixPath -> (CString -> IO a) -> IO a
withFilePath PosixPath
path ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
String -> PosixPath -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> PosixPath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
"removeDirectory" PosixPath
path (CString -> IO CInt
c_rmdir CString
s)
foreign import ccall unsafe "rmdir"
c_rmdir :: CString -> IO CInt