use Win32-inotify

May mean the watcher works on Windows. Untested.
This commit is contained in:
Joey Hess 2013-11-12 17:17:50 -04:00
parent 8585465a24
commit 7ebdcc7535
2 changed files with 30 additions and 17 deletions

View file

@ -1,10 +1,10 @@
{- generic directory watching interface {- generic directory watching interface
- -
- Uses inotify, or kqueue, or fsevents to watch a directory - Uses inotify, or kqueue, or fsevents, or win32-notify to watch a directory
- (and subdirectories) for changes, and runs hooks for different - (and subdirectories) for changes, and runs hooks for different
- sorts of events as they occur. - sorts of events as they occur.
- -
- Copyright 2012 Joey Hess <joey@kitenet.net> - Copyright 2012-2013 Joey Hess <joey@kitenet.net>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -27,11 +27,15 @@ import Control.Concurrent
import qualified Utility.FSEvents as FSEvents import qualified Utility.FSEvents as FSEvents
import qualified System.OSX.FSEvents as FSEvents import qualified System.OSX.FSEvents as FSEvents
#endif #endif
#if WITH_WIN32NOTIFY
import qualified Utility.Win32Notify as Win32Notify
import qualified System.Win32.Notify as Win32Notify
#endif
type Pruner = FilePath -> Bool type Pruner = FilePath -> Bool
canWatch :: Bool canWatch :: Bool
#if (WITH_INOTIFY || WITH_KQUEUE || WITH_FSEVENTS) #if (WITH_INOTIFY || WITH_KQUEUE || WITH_FSEVENTS || WITH_WIN32NOTIFY)
canWatch = True canWatch = True
#else #else
#if defined linux_HOST_OS #if defined linux_HOST_OS
@ -47,7 +51,7 @@ canWatch = False
- OTOH, with kqueue, often only one event is received, indicating the most - OTOH, with kqueue, often only one event is received, indicating the most
- recent state of the file. -} - recent state of the file. -}
eventsCoalesce :: Bool eventsCoalesce :: Bool
#if WITH_INOTIFY #if (WITH_INOTIFY || WITH_WIN32NOTIFY)
eventsCoalesce = False eventsCoalesce = False
#else #else
#if (WITH_KQUEUE || WITH_FSEVENTS) #if (WITH_KQUEUE || WITH_FSEVENTS)
@ -68,7 +72,7 @@ eventsCoalesce = undefined
- still being written to, and then no add event will be received once the - still being written to, and then no add event will be received once the
- writer closes it. -} - writer closes it. -}
closingTracked :: Bool closingTracked :: Bool
#if (WITH_INOTIFY || WITH_FSEVENTS) #if (WITH_INOTIFY || WITH_FSEVENTS || WITH_WIN32NOTIFY)
closingTracked = True closingTracked = True
#else #else
#if WITH_KQUEUE #if WITH_KQUEUE
@ -83,7 +87,7 @@ closingTracked = undefined
- Fsevents generates events when an existing file is reopened and rewritten, - Fsevents generates events when an existing file is reopened and rewritten,
- but not necessarily when it's opened once and modified repeatedly. -} - but not necessarily when it's opened once and modified repeatedly. -}
modifyTracked :: Bool modifyTracked :: Bool
#if (WITH_INOTIFY || WITH_FSEVENTS) #if (WITH_INOTIFY || WITH_FSEVENTS || WITH_WIN32NOTIFY)
modifyTracked = True modifyTracked = True
#else #else
#if WITH_KQUEUE #if WITH_KQUEUE
@ -119,27 +123,35 @@ watchDir :: FilePath -> Pruner -> WatchHooks -> (IO FSEvents.EventStream -> IO F
watchDir dir prune hooks runstartup = watchDir dir prune hooks runstartup =
runstartup $ FSEvents.watchDir dir prune hooks runstartup $ FSEvents.watchDir dir prune hooks
#else #else
#if WITH_WIN32NOTIFY
type DirWatcherHandle = Win32Notify.WatchManager
watchDir :: FilePath -> Pruner -> WatchHooks -> (IO Win32Notify.WatchManager -> IO Win32Notify.WatchManager) -> IO DirWatcherHandle
watchDir dir prune hooks runstartup =
runstartup $ Win32Notify.watchDir dir prune hooks
#else
type DirWatcherHandle = () type DirWatcherHandle = ()
watchDir :: FilePath -> Pruner -> WatchHooks -> (IO () -> IO ()) -> IO DirWatcherHandle watchDir :: FilePath -> Pruner -> WatchHooks -> (IO () -> IO ()) -> IO DirWatcherHandle
watchDir = undefined watchDir = undefined
#endif #endif
#endif #endif
#endif #endif
#endif
#if WITH_INOTIFY
stopWatchDir :: DirWatcherHandle -> IO () stopWatchDir :: DirWatcherHandle -> IO ()
#if WITH_INOTIFY
stopWatchDir = INotify.killINotify stopWatchDir = INotify.killINotify
#else #else
#if WITH_KQUEUE #if WITH_KQUEUE
stopWatchDir :: DirWatcherHandle -> IO ()
stopWatchDir = killThread stopWatchDir = killThread
#else #else
#if WITH_FSEVENTS #if WITH_FSEVENTS
stopWatchDir :: DirWatcherHandle -> IO ()
stopWatchDir = FSEvents.eventStreamDestroy stopWatchDir = FSEvents.eventStreamDestroy
#else #else
stopWatchDir :: DirWatcherHandle -> IO () #if WITH_WIN32NOTIFY
stopWatchDir = Win32Notify.killWatchManager
#else
stopWatchDir = undefined stopWatchDir = undefined
#endif #endif
#endif #endif
#endif #endif
#endif

View file

@ -11,32 +11,33 @@ import Common hiding (isDirectory)
import Utility.DirWatcher.Types import Utility.DirWatcher.Types
import System.Win32.Notify import System.Win32.Notify
import qualified System.PosixCompat.Files as Files
watchDir :: FilePath -> (FilePath -> Bool) -> WatchHooks -> IO WatchManager watchDir :: FilePath -> (FilePath -> Bool) -> WatchHooks -> IO WatchManager
watchDir dir ignored hooks = do watchDir dir ignored hooks = do
scan dir scan dir
wm <- initWatchManager wm <- initWatchManager
void $ watchDirectory wm dir True [Create, Delete, Modify, Move] handle void $ watchDirectory wm dir True [Create, Delete, Modify, Move] handle
retufn wm return wm
where where
handle evt handle evt
| ignoredPath ignored (filePath evt) = noop | ignoredPath ignored (filePath evt) = noop
| otherwise = case eventToVariety evt of | otherwise = case evt of
Delete (Deleted _ _)
| isDirectory evt -> runhook delDirHook Nothing | isDirectory evt -> runhook delDirHook Nothing
| otherwise -> runhook delHook Nothing | otherwise -> runhook delHook Nothing
Create (Created _ _)
| isDirectory evt -> noop | isDirectory evt -> noop
| otherwise -> runhook addHook Nothing | otherwise -> runhook addHook Nothing
Modify (Modified _ _)
| isDirectory evt -> noop | isDirectory evt -> noop
{- Add hooks are run when a file is modified for {- Add hooks are run when a file is modified for
- compatability with INotify, which calls the add - compatability with INotify, which calls the add
- hook when a file is closed, and so tends to call - hook when a file is closed, and so tends to call
- both add and modify for file modifications. -} - both add and modify for file modifications. -}
| otherwise -> do | otherwise -> do
runHook addHook Nothing runhook addHook Nothing
runHook modifyHook Nothing runhook modifyHook Nothing
where where
runhook h s = maybe noop (\a -> a (filePath evt) s) (h hooks) runhook h s = maybe noop (\a -> a (filePath evt) s) (h hooks)