HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduledirectory-tree-0.12.1Haskell98

System.Directory.Tree

Provides a simple data structure mirroring a directory tree on the filesystem, as well as useful functions for reading and writing file and directory structures in the IO monad.

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!

  • 3 types
  • 31 values

Data types for representing directory trees

3 declarations
datadata DirTree a
#

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.

Instances6Functor, Foldable, Traversable, Eq, Ord, Show
  • Functor DirTreeDefined in directory-tree-0.12.1 · System.Directory.Tree
  • Foldable DirTreeDefined in directory-tree-0.12.1 · System.Directory.Tree
  • Traversable DirTreeDefined in directory-tree-0.12.1 · System.Directory.Tree
  • Eq a => Eq (DirTree a)Defined in directory-tree-0.12.1 · System.Directory.Tree

    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 a, Eq a) => Ord (DirTree a)Defined in directory-tree-0.12.1 · System.Directory.Tree

    First compare constructors: Failed < Dir < File... Then compare name... Then compare free variable parameter of File constructors

  • Show a => Show (DirTree a)Defined in directory-tree-0.12.1 · System.Directory.Tree
datadata AnchoredDirTree a
#

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)

Constructors

Instances4Functor, Eq, Ord, Show

High level IO functions

5 declarations

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

Build a DirTree rooted at p and using f to fill the file field of File nodes.

The FilePath arguments to f will be the full path to the current file, and will include the root p as a prefix. For example, the following would return a tree of full FilePaths like "../tmp/foo" and "../tmp/bar/baz":

readDirectoryWith return "../tmp"

Note though that the build function below already does this.

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:

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

4 declarations

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.

Manipulating FilePaths

valuezipPaths :: AnchoredDirTree a -> DirTree (FilePath, a)
#

tuple up the complete file path with the file contents, by building up the path, trie-style, from the root. The filepath will be relative to "anchored" directory.

This allows us to, for example, mapM_ uncurry writeFile over a DirTree of strings, although writeDirectory does a better job of this.

Utility functions

0 declarations

Shape comparison and equality

valueequalShape :: DirTree a -> DirTree b -> Bool
#

Tests equality of two trees, ignoring their free variable portion. Can be used to check if any files have been added or deleted, for instance.

Handling failure

Tree Manipulations

valuefilterDir :: (DirTree a -> Bool) -> DirTree a -> DirTree a
#

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:

Low-level

valuetransformDir :: (DirTree a -> DirTree a) -> DirTree a -> DirTree a
#

At Dir constructor, apply transformation function to all of directory's contents, then remove the Nothing's and recurse. This always preserves the topomst constructor.

Navigation

Operators

Lenses

6 declarations

These are compatible with the "lens" library