Created
February 15, 2011 02:01
-
-
Save kfish/826968 to your computer and use it in GitHub Desktop.
Haskell inotify example: monitor file modifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS -Wall #-} | |
module Main where | |
import Control.Concurrent (threadDelay) | |
import System.Environment (getArgs) | |
import System.INotify | |
main :: IO () | |
main = do | |
args <- getArgs | |
if (null args) | |
then usage | |
else watch args | |
usage :: IO () | |
usage = putStrLn "Usage: watch-read file ..." | |
watch :: [FilePath] -> IO () | |
watch fs = withINotify $ \inotify -> do | |
print fs | |
mapM_ (\f -> addWatch inotify [Modify, CloseWrite] f (handleEvent f)) fs | |
threadDelay (10 * microsecsPerSec) | |
where | |
handleEvent :: FilePath -> Event -> IO () | |
handleEvent f e = putStrLn (f ++ ": " ++ show e) | |
microsecsPerSec = 1000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment