{-# LINE 1 "System/Posix/Directory/Common.hsc" #-}
{-# LANGUAGE Safe, CApiFFI #-}
module System.Posix.Directory.Common (
DirStream(..), CDir, CDirent, DirStreamOffset(..),
unsafeOpenDirStreamFd,
rewindDirStream,
closeDirStream,
{-# LINE 25 "System/Posix/Directory/Common.hsc" #-}
seekDirStream,
{-# LINE 27 "System/Posix/Directory/Common.hsc" #-}
{-# LINE 28 "System/Posix/Directory/Common.hsc" #-}
tellDirStream,
{-# LINE 30 "System/Posix/Directory/Common.hsc" #-}
changeWorkingDirectoryFd,
) where
import Control.Exception (mask_)
import Control.Monad (void, when)
import System.Posix.Types
import Foreign hiding (void)
import Foreign.C
{-# LINE 43 "System/Posix/Directory/Common.hsc" #-}
newtype DirStream = DirStream (Ptr CDir)
data {-# CTYPE "DIR" #-} CDir
data {-# CTYPE "struct dirent" #-} CDirent
unsafeOpenDirStreamFd :: Fd -> IO DirStream
unsafeOpenDirStreamFd (Fd fd) = mask_ $ do
ptr <- c_fdopendir fd
when (ptr == nullPtr) $ do
errno <- getErrno
void $ c_close fd
ioError (errnoToIOError "openDirStreamFd" errno Nothing Nothing)
return $ DirStream ptr
foreign import ccall unsafe "HsUnix.h close"
c_close :: CInt -> IO CInt
foreign import capi unsafe "dirent.h fdopendir"
c_fdopendir :: CInt -> IO (Ptr CDir)
rewindDirStream :: DirStream -> IO ()
rewindDirStream (DirStream dirp) = c_rewinddir dirp
foreign import ccall unsafe "rewinddir"
c_rewinddir :: Ptr CDir -> IO ()
closeDirStream :: DirStream -> IO ()
closeDirStream (DirStream dirp) = do
throwErrnoIfMinus1Retry_ "closeDirStream" (c_closedir dirp)
foreign import ccall unsafe "closedir"
c_closedir :: Ptr CDir -> IO CInt
newtype DirStreamOffset = DirStreamOffset COff
{-# LINE 101 "System/Posix/Directory/Common.hsc" #-}
seekDirStream :: DirStream -> DirStreamOffset -> IO ()
seekDirStream (DirStream dirp) (DirStreamOffset off) =
c_seekdir dirp (fromIntegral off)
foreign import ccall unsafe "seekdir"
c_seekdir :: Ptr CDir -> CLong -> IO ()
{-# LINE 108 "System/Posix/Directory/Common.hsc" #-}
{-# LINE 110 "System/Posix/Directory/Common.hsc" #-}
tellDirStream :: DirStream -> IO DirStreamOffset
tellDirStream (DirStream dirp) = do
off <- c_telldir dirp
return (DirStreamOffset (fromIntegral off))
foreign import ccall unsafe "telldir"
c_telldir :: Ptr CDir -> IO CLong
{-# LINE 118 "System/Posix/Directory/Common.hsc" #-}
{-# LINE 120 "System/Posix/Directory/Common.hsc" #-}
changeWorkingDirectoryFd :: Fd -> IO ()
changeWorkingDirectoryFd (Fd fd) =
throwErrnoIfMinus1Retry_ "changeWorkingDirectoryFd" (c_fchdir fd)
foreign import ccall unsafe "fchdir"
c_fchdir :: CInt -> IO CInt
{-# LINE 135 "System/Posix/Directory/Common.hsc" #-}