Portability | portable |
---|---|
Stability | experimental |
Maintainer | Brandon Simmons <brandon.m.simmons@gmail.com> |
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 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)
- writeDirectory :: AnchoredDirTree String -> IO ()
- writeDirectoryWith :: (FilePath -> a -> IO ()) -> AnchoredDirTree a -> IO ()
- zipPaths :: AnchoredDirTree a -> DirTree (FilePath, a)
- build :: FilePath -> IO (AnchoredDirTree FilePath)
- openDirectory :: FilePath -> IOMode -> IO (AnchoredDirTree Handle)
- writeJustDirs :: AnchoredDirTree a -> IO ()
- 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
- free :: AnchoredDirTree a -> DirTree a
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 show
.
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)
Eq a => Eq (AnchoredDirTree a) | |
Ord a => Ord (AnchoredDirTree a) | |
Show a => Show (AnchoredDirTree a) |
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, so is not suitable for running on large directory
trees (suggestions or patches welcomed):
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.
writeDirectory :: AnchoredDirTree String -> IO ()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:
writeDirectoryWith :: (FilePath -> a -> IO ()) -> AnchoredDirTree a -> IO ()Source
writes the directory structure to disc, then uses the provided function to write the contents of Files to disc.
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.
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.
openDirectory :: FilePath -> IOMode -> IO (AnchoredDirTree Handle)Source
a simple application of readDirectoryWith openFile:
writeJustDirs :: AnchoredDirTree a -> IO ()Source
writes the directory structure (not files) of a DirTree to the anchored directory. can be preparation for writing files:
Utility functions
Handling failure
successful :: DirTree a -> BoolSource
True if there are no Failed constructors in the tree
failedMap :: (FileName -> IOException -> DirTree a) -> DirTree a -> DirTree aSource
maps a function to convert Failed DirTrees to Files or Dirs
Misc.
free :: AnchoredDirTree a -> DirTree aSource
strips away base directory wrapper: