|Portability||portable Provides a simple data structure mirroring a directory tree on the|
|Maintainer||Brandon Simmons <email@example.com>|
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.
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 DirTree a
- data AnchoredDirTree a = FilePath :/ (DirTree a)
- type FileName = String
- readDirectory :: FilePath -> IO (AnchoredDirTree String)
- readDirectoryWith :: (FilePath -> IO a) -> FilePath -> IO (AnchoredDirTree a)
- readDirectoryWithL :: (FilePath -> IO a) -> FilePath -> IO (AnchoredDirTree a)
- writeDirectory :: AnchoredDirTree String -> IO (AnchoredDirTree ())
- writeDirectoryWith :: (FilePath -> a -> IO b) -> AnchoredDirTree a -> IO (AnchoredDirTree b)
- zipPaths :: AnchoredDirTree a -> DirTree (FilePath, a)
- build :: FilePath -> IO (AnchoredDirTree FilePath)
- buildL :: FilePath -> IO (AnchoredDirTree FilePath)
- openDirectory :: FilePath -> IOMode -> IO (AnchoredDirTree Handle)
- writeJustDirs :: AnchoredDirTree a -> IO (AnchoredDirTree a)
- equalShape :: DirTree a -> DirTree b -> Bool
- comparingShape :: DirTree a -> DirTree b -> Ordering
- successful :: DirTree a -> Bool
- anyFailed :: DirTree a -> Bool
- failed :: DirTree a -> Bool
- failures :: DirTree a -> [DirTree a]
- failedMap :: (FileName -> IOException -> DirTree a) -> DirTree a -> DirTree a
- flattenDir :: DirTree a -> [DirTree a]
- sortDir :: Ord a => DirTree a -> DirTree a
- sortDirShape :: DirTree a -> DirTree a
- filterDir :: (DirTree a -> Bool) -> DirTree a -> DirTree a
- free :: AnchoredDirTree a -> DirTree a
- (</$>) :: Functor f => (DirTree a -> DirTree b) -> f (AnchoredDirTree a) -> f (AnchoredDirTree b)
Data types for representing directory trees
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
|Eq a => Eq (DirTree a)|
|(Ord a, Eq a) => Ord (DirTree a)|
|Show a => Show (DirTree 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)
High level IO functions
same as readDirectory but allows us to, for example, use ByteString.readFile to return a tree of ByteStrings.
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
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
Failed constructors. The returned tree can be compared to
the passed tree to see what operations, if any, failed:
Lower level functions
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
This allows us to, for example, mapM_ 'uncurry writeFile' over a DirTree of
writeDirectory does a better job of this.
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.
build but does directory reading IO lazily as needed:
a simple application of readDirectoryWith openFile:
writes the directory structure (not files) of a DirTree to the anchored
directory. Returns a structure identical to the supplied tree with errors
Shape comparison and equality
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.
a compare function that ignores the free file type variable:
maps a function to convert Failed DirTrees to Files or Dirs
Recursively sort a directory tree according to the Ord instance
Recursively sort a tree as in
sortDir but ignore the file contents of 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: