2012-12-27 19:19:12 +00:00
|
|
|
{- FSEvents interface
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-12-27 19:19:12 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-12-27 19:19:12 +00:00
|
|
|
-}
|
|
|
|
|
2013-12-05 03:09:54 +00:00
|
|
|
module Utility.DirWatcher.FSEvents where
|
2012-12-27 19:19:12 +00:00
|
|
|
|
|
|
|
import Common hiding (isDirectory)
|
2013-03-11 02:24:13 +00:00
|
|
|
import Utility.DirWatcher.Types
|
2012-12-27 19:19:12 +00:00
|
|
|
|
|
|
|
import System.OSX.FSEvents
|
|
|
|
import qualified System.Posix.Files as Files
|
|
|
|
import Data.Bits ((.&.))
|
|
|
|
|
2014-03-05 21:44:14 +00:00
|
|
|
watchDir :: FilePath -> (FilePath -> Bool) -> Bool -> WatchHooks -> IO EventStream
|
|
|
|
watchDir dir ignored scanevents hooks = do
|
2012-12-27 19:19:12 +00:00
|
|
|
unlessM fileLevelEventsSupported $
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup "Need at least OSX 10.7.0 for file-level FSEvents"
|
2012-12-27 19:46:18 +00:00
|
|
|
scan dir
|
2014-12-29 21:35:19 +00:00
|
|
|
eventStreamCreate [dir] 1.0 True True True dispatch
|
2012-12-27 19:19:12 +00:00
|
|
|
where
|
2014-12-29 21:35:19 +00:00
|
|
|
dispatch evt
|
2012-12-27 19:19:12 +00:00
|
|
|
| ignoredPath ignored (eventPath evt) = noop
|
|
|
|
| otherwise = do
|
|
|
|
{- More than one flag may be set, if events occurred
|
|
|
|
- close together.
|
|
|
|
-
|
|
|
|
- Order is important..
|
|
|
|
- If a file is added and then deleted, we'll see it's
|
|
|
|
- not present, and addHook won't run.
|
|
|
|
- OTOH, if a file is deleted and then re-added,
|
|
|
|
- the delHook will run first, followed by the addHook.
|
|
|
|
-}
|
|
|
|
|
|
|
|
when (hasflag eventFlagItemRemoved) $
|
2012-12-27 19:34:55 +00:00
|
|
|
if hasflag eventFlagItemIsDir
|
|
|
|
then runhook delDirHook Nothing
|
|
|
|
else runhook delHook Nothing
|
2012-12-28 20:20:05 +00:00
|
|
|
when (hasflag eventFlagItemCreated) $
|
|
|
|
maybe noop handleadd =<< getstatus (eventPath evt)
|
|
|
|
{- When a file or dir is renamed, a rename event is
|
|
|
|
- received for both its old and its new name. -}
|
|
|
|
when (hasflag eventFlagItemRenamed) $
|
|
|
|
if hasflag eventFlagItemIsDir
|
|
|
|
then ifM (doesDirectoryExist $ eventPath evt)
|
|
|
|
( scan $ eventPath evt
|
|
|
|
, runhook delDirHook Nothing
|
|
|
|
)
|
|
|
|
else maybe (runhook delHook Nothing) handleadd
|
|
|
|
=<< getstatus (eventPath evt)
|
2012-12-29 18:58:13 +00:00
|
|
|
{- 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. -}
|
2012-12-27 19:34:55 +00:00
|
|
|
when (hasflag eventFlagItemModified && not (hasflag eventFlagItemIsDir)) $ do
|
2012-12-27 19:19:12 +00:00
|
|
|
ms <- getstatus $ eventPath evt
|
2012-12-29 18:58:13 +00:00
|
|
|
maybe noop handleadd ms
|
2012-12-27 19:19:12 +00:00
|
|
|
runhook modifyHook ms
|
|
|
|
where
|
|
|
|
hasflag f = eventFlags evt .&. f /= 0
|
|
|
|
runhook h s = maybe noop (\a -> a (eventPath evt) s) (h hooks)
|
2012-12-28 20:20:05 +00:00
|
|
|
handleadd s
|
|
|
|
| Files.isSymbolicLink s = runhook addSymlinkHook $ Just s
|
|
|
|
| Files.isRegularFile s = runhook addHook $ Just s
|
|
|
|
| otherwise = noop
|
2012-12-27 19:46:18 +00:00
|
|
|
|
|
|
|
scan d = unless (ignoredPath ignored d) $
|
2013-12-18 19:05:29 +00:00
|
|
|
-- Do not follow symlinks when scanning.
|
|
|
|
-- This mirrors the inotify startup scan behavior.
|
|
|
|
mapM_ go =<< dirContentsRecursiveSkipping (const False) False d
|
2012-12-27 19:46:18 +00:00
|
|
|
where
|
|
|
|
go f
|
|
|
|
| ignoredPath ignored f = noop
|
|
|
|
| otherwise = do
|
|
|
|
ms <- getstatus f
|
|
|
|
case ms of
|
|
|
|
Nothing -> noop
|
|
|
|
Just s
|
|
|
|
| Files.isSymbolicLink s ->
|
2014-03-05 21:44:14 +00:00
|
|
|
when scanevents $
|
|
|
|
runhook addSymlinkHook ms
|
2012-12-27 19:46:18 +00:00
|
|
|
| Files.isRegularFile s ->
|
2014-03-05 21:44:14 +00:00
|
|
|
when scanevents $
|
|
|
|
runhook addHook ms
|
2012-12-27 19:46:18 +00:00
|
|
|
| otherwise ->
|
|
|
|
noop
|
|
|
|
where
|
|
|
|
runhook h s = maybe noop (\a -> a f s) (h hooks)
|
|
|
|
|
|
|
|
getstatus = catchMaybeIO . getSymbolicLinkStatus
|
2012-12-27 19:19:12 +00:00
|
|
|
|
|
|
|
{- Check each component of the path to see if it's ignored. -}
|
|
|
|
ignoredPath :: (FilePath -> Bool) -> FilePath -> Bool
|
|
|
|
ignoredPath ignored = any ignored . map dropTrailingPathSeparator . splitPath
|