2012-06-18 16:25:20 +00:00
|
|
|
{- BSD kqueue file modification notification interface
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-06-18 16:25:20 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-06-18 16:25:20 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
|
|
|
|
2013-12-05 03:09:54 +00:00
|
|
|
module Utility.DirWatcher.Kqueue (
|
2012-06-19 04:04:40 +00:00
|
|
|
Kqueue,
|
2012-06-18 20:33:27 +00:00
|
|
|
initKqueue,
|
|
|
|
stopKqueue,
|
2012-06-18 17:01:58 +00:00
|
|
|
waitChange,
|
2012-06-19 01:29:30 +00:00
|
|
|
Change(..),
|
2012-06-19 02:13:26 +00:00
|
|
|
changedFile,
|
2012-06-19 03:47:48 +00:00
|
|
|
runHooks,
|
2012-06-18 17:01:58 +00:00
|
|
|
) where
|
2012-06-18 16:25:20 +00:00
|
|
|
|
|
|
|
import Common
|
2013-03-11 02:24:13 +00:00
|
|
|
import Utility.DirWatcher.Types
|
2012-06-18 16:25:20 +00:00
|
|
|
|
|
|
|
import System.Posix.Types
|
|
|
|
import Foreign.C.Types
|
2012-06-19 04:52:55 +00:00
|
|
|
import Foreign.C.Error
|
2012-06-18 16:25:20 +00:00
|
|
|
import Foreign.Ptr
|
|
|
|
import Foreign.Marshal
|
2018-04-22 17:28:31 +00:00
|
|
|
import qualified Data.Map.Strict as M
|
2012-06-19 01:29:30 +00:00
|
|
|
import qualified Data.Set as S
|
2018-09-24 15:25:51 +00:00
|
|
|
import qualified System.Posix.Files as Posix
|
|
|
|
import qualified System.Posix.IO as Posix
|
2012-06-19 04:52:55 +00:00
|
|
|
import Control.Concurrent
|
2012-06-19 01:29:30 +00:00
|
|
|
|
|
|
|
data Change
|
|
|
|
= Deleted FilePath
|
2012-07-17 22:32:55 +00:00
|
|
|
| DeletedDir FilePath
|
2012-06-19 01:29:30 +00:00
|
|
|
| Added FilePath
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
isAdd :: Change -> Bool
|
|
|
|
isAdd (Added _) = True
|
|
|
|
isAdd (Deleted _) = False
|
2012-07-17 22:32:55 +00:00
|
|
|
isAdd (DeletedDir _) = False
|
2012-06-19 01:29:30 +00:00
|
|
|
|
|
|
|
changedFile :: Change -> FilePath
|
|
|
|
changedFile (Added f) = f
|
|
|
|
changedFile (Deleted f) = f
|
2012-07-17 22:35:56 +00:00
|
|
|
changedFile (DeletedDir f) = f
|
2012-06-18 17:01:58 +00:00
|
|
|
|
2012-06-19 13:56:03 +00:00
|
|
|
data Kqueue = Kqueue
|
|
|
|
{ kqueueFd :: Fd
|
2012-06-19 14:08:06 +00:00
|
|
|
, kqueueTop :: FilePath
|
2012-06-19 13:56:03 +00:00
|
|
|
, kqueueMap :: DirMap
|
2012-06-19 14:08:06 +00:00
|
|
|
, _kqueuePruner :: Pruner
|
2012-06-19 13:56:03 +00:00
|
|
|
}
|
2012-06-18 16:25:20 +00:00
|
|
|
|
2012-06-19 01:29:30 +00:00
|
|
|
type Pruner = FilePath -> Bool
|
|
|
|
|
|
|
|
type DirMap = M.Map Fd DirInfo
|
|
|
|
|
2012-07-17 22:32:55 +00:00
|
|
|
{- Enough information to uniquely identify a file in a directory,
|
|
|
|
- but not too much. -}
|
|
|
|
data DirEnt = DirEnt
|
|
|
|
{ dirEnt :: FilePath -- relative to the parent directory
|
|
|
|
, _dirInode :: FileID -- included to notice file replacements
|
|
|
|
, isSubDir :: Bool
|
|
|
|
}
|
|
|
|
deriving (Eq, Ord, Show)
|
|
|
|
|
|
|
|
{- A directory, and its last known contents. -}
|
2012-06-19 01:29:30 +00:00
|
|
|
data DirInfo = DirInfo
|
|
|
|
{ dirName :: FilePath
|
2012-07-17 22:32:55 +00:00
|
|
|
, dirCache :: S.Set DirEnt
|
2012-06-19 01:29:30 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
getDirInfo :: FilePath -> IO DirInfo
|
|
|
|
getDirInfo dir = do
|
2012-07-17 19:57:49 +00:00
|
|
|
l <- filter (not . dirCruft) <$> getDirectoryContents dir
|
2012-07-17 22:32:55 +00:00
|
|
|
contents <- S.fromList . catMaybes <$> mapM getDirEnt l
|
2012-06-19 01:29:30 +00:00
|
|
|
return $ DirInfo dir contents
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
getDirEnt f = catchMaybeIO $ do
|
2012-12-14 20:28:27 +00:00
|
|
|
s <- getSymbolicLinkStatus (dir </> f)
|
2012-12-13 04:24:19 +00:00
|
|
|
return $ DirEnt f (fileID s) (isDirectory s)
|
2012-06-19 01:29:30 +00:00
|
|
|
|
|
|
|
{- Difference between the dirCaches of two DirInfos. -}
|
|
|
|
(//) :: DirInfo -> DirInfo -> [Change]
|
2012-06-19 02:13:26 +00:00
|
|
|
oldc // newc = deleted ++ added
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
deleted = calc gendel oldc newc
|
|
|
|
added = calc genadd newc oldc
|
|
|
|
gendel x = (if isSubDir x then DeletedDir else Deleted) $
|
|
|
|
dirName oldc </> dirEnt x
|
|
|
|
genadd x = Added $ dirName newc </> dirEnt x
|
|
|
|
calc a x y = map a $ S.toList $
|
|
|
|
S.difference (dirCache x) (dirCache y)
|
2012-06-18 20:18:59 +00:00
|
|
|
|
2012-06-18 17:19:40 +00:00
|
|
|
{- Builds a map of directories in a tree, possibly pruning some.
|
2012-06-19 01:29:30 +00:00
|
|
|
- Opens each directory in the tree, and records its current contents. -}
|
|
|
|
scanRecursive :: FilePath -> Pruner -> IO DirMap
|
|
|
|
scanRecursive topdir prune = M.fromList <$> walk [] [topdir]
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
walk c [] = return c
|
|
|
|
walk c (dir:rest)
|
|
|
|
| prune dir = walk c rest
|
|
|
|
| otherwise = do
|
|
|
|
minfo <- catchMaybeIO $ getDirInfo dir
|
|
|
|
case minfo of
|
|
|
|
Nothing -> walk c rest
|
|
|
|
Just info -> do
|
|
|
|
mfd <- catchMaybeIO $
|
2018-09-24 15:25:51 +00:00
|
|
|
Posix.openFd dir Posix.ReadOnly Nothing Posix.defaultFileFlags
|
2012-12-13 04:24:19 +00:00
|
|
|
case mfd of
|
|
|
|
Nothing -> walk c rest
|
|
|
|
Just fd -> do
|
|
|
|
let subdirs = map (dir </>) . map dirEnt $
|
|
|
|
S.toList $ dirCache info
|
|
|
|
walk ((fd, info):c) (subdirs ++ rest)
|
2012-06-19 01:29:30 +00:00
|
|
|
|
|
|
|
{- Adds a list of subdirectories (and all their children), unless pruned to a
|
|
|
|
- directory map. Adding a subdirectory that's already in the map will
|
|
|
|
- cause its contents to be refreshed. -}
|
|
|
|
addSubDirs :: DirMap -> Pruner -> [FilePath] -> IO DirMap
|
|
|
|
addSubDirs dirmap prune dirs = do
|
|
|
|
newmap <- foldr M.union M.empty <$>
|
|
|
|
mapM (\d -> scanRecursive d prune) dirs
|
|
|
|
return $ M.union newmap dirmap -- prefer newmap
|
|
|
|
|
|
|
|
{- Removes a subdirectory (and all its children) from a directory map. -}
|
|
|
|
removeSubDir :: DirMap -> FilePath -> IO DirMap
|
|
|
|
removeSubDir dirmap dir = do
|
2018-09-24 15:25:51 +00:00
|
|
|
mapM_ Posix.closeFd $ M.keys toremove
|
2012-06-18 23:14:58 +00:00
|
|
|
return rest
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2021-02-01 15:53:31 +00:00
|
|
|
(toremove, rest) = M.partition (dirContains (toRawFilePath dir) . toRawFilePath . dirName) dirmap
|
2012-06-18 17:19:40 +00:00
|
|
|
|
2012-06-19 13:56:03 +00:00
|
|
|
findDirContents :: DirMap -> FilePath -> [FilePath]
|
|
|
|
findDirContents dirmap dir = concatMap absolutecontents $ search
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
absolutecontents i = map (dirName i </>)
|
|
|
|
(map dirEnt $ S.toList $ dirCache i)
|
|
|
|
search = map snd $ M.toList $
|
|
|
|
M.filter (\i -> dirName i == dir) dirmap
|
2012-06-19 13:56:03 +00:00
|
|
|
|
2012-07-20 19:03:24 +00:00
|
|
|
foreign import ccall safe "libkqueue.h init_kqueue" c_init_kqueue
|
2012-06-19 01:46:04 +00:00
|
|
|
:: IO Fd
|
2012-07-20 19:03:24 +00:00
|
|
|
foreign import ccall safe "libkqueue.h addfds_kqueue" c_addfds_kqueue
|
2012-06-19 01:46:04 +00:00
|
|
|
:: Fd -> CInt -> Ptr Fd -> IO ()
|
2012-07-20 19:03:24 +00:00
|
|
|
foreign import ccall safe "libkqueue.h waitchange_kqueue" c_waitchange_kqueue
|
2012-06-18 20:18:59 +00:00
|
|
|
:: Fd -> IO Fd
|
|
|
|
|
2012-06-19 01:29:30 +00:00
|
|
|
{- Initializes a Kqueue to watch a directory, and all its subdirectories. -}
|
|
|
|
initKqueue :: FilePath -> Pruner -> IO Kqueue
|
|
|
|
initKqueue dir pruned = do
|
|
|
|
dirmap <- scanRecursive dir pruned
|
2012-06-19 01:46:04 +00:00
|
|
|
h <- c_init_kqueue
|
2012-06-19 14:08:06 +00:00
|
|
|
let kq = Kqueue h dir dirmap pruned
|
2012-06-19 01:46:04 +00:00
|
|
|
updateKqueue kq
|
|
|
|
return kq
|
|
|
|
|
|
|
|
{- Updates a Kqueue, adding watches for its map. -}
|
|
|
|
updateKqueue :: Kqueue -> IO ()
|
2012-06-19 14:08:06 +00:00
|
|
|
updateKqueue (Kqueue h _ dirmap _) =
|
2012-06-19 01:29:30 +00:00
|
|
|
withArrayLen (M.keys dirmap) $ \fdcnt c_fds -> do
|
2012-06-19 01:46:04 +00:00
|
|
|
c_addfds_kqueue h (fromIntegral fdcnt) c_fds
|
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 ()
|
2018-09-24 15:25:51 +00:00
|
|
|
stopKqueue = Posix.closeFd . kqueueFd
|
2012-06-18 20:18:59 +00:00
|
|
|
|
2012-06-19 01:29:30 +00:00
|
|
|
{- Waits for a change on a Kqueue.
|
|
|
|
- May update the Kqueue.
|
|
|
|
-}
|
|
|
|
waitChange :: Kqueue -> IO (Kqueue, [Change])
|
2012-06-19 14:08:06 +00:00
|
|
|
waitChange kq@(Kqueue h _ dirmap _) = do
|
2012-06-19 01:29:30 +00:00
|
|
|
changedfd <- c_waitchange_kqueue h
|
2012-06-19 04:52:55 +00:00
|
|
|
if changedfd == -1
|
|
|
|
then ifM ((==) eINTR <$> getErrno)
|
|
|
|
(yield >> waitChange kq, nochange)
|
|
|
|
else case M.lookup changedfd dirmap of
|
|
|
|
Nothing -> nochange
|
|
|
|
Just info -> handleChange kq changedfd info
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
nochange = return (kq, [])
|
2012-06-19 01:29:30 +00:00
|
|
|
|
|
|
|
{- The kqueue interface does not tell what type of change took place in
|
2012-06-18 17:19:40 +00:00
|
|
|
- the directory; it could be an added file, a deleted file, a renamed
|
|
|
|
- file, a new subdirectory, or a deleted subdirectory, or a moved
|
2012-06-19 01:29:30 +00:00
|
|
|
- subdirectory.
|
2012-06-18 17:19:40 +00:00
|
|
|
-
|
2012-06-19 01:29:30 +00:00
|
|
|
- So to determine this, the contents of the directory are compared
|
|
|
|
- with its last cached contents. The Kqueue is updated to watch new
|
|
|
|
- directories as necessary.
|
|
|
|
-}
|
|
|
|
handleChange :: Kqueue -> Fd -> DirInfo -> IO (Kqueue, [Change])
|
2012-06-19 14:08:06 +00:00
|
|
|
handleChange kq@(Kqueue _ _ dirmap pruner) fd olddirinfo =
|
2012-06-19 01:29:30 +00:00
|
|
|
go =<< catchMaybeIO (getDirInfo $ dirName olddirinfo)
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
go (Just newdirinfo) = do
|
|
|
|
let changes = filter (not . pruner . changedFile) $
|
|
|
|
olddirinfo // newdirinfo
|
|
|
|
let (added, deleted) = partition isAdd changes
|
|
|
|
|
|
|
|
-- Scan newly added directories to add to the map.
|
|
|
|
-- (Newly added files will fail getDirInfo.)
|
|
|
|
newdirinfos <- catMaybes <$>
|
|
|
|
mapM (catchMaybeIO . getDirInfo . changedFile) added
|
|
|
|
newmap <- addSubDirs dirmap pruner $ map dirName newdirinfos
|
|
|
|
|
|
|
|
-- Remove deleted directories from the map.
|
|
|
|
newmap' <- foldM removeSubDir newmap (map changedFile deleted)
|
|
|
|
|
|
|
|
-- Update the cached dirinfo just looked up.
|
2018-04-22 17:28:31 +00:00
|
|
|
let newmap'' = M.insert fd newdirinfo newmap'
|
2012-12-13 04:24:19 +00:00
|
|
|
|
|
|
|
-- When new directories were added, need to update
|
|
|
|
-- the kqueue to watch them.
|
|
|
|
let kq' = kq { kqueueMap = newmap'' }
|
|
|
|
unless (null newdirinfos) $
|
|
|
|
updateKqueue kq'
|
|
|
|
|
|
|
|
return (kq', changes)
|
|
|
|
go Nothing = do
|
|
|
|
-- The directory has been moved or deleted, so
|
|
|
|
-- remove it from our map.
|
|
|
|
newmap <- removeSubDir dirmap (dirName olddirinfo)
|
|
|
|
return (kq { kqueueMap = newmap }, [])
|
2012-06-19 03:47:48 +00:00
|
|
|
|
|
|
|
{- Processes changes on the Kqueue, calling the hooks as appropriate.
|
|
|
|
- Never returns. -}
|
|
|
|
runHooks :: Kqueue -> WatchHooks -> IO ()
|
|
|
|
runHooks kq hooks = do
|
2012-06-19 14:08:06 +00:00
|
|
|
-- First, synthetic add events for the whole directory tree contents,
|
|
|
|
-- to catch any files created beforehand.
|
|
|
|
recursiveadd (kqueueMap kq) (Added $ kqueueTop kq)
|
|
|
|
loop kq
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
loop q = do
|
|
|
|
(q', changes) <- waitChange q
|
|
|
|
forM_ changes $ dispatch (kqueueMap q')
|
|
|
|
loop q'
|
|
|
|
|
|
|
|
dispatch _ change@(Deleted _) =
|
|
|
|
callhook delHook Nothing change
|
|
|
|
dispatch _ change@(DeletedDir _) =
|
|
|
|
callhook delDirHook Nothing change
|
|
|
|
dispatch dirmap change@(Added _) =
|
|
|
|
withstatus change $ dispatchadd dirmap
|
2012-07-17 22:32:55 +00:00
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
dispatchadd dirmap change s
|
2018-09-24 15:25:51 +00:00
|
|
|
| Posix.isSymbolicLink s = callhook addSymlinkHook (Just s) change
|
|
|
|
| Posix.isDirectory s = recursiveadd dirmap change
|
|
|
|
| Posix.isRegularFile s = callhook addHook (Just s) change
|
2012-12-13 04:24:19 +00:00
|
|
|
| otherwise = noop
|
|
|
|
|
|
|
|
recursiveadd dirmap change = do
|
|
|
|
let contents = findDirContents dirmap $ changedFile change
|
|
|
|
forM_ contents $ \f ->
|
|
|
|
withstatus (Added f) $ dispatchadd dirmap
|
|
|
|
|
|
|
|
callhook h s change = case h hooks of
|
|
|
|
Nothing -> noop
|
|
|
|
Just a -> a (changedFile change) s
|
|
|
|
|
|
|
|
withstatus change a = maybe noop (a change) =<<
|
|
|
|
(catchMaybeIO (getSymbolicLinkStatus (changedFile change)))
|