{-# LINE 1 "System/Posix/Directory/ByteString.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE NondecreasingIndentation #-}
{-# LANGUAGE Safe #-}
{-# LINE 25 "System/Posix/Directory/ByteString.hsc" #-}
module System.Posix.Directory.ByteString (
createDirectory, removeDirectory,
DirStream,
openDirStream,
readDirStream,
readDirStreamMaybe,
rewindDirStream,
closeDirStream,
DirStreamOffset,
{-# LINE 39 "System/Posix/Directory/ByteString.hsc" #-}
tellDirStream,
{-# LINE 41 "System/Posix/Directory/ByteString.hsc" #-}
{-# LINE 42 "System/Posix/Directory/ByteString.hsc" #-}
seekDirStream,
{-# LINE 44 "System/Posix/Directory/ByteString.hsc" #-}
getWorkingDirectory,
changeWorkingDirectory,
changeWorkingDirectoryFd,
) where
import Data.Maybe
import System.Posix.Types
import Foreign
import Foreign.C
import Data.ByteString.Char8 as BC
import System.Posix.Directory.Common
import System.Posix.ByteString.FilePath
createDirectory :: RawFilePath -> FileMode -> IO ()
createDirectory :: RawFilePath -> FileMode -> IO ()
createDirectory RawFilePath
name FileMode
mode =
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
"createDirectory" RawFilePath
name (CString -> FileMode -> IO CInt
c_mkdir CString
s FileMode
mode)
foreign import ccall unsafe "mkdir"
c_mkdir :: CString -> CMode -> IO CInt
openDirStream :: RawFilePath -> IO DirStream
openDirStream :: RawFilePath -> IO DirStream
openDirStream RawFilePath
name =
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
name forall a b. (a -> b) -> a -> b
$ \CString
s -> do
Ptr CDir
dirp <- forall a. String -> RawFilePath -> IO (Ptr a) -> IO (Ptr a)
throwErrnoPathIfNullRetry String
"openDirStream" RawFilePath
name forall a b. (a -> b) -> a -> b
$ CString -> IO (Ptr CDir)
c_opendir CString
s
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr CDir -> DirStream
DirStream Ptr CDir
dirp)
foreign import capi unsafe "HsUnix.h opendir"
c_opendir :: CString -> IO (Ptr CDir)
readDirStream :: DirStream -> IO RawFilePath
readDirStream :: DirStream -> IO RawFilePath
readDirStream = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe RawFilePath
BC.empty) forall b c a. (b -> c) -> (a -> b) -> a -> c
. DirStream -> IO (Maybe RawFilePath)
readDirStreamMaybe
readDirStreamMaybe :: DirStream -> IO (Maybe RawFilePath)
readDirStreamMaybe :: DirStream -> IO (Maybe RawFilePath)
readDirStreamMaybe (DirStream Ptr CDir
dirp) =
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr CDirent)
ptr_dEnt -> Ptr (Ptr CDirent) -> IO (Maybe RawFilePath)
loop Ptr (Ptr CDirent)
ptr_dEnt
where
loop :: Ptr (Ptr CDirent) -> IO (Maybe RawFilePath)
loop Ptr (Ptr CDirent)
ptr_dEnt = do
IO ()
resetErrno
CInt
r <- Ptr CDir -> Ptr (Ptr CDirent) -> IO CInt
c_readdir Ptr CDir
dirp Ptr (Ptr CDirent)
ptr_dEnt
if (CInt
r forall a. Eq a => a -> a -> Bool
== CInt
0)
then do Ptr CDirent
dEnt <- forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CDirent)
ptr_dEnt
if (Ptr CDirent
dEnt forall a. Eq a => a -> a -> Bool
== forall a. Ptr a
nullPtr)
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else do
RawFilePath
entry <- (Ptr CDirent -> IO CString
d_name Ptr CDirent
dEnt forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO RawFilePath
peekFilePath)
Ptr CDirent -> IO ()
c_freeDirEnt Ptr CDirent
dEnt
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just RawFilePath
entry
else do Errno
errno <- IO Errno
getErrno
if (Errno
errno forall a. Eq a => a -> a -> Bool
== Errno
eINTR) then Ptr (Ptr CDirent) -> IO (Maybe RawFilePath)
loop Ptr (Ptr CDirent)
ptr_dEnt else do
let (Errno CInt
eo) = Errno
errno
if (CInt
eo forall a. Eq a => a -> a -> Bool
== CInt
0)
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else forall a. String -> IO a
throwErrno String
"readDirStream"
foreign import ccall unsafe "__hscore_readdir"
c_readdir :: Ptr CDir -> Ptr (Ptr CDirent) -> IO CInt
foreign import ccall unsafe "__hscore_free_dirent"
c_freeDirEnt :: Ptr CDirent -> IO ()
foreign import ccall unsafe "__hscore_d_name"
d_name :: Ptr CDirent -> IO CString
getWorkingDirectory :: IO RawFilePath
getWorkingDirectory :: IO RawFilePath
getWorkingDirectory = Int -> IO RawFilePath
go (Int
4096)
{-# LINE 138 "System/Posix/Directory/ByteString.hsc" #-}
where
go :: Int -> IO RawFilePath
go Int
bytes = do
Maybe RawFilePath
r <- forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
bytes forall a b. (a -> b) -> a -> b
$ \CString
buf -> do
CString
buf' <- CString -> CSize -> IO CString
c_getcwd CString
buf (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bytes)
if CString
buf' forall a. Eq a => a -> a -> Bool
/= forall a. Ptr a
nullPtr
then do RawFilePath
s <- CString -> IO RawFilePath
peekFilePath CString
buf
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just RawFilePath
s)
else do Errno
errno <- IO Errno
getErrno
if Errno
errno forall a. Eq a => a -> a -> Bool
== Errno
eRANGE
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else forall a. String -> IO a
throwErrno String
"getWorkingDirectory"
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> IO RawFilePath
go (Int
2 forall a. Num a => a -> a -> a
* Int
bytes)) forall (m :: * -> *) a. Monad m => a -> m a
return Maybe RawFilePath
r
foreign import ccall unsafe "getcwd"
c_getcwd :: Ptr CChar -> CSize -> IO (Ptr CChar)
changeWorkingDirectory :: RawFilePath -> IO ()
changeWorkingDirectory :: RawFilePath -> IO ()
changeWorkingDirectory RawFilePath
path =
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
path forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
"changeWorkingDirectory" RawFilePath
path (CString -> IO CInt
c_chdir CString
s)
foreign import ccall unsafe "chdir"
c_chdir :: CString -> IO CInt
removeDirectory :: RawFilePath -> IO ()
removeDirectory :: RawFilePath -> IO ()
removeDirectory RawFilePath
path =
forall a. RawFilePath -> (CString -> IO a) -> IO a
withFilePath RawFilePath
path forall a b. (a -> b) -> a -> b
$ \CString
s ->
forall a. (Eq a, Num a) => String -> RawFilePath -> IO a -> IO ()
throwErrnoPathIfMinus1Retry_ String
"removeDirectory" RawFilePath
path (CString -> IO CInt
c_rmdir CString
s)
foreign import ccall unsafe "rmdir"
c_rmdir :: CString -> IO CInt