Safe Haskell | None |
---|
- find :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FilePath
- find' :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FileEntry
- lfind :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FilePath
- lfind' :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FileEntry
- stat :: MonadIO m => Predicate m FileEntry
- lstat :: MonadIO m => Predicate m FileEntry
- test :: MonadIO m => Predicate m FileEntry -> FilePath -> m Bool
- findRaw :: (MonadIO m, MonadResource m) => FilePath -> Bool -> Predicate m FileEntry -> Source m FileEntry
- ignoreVcs :: Monad m => Predicate m FileEntry
- regex :: Monad m => Text -> Predicate m FileEntry
- glob :: Monad m => Text -> Predicate m FileEntry
- filename_ :: Monad m => (FilePath -> Bool) -> Predicate m FileEntry
- filenameS_ :: Monad m => (String -> Bool) -> Predicate m FileEntry
- filepath_ :: Monad m => (FilePath -> Bool) -> Predicate m FileEntry
- filepathS_ :: Monad m => (String -> Bool) -> Predicate m FileEntry
- withPath :: Monad m => (FilePath -> m Bool) -> Predicate m FileEntry
- regular :: Monad m => Predicate m FileEntry
- hasMode :: Monad m => FileMode -> Predicate m FileEntry
- executable :: Monad m => Predicate m FileEntry
- depth :: Monad m => (Int -> Bool) -> Predicate m FileEntry
- lastAccessed :: Monad m => (UTCTime -> Bool) -> Predicate m FileEntry
- lastModified :: Monad m => (UTCTime -> Bool) -> Predicate m FileEntry
- withFileStatus :: Monad m => (FileStatus -> m Bool) -> Predicate m FileEntry
- (=~) :: FilePath -> Text -> Bool
- data FileEntry = FileEntry {}
Introduction
- *find-conduit** is essentially a souped version of GNU find for Haskell, using a DSL to provide both ease of us, and extensive flexbility.
In its simplest form, let's compare some uses of find to find-conduit. Bear in mind that the result of the find function is a conduit, so you're expected to either sink it to a list, or operate on the file paths as they are yielded.
Basic comparison with GNU find
A typical find command:
find src -name '*.hs' -type f -print
Would in find-conduit be:
find src (glob "*.hs" <> regular) $$ mapM_C (liftIO . print)
The glob
predicate matches the file basename against the globbing pattern,
while the regular
predicate matches plain files.
A more complicated example:
find . -size +100M -perm 644 -mtime 1
Now in find-conduit:
let megs = 1024 * 1024 days = 86400 now <- liftIO getCurrentTime find "." ( fileSize (> 100*megs) <> hasMode 0o644 <> lastModified (> addUTCTime now (-(1*days))) )
Appending predicates like this expressing an and relationship. Use <|>
to
express or. You can also negate any predicate:
find "." (not_ (hasMode 0o644))
By default, predicates, whether matching or not, will allow recursion into
directories. In order to express that matching predicate should disallow
recursion, use prune
:
find "." (prune (depth (> 2)))
This is the same as using '-maxdepth 2' in find.
find "." (prune (filename_ (== "dist")))
This is the same as:
find . \( -name dist -prune \) -o -print
Performance
find-conduit strives to make file-finding a well performing operation. To this end, a composed Predicate will only call stat once per entry being considered; and if you prune a directory, it is not traversed at all.
By default, find
calls stat for every file before it applies the predicate,
in order to ensure that only one such call is needed. Sometimes, however, you
know just from the FilePath that you don't want to consider a certain file, or
you want to prune a directory tree.
To support these types of optimized queries, a variant of find is provided
called findWithPreFilter
. This takes two predicates: one that is applied to
only the FilePath, before stat (or lstat) is called; and one that is applied
to the full file information after the stat.
Other notes
See Cond
for more details on the Monad used to build predicates.
Finding functions
find :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FilePathSource
find' :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FileEntrySource
lfind :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FilePathSource
lfind' :: (MonadIO m, MonadResource m) => FilePath -> Predicate m FileEntry -> Source m FileEntrySource
test :: MonadIO m => Predicate m FileEntry -> FilePath -> m BoolSource
Test a file path using the same type of Predicate
that is accepted by
find
.
findRaw :: (MonadIO m, MonadResource m) => FilePath -> Bool -> Predicate m FileEntry -> Source m FileEntrySource
A raw find does no processing on the FileEntry, leaving it up to the user to determine when and if stat should be called. Note that unless you take care to indicate when recursion should happen, an error will result when the raw finder attempts to recurse on a non-directory. The bare minimum for a proper finder should look like this for non-recursion:
findRaw <path> $ do <apply predicates needing only pathname or depth> localM stat $ do directory ||: norecurse <apply predicates needing stat info>
To apply predicates only to a single directory, without recursing, simply
start (or end) the predicate with norecurse
, and use localM stat
or
localM lstat
at the point where you need FileStatus
information.
File path predicates
ignoreVcs :: Monad m => Predicate m FileEntrySource
Return all entries, except for those within version-control metadata directories (and not including the version control directory itself either).
glob :: Monad m => Text -> Predicate m FileEntrySource
Find every entry whose filename part matching the given filename globbing
expression. For example: glob *.hs
.
File entry predicates (uses stat information)
executable :: Monad m => Predicate m FileEntrySource
withFileStatus :: Monad m => (FileStatus -> m Bool) -> Predicate m FileEntrySource
Predicate combinators
(=~) :: FilePath -> Text -> BoolSource
This is a re-export of =~
, with the types changed for
ease of use with this module. For example, you can simply say:
filename_ (=~ "\\.hs$")
Which is the same thing as:
regex "\\.hs$"
Types and type classes
FileEntry | |
|