2012-06-19 03:47:48 +00:00
|
|
|
{- generic directory watching interface
|
|
|
|
-
|
2013-11-12 21:17:50 +00:00
|
|
|
- Uses inotify, or kqueue, or fsevents, or win32-notify to watch a directory
|
2012-12-27 19:19:12 +00:00
|
|
|
- (and subdirectories) for changes, and runs hooks for different
|
|
|
|
- sorts of events as they occur.
|
2012-06-19 03:47:48 +00:00
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012-2013 Joey Hess <id@joeyh.name>
|
2012-06-19 03:47:48 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-06-19 03:47:48 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Utility.DirWatcher where
|
|
|
|
|
2013-03-11 02:24:13 +00:00
|
|
|
import Utility.DirWatcher.Types
|
2012-06-19 03:47:48 +00:00
|
|
|
|
|
|
|
#if WITH_INOTIFY
|
2013-12-05 03:09:54 +00:00
|
|
|
import qualified Utility.DirWatcher.INotify as INotify
|
2012-06-19 03:47:48 +00:00
|
|
|
import qualified System.INotify as INotify
|
|
|
|
#endif
|
|
|
|
#if WITH_KQUEUE
|
2013-12-05 03:09:54 +00:00
|
|
|
import qualified Utility.DirWatcher.Kqueue as Kqueue
|
2012-06-28 18:13:15 +00:00
|
|
|
import Control.Concurrent
|
2012-06-19 03:47:48 +00:00
|
|
|
#endif
|
2012-12-27 19:19:12 +00:00
|
|
|
#if WITH_FSEVENTS
|
2013-12-05 03:09:54 +00:00
|
|
|
import qualified Utility.DirWatcher.FSEvents as FSEvents
|
2012-12-27 19:19:12 +00:00
|
|
|
import qualified System.OSX.FSEvents as FSEvents
|
|
|
|
#endif
|
2013-11-12 21:17:50 +00:00
|
|
|
#if WITH_WIN32NOTIFY
|
2013-12-05 03:09:54 +00:00
|
|
|
import qualified Utility.DirWatcher.Win32Notify as Win32Notify
|
2013-11-12 21:17:50 +00:00
|
|
|
import qualified System.Win32.Notify as Win32Notify
|
|
|
|
#endif
|
2012-06-19 03:47:48 +00:00
|
|
|
|
|
|
|
type Pruner = FilePath -> Bool
|
|
|
|
|
|
|
|
canWatch :: Bool
|
2013-11-12 21:17:50 +00:00
|
|
|
#if (WITH_INOTIFY || WITH_KQUEUE || WITH_FSEVENTS || WITH_WIN32NOTIFY)
|
2012-06-19 03:47:48 +00:00
|
|
|
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
|
2013-11-12 21:17:50 +00:00
|
|
|
#if (WITH_INOTIFY || WITH_WIN32NOTIFY)
|
2012-06-19 06:23:45 +00:00
|
|
|
eventsCoalesce = False
|
|
|
|
#else
|
2013-03-17 21:01:43 +00:00
|
|
|
#if (WITH_KQUEUE || WITH_FSEVENTS)
|
2012-06-19 06:23:45 +00:00
|
|
|
eventsCoalesce = True
|
|
|
|
#else
|
2015-04-19 04:38:29 +00:00
|
|
|
eventsCoalesce = error "eventsCoalesce not defined"
|
2012-06-19 06:23:45 +00:00
|
|
|
#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
|
2017-06-09 18:18:58 +00:00
|
|
|
- writer closes it.
|
|
|
|
-
|
|
|
|
- fsevents sometimes behaves similarly, but has sometimes been
|
|
|
|
- seen to behave like kqueue. -}
|
2012-06-19 14:22:36 +00:00
|
|
|
closingTracked :: Bool
|
2017-06-09 18:18:58 +00:00
|
|
|
#if (WITH_INOTIFY || WITH_WIN32NOTIFY)
|
2012-06-19 14:22:36 +00:00
|
|
|
closingTracked = True
|
|
|
|
#else
|
2017-06-09 18:18:58 +00:00
|
|
|
#if (WITH_KQUEUE || WITH_FSEVENTS)
|
2012-06-19 14:22:36 +00:00
|
|
|
closingTracked = False
|
|
|
|
#else
|
2015-04-19 04:38:29 +00:00
|
|
|
closingTracked = error "closingTracked not defined"
|
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.
|
2012-12-27 19:19:12 +00:00
|
|
|
- Kqueue does not support this.
|
|
|
|
- Fsevents generates events when an existing file is reopened and rewritten,
|
|
|
|
- but not necessarily when it's opened once and modified repeatedly. -}
|
2012-09-20 21:24:40 +00:00
|
|
|
modifyTracked :: Bool
|
2013-11-12 21:17:50 +00:00
|
|
|
#if (WITH_INOTIFY || WITH_FSEVENTS || WITH_WIN32NOTIFY)
|
2012-09-20 21:24:40 +00:00
|
|
|
modifyTracked = True
|
|
|
|
#else
|
|
|
|
#if WITH_KQUEUE
|
|
|
|
modifyTracked = False
|
|
|
|
#else
|
2015-04-19 04:38:29 +00:00
|
|
|
modifyTracked = error "modifyTracked not defined"
|
2012-09-20 21:24:40 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-12-27 19:19:12 +00:00
|
|
|
{- Starts a watcher thread. The runstartup action is passed a scanner action
|
2012-12-13 04:45:27 +00:00
|
|
|
- to run, that will return once the initial directory scan is complete.
|
2012-12-27 19:19:12 +00:00
|
|
|
- Once runstartup returns, the watcher thread continues running,
|
2012-12-13 04:45:27 +00:00
|
|
|
- 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
|
2014-03-05 21:44:14 +00:00
|
|
|
watchDir :: FilePath -> Pruner -> Bool -> WatchHooks -> (IO () -> IO ()) -> IO DirWatcherHandle
|
|
|
|
watchDir dir prune scanevents hooks runstartup = do
|
2012-06-28 17:37:03 +00:00
|
|
|
i <- INotify.initINotify
|
2014-03-05 21:44:14 +00:00
|
|
|
runstartup $ INotify.watchDir i dir prune scanevents 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
|
2014-03-05 21:44:14 +00:00
|
|
|
watchDir :: FilePath -> Pruner -> Bool -> WatchHooks -> (IO Kqueue.Kqueue -> IO Kqueue.Kqueue) -> IO DirWatcherHandle
|
|
|
|
watchDir dir prune _scanevents hooks runstartup = do
|
2012-09-28 21:31:54 +00:00
|
|
|
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-12-27 19:19:12 +00:00
|
|
|
#if WITH_FSEVENTS
|
|
|
|
type DirWatcherHandle = FSEvents.EventStream
|
2014-03-05 21:44:14 +00:00
|
|
|
watchDir :: FilePath -> Pruner -> Bool -> WatchHooks -> (IO FSEvents.EventStream -> IO FSEvents.EventStream) -> IO DirWatcherHandle
|
|
|
|
watchDir dir prune scanevents hooks runstartup =
|
|
|
|
runstartup $ FSEvents.watchDir dir prune scanevents hooks
|
2012-12-27 19:19:12 +00:00
|
|
|
#else
|
2013-11-12 21:17:50 +00:00
|
|
|
#if WITH_WIN32NOTIFY
|
|
|
|
type DirWatcherHandle = Win32Notify.WatchManager
|
2014-03-05 21:44:14 +00:00
|
|
|
watchDir :: FilePath -> Pruner -> Bool -> WatchHooks -> (IO Win32Notify.WatchManager -> IO Win32Notify.WatchManager) -> IO DirWatcherHandle
|
|
|
|
watchDir dir prune scanevents hooks runstartup =
|
|
|
|
runstartup $ Win32Notify.watchDir dir prune scanevents hooks
|
2013-11-12 21:17:50 +00:00
|
|
|
#else
|
2012-06-28 17:37:03 +00:00
|
|
|
type DirWatcherHandle = ()
|
2014-03-05 21:44:14 +00:00
|
|
|
watchDir :: FilePath -> Pruner -> Bool -> WatchHooks -> (IO () -> IO ()) -> IO DirWatcherHandle
|
2015-04-19 04:38:29 +00:00
|
|
|
watchDir = error "watchDir not defined"
|
2012-06-19 03:47:48 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2012-12-27 19:19:12 +00:00
|
|
|
#endif
|
2013-11-12 21:17:50 +00:00
|
|
|
#endif
|
2012-06-28 17:37:03 +00:00
|
|
|
|
|
|
|
stopWatchDir :: DirWatcherHandle -> IO ()
|
2013-11-12 21:17:50 +00:00
|
|
|
#if WITH_INOTIFY
|
2012-06-28 17:37:03 +00:00
|
|
|
stopWatchDir = INotify.killINotify
|
|
|
|
#else
|
|
|
|
#if WITH_KQUEUE
|
|
|
|
stopWatchDir = killThread
|
|
|
|
#else
|
2012-12-27 19:19:12 +00:00
|
|
|
#if WITH_FSEVENTS
|
|
|
|
stopWatchDir = FSEvents.eventStreamDestroy
|
|
|
|
#else
|
2013-11-12 21:17:50 +00:00
|
|
|
#if WITH_WIN32NOTIFY
|
|
|
|
stopWatchDir = Win32Notify.killWatchManager
|
|
|
|
#else
|
2015-04-19 04:38:29 +00:00
|
|
|
stopWatchDir = error "stopWatchDir not defined"
|
2012-06-28 17:37:03 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
2012-12-27 19:19:12 +00:00
|
|
|
#endif
|
2013-11-12 21:17:50 +00:00
|
|
|
#endif
|