@@ -1,3 +1,5 @@
1+{-# LANGUAGE CPP #-}
2+
13--------------------------------------------------------------------------------
24module Hakyll.Preview.Poll
35 ( watchUpdates
@@ -9,10 +11,16 @@ import Control.Concurrent.MVar (newMVar, putMVar, takeMVar)
911import Control.Monad (when)
1012import Filesystem.Path.CurrentOS (decodeString, encodeString)
1113import System.Directory (canonicalizePath)
12−import System.FilePath (pathSeparators)
14+import System.FilePath (pathSeparators, (</>))
1315import System.FSNotify (Event (..), WatchConfig (..),
1416 startManagerConf, watchTree)
1517
18+#ifdef mingw32_HOST_OS
19+import System.IO (IOMode(ReadMode), Handle, openFile, hClose)
20+import System.IO.Error (isPermissionError)
21+import Control.Concurrent (threadDelay)
22+import Control.Exception (IOException, throw, try)
23+#endif
1624
1725--------------------------------------------------------------------------------
1826import Hakyll.Core.Configuration
@@ -45,9 +53,30 @@ watchUpdates conf update = do
4553 watchTree manager providerDir (not . isRemove) $ \event -> do
4654 () <- takeMVar lock
4755 allowed' <- allowed event
48− when allowed' $ update >> return ()
56+ when allowed' $ update' ((encodeString providerDir) </> (eventPath event))
4957 putMVar lock ()
58+ where
59+#ifndef mingw32_HOST_OS
60+ update' _ = update >> return ()
61+#else
62+ update' path = waitOpen path ReadMode (\_ -> update) >> return ()
5063
64+ -- continuously attempts to open the file in between sleep intervals
65+ -- handler is run only once it is able to open the file
66+ waitOpen :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
67+ waitOpen path mode handler = do
68+ res <- try $ openFile path mode :: IO (Either IOException Handle)
69+ case res of
70+ Left ex -> if isPermissionError ex
71+ then do
72+ threadDelay 100000
73+ waitOpen path mode handler
74+ else throw ex
75+ Right h -> do
76+ handled <- handler h
77+ hClose h
78+ return handled
79+#endif
5180
5281--------------------------------------------------------------------------------
5382eventPath :: Event -> FilePath