Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add traverse path filter for Linux #112

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/System/FSNotify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ module System.FSNotify (
, confWatchMode
, confThreadingMode
, confOnHandlerException
#ifdef OS_Linux
, confPathFilter
#endif
, WatchMode(..)
, ThreadingMode(..)

Expand Down Expand Up @@ -121,6 +124,9 @@ defaultConfig = WatchConfig {
#endif
, confThreadingMode = SingleThread
, confOnHandlerException = defaultOnHandlerException
#ifdef OS_Linux
, confPathFilter = const (return True)
#endif
}

defaultOnHandlerException :: SomeException -> IO ()
Expand Down
23 changes: 14 additions & 9 deletions src/System/FSNotify/Linux.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Control.Concurrent.MVar
import Control.Exception.Safe as E
import Control.Monad
import qualified Data.ByteString as BS
import Data.Bool
import Data.Function
import Data.Monoid
import Data.String
Expand Down Expand Up @@ -109,7 +110,7 @@ instance FileListener INotifyListener () where
when wse $ INo.removeWatch wd
return False

listenRecursive _conf listener initialPath actPred callback = do
listenRecursive conf listener initialPath actPred callback = do
-- wdVar stores the list of created watch descriptors. We use it to
-- cancel the whole recursive listening task.
--
Expand All @@ -133,7 +134,7 @@ instance FileListener INotifyListener () where
rawInitialPath <- toRawFilePath initialPath
rawCanonicalInitialPath <- canonicalizeRawDirPath rawInitialPath
watchDirectoryRecursively listener wdVar actPred callback True rawCanonicalInitialPath
traverseAllDirs rawCanonicalInitialPath $ \subPath ->
traverseAllDirs rawCanonicalInitialPath (confPathFilter conf) $ \subPath ->
watchDirectoryRecursively listener wdVar actPred callback False subPath

return stopListening
Expand Down Expand Up @@ -201,13 +202,17 @@ canonicalizeRawDirPath p = fromRawFilePath p >>= canonicalizePath >>= toRawFileP
(<//>) :: RawFilePath -> RawFilePath -> RawFilePath
x <//> y = x <> "/" <> y

traverseAllDirs :: RawFilePath -> (RawFilePath -> IO ()) -> IO ()
traverseAllDirs dir cb = traverseAll dir $ \subPath ->
-- TODO: wish we didn't need fromRawFilePath here
-- TODO: make sure this does the right thing with symlinks
fromRawFilePath subPath >>= getFileStatus >>= \case
(isDirectory -> True) -> cb subPath >> return True
_ -> return False
traverseAllDirs :: RawFilePath -> (FilePath -> IO Bool) -> (RawFilePath -> IO ()) -> IO ()
traverseAllDirs dir predicate cb = traverseAll dir $ \subRawPath -> do
subPath <- fromRawFilePath subRawPath
needWatch <- predicate subPath
if not needWatch then return False
else do
-- TODO: wish we didn't need fromRawFilePath here
-- TODO: make sure this does the right thing with symlinks
getFileStatus subPath >>= \case
(isDirectory -> True) -> cb subRawPath >> return True
_ -> return False

traverseAll :: RawFilePath -> (RawFilePath -> IO Bool) -> IO ()
traverseAll dir cb = bracket (openDirStream dir) closeDirStream $ \dirStream ->
Expand Down
4 changes: 4 additions & 0 deletions src/System/FSNotify/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ data WatchConfig = WatchConfig
-- ^ Threading mode to use.
, confOnHandlerException :: SomeException -> IO ()
-- ^ Called when a handler throws an exception.
#ifdef OS_Linux
, confPathFilter :: FilePath->IO Bool
-- ^ Called to determine whether to watch a path.
#endif
}

type IOEvent = IORef Event
Expand Down
Loading