{-# LINE 1 "System/Posix/Directory.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE NondecreasingIndentation #-}
{-# LANGUAGE Safe #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  System.Posix.Directory
-- 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)
--
-- String-based POSIX directory support
--
-----------------------------------------------------------------------------



-- hack copied from System.Posix.Files

{-# LINE 25 "System/Posix/Directory.hsc" #-}

module System.Posix.Directory (
   -- * Creating and removing directories
   createDirectory, removeDirectory,

   -- * Reading directories
   DirStream,
   openDirStream,
   readDirStream,
   readDirStreamMaybe,
   rewindDirStream,
   closeDirStream,
   DirStreamOffset,

{-# LINE 39 "System/Posix/Directory.hsc" #-}
   tellDirStream,

{-# LINE 41 "System/Posix/Directory.hsc" #-}

{-# LINE 42 "System/Posix/Directory.hsc" #-}
   seekDirStream,

{-# LINE 44 "System/Posix/Directory.hsc" #-}

   -- * The working dirctory
   getWorkingDirectory,
   changeWorkingDirectory,
   changeWorkingDirectoryFd,
  ) where

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 dir mode@ calls @mkdir@ to
--   create a new directory, @dir@, with permissions based on
--  @mode@.
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)
    -- POSIX doesn't allow mkdir() to return EINTR, but it does on
    -- OS X (#5184), so we need the Retry variant here.

foreign import ccall unsafe "mkdir"
  c_mkdir :: CString -> CMode -> IO CInt

-- | @openDirStream dir@ calls @opendir@ to obtain a
--   directory stream for @dir@.
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
    Ptr CDir
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
    DirStream -> IO DirStream
forall a. a -> IO a
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 dp@ calls @readdir@ to obtain the
--   next directory entry (@struct dirent@) for the open directory
--   stream @dp@, and returns the @d_name@ member of that
--  structure.
--
--  Note that this function returns an empty filepath if the end of the
--  directory stream is reached. For a safer alternative use
--  'readDirStreamMaybe'.
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 dp@ calls @readdir@ to obtain the
--   next directory entry (@struct dirent@) for the open directory
--   stream @dp@. It returns the @d_name@ member of that
--  structure wrapped in a @Just d_name@ if an entry was read and @Nothing@ if
--  the end of the directory stream was reached.
readDirStreamMaybe :: DirStream -> IO (Maybe FilePath)
readDirStreamMaybe :: DirStream -> IO (Maybe FilePath)
readDirStreamMaybe (DirStream Ptr CDir
dirp) =
  (Ptr (Ptr CDirent) -> IO (Maybe FilePath)) -> IO (Maybe FilePath)
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr CDirent) -> IO (Maybe FilePath)) -> IO (Maybe FilePath))
-> (Ptr (Ptr CDirent) -> IO (Maybe FilePath))
-> IO (Maybe FilePath)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr CDirent)
ptr_dEnt  -> Ptr (Ptr CDirent) -> IO (Maybe FilePath)
loop Ptr (Ptr CDirent)
ptr_dEnt
 where
  loop :: Ptr (Ptr CDirent) -> IO (Maybe FilePath)
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 CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0)
         then do Ptr CDirent
dEnt <- Ptr (Ptr CDirent) -> IO (Ptr CDirent)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CDirent)
ptr_dEnt
                 if (Ptr CDirent
dEnt Ptr CDirent -> Ptr CDirent -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr CDirent
forall a. Ptr a
nullPtr)
                    then Maybe FilePath -> IO (Maybe FilePath)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FilePath
forall a. Maybe a
Nothing
                    else do
                     FilePath
entry <- (Ptr CDirent -> IO CString
d_name Ptr CDirent
dEnt IO CString -> (CString -> IO FilePath) -> IO FilePath
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO FilePath
peekFilePath)
                     Ptr CDirent -> IO ()
c_freeDirEnt Ptr CDirent
dEnt
                     Maybe FilePath -> IO (Maybe FilePath)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe FilePath -> IO (Maybe FilePath))
-> Maybe FilePath -> IO (Maybe FilePath)
forall a b. (a -> b) -> a -> b
$ FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
entry
         else do Errno
errno <- IO Errno
getErrno
                 if (Errno
errno Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eINTR) then Ptr (Ptr CDirent) -> IO (Maybe FilePath)
loop Ptr (Ptr CDirent)
ptr_dEnt else do
                 let (Errno CInt
eo) = Errno
errno
                 if (CInt
eo CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0)
                    then Maybe FilePath -> IO (Maybe FilePath)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FilePath
forall a. Maybe a
Nothing
                    else FilePath -> IO (Maybe FilePath)
forall a. FilePath -> IO a
throwErrno FilePath
"readDirStream"

-- traversing directories
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@ calls @getcwd@ to obtain the name
--   of the current working directory.
getWorkingDirectory :: IO FilePath
getWorkingDirectory :: IO FilePath
getWorkingDirectory = Int -> IO FilePath
go (Int
4096)
{-# LINE 137 "System/Posix/Directory.hsc" #-}
  where
    go :: Int -> IO FilePath
go Int
bytes = do
        Maybe FilePath
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
            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 FilePath
s <- CString -> IO FilePath
peekFilePath CString
buf
                        Maybe FilePath -> IO (Maybe FilePath)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (FilePath -> Maybe FilePath
forall a. a -> Maybe a
Just FilePath
s)
                else do Errno
errno <- IO Errno
getErrno
                        if Errno
errno Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
eRANGE
                            -- we use Nothing to indicate that we should
                            -- try again with a bigger buffer
                            then Maybe FilePath -> IO (Maybe FilePath)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FilePath
forall a. Maybe a
Nothing
                            else FilePath -> IO (Maybe FilePath)
forall a. FilePath -> IO a
throwErrno FilePath
"getWorkingDirectory"
        IO FilePath
-> (FilePath -> IO FilePath) -> Maybe FilePath -> IO FilePath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> IO FilePath
go (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
bytes)) FilePath -> IO FilePath
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FilePath
r

foreign import ccall unsafe "getcwd"
   c_getcwd   :: Ptr CChar -> CSize -> IO (Ptr CChar)

-- | @changeWorkingDirectory dir@ calls @chdir@ to change
--   the current working directory to @dir@.
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