Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getDirectoryContents' which ignores "." and ".." #36

Merged
merged 3 commits into from
Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions System/Directory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module System.Directory
, removeDirectoryRecursive
, renameDirectory
, getDirectoryContents
, getDirectoryContentsA
-- ** Current working directory
, getCurrentDirectory
, setCurrentDirectory
Expand Down Expand Up @@ -571,8 +572,8 @@ removePathRecursive path =
removeContentsRecursive :: FilePath -> IO ()
removeContentsRecursive path =
(`ioeSetLocation` "removeContentsRecursive") `modifyIOError` do
cont <- getDirectoryContents path
mapM_ removePathRecursive [path </> x | x <- cont, x /= "." && x /= ".."]
cont <- getDirectoryContentsA path
mapM_ removePathRecursive [path </> x | x <- cont]
removeDirectory path

#if __GLASGOW_HASKELL__
Expand Down Expand Up @@ -1030,6 +1031,15 @@ getDirectoryContents path =
-- no need to reverse, ordering is undefined
#endif /* mingw32 */

{- | A version of 'getDirectoryContents' which returns /almost all/
entries, ignoring the current and parent directories, @.@ and @..@.

-}
getDirectoryContentsA :: FilePath -> IO [FilePath]
getDirectoryContentsA path =
(filter f) <$> (getDirectoryContents path)
where f filename = filename /= "." && filename /= ".."

#endif /* __GLASGOW_HASKELL__ */


Expand Down
6 changes: 2 additions & 4 deletions tests/CopyFile001.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ module CopyFile001 where
#include "util.inl"
import System.Directory
import Data.List (sort)
import Data.Monoid ((<>))
import System.FilePath ((</>))

main :: TestEnv -> IO ()
main _t = do
createDirectory dir
writeFile (dir </> from) contents
T(expectEq) () (specials <> [from]) . sort =<< getDirectoryContents dir
T(expectEq) () [from] . sort =<< getDirectoryContentsA dir
copyFile (dir </> from) (dir </> to)
T(expectEq) () (specials <> [from, to]) . sort =<< getDirectoryContents dir
T(expectEq) () [from, to] . sort =<< getDirectoryContentsA dir
T(expectEq) () contents =<< readFile (dir </> to)
where
specials = [".", ".."]
contents = "This is the data\n"
from = "source"
to = "target"
Expand Down
6 changes: 2 additions & 4 deletions tests/CopyFile002.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ module CopyFile002 where
#include "util.inl"
import System.Directory
import Data.List (sort)
import Data.Monoid ((<>))

main :: TestEnv -> IO ()
main _t = do
-- Similar to CopyFile001 but moves a file in the current directory
-- (Bug #1652 on GHC Trac)
writeFile from contents
T(expectEq) () (specials <> [from]) . sort =<< getDirectoryContents "."
T(expectEq) () [from] . sort =<< getDirectoryContentsA "."
copyFile from to
T(expectEq) () (specials <> [from, to]) . sort =<< getDirectoryContents "."
T(expectEq) () [from, to] . sort =<< getDirectoryContentsA "."
T(expectEq) () contents =<< readFile to
where
specials = [".", ".."]
contents = "This is the data\n"
from = "source"
to = "target"
5 changes: 2 additions & 3 deletions tests/GetDirContents001.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import System.FilePath ((</>))
main :: TestEnv -> IO ()
main _t = do
createDirectory dir
T(expectEq) () specials . sort =<< getDirectoryContents dir
T(expectEq) () [] . sort =<< getDirectoryContentsA dir
names <- for [1 .. 100 :: Int] $ \ i -> do
let name = 'f' : show i
writeFile (dir </> name) ""
return name
T(expectEq) () (sort (specials <> names)) . sort =<< getDirectoryContents dir
T(expectEq) () (sort names) . sort =<< getDirectoryContentsA dir
where dir = "dir"
specials = [".", ".."]
4 changes: 2 additions & 2 deletions tests/TestUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ copyPathRecursive source dest =
dirExists <- doesDirectoryExist source
if dirExists
then do
contents <- getDirectoryContents source
contents <- getDirectoryContentsA source
createDirectory dest
mapM_ (uncurry copyPathRecursive)
[(source </> x, dest </> x) | x <- contents, x /= "." && x /= ".."]
[(source </> x, dest </> x) | x <- contents]
else copyFile source dest

modifyPermissions :: FilePath -> (Permissions -> Permissions) -> IO ()
Expand Down
5 changes: 2 additions & 3 deletions tests/WithCurrentDirectory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ main :: TestEnv -> IO ()
main _t = do
createDirectory dir
-- Make sure we're starting empty
T(expectEq) () specials . sort =<< getDirectoryContents dir
T(expectEq) () [] . sort =<< getDirectoryContentsA dir
cwd <- getCurrentDirectory
withCurrentDirectory dir (writeFile testfile contents)
-- Are we still in original directory?
T(expectEq) () cwd =<< getCurrentDirectory
-- Did the test file get created?
T(expectEq) () (specials <> [testfile]) . sort =<< getDirectoryContents dir
T(expectEq) () [testfile] . sort =<< getDirectoryContentsA dir
-- Does the file contain what we expected to write?
T(expectEq) () contents =<< readFile (dir </> testfile)
where
testfile = "testfile"
contents = "some data\n"
dir = "dir"
specials = [".", ".."]