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