2012-06-19 03:47:48 +00:00
|
|
|
{- generic directory watching interface
|
|
|
|
-
|
|
|
|
- Uses either inotify or kqueue to watch a directory (and subdirectories)
|
|
|
|
- for changes, and runs hooks for different sorts of events as they occur.
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Utility.DirWatcher where
|
|
|
|
|
|
|
|
import Utility.Types.DirWatcher
|
|
|
|
|
|
|
|
#if WITH_INOTIFY
|
|
|
|
import qualified Utility.INotify as INotify
|
|
|
|
import qualified System.INotify as INotify
|
|
|
|
#endif
|
|
|
|
#if WITH_KQUEUE
|
|
|
|
import qualified Utility.Kqueue as Kqueue
|
2012-06-28 18:13:15 +00:00
|
|
|
import Control.Concurrent
|
2012-06-19 03:47:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
type Pruner = FilePath -> Bool
|
|
|
|
|
|
|
|
canWatch :: Bool
|
|
|
|
#if (WITH_INOTIFY || WITH_KQUEUE)
|
|
|
|
canWatch = True
|
|
|
|
#else
|
|
|
|
#if defined linux_HOST_OS
|
|
|
|
#warning "Building without inotify support"
|
|
|
|
#endif
|
|
|
|
canWatch = False
|
|
|
|
#endif
|
|
|
|
|
2012-12-13 04:45:27 +00:00
|
|
|
{- With inotify, discrete events will be received when making multiple changes
|
|
|
|
- to the same filename. For example, adding it, deleting it, and adding it
|
|
|
|
- again will be three events.
|
|
|
|
-
|
|
|
|
- OTOH, with kqueue, often only one event is received, indicating the most
|
|
|
|
- recent state of the file. -}
|
2012-06-19 06:23:45 +00:00
|
|
|
eventsCoalesce :: Bool
|
|
|
|
#if WITH_INOTIFY
|
|
|
|
eventsCoalesce = False
|
|
|
|
#else
|
|
|
|
#if WITH_KQUEUE
|
|
|
|
eventsCoalesce = True
|
|
|
|
#else
|
|
|
|
eventsCoalesce = undefined
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-12-13 04:45:27 +00:00
|
|
|
{- With inotify, file closing is tracked to some extent, so an add event
|
|
|
|
- will always be received for a file once its writer closes it, and
|
|
|
|
- (typically) not before. This may mean multiple add events for the same file.
|
|
|
|
-
|
|
|
|
- OTOH, with kqueue, add events will often be received while a file is
|
|
|
|
- still being written to, and then no add event will be received once the
|
|
|
|
- writer closes it. -}
|
2012-06-19 14:22:36 +00:00
|
|
|
closingTracked :: Bool
|
|
|
|
#if WITH_INOTIFY
|
|
|
|
closingTracked = True
|
|
|
|
#else
|
|
|
|
#if WITH_KQUEUE
|
|
|
|
closingTracked = False
|
|
|
|
#else
|
2012-06-20 19:53:56 +00:00
|
|
|
closingTracked = undefined
|
2012-06-19 14:22:36 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-12-13 04:45:27 +00:00
|
|
|
{- With inotify, modifications to existing files can be tracked.
|
|
|
|
- Kqueue does not support this. -}
|
2012-09-20 21:24:40 +00:00
|
|
|
modifyTracked :: Bool
|
|
|
|
#if WITH_INOTIFY
|
|
|
|
modifyTracked = True
|
|
|
|
#else
|
|
|
|
#if WITH_KQUEUE
|
|
|
|
modifyTracked = False
|
|
|
|
#else
|
|
|
|
modifyTracked = undefined
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-12-13 04:45:27 +00:00
|
|
|
{- Starts a watcher thread. The runStartup action is passed a scanner action
|
|
|
|
- to run, that will return once the initial directory scan is complete.
|
|
|
|
- Once runStartup returns, the watcher thread continues running,
|
|
|
|
- and processing events. Returns a DirWatcherHandle that can be used
|
|
|
|
- to shutdown later. -}
|
2012-06-19 03:47:48 +00:00
|
|
|
#if WITH_INOTIFY
|
2012-06-28 17:37:03 +00:00
|
|
|
type DirWatcherHandle = INotify.INotify
|
|
|
|
watchDir :: FilePath -> Pruner -> WatchHooks -> (IO () -> IO ()) -> IO DirWatcherHandle
|
|
|
|
watchDir dir prune hooks runstartup = do
|
|
|
|
i <- INotify.initINotify
|
2012-06-19 03:47:48 +00:00
|
|
|
runstartup $ INotify.watchDir i dir prune hooks
|
2012-06-28 17:37:03 +00:00
|
|
|
return i
|
2012-06-19 03:47:48 +00:00
|
|
|
#else
|
|
|
|
#if WITH_KQUEUE
|
2012-06-28 18:15:49 +00:00
|
|
|
type DirWatcherHandle = ThreadId
|
2012-06-28 17:37:03 +00:00
|
|
|
watchDir :: FilePath -> Pruner -> WatchHooks -> (IO Kqueue.Kqueue -> IO Kqueue.Kqueue) -> IO DirWatcherHandle
|
2012-09-28 21:31:54 +00:00
|
|
|
watchDir dir prune hooks runstartup = do
|
|
|
|
kq <- runstartup $ Kqueue.initKqueue dir prune
|
2012-06-28 17:37:03 +00:00
|
|
|
forkIO $ Kqueue.runHooks kq hooks
|
2012-06-19 03:47:48 +00:00
|
|
|
#else
|
2012-06-28 17:37:03 +00:00
|
|
|
type DirWatcherHandle = ()
|
|
|
|
watchDir :: FilePath -> Pruner -> WatchHooks -> (IO () -> IO ()) -> IO DirWatcherHandle
|
2012-06-19 03:47:48 +00:00
|
|
|
watchDir = undefined
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-06-28 17:37:03 +00:00
|
|
|
|
|
|
|
#if WITH_INOTIFY
|
|
|
|
stopWatchDir :: DirWatcherHandle -> IO ()
|
|
|
|
stopWatchDir = INotify.killINotify
|
|
|
|
#else
|
|
|
|
#if WITH_KQUEUE
|
|
|
|
stopWatchDir :: DirWatcherHandle -> IO ()
|
|
|
|
stopWatchDir = killThread
|
|
|
|
#else
|
|
|
|
stopWatchDir :: DirWatcherHandle -> IO ()
|
|
|
|
stopWatchDir = undefined
|
|
|
|
#endif
|
|
|
|
#endif
|