| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
System.Directory.Contents
Description
Modeled after the linux tree command (when invoked with the follow-symlinks
option), this module recursively lists the contents of a directory while
avoiding symlink loops. See the documentation of buildDirTree for an example.
In addition to building the directory-contents tree, this module provides facilities for filtering, displaying, and navigating the directory hierarchy.
Synopsis
- data DirTree a
- = DirTree_Dir FilePath [DirTree a]
- | DirTree_File FilePath a
- | DirTree_Symlink FilePath (Symlink a)
- data Symlink a
- buildDirTree :: FilePath -> IO (Maybe (DirTree FilePath))
- dereferenceSymlinks :: DirTree FilePath -> IO (DirTree FilePath)
- walkDirTree :: FilePath -> DirTree a -> Maybe (DirTree a)
- walkContents :: FilePath -> DirTree a -> Maybe (DirTree a)
- newtype DirTreeMaybe a = DirTreeMaybe {
- unDirTreeMaybe :: Maybe (DirTree a)
- withDirTreeMaybe :: (DirTreeMaybe a -> DirTreeMaybe b) -> DirTree a -> Maybe (DirTree b)
- withDirTreeMaybeF :: Functor f => (DirTreeMaybe a -> f (DirTreeMaybe b)) -> DirTree a -> f (Maybe (DirTree b))
- witherDirTree :: Applicative f => (a -> f (Maybe b)) -> DirTree a -> f (Maybe (DirTree b))
- filterADirTree :: Applicative f => (a -> f Bool) -> DirTree a -> f (Maybe (DirTree a))
- mapMaybeDirTree :: (a -> Maybe b) -> DirTree a -> Maybe (DirTree b)
- catMaybesDirTree :: DirTree (Maybe a) -> Maybe (DirTree a)
- filterDirTree :: (a -> Bool) -> DirTree a -> Maybe (DirTree a)
- pruneDirTree :: DirTree a -> Maybe (DirTree a)
- drawDirTree :: DirTree a -> Text
- drawDirTreeWith :: (String -> a -> String) -> DirTree a -> String
- printDirTree :: DirTree a -> IO ()
- mkRelative :: FilePath -> FilePath -> FilePath
- alternative :: Alternative f => [f a] -> f a
- filePath :: DirTree a -> FilePath
Documentation
The contents of a directory, represented as a tree. See Symlink for
special handling of symlinks.
Constructors
| DirTree_Dir FilePath [DirTree a] | |
| DirTree_File FilePath a | |
| DirTree_Symlink FilePath (Symlink a) |
Instances
Symlink cycles are prevented by separating symlinks into two categories: those that point to paths already within the directory hierarchy being recursively listed, and those that are not. In the former case, rather than following the symlink and listing the target redundantly, we simply store the symlink reference itself. In the latter case, we treat the symlink as we would any other folder and produce a list of its contents.
The String argument represents the symlink reference (e.g., "../somefile").
In the Symlink_Internal case, the second (FilePath) argument is the path
to the symlink target.
In the Symlink_External case, the second ([DirTree a]) argument contains
the contents of the symlink target.
Constructors
| Symlink_Internal String FilePath | |
| Symlink_External String [DirTree a] |
Instances
Constructing a tree
buildDirTree :: FilePath -> IO (Maybe (DirTree FilePath)) Source #
Recursively list the contents of a FilePath, representing the results as
a hierarchical DirTree. This function should produce results similar to
the linux command tree -l.
For example, given this directory and symlink structure
(as shown by tree -l):
test
├── A
│ ├── a
│ ├── A -> ../A [recursive, not followed]
│ └── B -> ../B
│ ├── A -> ../A [recursive, not followed]
│ └── b
├── B
│ ├── A -> ../A [recursive, not followed]
│ └── b
└── C -> ../C
└── cthis function will produce the following (as rendererd by drawDirTree):
test | +- A | | | +- A -> ../A | | | +- B -> ../B | | | `- a | +- B | | | +- A -> ../A | | | `- b | `- C -> ../C | `- c
dereferenceSymlinks :: DirTree FilePath -> IO (DirTree FilePath) Source #
De-reference one layer of symlinks
Example
Given:
tmp | +- A | | | `- a | +- a -> A/a | `- C | `- A -> ../A
This function will follow one level of symlinks, producing:
tmp
|
+- A
| |
| `- a
|
+- a
|
`- C
|
`- A
|
`- aNavigate
walkDirTree :: FilePath -> DirTree a -> Maybe (DirTree a) Source #
Starting from the root directory, try to walk the given filepath and return
the DirTree at the end of the route. For example, given the following tree:
src
└── System
└── Directory
└── Contents.hswalkDirTree "src/System" should produce
Directory | `- Contents.hs
This function does not dereference symlinks, nor does it handle the special
paths . and ...
walkContents :: FilePath -> DirTree a -> Maybe (DirTree a) Source #
Like walkDirTree but skips the outermost containing directory. Useful for
walking paths relative from the root directory passed to buildDirTree.
Given the following DirTree:
src
└── System
└── Directory
└── Contents.hswalkContents System should produce
Directory | `- Contents.hs
Filter
newtype DirTreeMaybe a Source #
This wrapper really just represents the no-path/empty case so that filtering works
Constructors
| DirTreeMaybe | |
Fields
| |
Instances
withDirTreeMaybe :: (DirTreeMaybe a -> DirTreeMaybe b) -> DirTree a -> Maybe (DirTree b) Source #
Map a function that could produce an empty result over a DirTree
withDirTreeMaybeF :: Functor f => (DirTreeMaybe a -> f (DirTreeMaybe b)) -> DirTree a -> f (Maybe (DirTree b)) Source #
Map a function that could produce an empty result in the given functor
witherDirTree :: Applicative f => (a -> f (Maybe b)) -> DirTree a -> f (Maybe (DirTree b)) Source #
wither for DirTree. This represents the case of no paths left after
filtering with Nothing (something that the DirTree type can't represent on
its own). NB: Filtering does not remove directories, only files. The
directory structure remains intact. To remove empty directories, see
pruneDirTree.
filterADirTree :: Applicative f => (a -> f Bool) -> DirTree a -> f (Maybe (DirTree a)) Source #
filterA for DirTree. See witherDirTree.
mapMaybeDirTree :: (a -> Maybe b) -> DirTree a -> Maybe (DirTree b) Source #
mapMaybe for DirTree. See witherDirTree.
catMaybesDirTree :: DirTree (Maybe a) -> Maybe (DirTree a) Source #
catMaybes for DirTree. See witherDirTree.
filterDirTree :: (a -> Bool) -> DirTree a -> Maybe (DirTree a) Source #
filter for DirTree. See witherDirTree.
Display
drawDirTree :: DirTree a -> Text Source #
Produces a tree drawing (using only text) of a DirTree hierarchy.
drawDirTreeWith :: (String -> a -> String) -> DirTree a -> String Source #
Apply a rendering function to each file when drawing the directory hierarchy
printDirTree :: DirTree a -> IO () Source #
Print the DirTree as a tree. For example:
System | `- Directory | `- Contents.hs
Utilities
alternative :: Alternative f => [f a] -> f a Source #
Get the first Alternative