2012-06-18 16:25:20 +00:00
|
|
|
{- BSD kqueue file modification notification interface
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
|
|
|
|
2012-06-18 17:01:58 +00:00
|
|
|
module Utility.Kqueue (
|
2012-06-18 17:19:40 +00:00
|
|
|
scanRecursive,
|
|
|
|
addSubDir,
|
|
|
|
removeSubDir,
|
2012-06-18 20:33:27 +00:00
|
|
|
|
|
|
|
initKqueue,
|
|
|
|
stopKqueue,
|
|
|
|
|
2012-06-18 17:01:58 +00:00
|
|
|
waitChange,
|
|
|
|
) where
|
2012-06-18 16:25:20 +00:00
|
|
|
|
|
|
|
import Common
|
|
|
|
|
|
|
|
import System.Posix.Types
|
|
|
|
import Foreign.C.Types
|
|
|
|
import Foreign.Ptr
|
|
|
|
import Foreign.Marshal
|
2012-06-18 17:01:58 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
type DirMap = M.Map Fd FilePath
|
2012-06-18 16:25:20 +00:00
|
|
|
|
2012-06-18 20:18:59 +00:00
|
|
|
data Kqueue = Kqueue Fd DirMap
|
|
|
|
|
2012-06-18 17:19:40 +00:00
|
|
|
{- Builds a map of directories in a tree, possibly pruning some.
|
|
|
|
- Opens each directory in the tree. -}
|
|
|
|
scanRecursive :: FilePath -> (FilePath -> Bool) -> IO DirMap
|
|
|
|
scanRecursive dir prune = M.fromList <$> (mapM opendir =<< dirTree dir prune)
|
|
|
|
where
|
|
|
|
opendir d = (,)
|
|
|
|
<$> openFd d ReadOnly Nothing defaultFileFlags
|
|
|
|
<*> pure d
|
|
|
|
|
|
|
|
{- Adds a subdirectory (and all its subdirectories, unless pruned) to a
|
|
|
|
- directory map. -}
|
|
|
|
addSubDir :: DirMap -> FilePath -> (FilePath -> Bool) -> IO DirMap
|
|
|
|
addSubDir dirmap dir prune = M.union dirmap <$> scanRecursive dir prune
|
|
|
|
|
|
|
|
{- Removes a subdirectory (and all its subdirectories) from a directory map. -}
|
2012-06-18 23:14:58 +00:00
|
|
|
removeSubDir :: FilePath -> DirMap -> IO DirMap
|
|
|
|
removeSubDir dir dirmap = do
|
|
|
|
mapM_ closeFd $ M.keys toremove) $ closeFd
|
|
|
|
return rest
|
|
|
|
where
|
|
|
|
(toremove, rest) = M.partition (dirContains dir) dirmap
|
2012-06-18 17:19:40 +00:00
|
|
|
|
2012-06-18 20:18:59 +00:00
|
|
|
foreign import ccall unsafe "libkqueue.h init_kqueue" c_init_kqueue
|
|
|
|
:: CInt -> Ptr Fd -> IO Fd
|
|
|
|
foreign import ccall unsafe "libkqueue.h waitchange_kqueue" c_waitchange_kqueue
|
|
|
|
:: Fd -> IO Fd
|
|
|
|
|
|
|
|
{- Initializes a Kqueue to watch a map of directories. -}
|
|
|
|
initKqueue :: DirMap -> IO Kqueue
|
2012-06-18 20:33:27 +00:00
|
|
|
initKqueue dirmap = withArrayLen (M.keys dirmap) $ \fdcnt c_fds -> do
|
2012-06-18 20:18:59 +00:00
|
|
|
h <- c_init_kqueue (fromIntegral fdcnt) c_fds
|
|
|
|
return $ Kqueue h dirmap
|
2012-06-18 16:25:20 +00:00
|
|
|
|
2012-06-18 20:18:59 +00:00
|
|
|
{- Stops a Kqueue. Note: Does not directly close the Fds in the dirmap,
|
|
|
|
- so it can be reused. -}
|
2012-06-18 20:33:27 +00:00
|
|
|
stopKqueue :: Kqueue -> IO ()
|
2012-06-18 20:18:59 +00:00
|
|
|
stopKqueue (Kqueue h _) = closeFd h
|
|
|
|
|
|
|
|
{- Waits for a change on a Kqueue, and returns the directory
|
2012-06-18 20:33:27 +00:00
|
|
|
- where a change took place.
|
2012-06-18 17:19:40 +00:00
|
|
|
-
|
|
|
|
- The kqueue interface does not tell what type of change took place in
|
|
|
|
- the directory; it could be an added file, a deleted file, a renamed
|
|
|
|
- file, a new subdirectory, or a deleted subdirectory, or a moved
|
|
|
|
- subdirectory.
|
|
|
|
-
|
2012-06-18 20:18:59 +00:00
|
|
|
- Note that if subdirectories have changed, the caller should re-run
|
|
|
|
- initKqueue to get them watched. -}
|
2012-06-18 20:33:27 +00:00
|
|
|
waitChange :: Kqueue -> IO (Maybe FilePath)
|
2012-06-18 20:18:59 +00:00
|
|
|
waitChange (Kqueue h dirmap) = do
|
|
|
|
changed <- c_waitchange_kqueue h
|
|
|
|
return $ M.lookup changed dirmap
|