2012-06-04 17:22:56 +00:00
|
|
|
{- higher-level inotify interface
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-06-19 03:47:48 +00:00
|
|
|
module Utility.INotify where
|
2012-04-12 00:03:49 +00:00
|
|
|
|
|
|
|
import Common hiding (isDirectory)
|
2012-06-04 23:43:29 +00:00
|
|
|
import Utility.ThreadLock
|
2012-06-19 03:47:48 +00:00
|
|
|
import Utility.Types.DirWatcher
|
2012-06-04 23:43:29 +00:00
|
|
|
|
2012-04-12 00:03:49 +00:00
|
|
|
import System.INotify
|
|
|
|
import qualified System.Posix.Files as Files
|
2012-06-06 20:50:28 +00:00
|
|
|
import System.IO.Error
|
|
|
|
import Control.Exception (throw)
|
2012-04-12 00:03:49 +00:00
|
|
|
|
2012-04-12 20:59:33 +00:00
|
|
|
{- Watches for changes to files in a directory, and all its subdirectories
|
2012-06-04 17:22:56 +00:00
|
|
|
- that are not ignored, using inotify. This function returns after
|
|
|
|
- its initial scan is complete, leaving a thread running. Callbacks are
|
|
|
|
- made for different events.
|
2012-04-12 00:03:49 +00:00
|
|
|
-
|
|
|
|
- Inotify is weak at recursive directory watching; the whole directory
|
2012-06-12 18:34:09 +00:00
|
|
|
- tree must be scanned and watches set explicitly for each subdirectory.
|
2012-04-12 00:03:49 +00:00
|
|
|
-
|
|
|
|
- To notice newly created subdirectories, inotify is used, and
|
|
|
|
- watches are registered for those directories. There is a race there;
|
|
|
|
- things can be added to a directory before the watch gets registered.
|
|
|
|
-
|
|
|
|
- To close the inotify race, each time a new directory is found, it also
|
|
|
|
- recursively scans it, assuming all files in it were just added,
|
|
|
|
- and registering each subdirectory.
|
|
|
|
-
|
|
|
|
- Note: Due to the race amelioration, multiple add events may occur
|
|
|
|
- for the same file.
|
|
|
|
-
|
2012-06-04 17:22:56 +00:00
|
|
|
- Note: Moving a file will cause events deleting it from its old location
|
|
|
|
- and adding it to the new location.
|
2012-04-12 00:03:49 +00:00
|
|
|
-
|
2012-09-20 21:24:40 +00:00
|
|
|
- Note: It's assumed that when a file that was open for write is closed,
|
|
|
|
- it's finished being written to, and can be added.
|
2012-04-12 00:03:49 +00:00
|
|
|
-
|
|
|
|
- Note: inotify has a limit to the number of watches allowed,
|
|
|
|
- /proc/sys/fs/inotify/max_user_watches (default 8192).
|
2012-06-06 20:50:28 +00:00
|
|
|
- So this will fail if there are too many subdirectories. The
|
2012-06-07 03:20:09 +00:00
|
|
|
- errHook is called when this happens.
|
2012-04-12 00:03:49 +00:00
|
|
|
-}
|
2012-06-07 03:20:09 +00:00
|
|
|
watchDir :: INotify -> FilePath -> (FilePath -> Bool) -> WatchHooks -> IO ()
|
|
|
|
watchDir i dir ignored hooks
|
2012-06-06 20:50:28 +00:00
|
|
|
| ignored dir = noop
|
|
|
|
| otherwise = do
|
2012-06-12 18:50:54 +00:00
|
|
|
-- Use a lock to make sure events generated during initial
|
|
|
|
-- scan come before real inotify events.
|
2012-06-06 20:50:28 +00:00
|
|
|
lock <- newLock
|
|
|
|
let handler event = withLock lock (void $ go event)
|
|
|
|
void (addWatch i watchevents dir handler)
|
|
|
|
`catchIO` failedaddwatch
|
|
|
|
withLock lock $
|
2012-06-12 18:34:09 +00:00
|
|
|
mapM_ scan =<< filter (not . dirCruft) <$>
|
2012-06-06 20:50:28 +00:00
|
|
|
getDirectoryContents dir
|
2012-04-12 00:03:49 +00:00
|
|
|
where
|
2012-06-07 03:20:09 +00:00
|
|
|
recurse d = watchDir i d ignored hooks
|
2012-06-04 17:22:56 +00:00
|
|
|
|
|
|
|
-- Select only inotify events required by the enabled
|
|
|
|
-- hooks, but always include Create so new directories can
|
2012-06-12 18:34:09 +00:00
|
|
|
-- be scanned.
|
2012-09-20 21:24:40 +00:00
|
|
|
watchevents = Create : addevents ++ delevents ++ modifyevents
|
2012-06-04 17:22:56 +00:00
|
|
|
addevents
|
2012-06-07 03:20:09 +00:00
|
|
|
| hashook addHook || hashook addSymlinkHook = [MoveIn, CloseWrite]
|
2012-06-04 17:22:56 +00:00
|
|
|
| otherwise = []
|
|
|
|
delevents
|
2012-06-07 03:20:09 +00:00
|
|
|
| hashook delHook || hashook delDirHook = [MoveOut, Delete]
|
2012-06-04 17:22:56 +00:00
|
|
|
| otherwise = []
|
2012-09-20 21:24:40 +00:00
|
|
|
modifyevents
|
|
|
|
| hashook modifyHook = [Modify]
|
|
|
|
| otherwise = []
|
2012-04-12 21:28:40 +00:00
|
|
|
|
2012-06-12 18:34:09 +00:00
|
|
|
scan f = unless (ignored f) $ do
|
2012-06-13 05:20:37 +00:00
|
|
|
ms <- getstatus f
|
|
|
|
case ms of
|
2012-06-04 17:22:56 +00:00
|
|
|
Nothing -> return ()
|
|
|
|
Just s
|
2012-06-13 05:20:37 +00:00
|
|
|
| Files.isDirectory s ->
|
|
|
|
recurse $ indir f
|
|
|
|
| Files.isSymbolicLink s ->
|
|
|
|
runhook addSymlinkHook f ms
|
|
|
|
| Files.isRegularFile s ->
|
|
|
|
runhook addHook f ms
|
|
|
|
| otherwise ->
|
|
|
|
noop
|
2012-04-12 21:28:40 +00:00
|
|
|
|
2012-06-04 17:22:56 +00:00
|
|
|
-- Ignore creation events for regular files, which won't be
|
|
|
|
-- done being written when initially created, but handle for
|
|
|
|
-- directories and symlinks.
|
2012-06-04 21:17:05 +00:00
|
|
|
go (Created { isDirectory = isd, filePath = f })
|
|
|
|
| isd = recurse $ indir f
|
2012-06-07 03:20:09 +00:00
|
|
|
| hashook addSymlinkHook =
|
2012-06-13 05:20:37 +00:00
|
|
|
checkfiletype Files.isSymbolicLink addSymlinkHook f
|
2012-06-04 17:22:56 +00:00
|
|
|
| otherwise = noop
|
|
|
|
-- Closing a file is assumed to mean it's done being written.
|
2012-06-04 18:31:06 +00:00
|
|
|
go (Closed { isDirectory = False, maybeFilePath = Just f }) =
|
2012-06-13 05:20:37 +00:00
|
|
|
checkfiletype Files.isRegularFile addHook f
|
2012-06-12 18:34:09 +00:00
|
|
|
-- When a file or directory is moved in, scan it to add new
|
2012-06-04 17:22:56 +00:00
|
|
|
-- stuff.
|
2012-06-12 18:34:09 +00:00
|
|
|
go (MovedIn { filePath = f }) = scan f
|
2012-06-04 21:17:05 +00:00
|
|
|
go (MovedOut { isDirectory = isd, filePath = f })
|
2012-06-13 05:20:37 +00:00
|
|
|
| isd = runhook delDirHook f Nothing
|
|
|
|
| otherwise = runhook delHook f Nothing
|
2012-06-04 21:17:05 +00:00
|
|
|
-- Verify that the deleted item really doesn't exist,
|
|
|
|
-- since there can be spurious deletion events for items
|
|
|
|
-- in a directory that has been moved out, but is still
|
|
|
|
-- being watched.
|
|
|
|
go (Deleted { isDirectory = isd, filePath = f })
|
2012-06-13 05:20:37 +00:00
|
|
|
| isd = guarded $ runhook delDirHook f Nothing
|
|
|
|
| otherwise = guarded $ runhook delHook f Nothing
|
2012-06-04 21:17:05 +00:00
|
|
|
where
|
|
|
|
guarded = unlessM (filetype (const True) f)
|
2012-09-20 21:24:40 +00:00
|
|
|
go (Modified { isDirectory = isd, maybeFilePath = Just f })
|
|
|
|
| isd = noop
|
|
|
|
| otherwise = runhook modifyHook f Nothing
|
2012-04-22 03:32:33 +00:00
|
|
|
go _ = noop
|
2012-06-04 17:22:56 +00:00
|
|
|
|
2012-06-07 03:20:09 +00:00
|
|
|
hashook h = isJust $ h hooks
|
|
|
|
|
2012-06-13 05:20:37 +00:00
|
|
|
runhook h f s
|
2012-06-07 03:20:09 +00:00
|
|
|
| ignored f = noop
|
2012-06-13 05:20:37 +00:00
|
|
|
| otherwise = maybe noop (\a -> a (indir f) s) (h hooks)
|
2012-04-12 00:28:01 +00:00
|
|
|
|
2012-06-04 17:22:56 +00:00
|
|
|
indir f = dir </> f
|
2012-06-04 18:46:09 +00:00
|
|
|
|
2012-06-13 05:20:37 +00:00
|
|
|
getstatus f = catchMaybeIO $ getSymbolicLinkStatus $ indir f
|
|
|
|
checkfiletype check h f = do
|
|
|
|
ms <- getstatus f
|
|
|
|
case ms of
|
|
|
|
Just s
|
|
|
|
| check s -> runhook h f ms
|
|
|
|
_ -> noop
|
2012-06-04 18:31:06 +00:00
|
|
|
filetype t f = catchBoolIO $ t <$> getSymbolicLinkStatus (indir f)
|
2012-06-06 20:50:28 +00:00
|
|
|
|
|
|
|
-- Inotify fails when there are too many watches with a
|
|
|
|
-- disk full error.
|
|
|
|
failedaddwatch e
|
|
|
|
| isFullError e =
|
2012-06-07 03:20:09 +00:00
|
|
|
case errHook hooks of
|
2012-06-06 20:50:28 +00:00
|
|
|
Nothing -> throw e
|
2012-06-07 03:20:09 +00:00
|
|
|
Just hook -> tooManyWatches hook dir
|
2012-06-06 20:50:28 +00:00
|
|
|
| otherwise = throw e
|
2012-06-07 03:20:09 +00:00
|
|
|
|
2012-06-13 05:20:37 +00:00
|
|
|
tooManyWatches :: (String -> Maybe FileStatus -> IO ()) -> FilePath -> IO ()
|
2012-06-07 03:20:09 +00:00
|
|
|
tooManyWatches hook dir = do
|
|
|
|
sysctlval <- querySysctl [Param maxwatches] :: IO (Maybe Integer)
|
2012-06-13 05:20:37 +00:00
|
|
|
hook (unlines $ basewarning : maybe withoutsysctl withsysctl sysctlval) Nothing
|
2012-06-07 03:20:09 +00:00
|
|
|
where
|
|
|
|
maxwatches = "fs.inotify.max_user_watches"
|
|
|
|
basewarning = "Too many directories to watch! (Not watching " ++ dir ++")"
|
|
|
|
withoutsysctl = ["Increase the value in /proc/sys/fs/inotify/max_user_watches"]
|
|
|
|
withsysctl n = let new = n * 10 in
|
2012-06-07 04:47:09 +00:00
|
|
|
[ "Increase the limit permanently by running:"
|
2012-06-07 03:20:09 +00:00
|
|
|
, " echo " ++ maxwatches ++ "=" ++ show new ++
|
|
|
|
" | sudo tee -a /etc/sysctl.conf; sudo sysctl -p"
|
2012-06-07 04:47:09 +00:00
|
|
|
, "Or temporarily by running:"
|
|
|
|
, " sudo sysctl -w " ++ maxwatches ++ "=" ++ show new
|
2012-06-07 03:20:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
querySysctl :: Read a => [CommandParam] -> IO (Maybe a)
|
|
|
|
querySysctl ps = do
|
2012-07-19 04:57:40 +00:00
|
|
|
v <- catchMaybeIO $ readProcess "sysctl" (toCommand ps)
|
2012-06-07 03:20:09 +00:00
|
|
|
case v of
|
|
|
|
Nothing -> return Nothing
|
2012-07-18 19:30:26 +00:00
|
|
|
Just s -> return $ parsesysctl s
|
2012-06-07 03:20:09 +00:00
|
|
|
where
|
|
|
|
parsesysctl s = readish =<< lastMaybe (words s)
|