directory-tree 0.2.0 → 0.2.1
raw patch · 2 files changed
+47/−7 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ System.Directory.Tree: failed :: DirTree a -> Bool
Files
- System/Directory/Tree.hs +40/−5
- directory-tree.cabal +7/−2
System/Directory/Tree.hs view
@@ -49,6 +49,7 @@ -- ** Handling failure , successful , anyFailed+ , failed , failures , failedMap -- ** Misc.@@ -57,6 +58,11 @@ {- TODO:+ - add whatever needed to make an efficient 'du' simple+ - look at using 'withFile' ?+ - strictness ? what does this do when called on a big + directory tree and we only use the top level ?+ - add some tests - tree combining functions - tree searching based on file names@@ -68,8 +74,9 @@ import System.FilePath import System.IO import Control.Exception (handle, IOException)+import System.IO.Error(ioeGetErrorType,isDoesNotExistErrorType) -import Data.Function (on)+import Data.Ord (comparing) import Data.List (sort, (\\)) import Control.Applicative@@ -94,7 +101,7 @@ instance (Ord a)=> Ord (DirTree a) where- compare = compare `on` name+ compare = comparing name -- | a simple wrapper to hold a base directory name, which can be either @@ -129,7 +136,10 @@ -- | build an AnchoredDirTree, given the path to a directory, opening the files--- using readFile.+-- using readFile. +-- Uses `readDirectoryWith` internally and has the effect of traversing the+-- entire directory structure, so is not suitable for running on large directory+-- trees (suggestions or patches welcomed): readDirectory :: FilePath -> IO (AnchoredDirTree String) readDirectory = readDirectoryWith readFile @@ -175,7 +185,11 @@ build :: FilePath -> IO (AnchoredDirTree FilePath) build p = do let base = baseDir p tree <- build' p- return (base :/ tree)+ -- we make sure the directory tree is free of non-existent+ -- file errors, which are artifacts of the "non-atomic"+ -- nature of traversing a system firectory tree.+ let treeClean = removeNonexistent tree+ return (base :/ treeClean) -- HELPER: not exported: build' :: FilePath -> IO (DirTree FilePath)@@ -202,7 +216,6 @@ - ---- HANDLING FAILURES ---- -- | True if any Failed constructors in the tree@@ -214,6 +227,12 @@ successful = null . failures +-- | returns true if argument is a `Failed` constructor:+failed :: DirTree a -> Bool+failed (Failed _ _) = True+failed _ = False++ -- | returns a list of 'Failed' constructors only: failures :: DirTree a -> [DirTree a] failures (Dir _ cs) = concatMap failures cs@@ -286,3 +305,19 @@ getDirsFiles cs = do let cs' = if null cs then "." else cs dfs <- getDirectoryContents cs' return $ sort $ dfs \\ [".",".."]++++-- DoesNotExist errors not present at the topmost level could happen if a+-- named file or directory is deleted after being listed by +-- getDirectoryContents but before we can get it into memory. +-- So we filter those errors out because the user should not see errors +-- raised by the internal implementation of this module:+-- This leaves the error if it exists in the top (user-supplied) level:+removeNonexistent :: DirTree a -> DirTree a+removeNonexistent (Dir n cs) = + Dir n $ map removeNonexistent $ filter isOkConstructor cs+ + where isOkConstructor c = not (failed c) || isOkError c+ isOkError = not . isDoesNotExistErrorType . ioeGetErrorType . err+removeNonexistent f = f
directory-tree.cabal view
@@ -1,5 +1,5 @@ name: directory-tree-version: 0.2.0+version: 0.2.1 homepage: http://coder.bsimmons.name/blog/2009/05/directory-tree-module-released/ synopsis: A simple directory-like tree datatype, with useful IO functions description: A simple directory-like tree datatype, with useful IO functions and Foldable and Traversable instance @@ -39,7 +39,12 @@ > import qualified Data.ByteString.Lazy as B > do (_ :/ dTree) <- readDirectoryWith B.readFile "./" .- Please send me any comments, requests or bug reports+ *NOTE:* the IO functions like `readDirectoryWith` in this library use standard lazy IO + IOfunctions and will (necessarily) traverse an entire system directory tree before+ returning a DirTree constructor. This unfortunately makes it not suitable for large+ directory trees.+ + Any ideas or suggestions for improvements would be most welcomed :-) . category: Data, System