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
-
- 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
- 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.
-}
@ -27,11 +27,15 @@ import Control.Concurrent
import qualified Utility.FSEvents as FSEvents
import qualified System.OSX.FSEvents as FSEvents
#endif
#if WITH_WIN32NOTIFY
import qualified Utility.Win32Notify as Win32Notify
import qualified System.Win32.Notify as Win32Notify
#endif
type Pruner = FilePath -> Bool
canWatch :: Bool
#if (WITH_INOTIFY || WITH_KQUEUE || WITH_FSEVENTS)
#if (WITH_INOTIFY || WITH_KQUEUE || WITH_FSEVENTS || WITH_WIN32NOTIFY)
canWatch = True
#else
#if defined linux_HOST_OS
@ -47,7 +51,7 @@ canWatch = False
- OTOH, with kqueue, often only one event is received, indicating the most
- recent state of the file. -}
eventsCoalesce :: Bool
#if WITH_INOTIFY
#if (WITH_INOTIFY || WITH_WIN32NOTIFY)
eventsCoalesce = False
#else
#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
- writer closes it. -}
closingTracked :: Bool
#if (WITH_INOTIFY || WITH_FSEVENTS)
#if (WITH_INOTIFY || WITH_FSEVENTS || WITH_WIN32NOTIFY)
closingTracked = True
#else
#if WITH_KQUEUE
@ -83,7 +87,7 @@ closingTracked = undefined
- Fsevents generates events when an existing file is reopened and rewritten,
- but not necessarily when it's opened once and modified repeatedly. -}
modifyTracked :: Bool
#if (WITH_INOTIFY || WITH_FSEVENTS)
#if (WITH_INOTIFY || WITH_FSEVENTS || WITH_WIN32NOTIFY)
modifyTracked = True
#else
#if WITH_KQUEUE
@ -119,27 +123,35 @@ watchDir :: FilePath -> Pruner -> WatchHooks -> (IO FSEvents.EventStream -> IO F
watchDir dir prune hooks runstartup =
runstartup $ FSEvents.watchDir dir prune hooks
#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 = ()
watchDir :: FilePath -> Pruner -> WatchHooks -> (IO () -> IO ()) -> IO DirWatcherHandle
watchDir = undefined
#endif
#endif
#endif
#endif
#if WITH_INOTIFY
stopWatchDir :: DirWatcherHandle -> IO ()
#if WITH_INOTIFY
stopWatchDir = INotify.killINotify
#else
#if WITH_KQUEUE
stopWatchDir :: DirWatcherHandle -> IO ()
stopWatchDir = killThread
#else
#if WITH_FSEVENTS
stopWatchDir :: DirWatcherHandle -> IO ()
stopWatchDir = FSEvents.eventStreamDestroy
#else
stopWatchDir :: DirWatcherHandle -> IO ()
#if WITH_WIN32NOTIFY
stopWatchDir = Win32Notify.killWatchManager
#else
stopWatchDir = undefined
#endif
#endif
#endif
#endif

View file

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