-----------------------------------------------------------------------------
---
+-- |
-- Module : System.Directory
-- Copyright : (c) The University of Glasgow 2001
--- License : BSD-style (see the file libraries/core/LICENSE)
+-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : libraries@haskell.org
-- Stability : provisional
-- Portability : portable
--
--- $Id: Directory.hs,v 1.1 2002/02/05 17:32:27 simonmar Exp $
---
-- System-independent interface to directory manipulation.
--
-----------------------------------------------------------------------------
getDirectoryContents :: FilePath -> IO [FilePath]
getDirectoryContents path = do
- alloca $ \ ptr_dEnt -> do
- p <- withCString path $ \s ->
- throwErrnoIfNullRetry "getDirectoryContents" (c_opendir s)
- loop ptr_dEnt p
+ alloca $ \ ptr_dEnt ->
+ bracket
+ (withCString path $ \s ->
+ throwErrnoIfNullRetry desc (c_opendir s))
+ (\p -> throwErrnoIfMinus1_ desc (c_closedir p))
+ (\p -> loop ptr_dEnt p)
where
+ desc = "getDirectoryContents"
+
loop :: Ptr (Ptr CDirent) -> Ptr CDir -> IO [String]
loop ptr_dEnt dir = do
resetErrno
r <- readdir dir ptr_dEnt
- if (r == 0)
+ if (r == 0)
then do
dEnt <- peek ptr_dEnt
- if (dEnt == nullPtr)
+ if (dEnt == nullPtr)
then return []
else do
entry <- (d_name dEnt >>= peekCString)
return (entry:entries)
else do errno <- getErrno
if (errno == eINTR) then loop ptr_dEnt dir else do
- throwErrnoIfMinus1_ "getDirectoryContents" $ c_closedir dir
let (Errno eo) = errno
if (eo == end_of_dir)
then return []
- else throwErrno "getDirectoryContents"
+ else throwErrno desc
exec <- c_access s x_OK
withFileStatus name $ \st -> do
is_dir <- isDirectory st
- is_reg <- isRegularFile st
return (
Permissions {
readable = read == 0,
writable = write == 0,
executable = not is_dir && exec == 0,
- searchable = not is_reg && exec == 0
+ searchable = is_dir && exec == 0
}
)
mode <- st_mode stat
return (s_isdir mode)
-isRegularFile :: Ptr CStat -> IO Bool
-isRegularFile stat = do
- mode <- st_mode stat
- return (s_isreg mode)
-
emptyCMode :: CMode
emptyCMode = 0