directory-tree-0.9.0: A simple directory-like tree datatype, with useful IO functions

Portabilityportable Provides a simple data structure mirroring a directory tree on the
MaintainerBrandon Simmons <>





Errors are caught in a special constructor in the DirTree type.

Defined instances of Functor, Traversable and Foldable allow for easily operating on a directory of files. For example, you could use Foldable.foldr to create a hash of the entire contents of a directory.

The functions readDirectoryWithL and buildL allow for doing directory-traversing IO lazily as required by the execution of pure code. This allows you to treat large directories the same way as you would a lazy infinite list.

The AnchoredDirTree type is a simple wrapper for DirTree to keep track of a base directory context for the DirTree.

Please send me any requests, bugs, or other feedback on this module!


Data types for representing directory trees

data DirTree a Source

the String in the name field is always a file name, never a full path. The free type variable is used in the File constructor and can hold Handles, Strings representing a file's contents or anything else you can think of. We catch any IO errors in the Failed constructor. an Exception can be converted to a String with show.




name :: FileName
err :: IOException


name :: FileName
contents :: [DirTree a]


name :: FileName
file :: a


Functor DirTree 
Foldable DirTree 
Traversable DirTree 
Eq (DirTree a)

Two DirTrees are equal if they have the same constructor, the same name (and in the case of Dirs) their sorted contents are equal:

Ord (DirTree a)

FIRST: Failed < Dir < File, THEN: compare on name

Show a => Show (DirTree a) 

data AnchoredDirTree a Source

a simple wrapper to hold a base directory name, which can be either an absolute or relative path. This lets us give the DirTree a context, while still letting us store only directory and file NAMES (not full paths) in the DirTree. (uses an infix constructor; don't be scared)


FilePath :/ (DirTree a) 

type FileName = StringSource

an element in a FilePath:

High level IO functions

readDirectory :: FilePath -> IO (AnchoredDirTree String)Source

build an AnchoredDirTree, given the path to a directory, opening the files using readFile. Uses readDirectoryWith internally and has the effect of traversing the entire directory structure. See readDirectoryWithL for lazy production of a DirTree structure.

readDirectoryWith :: (FilePath -> IO a) -> FilePath -> IO (AnchoredDirTree a)Source

same as readDirectory but allows us to, for example, use ByteString.readFile to return a tree of ByteStrings.


A lazy version of readDirectoryWith that does IO operations as needed i.e. as the tree is traversed in pure code. NOTE: This function uses unsafePerformIO under the hood. I believe our use here is safe, but this function is experimental in this release:

writeDirectory :: AnchoredDirTree String -> IO (AnchoredDirTree ())Source

write a DirTree of strings to disk. Clobbers files of the same name. Doesn't affect files in the directories (if any already exist) with different names. Returns a new AnchoredDirTree where failures were lifted into a Failed constructor:

writeDirectoryWith :: (FilePath -> a -> IO b) -> AnchoredDirTree a -> IO (AnchoredDirTree b)Source

writes the directory structure to disk and uses the provided function to write the contents of Files to disk. The return value of the function will become the new contents of the returned, where IO errors at each node are replaced with Failed constructors. The returned tree can be compared to the passed tree to see what operations, if any, failed:

Lower level functions

zipPaths :: AnchoredDirTree a -> DirTree (FilePath, a)Source

tuple up the complete filename with the File contents, by building up the path, trie-style, from the root. The filepath will be relative to the current directory. This allows us to, for example, mapM_ 'uncurry writeFile' over a DirTree of strings, although writeDirectory does a better job of this.

build :: FilePath -> IO (AnchoredDirTree FilePath)Source

builds a DirTree from the contents of the directory passed to it, saving the base directory in the Anchored* wrapper. Errors are caught in the tree in the Failed constructor. The file fields initially are populated with full paths to the files they are abstracting.

buildL :: FilePath -> IO (AnchoredDirTree FilePath)Source

identical to build but does directory reading IO lazily as needed:

openDirectory :: FilePath -> IOMode -> IO (AnchoredDirTree Handle)Source

a simple application of readDirectoryWith openFile:

writeJustDirs :: AnchoredDirTree a -> IO (AnchoredDirTree a)Source

writes the directory structure (not files) of a DirTree to the anchored directory. Returns a structure identical to the supplied tree with errors replaced by Failed constructors:

Utility functions

Handling failure

successful :: DirTree a -> BoolSource

True if there are no Failed constructors in the tree

anyFailed :: DirTree a -> BoolSource

True if any Failed constructors in the tree

failed :: DirTree a -> BoolSource

returns true if argument is a Failed constructor:

failures :: DirTree a -> [DirTree a]Source

returns a list of Failed constructors only:

failedMap :: (FileName -> IOException -> DirTree a) -> DirTree a -> DirTree aSource

maps a function to convert Failed DirTrees to Files or Dirs

Tree Manipulations

flattenDir :: DirTree a -> [DirTree a]Source

Flattens a DirTree into a (never empty) list of tree constructors. Dir constructors will have [] as their contents:

sortDir :: DirTree a -> DirTree aSource

Sort the contents of every Dir constructor, see Ord instance above:

filterDir :: (DirTree a -> Bool) -> DirTree a -> DirTree aSource

applies the predicate to each constructor in the tree, removing it (and its children, of course) when the predicate returns False. The topmost constructor will always be preserved:

free :: AnchoredDirTree a -> DirTree aSource

strips away base directory wrapper:


(</$>) :: Functor f => (DirTree a -> DirTree b) -> f (AnchoredDirTree a) -> f (AnchoredDirTree b)Source

Allows for a function on a bare DirTree to be applied to an AnchoredDirTree within a Functor. Very similar to and useful in combination with <$>: