mirror of https://github.com/voidlizard/hbs2
wip, works, suspictious
This commit is contained in:
parent
91a9739ca7
commit
3df586c7a1
|
@ -93,6 +93,7 @@ common shared-properties
|
|||
, random
|
||||
, vector
|
||||
, unix
|
||||
, split
|
||||
|
||||
|
||||
library
|
||||
|
|
|
@ -18,6 +18,8 @@ import UnliftIO
|
|||
import Control.Concurrent.STM qualified as STM
|
||||
import Streaming.Prelude qualified as S
|
||||
|
||||
import Data.List.Split (chunksOf)
|
||||
|
||||
glob :: forall m . MonadIO m
|
||||
=> [FilePattern] -- ^ search patterns
|
||||
-> [FilePattern] -- ^ ignore patterns
|
||||
|
@ -28,22 +30,40 @@ glob :: forall m . MonadIO m
|
|||
glob pat ignore dir action = do
|
||||
let seed = [dir]
|
||||
|
||||
q <- newTQueueIO
|
||||
|
||||
a <- liftIO $ async do
|
||||
flip fix seed $ \next items -> do
|
||||
case items of
|
||||
[] -> pure ()
|
||||
[] -> atomically (writeTQueue q Nothing)
|
||||
(p:rest) -> do
|
||||
isD <- liftIO (doesDirectoryExist p)
|
||||
if isD && not (skip p) then do
|
||||
content <- liftIO (try @_ @IOError $ listDirectory p)
|
||||
<&> fromRight mempty
|
||||
|
||||
let found = [ p </> x | x <- content ]
|
||||
next (rest <> found)
|
||||
|
||||
forConcurrently_ found $ \f -> do
|
||||
glob pat ignore f $ \fn -> do
|
||||
atomically $ writeTQueue q (Just fn)
|
||||
pure True
|
||||
|
||||
next []
|
||||
|
||||
else do
|
||||
isF <- liftIO (doesFileExist p)
|
||||
when (isF && matches pat p ) do
|
||||
void $ action p
|
||||
atomically (writeTQueue q (Just p))
|
||||
|
||||
next rest
|
||||
|
||||
fix \next -> do
|
||||
r <- atomically (readTQueue q)
|
||||
case r of
|
||||
Nothing -> pure ()
|
||||
Just e -> void (action e) >> next
|
||||
|
||||
where
|
||||
matches p f = or [ i ?== f | i <- p ]
|
||||
skip p = or [ i ?== p | i <- ignore ]
|
||||
|
|
Loading…
Reference in New Issue