{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Distribution.Client.FileMonitor
(
module Distribution.Simple.FileMonitor.Types
, FileMonitor (..)
, newFileMonitor
, MonitorChanged (..)
, MonitorChangedReason (..)
, checkFileMonitorChanged
, updateFileMonitor
, MonitorTimestamp
, beginUpdateFileMonitor
, MonitorStateFileSet
, MonitorStateFile
, MonitorStateGlob
) where
import Distribution.Client.Compat.Prelude
import qualified Distribution.Compat.Binary as Binary
import Prelude ()
import Data.Binary.Get (runGetOrFail)
import qualified Data.ByteString.Lazy as BS
import qualified Data.Map.Strict as Map
import Control.Exception
import Control.Monad
import Control.Monad.Except
( ExceptT
, runExceptT
, throwError
, withExceptT
)
import Control.Monad.State (StateT, mapStateT)
import qualified Control.Monad.State as State
import Control.Monad.Trans (MonadIO, liftIO)
import Distribution.Client.Glob
import Distribution.Client.HashValue
import Distribution.Client.Utils (MergeResult (..), mergeBy)
import Distribution.Compat.Time
import Distribution.Simple.FileMonitor.Types
import Distribution.Simple.Utils (handleDoesNotExist, writeFileAtomic)
import Distribution.Utils.Structured (Tag (..), structuredEncode)
import System.Directory
import System.FilePath
import System.IO
data MonitorStateFileSet
= MonitorStateFileSet
![MonitorStateFile]
![MonitorStateGlob]
deriving (Int -> MonitorStateFileSet -> ShowS
[MonitorStateFileSet] -> ShowS
MonitorStateFileSet -> [Char]
(Int -> MonitorStateFileSet -> ShowS)
-> (MonitorStateFileSet -> [Char])
-> ([MonitorStateFileSet] -> ShowS)
-> Show MonitorStateFileSet
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MonitorStateFileSet -> ShowS
showsPrec :: Int -> MonitorStateFileSet -> ShowS
$cshow :: MonitorStateFileSet -> [Char]
show :: MonitorStateFileSet -> [Char]
$cshowList :: [MonitorStateFileSet] -> ShowS
showList :: [MonitorStateFileSet] -> ShowS
Show, (forall x. MonitorStateFileSet -> Rep MonitorStateFileSet x)
-> (forall x. Rep MonitorStateFileSet x -> MonitorStateFileSet)
-> Generic MonitorStateFileSet
forall x. Rep MonitorStateFileSet x -> MonitorStateFileSet
forall x. MonitorStateFileSet -> Rep MonitorStateFileSet x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MonitorStateFileSet -> Rep MonitorStateFileSet x
from :: forall x. MonitorStateFileSet -> Rep MonitorStateFileSet x
$cto :: forall x. Rep MonitorStateFileSet x -> MonitorStateFileSet
to :: forall x. Rep MonitorStateFileSet x -> MonitorStateFileSet
Generic)
instance Binary MonitorStateFileSet
instance Structured MonitorStateFileSet
data MonitorStateFile
= MonitorStateFile
!MonitorKindFile
!MonitorKindDir
!FilePath
!MonitorStateFileStatus
deriving (Int -> MonitorStateFile -> ShowS
[MonitorStateFile] -> ShowS
MonitorStateFile -> [Char]
(Int -> MonitorStateFile -> ShowS)
-> (MonitorStateFile -> [Char])
-> ([MonitorStateFile] -> ShowS)
-> Show MonitorStateFile
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MonitorStateFile -> ShowS
showsPrec :: Int -> MonitorStateFile -> ShowS
$cshow :: MonitorStateFile -> [Char]
show :: MonitorStateFile -> [Char]
$cshowList :: [MonitorStateFile] -> ShowS
showList :: [MonitorStateFile] -> ShowS
Show, (forall x. MonitorStateFile -> Rep MonitorStateFile x)
-> (forall x. Rep MonitorStateFile x -> MonitorStateFile)
-> Generic MonitorStateFile
forall x. Rep MonitorStateFile x -> MonitorStateFile
forall x. MonitorStateFile -> Rep MonitorStateFile x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MonitorStateFile -> Rep MonitorStateFile x
from :: forall x. MonitorStateFile -> Rep MonitorStateFile x
$cto :: forall x. Rep MonitorStateFile x -> MonitorStateFile
to :: forall x. Rep MonitorStateFile x -> MonitorStateFile
Generic)
data MonitorStateFileStatus
= MonitorStateFileExists
|
MonitorStateFileModTime !ModTime
|
MonitorStateFileHashed !ModTime !HashValue
| MonitorStateDirExists
|
MonitorStateDirModTime !ModTime
| MonitorStateNonExistent
| MonitorStateAlreadyChanged
deriving (Int -> MonitorStateFileStatus -> ShowS
[MonitorStateFileStatus] -> ShowS
MonitorStateFileStatus -> [Char]
(Int -> MonitorStateFileStatus -> ShowS)
-> (MonitorStateFileStatus -> [Char])
-> ([MonitorStateFileStatus] -> ShowS)
-> Show MonitorStateFileStatus
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MonitorStateFileStatus -> ShowS
showsPrec :: Int -> MonitorStateFileStatus -> ShowS
$cshow :: MonitorStateFileStatus -> [Char]
show :: MonitorStateFileStatus -> [Char]
$cshowList :: [MonitorStateFileStatus] -> ShowS
showList :: [MonitorStateFileStatus] -> ShowS
Show, (forall x. MonitorStateFileStatus -> Rep MonitorStateFileStatus x)
-> (forall x.
Rep MonitorStateFileStatus x -> MonitorStateFileStatus)
-> Generic MonitorStateFileStatus
forall x. Rep MonitorStateFileStatus x -> MonitorStateFileStatus
forall x. MonitorStateFileStatus -> Rep MonitorStateFileStatus x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MonitorStateFileStatus -> Rep MonitorStateFileStatus x
from :: forall x. MonitorStateFileStatus -> Rep MonitorStateFileStatus x
$cto :: forall x. Rep MonitorStateFileStatus x -> MonitorStateFileStatus
to :: forall x. Rep MonitorStateFileStatus x -> MonitorStateFileStatus
Generic)
instance Binary MonitorStateFile
instance Binary MonitorStateFileStatus
instance Structured MonitorStateFile
instance Structured MonitorStateFileStatus
data MonitorStateGlob
= MonitorStateGlob
!MonitorKindFile
!MonitorKindDir
!FilePathRoot
!MonitorStateGlobRel
deriving (Int -> MonitorStateGlob -> ShowS
[MonitorStateGlob] -> ShowS
MonitorStateGlob -> [Char]
(Int -> MonitorStateGlob -> ShowS)
-> (MonitorStateGlob -> [Char])
-> ([MonitorStateGlob] -> ShowS)
-> Show MonitorStateGlob
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MonitorStateGlob -> ShowS
showsPrec :: Int -> MonitorStateGlob -> ShowS
$cshow :: MonitorStateGlob -> [Char]
show :: MonitorStateGlob -> [Char]
$cshowList :: [MonitorStateGlob] -> ShowS
showList :: [MonitorStateGlob] -> ShowS
Show, (forall x. MonitorStateGlob -> Rep MonitorStateGlob x)
-> (forall x. Rep MonitorStateGlob x -> MonitorStateGlob)
-> Generic MonitorStateGlob
forall x. Rep MonitorStateGlob x -> MonitorStateGlob
forall x. MonitorStateGlob -> Rep MonitorStateGlob x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MonitorStateGlob -> Rep MonitorStateGlob x
from :: forall x. MonitorStateGlob -> Rep MonitorStateGlob x
$cto :: forall x. Rep MonitorStateGlob x -> MonitorStateGlob
to :: forall x. Rep MonitorStateGlob x -> MonitorStateGlob
Generic)
data MonitorStateGlobRel
= MonitorStateGlobDirs
!GlobPieces
!Glob
!ModTime
![(FilePath, MonitorStateGlobRel)]
| MonitorStateGlobFiles
!GlobPieces
!ModTime
![(FilePath, MonitorStateFileStatus)]
| MonitorStateGlobDirTrailing
deriving (Int -> MonitorStateGlobRel -> ShowS
[MonitorStateGlobRel] -> ShowS
MonitorStateGlobRel -> [Char]
(Int -> MonitorStateGlobRel -> ShowS)
-> (MonitorStateGlobRel -> [Char])
-> ([MonitorStateGlobRel] -> ShowS)
-> Show MonitorStateGlobRel
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MonitorStateGlobRel -> ShowS
showsPrec :: Int -> MonitorStateGlobRel -> ShowS
$cshow :: MonitorStateGlobRel -> [Char]
show :: MonitorStateGlobRel -> [Char]
$cshowList :: [MonitorStateGlobRel] -> ShowS
showList :: [MonitorStateGlobRel] -> ShowS
Show, (forall x. MonitorStateGlobRel -> Rep MonitorStateGlobRel x)
-> (forall x. Rep MonitorStateGlobRel x -> MonitorStateGlobRel)
-> Generic MonitorStateGlobRel
forall x. Rep MonitorStateGlobRel x -> MonitorStateGlobRel
forall x. MonitorStateGlobRel -> Rep MonitorStateGlobRel x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MonitorStateGlobRel -> Rep MonitorStateGlobRel x
from :: forall x. MonitorStateGlobRel -> Rep MonitorStateGlobRel x
$cto :: forall x. Rep MonitorStateGlobRel x -> MonitorStateGlobRel
to :: forall x. Rep MonitorStateGlobRel x -> MonitorStateGlobRel
Generic)
instance Binary MonitorStateGlob
instance Binary MonitorStateGlobRel
instance Structured MonitorStateGlob
instance Structured MonitorStateGlobRel
reconstructMonitorFilePaths :: MonitorStateFileSet -> [MonitorFilePath]
reconstructMonitorFilePaths :: MonitorStateFileSet -> [MonitorFilePath]
reconstructMonitorFilePaths (MonitorStateFileSet [MonitorStateFile]
singlePaths [MonitorStateGlob]
globPaths) =
(MonitorStateFile -> MonitorFilePath)
-> [MonitorStateFile] -> [MonitorFilePath]
forall a b. (a -> b) -> [a] -> [b]
map MonitorStateFile -> MonitorFilePath
getSinglePath [MonitorStateFile]
singlePaths [MonitorFilePath] -> [MonitorFilePath] -> [MonitorFilePath]
forall a. [a] -> [a] -> [a]
++ (MonitorStateGlob -> MonitorFilePath)
-> [MonitorStateGlob] -> [MonitorFilePath]
forall a b. (a -> b) -> [a] -> [b]
map MonitorStateGlob -> MonitorFilePath
getGlobPath [MonitorStateGlob]
globPaths
where
getSinglePath :: MonitorStateFile -> MonitorFilePath
getSinglePath :: MonitorStateFile -> MonitorFilePath
getSinglePath (MonitorStateFile MonitorKindFile
kindfile MonitorKindDir
kinddir [Char]
filepath MonitorStateFileStatus
_) =
MonitorKindFile -> MonitorKindDir -> [Char] -> MonitorFilePath
MonitorFile MonitorKindFile
kindfile MonitorKindDir
kinddir [Char]
filepath
getGlobPath :: MonitorStateGlob -> MonitorFilePath
getGlobPath :: MonitorStateGlob -> MonitorFilePath
getGlobPath (MonitorStateGlob MonitorKindFile
kindfile MonitorKindDir
kinddir FilePathRoot
root MonitorStateGlobRel
gstate) =
MonitorKindFile -> MonitorKindDir -> RootedGlob -> MonitorFilePath
MonitorFileGlob MonitorKindFile
kindfile MonitorKindDir
kinddir (RootedGlob -> MonitorFilePath) -> RootedGlob -> MonitorFilePath
forall a b. (a -> b) -> a -> b
$
FilePathRoot -> Glob -> RootedGlob
RootedGlob FilePathRoot
root (Glob -> RootedGlob) -> Glob -> RootedGlob
forall a b. (a -> b) -> a -> b
$
case MonitorStateGlobRel
gstate of
MonitorStateGlobDirs [GlobPiece]
glob Glob
globs ModTime
_ [([Char], MonitorStateGlobRel)]
_ -> [GlobPiece] -> Glob -> Glob
GlobDir [GlobPiece]
glob Glob
globs
MonitorStateGlobFiles [GlobPiece]
glob ModTime
_ [([Char], MonitorStateFileStatus)]
_ -> [GlobPiece] -> Glob
GlobFile [GlobPiece]
glob
MonitorStateGlobRel
MonitorStateGlobDirTrailing -> Glob
GlobDirTrailing
data FileMonitor a b = FileMonitor
{ forall a b. FileMonitor a b -> [Char]
fileMonitorCacheFile :: FilePath
, forall a b. FileMonitor a b -> a -> a -> Bool
fileMonitorKeyValid :: a -> a -> Bool
, forall a b. FileMonitor a b -> Bool
fileMonitorCheckIfOnlyValueChanged :: Bool
}
newFileMonitor
:: Eq a
=> FilePath
-> FileMonitor a b
newFileMonitor :: forall a b. Eq a => [Char] -> FileMonitor a b
newFileMonitor [Char]
path = [Char] -> (a -> a -> Bool) -> Bool -> FileMonitor a b
forall a b. [Char] -> (a -> a -> Bool) -> Bool -> FileMonitor a b
FileMonitor [Char]
path a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) Bool
False
data MonitorChanged a b
=
MonitorUnchanged b [MonitorFilePath]
|
MonitorChanged (MonitorChangedReason a)
deriving (Int -> MonitorChanged a b -> ShowS
[MonitorChanged a b] -> ShowS
MonitorChanged a b -> [Char]
(Int -> MonitorChanged a b -> ShowS)
-> (MonitorChanged a b -> [Char])
-> ([MonitorChanged a b] -> ShowS)
-> Show (MonitorChanged a b)
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
forall a b. (Show b, Show a) => Int -> MonitorChanged a b -> ShowS
forall a b. (Show b, Show a) => [MonitorChanged a b] -> ShowS
forall a b. (Show b, Show a) => MonitorChanged a b -> [Char]
$cshowsPrec :: forall a b. (Show b, Show a) => Int -> MonitorChanged a b -> ShowS
showsPrec :: Int -> MonitorChanged a b -> ShowS
$cshow :: forall a b. (Show b, Show a) => MonitorChanged a b -> [Char]
show :: MonitorChanged a b -> [Char]
$cshowList :: forall a b. (Show b, Show a) => [MonitorChanged a b] -> ShowS
showList :: [MonitorChanged a b] -> ShowS
Show)
data MonitorChangedReason a
=
MonitoredFileChanged FilePath
|
MonitoredValueChanged a
|
MonitorFirstRun
|
MonitorCorruptCache
deriving (MonitorChangedReason a -> MonitorChangedReason a -> Bool
(MonitorChangedReason a -> MonitorChangedReason a -> Bool)
-> (MonitorChangedReason a -> MonitorChangedReason a -> Bool)
-> Eq (MonitorChangedReason a)
forall a.
Eq a =>
MonitorChangedReason a -> MonitorChangedReason a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a.
Eq a =>
MonitorChangedReason a -> MonitorChangedReason a -> Bool
== :: MonitorChangedReason a -> MonitorChangedReason a -> Bool
$c/= :: forall a.
Eq a =>
MonitorChangedReason a -> MonitorChangedReason a -> Bool
/= :: MonitorChangedReason a -> MonitorChangedReason a -> Bool
Eq, Int -> MonitorChangedReason a -> ShowS
[MonitorChangedReason a] -> ShowS
MonitorChangedReason a -> [Char]
(Int -> MonitorChangedReason a -> ShowS)
-> (MonitorChangedReason a -> [Char])
-> ([MonitorChangedReason a] -> ShowS)
-> Show (MonitorChangedReason a)
forall a. Show a => Int -> MonitorChangedReason a -> ShowS
forall a. Show a => [MonitorChangedReason a] -> ShowS
forall a. Show a => MonitorChangedReason a -> [Char]
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> MonitorChangedReason a -> ShowS
showsPrec :: Int -> MonitorChangedReason a -> ShowS
$cshow :: forall a. Show a => MonitorChangedReason a -> [Char]
show :: MonitorChangedReason a -> [Char]
$cshowList :: forall a. Show a => [MonitorChangedReason a] -> ShowS
showList :: [MonitorChangedReason a] -> ShowS
Show, (forall a b.
(a -> b) -> MonitorChangedReason a -> MonitorChangedReason b)
-> (forall a b.
a -> MonitorChangedReason b -> MonitorChangedReason a)
-> Functor MonitorChangedReason
forall a b. a -> MonitorChangedReason b -> MonitorChangedReason a
forall a b.
(a -> b) -> MonitorChangedReason a -> MonitorChangedReason b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b.
(a -> b) -> MonitorChangedReason a -> MonitorChangedReason b
fmap :: forall a b.
(a -> b) -> MonitorChangedReason a -> MonitorChangedReason b
$c<$ :: forall a b. a -> MonitorChangedReason b -> MonitorChangedReason a
<$ :: forall a b. a -> MonitorChangedReason b -> MonitorChangedReason a
Functor)
checkFileMonitorChanged
:: forall a b
. (Binary a, Structured a, Binary b, Structured b)
=> FileMonitor a b
-> FilePath
-> a
-> IO (MonitorChanged a b)
checkFileMonitorChanged :: forall a b.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b -> [Char] -> a -> IO (MonitorChanged a b)
checkFileMonitorChanged
monitor :: FileMonitor a b
monitor@FileMonitor
{ a -> a -> Bool
fileMonitorKeyValid :: forall a b. FileMonitor a b -> a -> a -> Bool
fileMonitorKeyValid :: a -> a -> Bool
fileMonitorKeyValid
, Bool
fileMonitorCheckIfOnlyValueChanged :: forall a b. FileMonitor a b -> Bool
fileMonitorCheckIfOnlyValueChanged :: Bool
fileMonitorCheckIfOnlyValueChanged
}
[Char]
root
a
currentKey =
MonitorChanged a b
-> IO (MonitorChanged a b) -> IO (MonitorChanged a b)
forall a. a -> IO a -> IO a
handleDoesNotExist (MonitorChangedReason a -> MonitorChanged a b
forall a b. MonitorChangedReason a -> MonitorChanged a b
MonitorChanged MonitorChangedReason a
forall a. MonitorChangedReason a
MonitorFirstRun) (IO (MonitorChanged a b) -> IO (MonitorChanged a b))
-> IO (MonitorChanged a b) -> IO (MonitorChanged a b)
forall a b. (a -> b) -> a -> b
$
MonitorChanged a b
-> IO (MonitorChanged a b) -> IO (MonitorChanged a b)
forall a. a -> IO a -> IO a
handleErrorCall (MonitorChangedReason a -> MonitorChanged a b
forall a b. MonitorChangedReason a -> MonitorChanged a b
MonitorChanged MonitorChangedReason a
forall a. MonitorChangedReason a
MonitorCorruptCache) (IO (MonitorChanged a b) -> IO (MonitorChanged a b))
-> IO (MonitorChanged a b) -> IO (MonitorChanged a b)
forall a b. (a -> b) -> a -> b
$
FileMonitor a b
-> (Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO (MonitorChanged a b))
-> IO (MonitorChanged a b)
forall a b r.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b
-> (Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO r)
-> IO r
withCacheFile FileMonitor a b
monitor ((Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO (MonitorChanged a b))
-> IO (MonitorChanged a b))
-> (Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO (MonitorChanged a b))
-> IO (MonitorChanged a b)
forall a b. (a -> b) -> a -> b
$
([Char] -> IO (MonitorChanged a b))
-> ((MonitorStateFileSet, a, Either [Char] b)
-> IO (MonitorChanged a b))
-> Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO (MonitorChanged a b)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
(\[Char]
_ -> MonitorChanged a b -> IO (MonitorChanged a b)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MonitorChangedReason a -> MonitorChanged a b
forall a b. MonitorChangedReason a -> MonitorChanged a b
MonitorChanged MonitorChangedReason a
forall a. MonitorChangedReason a
MonitorCorruptCache))
(MonitorStateFileSet, a, Either [Char] b)
-> IO (MonitorChanged a b)
checkStatusCache
where
checkStatusCache :: (MonitorStateFileSet, a, Either String b) -> IO (MonitorChanged a b)
checkStatusCache :: (MonitorStateFileSet, a, Either [Char] b)
-> IO (MonitorChanged a b)
checkStatusCache (MonitorStateFileSet
cachedFileStatus, a
cachedKey, Either [Char] b
cachedResult) = do
change <- IO (Maybe (MonitorChangedReason a))
checkForChanges
case change of
Just MonitorChangedReason a
reason -> MonitorChanged a b -> IO (MonitorChanged a b)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MonitorChangedReason a -> MonitorChanged a b
forall a b. MonitorChangedReason a -> MonitorChanged a b
MonitorChanged MonitorChangedReason a
reason)
Maybe (MonitorChangedReason a)
Nothing -> case Either [Char] b
cachedResult of
Left [Char]
_ -> MonitorChanged a b -> IO (MonitorChanged a b)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (MonitorChangedReason a -> MonitorChanged a b
forall a b. MonitorChangedReason a -> MonitorChanged a b
MonitorChanged MonitorChangedReason a
forall a. MonitorChangedReason a
MonitorCorruptCache)
Right b
cr -> MonitorChanged a b -> IO (MonitorChanged a b)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> [MonitorFilePath] -> MonitorChanged a b
forall a b. b -> [MonitorFilePath] -> MonitorChanged a b
MonitorUnchanged b
cr [MonitorFilePath]
monitorFiles)
where
monitorFiles :: [MonitorFilePath]
monitorFiles = MonitorStateFileSet -> [MonitorFilePath]
reconstructMonitorFilePaths MonitorStateFileSet
cachedFileStatus
where
checkForChanges :: IO (Maybe (MonitorChangedReason a))
checkForChanges :: IO (Maybe (MonitorChangedReason a))
checkForChanges
| Bool
fileMonitorCheckIfOnlyValueChanged =
MonitorStateFileSet
-> a -> Either [Char] b -> IO (Maybe (MonitorChangedReason a))
checkFileChange MonitorStateFileSet
cachedFileStatus a
cachedKey Either [Char] b
cachedResult
IO (Maybe (MonitorChangedReason a))
-> IO (Maybe (MonitorChangedReason a))
-> IO (Maybe (MonitorChangedReason a))
forall (m :: * -> *) a1.
Monad m =>
m (Maybe a1) -> m (Maybe a1) -> m (Maybe a1)
`mplusMaybeT` a -> IO (Maybe (MonitorChangedReason a))
checkValueChange a
cachedKey
| Bool
otherwise =
a -> IO (Maybe (MonitorChangedReason a))
checkValueChange a
cachedKey
IO (Maybe (MonitorChangedReason a))
-> IO (Maybe (MonitorChangedReason a))
-> IO (Maybe (MonitorChangedReason a))
forall (m :: * -> *) a1.
Monad m =>
m (Maybe a1) -> m (Maybe a1) -> m (Maybe a1)
`mplusMaybeT` MonitorStateFileSet
-> a -> Either [Char] b -> IO (Maybe (MonitorChangedReason a))
checkFileChange MonitorStateFileSet
cachedFileStatus a
cachedKey Either [Char] b
cachedResult
mplusMaybeT :: Monad m => m (Maybe a1) -> m (Maybe a1) -> m (Maybe a1)
mplusMaybeT :: forall (m :: * -> *) a1.
Monad m =>
m (Maybe a1) -> m (Maybe a1) -> m (Maybe a1)
mplusMaybeT m (Maybe a1)
ma m (Maybe a1)
mb = do
mx <- m (Maybe a1)
ma
case mx of
Maybe a1
Nothing -> m (Maybe a1)
mb
Just a1
x -> Maybe a1 -> m (Maybe a1)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a1 -> Maybe a1
forall a. a -> Maybe a
Just a1
x)
checkValueChange :: a -> IO (Maybe (MonitorChangedReason a))
checkValueChange :: a -> IO (Maybe (MonitorChangedReason a))
checkValueChange a
cachedKey
| Bool -> Bool
not (a -> a -> Bool
fileMonitorKeyValid a
currentKey a
cachedKey) =
Maybe (MonitorChangedReason a)
-> IO (Maybe (MonitorChangedReason a))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MonitorChangedReason a -> Maybe (MonitorChangedReason a)
forall a. a -> Maybe a
Just (a -> MonitorChangedReason a
forall a. a -> MonitorChangedReason a
MonitoredValueChanged a
cachedKey))
| Bool
otherwise =
Maybe (MonitorChangedReason a)
-> IO (Maybe (MonitorChangedReason a))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (MonitorChangedReason a)
forall a. Maybe a
Nothing
checkFileChange :: MonitorStateFileSet -> a -> Either String b -> IO (Maybe (MonitorChangedReason a))
checkFileChange :: MonitorStateFileSet
-> a -> Either [Char] b -> IO (Maybe (MonitorChangedReason a))
checkFileChange MonitorStateFileSet
cachedFileStatus a
cachedKey Either [Char] b
cachedResult = do
res <- [Char]
-> MonitorStateFileSet
-> IO (Either [Char] (MonitorStateFileSet, CacheChanged))
probeFileSystem [Char]
root MonitorStateFileSet
cachedFileStatus
case res of
Left [Char]
changedPath ->
Maybe (MonitorChangedReason a)
-> IO (Maybe (MonitorChangedReason a))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MonitorChangedReason a -> Maybe (MonitorChangedReason a)
forall a. a -> Maybe a
Just ([Char] -> MonitorChangedReason a
forall a. [Char] -> MonitorChangedReason a
MonitoredFileChanged (ShowS
normalise [Char]
changedPath)))
Right (MonitorStateFileSet
cachedFileStatus', CacheChanged
cacheStatus) -> do
CacheChanged -> IO () -> IO ()
forall (m :: * -> *). Monad m => CacheChanged -> m () -> m ()
whenCacheChanged CacheChanged
cacheStatus (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
case Either [Char] b
cachedResult of
Left [Char]
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Right b
cr -> FileMonitor a b -> MonitorStateFileSet -> a -> b -> IO ()
forall a b.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b -> MonitorStateFileSet -> a -> b -> IO ()
rewriteCacheFile FileMonitor a b
monitor MonitorStateFileSet
cachedFileStatus' a
cachedKey b
cr
Maybe (MonitorChangedReason a)
-> IO (Maybe (MonitorChangedReason a))
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (MonitorChangedReason a)
forall a. Maybe a
Nothing
structuredDecodeTriple
:: forall a b c
. (Structured a, Structured b, Structured c, Binary.Binary a, Binary.Binary b, Binary.Binary c)
=> BS.ByteString
-> Either String (a, b, Either String c)
structuredDecodeTriple :: forall a b c.
(Structured a, Structured b, Structured c, Binary a, Binary b,
Binary c) =>
ByteString -> Either [Char] (a, b, Either [Char] c)
structuredDecodeTriple ByteString
lbs =
let partialDecode :: Either (ByteString, Int64, [Char]) (ByteString, Int64, (a, b))
partialDecode =
(Get (a, b)
-> ByteString
-> Either (ByteString, Int64, [Char]) (ByteString, Int64, (a, b))
forall a.
Get a
-> ByteString
-> Either (ByteString, Int64, [Char]) (ByteString, Int64, a)
`runGetOrFail` ByteString
lbs) (Get (a, b)
-> Either (ByteString, Int64, [Char]) (ByteString, Int64, (a, b)))
-> Get (a, b)
-> Either (ByteString, Int64, [Char]) (ByteString, Int64, (a, b))
forall a b. (a -> b) -> a -> b
$ do
(_ :: Tag (a, b, c)) <- Get (Tag (a, b, c))
forall t. Binary t => Get t
Binary.get
(a :: a) <- Binary.get
(b :: b) <- Binary.get
pure (a, b)
cleanEither :: Either (a, a, [Char]) (a, b, b) -> Either [Char] b
cleanEither (Left (a
_, a
pos, [Char]
msg)) = [Char] -> Either [Char] b
forall a b. a -> Either a b
Left ([Char]
"Data.Binary.Get.runGet at position " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
pos [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
msg)
cleanEither (Right (a
_, b
_, b
v)) = b -> Either [Char] b
forall a b. b -> Either a b
Right b
v
in case Either (ByteString, Int64, [Char]) (ByteString, Int64, (a, b))
partialDecode of
Left (ByteString
_, Int64
pos, [Char]
msg) -> [Char] -> Either [Char] (a, b, Either [Char] c)
forall a b. a -> Either a b
Left ([Char]
"Data.Binary.Get.runGet at position " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int64 -> [Char]
forall a. Show a => a -> [Char]
show Int64
pos [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
msg)
Right (ByteString
lbs', Int64
_, (a
x, b
y)) -> (a, b, Either [Char] c) -> Either [Char] (a, b, Either [Char] c)
forall a b. b -> Either a b
Right (a
x, b
y, Either (ByteString, Int64, [Char]) (ByteString, Int64, c)
-> Either [Char] c
forall {a} {a} {a} {b} {b}.
Show a =>
Either (a, a, [Char]) (a, b, b) -> Either [Char] b
cleanEither (Either (ByteString, Int64, [Char]) (ByteString, Int64, c)
-> Either [Char] c)
-> Either (ByteString, Int64, [Char]) (ByteString, Int64, c)
-> Either [Char] c
forall a b. (a -> b) -> a -> b
$ Get c
-> ByteString
-> Either (ByteString, Int64, [Char]) (ByteString, Int64, c)
forall a.
Get a
-> ByteString
-> Either (ByteString, Int64, [Char]) (ByteString, Int64, a)
runGetOrFail (Get c
forall t. Binary t => Get t
Binary.get :: Binary.Get c) ByteString
lbs')
withCacheFile
:: (Binary a, Structured a, Binary b, Structured b)
=> FileMonitor a b
-> (Either String (MonitorStateFileSet, a, Either String b) -> IO r)
-> IO r
withCacheFile :: forall a b r.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b
-> (Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO r)
-> IO r
withCacheFile (FileMonitor{[Char]
fileMonitorCacheFile :: forall a b. FileMonitor a b -> [Char]
fileMonitorCacheFile :: [Char]
fileMonitorCacheFile}) Either [Char] (MonitorStateFileSet, a, Either [Char] b) -> IO r
k =
[Char] -> IOMode -> (Handle -> IO r) -> IO r
forall r. [Char] -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile [Char]
fileMonitorCacheFile IOMode
ReadMode ((Handle -> IO r) -> IO r) -> (Handle -> IO r) -> IO r
forall a b. (a -> b) -> a -> b
$ \Handle
hnd -> do
contents <- ByteString
-> Either [Char] (MonitorStateFileSet, a, Either [Char] b)
forall a b c.
(Structured a, Structured b, Structured c, Binary a, Binary b,
Binary c) =>
ByteString -> Either [Char] (a, b, Either [Char] c)
structuredDecodeTriple (ByteString
-> Either [Char] (MonitorStateFileSet, a, Either [Char] b))
-> IO ByteString
-> IO (Either [Char] (MonitorStateFileSet, a, Either [Char] b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Handle -> IO ByteString
BS.hGetContents Handle
hnd
k contents
rewriteCacheFile
:: (Binary a, Structured a, Binary b, Structured b)
=> FileMonitor a b
-> MonitorStateFileSet
-> a
-> b
-> IO ()
rewriteCacheFile :: forall a b.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b -> MonitorStateFileSet -> a -> b -> IO ()
rewriteCacheFile FileMonitor{[Char]
fileMonitorCacheFile :: forall a b. FileMonitor a b -> [Char]
fileMonitorCacheFile :: [Char]
fileMonitorCacheFile} MonitorStateFileSet
fileset a
key b
result =
[Char] -> ByteString -> IO ()
writeFileAtomic [Char]
fileMonitorCacheFile (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$
(MonitorStateFileSet, a, b) -> ByteString
forall a. (Binary a, Structured a) => a -> ByteString
structuredEncode (MonitorStateFileSet
fileset, a
key, b
result)
probeFileSystem
:: FilePath
-> MonitorStateFileSet
-> IO (Either FilePath (MonitorStateFileSet, CacheChanged))
probeFileSystem :: [Char]
-> MonitorStateFileSet
-> IO (Either [Char] (MonitorStateFileSet, CacheChanged))
probeFileSystem [Char]
root (MonitorStateFileSet [MonitorStateFile]
singlePaths [MonitorStateGlob]
globPaths) =
ChangedM MonitorStateFileSet
-> IO (Either [Char] (MonitorStateFileSet, CacheChanged))
forall a. ChangedM a -> IO (Either [Char] (a, CacheChanged))
runChangedM (ChangedM MonitorStateFileSet
-> IO (Either [Char] (MonitorStateFileSet, CacheChanged)))
-> ChangedM MonitorStateFileSet
-> IO (Either [Char] (MonitorStateFileSet, CacheChanged))
forall a b. (a -> b) -> a -> b
$ do
[ChangedM ()] -> ChangedM ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
[ [Char] -> [Char] -> MonitorStateFileStatus -> ChangedM ()
probeMonitorStateFileStatus [Char]
root [Char]
file MonitorStateFileStatus
status
| MonitorStateFile MonitorKindFile
_ MonitorKindDir
_ [Char]
file MonitorStateFileStatus
status <- [MonitorStateFile]
singlePaths
]
globPaths' <-
[ChangedM MonitorStateGlob] -> ChangedM [MonitorStateGlob]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence
[ [Char] -> MonitorStateGlob -> ChangedM MonitorStateGlob
probeMonitorStateGlob [Char]
root MonitorStateGlob
globPath
| MonitorStateGlob
globPath <- [MonitorStateGlob]
globPaths
]
return (MonitorStateFileSet singlePaths globPaths')
newtype ChangedM a = ChangedM (StateT CacheChanged (ExceptT FilePath IO) a)
deriving ((forall a b. (a -> b) -> ChangedM a -> ChangedM b)
-> (forall a b. a -> ChangedM b -> ChangedM a) -> Functor ChangedM
forall a b. a -> ChangedM b -> ChangedM a
forall a b. (a -> b) -> ChangedM a -> ChangedM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ChangedM a -> ChangedM b
fmap :: forall a b. (a -> b) -> ChangedM a -> ChangedM b
$c<$ :: forall a b. a -> ChangedM b -> ChangedM a
<$ :: forall a b. a -> ChangedM b -> ChangedM a
Functor, Functor ChangedM
Functor ChangedM =>
(forall a. a -> ChangedM a)
-> (forall a b. ChangedM (a -> b) -> ChangedM a -> ChangedM b)
-> (forall a b c.
(a -> b -> c) -> ChangedM a -> ChangedM b -> ChangedM c)
-> (forall a b. ChangedM a -> ChangedM b -> ChangedM b)
-> (forall a b. ChangedM a -> ChangedM b -> ChangedM a)
-> Applicative ChangedM
forall a. a -> ChangedM a
forall a b. ChangedM a -> ChangedM b -> ChangedM a
forall a b. ChangedM a -> ChangedM b -> ChangedM b
forall a b. ChangedM (a -> b) -> ChangedM a -> ChangedM b
forall a b c.
(a -> b -> c) -> ChangedM a -> ChangedM b -> ChangedM c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
$cpure :: forall a. a -> ChangedM a
pure :: forall a. a -> ChangedM a
$c<*> :: forall a b. ChangedM (a -> b) -> ChangedM a -> ChangedM b
<*> :: forall a b. ChangedM (a -> b) -> ChangedM a -> ChangedM b
$cliftA2 :: forall a b c.
(a -> b -> c) -> ChangedM a -> ChangedM b -> ChangedM c
liftA2 :: forall a b c.
(a -> b -> c) -> ChangedM a -> ChangedM b -> ChangedM c
$c*> :: forall a b. ChangedM a -> ChangedM b -> ChangedM b
*> :: forall a b. ChangedM a -> ChangedM b -> ChangedM b
$c<* :: forall a b. ChangedM a -> ChangedM b -> ChangedM a
<* :: forall a b. ChangedM a -> ChangedM b -> ChangedM a
Applicative, Applicative ChangedM
Applicative ChangedM =>
(forall a b. ChangedM a -> (a -> ChangedM b) -> ChangedM b)
-> (forall a b. ChangedM a -> ChangedM b -> ChangedM b)
-> (forall a. a -> ChangedM a)
-> Monad ChangedM
forall a. a -> ChangedM a
forall a b. ChangedM a -> ChangedM b -> ChangedM b
forall a b. ChangedM a -> (a -> ChangedM b) -> ChangedM b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
$c>>= :: forall a b. ChangedM a -> (a -> ChangedM b) -> ChangedM b
>>= :: forall a b. ChangedM a -> (a -> ChangedM b) -> ChangedM b
$c>> :: forall a b. ChangedM a -> ChangedM b -> ChangedM b
>> :: forall a b. ChangedM a -> ChangedM b -> ChangedM b
$creturn :: forall a. a -> ChangedM a
return :: forall a. a -> ChangedM a
Monad, Monad ChangedM
Monad ChangedM =>
(forall a. IO a -> ChangedM a) -> MonadIO ChangedM
forall a. IO a -> ChangedM a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
$cliftIO :: forall a. IO a -> ChangedM a
liftIO :: forall a. IO a -> ChangedM a
MonadIO)
runChangedM :: ChangedM a -> IO (Either FilePath (a, CacheChanged))
runChangedM :: forall a. ChangedM a -> IO (Either [Char] (a, CacheChanged))
runChangedM (ChangedM StateT CacheChanged (ExceptT [Char] IO) a
action) =
ExceptT [Char] IO (a, CacheChanged)
-> IO (Either [Char] (a, CacheChanged))
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT [Char] IO (a, CacheChanged)
-> IO (Either [Char] (a, CacheChanged)))
-> ExceptT [Char] IO (a, CacheChanged)
-> IO (Either [Char] (a, CacheChanged))
forall a b. (a -> b) -> a -> b
$ StateT CacheChanged (ExceptT [Char] IO) a
-> CacheChanged -> ExceptT [Char] IO (a, CacheChanged)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
State.runStateT StateT CacheChanged (ExceptT [Char] IO) a
action CacheChanged
CacheUnchanged
somethingChanged :: FilePath -> ChangedM a
somethingChanged :: forall a. [Char] -> ChangedM a
somethingChanged [Char]
path = StateT CacheChanged (ExceptT [Char] IO) a -> ChangedM a
forall a. StateT CacheChanged (ExceptT [Char] IO) a -> ChangedM a
ChangedM (StateT CacheChanged (ExceptT [Char] IO) a -> ChangedM a)
-> StateT CacheChanged (ExceptT [Char] IO) a -> ChangedM a
forall a b. (a -> b) -> a -> b
$ [Char] -> StateT CacheChanged (ExceptT [Char] IO) a
forall a. [Char] -> StateT CacheChanged (ExceptT [Char] IO) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError [Char]
path
cacheChanged :: ChangedM ()
cacheChanged :: ChangedM ()
cacheChanged = StateT CacheChanged (ExceptT [Char] IO) () -> ChangedM ()
forall a. StateT CacheChanged (ExceptT [Char] IO) a -> ChangedM a
ChangedM (StateT CacheChanged (ExceptT [Char] IO) () -> ChangedM ())
-> StateT CacheChanged (ExceptT [Char] IO) () -> ChangedM ()
forall a b. (a -> b) -> a -> b
$ CacheChanged -> StateT CacheChanged (ExceptT [Char] IO) ()
forall s (m :: * -> *). MonadState s m => s -> m ()
State.put CacheChanged
CacheChanged
mapChangedFile :: (FilePath -> FilePath) -> ChangedM a -> ChangedM a
mapChangedFile :: forall a. ShowS -> ChangedM a -> ChangedM a
mapChangedFile ShowS
adjust (ChangedM StateT CacheChanged (ExceptT [Char] IO) a
a) =
StateT CacheChanged (ExceptT [Char] IO) a -> ChangedM a
forall a. StateT CacheChanged (ExceptT [Char] IO) a -> ChangedM a
ChangedM ((ExceptT [Char] IO (a, CacheChanged)
-> ExceptT [Char] IO (a, CacheChanged))
-> StateT CacheChanged (ExceptT [Char] IO) a
-> StateT CacheChanged (ExceptT [Char] IO) a
forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
mapStateT (ShowS
-> ExceptT [Char] IO (a, CacheChanged)
-> ExceptT [Char] IO (a, CacheChanged)
forall (m :: * -> *) e e' a.
Functor m =>
(e -> e') -> ExceptT e m a -> ExceptT e' m a
withExceptT ShowS
adjust) StateT CacheChanged (ExceptT [Char] IO) a
a)
data CacheChanged = CacheChanged | CacheUnchanged
whenCacheChanged :: Monad m => CacheChanged -> m () -> m ()
whenCacheChanged :: forall (m :: * -> *). Monad m => CacheChanged -> m () -> m ()
whenCacheChanged CacheChanged
CacheChanged m ()
action = m ()
action
whenCacheChanged CacheChanged
CacheUnchanged m ()
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
probeMonitorStateFileStatus
:: FilePath
-> FilePath
-> MonitorStateFileStatus
-> ChangedM ()
probeMonitorStateFileStatus :: [Char] -> [Char] -> MonitorStateFileStatus -> ChangedM ()
probeMonitorStateFileStatus [Char]
root [Char]
file MonitorStateFileStatus
status =
case MonitorStateFileStatus
status of
MonitorStateFileStatus
MonitorStateFileExists ->
[Char] -> [Char] -> ChangedM ()
probeFileExistence [Char]
root [Char]
file
MonitorStateFileModTime ModTime
mtime ->
[Char] -> [Char] -> ModTime -> ChangedM ()
probeFileModificationTime [Char]
root [Char]
file ModTime
mtime
MonitorStateFileHashed ModTime
mtime HashValue
hash ->
[Char] -> [Char] -> ModTime -> HashValue -> ChangedM ()
probeFileModificationTimeAndHash [Char]
root [Char]
file ModTime
mtime HashValue
hash
MonitorStateFileStatus
MonitorStateDirExists ->
[Char] -> [Char] -> ChangedM ()
probeDirExistence [Char]
root [Char]
file
MonitorStateDirModTime ModTime
mtime ->
[Char] -> [Char] -> ModTime -> ChangedM ()
probeFileModificationTime [Char]
root [Char]
file ModTime
mtime
MonitorStateFileStatus
MonitorStateNonExistent ->
[Char] -> [Char] -> ChangedM ()
probeFileNonExistence [Char]
root [Char]
file
MonitorStateFileStatus
MonitorStateAlreadyChanged ->
[Char] -> ChangedM ()
forall a. [Char] -> ChangedM a
somethingChanged [Char]
file
probeMonitorStateGlob
:: FilePath
-> MonitorStateGlob
-> ChangedM MonitorStateGlob
probeMonitorStateGlob :: [Char] -> MonitorStateGlob -> ChangedM MonitorStateGlob
probeMonitorStateGlob
[Char]
relroot
(MonitorStateGlob MonitorKindFile
kindfile MonitorKindDir
kinddir FilePathRoot
globroot MonitorStateGlobRel
glob) = do
root <- IO [Char] -> ChangedM [Char]
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Char] -> ChangedM [Char]) -> IO [Char] -> ChangedM [Char]
forall a b. (a -> b) -> a -> b
$ FilePathRoot -> [Char] -> IO [Char]
getFilePathRootDirectory FilePathRoot
globroot [Char]
relroot
case globroot of
FilePathRoot
FilePathRelative ->
MonitorKindFile
-> MonitorKindDir
-> FilePathRoot
-> MonitorStateGlobRel
-> MonitorStateGlob
MonitorStateGlob MonitorKindFile
kindfile MonitorKindDir
kinddir FilePathRoot
globroot
(MonitorStateGlobRel -> MonitorStateGlob)
-> ChangedM MonitorStateGlobRel -> ChangedM MonitorStateGlob
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> MonitorStateGlobRel
-> ChangedM MonitorStateGlobRel
probeMonitorStateGlobRel MonitorKindFile
kindfile MonitorKindDir
kinddir [Char]
root [Char]
"." MonitorStateGlobRel
glob
FilePathRoot
_ ->
ShowS -> ChangedM MonitorStateGlob -> ChangedM MonitorStateGlob
forall a. ShowS -> ChangedM a -> ChangedM a
mapChangedFile ([Char]
root [Char] -> ShowS
</>) (ChangedM MonitorStateGlob -> ChangedM MonitorStateGlob)
-> ChangedM MonitorStateGlob -> ChangedM MonitorStateGlob
forall a b. (a -> b) -> a -> b
$
MonitorKindFile
-> MonitorKindDir
-> FilePathRoot
-> MonitorStateGlobRel
-> MonitorStateGlob
MonitorStateGlob MonitorKindFile
kindfile MonitorKindDir
kinddir FilePathRoot
globroot
(MonitorStateGlobRel -> MonitorStateGlob)
-> ChangedM MonitorStateGlobRel -> ChangedM MonitorStateGlob
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> MonitorStateGlobRel
-> ChangedM MonitorStateGlobRel
probeMonitorStateGlobRel MonitorKindFile
kindfile MonitorKindDir
kinddir [Char]
root [Char]
"" MonitorStateGlobRel
glob
probeMonitorStateGlobRel
:: MonitorKindFile
-> MonitorKindDir
-> FilePath
-> FilePath
-> MonitorStateGlobRel
-> ChangedM MonitorStateGlobRel
probeMonitorStateGlobRel :: MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> MonitorStateGlobRel
-> ChangedM MonitorStateGlobRel
probeMonitorStateGlobRel
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
[Char]
dirName
(MonitorStateGlobDirs [GlobPiece]
glob Glob
globPath ModTime
mtime [([Char], MonitorStateGlobRel)]
children) = do
change <- IO (Maybe ModTime) -> ChangedM (Maybe ModTime)
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe ModTime) -> ChangedM (Maybe ModTime))
-> IO (Maybe ModTime) -> ChangedM (Maybe ModTime)
forall a b. (a -> b) -> a -> b
$ [Char] -> ModTime -> IO (Maybe ModTime)
checkDirectoryModificationTime ([Char]
root [Char] -> ShowS
</> [Char]
dirName) ModTime
mtime
case change of
Maybe ModTime
Nothing -> do
children' <-
[ChangedM ([Char], MonitorStateGlobRel)]
-> ChangedM [([Char], MonitorStateGlobRel)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence
[ do
fstate' <-
MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> MonitorStateGlobRel
-> ChangedM MonitorStateGlobRel
probeMonitorStateGlobRel
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
([Char]
dirName [Char] -> ShowS
</> [Char]
fname)
MonitorStateGlobRel
fstate
return (fname, fstate')
| ([Char]
fname, MonitorStateGlobRel
fstate) <- [([Char], MonitorStateGlobRel)]
children
]
return $! MonitorStateGlobDirs glob globPath mtime children'
Just ModTime
mtime' -> do
matches <-
([Char] -> ChangedM Bool) -> [[Char]] -> ChangedM [[Char]]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM
( \[Char]
entry ->
let subdir :: [Char]
subdir = [Char]
root [Char] -> ShowS
</> [Char]
dirName [Char] -> ShowS
</> [Char]
entry
in IO Bool -> ChangedM Bool
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ChangedM Bool) -> IO Bool -> ChangedM Bool
forall a b. (a -> b) -> a -> b
$ [Char] -> IO Bool
doesDirectoryExist [Char]
subdir
)
([[Char]] -> ChangedM [[Char]])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> ChangedM [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter ([GlobPiece] -> [Char] -> Bool
matchGlobPieces [GlobPiece]
glob)
([[Char]] -> ChangedM [[Char]])
-> ChangedM [[Char]] -> ChangedM [[Char]]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO [[Char]] -> ChangedM [[Char]]
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([Char] -> IO [[Char]]
getDirectoryContents ([Char]
root [Char] -> ShowS
</> [Char]
dirName))
children' <-
traverse probeMergeResult $
mergeBy
(\([Char]
path1, MonitorStateGlobRel
_) [Char]
path2 -> [Char] -> [Char] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare [Char]
path1 [Char]
path2)
children
(sort matches)
return $! MonitorStateGlobDirs glob globPath mtime' children'
where
probeMergeResult
:: MergeResult (FilePath, MonitorStateGlobRel) FilePath
-> ChangedM (FilePath, MonitorStateGlobRel)
probeMergeResult :: MergeResult ([Char], MonitorStateGlobRel) [Char]
-> ChangedM ([Char], MonitorStateGlobRel)
probeMergeResult (OnlyInLeft ([Char]
path, MonitorStateGlobRel
fstate)) = do
case [Char] -> MonitorStateGlobRel -> [[Char]]
allMatchingFiles ([Char]
dirName [Char] -> ShowS
</> [Char]
path) MonitorStateGlobRel
fstate of
[] -> ([Char], MonitorStateGlobRel)
-> ChangedM ([Char], MonitorStateGlobRel)
forall a. a -> ChangedM a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char]
path, MonitorStateGlobRel
fstate)
([Char]
file : [[Char]]
_) -> [Char] -> ChangedM ([Char], MonitorStateGlobRel)
forall a. [Char] -> ChangedM a
somethingChanged [Char]
file
probeMergeResult (OnlyInRight [Char]
path) = do
fstate <-
IO MonitorStateGlobRel -> ChangedM MonitorStateGlobRel
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO MonitorStateGlobRel -> ChangedM MonitorStateGlobRel)
-> IO MonitorStateGlobRel -> ChangedM MonitorStateGlobRel
forall a b. (a -> b) -> a -> b
$
Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> Glob
-> IO MonitorStateGlobRel
buildMonitorStateGlobRel
Maybe MonitorTimestamp
forall a. Maybe a
Nothing
FileHashCache
forall k a. Map k a
Map.empty
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
([Char]
dirName [Char] -> ShowS
</> [Char]
path)
Glob
globPath
case allMatchingFiles (dirName </> path) fstate of
([Char]
file : [[Char]]
_) -> [Char] -> ChangedM ([Char], MonitorStateGlobRel)
forall a. [Char] -> ChangedM a
somethingChanged [Char]
file
[] -> ChangedM ()
cacheChanged ChangedM ()
-> ChangedM ([Char], MonitorStateGlobRel)
-> ChangedM ([Char], MonitorStateGlobRel)
forall a b. ChangedM a -> ChangedM b -> ChangedM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ([Char], MonitorStateGlobRel)
-> ChangedM ([Char], MonitorStateGlobRel)
forall a. a -> ChangedM a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char]
path, MonitorStateGlobRel
fstate)
probeMergeResult (InBoth ([Char]
path, MonitorStateGlobRel
fstate) [Char]
_) = do
fstate' <-
MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> MonitorStateGlobRel
-> ChangedM MonitorStateGlobRel
probeMonitorStateGlobRel
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
([Char]
dirName [Char] -> ShowS
</> [Char]
path)
MonitorStateGlobRel
fstate
return (path, fstate')
allMatchingFiles :: FilePath -> MonitorStateGlobRel -> [FilePath]
allMatchingFiles :: [Char] -> MonitorStateGlobRel -> [[Char]]
allMatchingFiles [Char]
dir (MonitorStateGlobFiles [GlobPiece]
_ ModTime
_ [([Char], MonitorStateFileStatus)]
entries) =
[[Char]
dir [Char] -> ShowS
</> [Char]
fname | ([Char]
fname, MonitorStateFileStatus
_) <- [([Char], MonitorStateFileStatus)]
entries]
allMatchingFiles [Char]
dir (MonitorStateGlobDirs [GlobPiece]
_ Glob
_ ModTime
_ [([Char], MonitorStateGlobRel)]
entries) =
[ [Char]
res
| ([Char]
subdir, MonitorStateGlobRel
fstate) <- [([Char], MonitorStateGlobRel)]
entries
, [Char]
res <- [Char] -> MonitorStateGlobRel -> [[Char]]
allMatchingFiles ([Char]
dir [Char] -> ShowS
</> [Char]
subdir) MonitorStateGlobRel
fstate
]
allMatchingFiles [Char]
dir MonitorStateGlobRel
MonitorStateGlobDirTrailing =
[[Char]
dir]
probeMonitorStateGlobRel
MonitorKindFile
_
MonitorKindDir
_
[Char]
root
[Char]
dirName
(MonitorStateGlobFiles [GlobPiece]
glob ModTime
mtime [([Char], MonitorStateFileStatus)]
children) = do
change <- IO (Maybe ModTime) -> ChangedM (Maybe ModTime)
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe ModTime) -> ChangedM (Maybe ModTime))
-> IO (Maybe ModTime) -> ChangedM (Maybe ModTime)
forall a b. (a -> b) -> a -> b
$ [Char] -> ModTime -> IO (Maybe ModTime)
checkDirectoryModificationTime ([Char]
root [Char] -> ShowS
</> [Char]
dirName) ModTime
mtime
mtime' <- case change of
Maybe ModTime
Nothing -> ModTime -> ChangedM ModTime
forall a. a -> ChangedM a
forall (m :: * -> *) a. Monad m => a -> m a
return ModTime
mtime
Just ModTime
mtime' -> do
matches <-
[[Char]] -> ChangedM [[Char]]
forall a. a -> ChangedM a
forall (m :: * -> *) a. Monad m => a -> m a
return ([[Char]] -> ChangedM [[Char]])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> ChangedM [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter ([GlobPiece] -> [Char] -> Bool
matchGlobPieces [GlobPiece]
glob)
([[Char]] -> ChangedM [[Char]])
-> ChangedM [[Char]] -> ChangedM [[Char]]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO [[Char]] -> ChangedM [[Char]]
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ([Char] -> IO [[Char]]
getDirectoryContents ([Char]
root [Char] -> ShowS
</> [Char]
dirName))
traverse_ probeMergeResult $
mergeBy
(\([Char]
path1, MonitorStateFileStatus
_) [Char]
path2 -> [Char] -> [Char] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare [Char]
path1 [Char]
path2)
children
(sort matches)
return mtime'
for_ children $ \([Char]
file, MonitorStateFileStatus
status) ->
[Char] -> [Char] -> MonitorStateFileStatus -> ChangedM ()
probeMonitorStateFileStatus [Char]
root ([Char]
dirName [Char] -> ShowS
</> [Char]
file) MonitorStateFileStatus
status
return (MonitorStateGlobFiles glob mtime' children)
where
probeMergeResult
:: MergeResult (FilePath, MonitorStateFileStatus) FilePath
-> ChangedM ()
probeMergeResult :: MergeResult ([Char], MonitorStateFileStatus) [Char] -> ChangedM ()
probeMergeResult MergeResult ([Char], MonitorStateFileStatus) [Char]
mr = case MergeResult ([Char], MonitorStateFileStatus) [Char]
mr of
InBoth ([Char], MonitorStateFileStatus)
_ [Char]
_ -> () -> ChangedM ()
forall a. a -> ChangedM a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
OnlyInLeft ([Char]
path, MonitorStateFileStatus
_) -> [Char] -> ChangedM ()
forall a. [Char] -> ChangedM a
somethingChanged ([Char]
dirName [Char] -> ShowS
</> [Char]
path)
OnlyInRight [Char]
path -> [Char] -> ChangedM ()
forall a. [Char] -> ChangedM a
somethingChanged ([Char]
dirName [Char] -> ShowS
</> [Char]
path)
probeMonitorStateGlobRel MonitorKindFile
_ MonitorKindDir
_ [Char]
_ [Char]
_ MonitorStateGlobRel
MonitorStateGlobDirTrailing =
MonitorStateGlobRel -> ChangedM MonitorStateGlobRel
forall a. a -> ChangedM a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateGlobRel
MonitorStateGlobDirTrailing
updateFileMonitor
:: (Binary a, Structured a, Binary b, Structured b)
=> FileMonitor a b
-> FilePath
-> Maybe MonitorTimestamp
-> [MonitorFilePath]
-> a
-> b
-> IO ()
updateFileMonitor :: forall a b.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b
-> [Char]
-> Maybe MonitorTimestamp
-> [MonitorFilePath]
-> a
-> b
-> IO ()
updateFileMonitor
FileMonitor a b
monitor
[Char]
root
Maybe MonitorTimestamp
startTime
[MonitorFilePath]
monitorFiles
a
cachedKey
b
cachedResult = do
hashcache <- FileMonitor a b -> IO FileHashCache
forall a b.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b -> IO FileHashCache
readCacheFileHashes FileMonitor a b
monitor
msfs <- buildMonitorStateFileSet startTime hashcache root monitorFiles
rewriteCacheFile monitor msfs cachedKey cachedResult
newtype MonitorTimestamp = MonitorTimestamp ModTime
beginUpdateFileMonitor :: IO MonitorTimestamp
beginUpdateFileMonitor :: IO MonitorTimestamp
beginUpdateFileMonitor = ModTime -> MonitorTimestamp
MonitorTimestamp (ModTime -> MonitorTimestamp) -> IO ModTime -> IO MonitorTimestamp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO ModTime
getCurTime
buildMonitorStateFileSet
:: Maybe MonitorTimestamp
-> FileHashCache
-> FilePath
-> [MonitorFilePath]
-> IO MonitorStateFileSet
buildMonitorStateFileSet :: Maybe MonitorTimestamp
-> FileHashCache
-> [Char]
-> [MonitorFilePath]
-> IO MonitorStateFileSet
buildMonitorStateFileSet Maybe MonitorTimestamp
mstartTime FileHashCache
hashcache [Char]
root =
[MonitorStateFile]
-> [MonitorStateGlob]
-> [MonitorFilePath]
-> IO MonitorStateFileSet
go [] []
where
go
:: [MonitorStateFile]
-> [MonitorStateGlob]
-> [MonitorFilePath]
-> IO MonitorStateFileSet
go :: [MonitorStateFile]
-> [MonitorStateGlob]
-> [MonitorFilePath]
-> IO MonitorStateFileSet
go ![MonitorStateFile]
singlePaths ![MonitorStateGlob]
globPaths [] =
MonitorStateFileSet -> IO MonitorStateFileSet
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([MonitorStateFile] -> [MonitorStateGlob] -> MonitorStateFileSet
MonitorStateFileSet ([MonitorStateFile] -> [MonitorStateFile]
forall a. [a] -> [a]
reverse [MonitorStateFile]
singlePaths) ([MonitorStateGlob] -> [MonitorStateGlob]
forall a. [a] -> [a]
reverse [MonitorStateGlob]
globPaths))
go
![MonitorStateFile]
singlePaths
![MonitorStateGlob]
globPaths
(MonitorFile MonitorKindFile
kindfile MonitorKindDir
kinddir [Char]
path : [MonitorFilePath]
monitors) = do
monitorState <-
MonitorKindFile
-> MonitorKindDir
-> [Char]
-> MonitorStateFileStatus
-> MonitorStateFile
MonitorStateFile MonitorKindFile
kindfile MonitorKindDir
kinddir [Char]
path
(MonitorStateFileStatus -> MonitorStateFile)
-> IO MonitorStateFileStatus -> IO MonitorStateFile
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> IO MonitorStateFileStatus
buildMonitorStateFile
Maybe MonitorTimestamp
mstartTime
FileHashCache
hashcache
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
[Char]
path
go (monitorState : singlePaths) globPaths monitors
go
![MonitorStateFile]
singlePaths
![MonitorStateGlob]
globPaths
(MonitorFileGlob MonitorKindFile
kindfile MonitorKindDir
kinddir RootedGlob
globPath : [MonitorFilePath]
monitors) = do
monitorState <-
Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> RootedGlob
-> IO MonitorStateGlob
buildMonitorStateGlob
Maybe MonitorTimestamp
mstartTime
FileHashCache
hashcache
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
RootedGlob
globPath
go singlePaths (monitorState : globPaths) monitors
buildMonitorStateFile
:: Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> FilePath
-> FilePath
-> IO MonitorStateFileStatus
buildMonitorStateFile :: Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> IO MonitorStateFileStatus
buildMonitorStateFile Maybe MonitorTimestamp
mstartTime FileHashCache
hashcache MonitorKindFile
kindfile MonitorKindDir
kinddir [Char]
root [Char]
path = do
let abspath :: [Char]
abspath = [Char]
root [Char] -> ShowS
</> [Char]
path
isFile <- [Char] -> IO Bool
doesFileExist [Char]
abspath
isDir <- doesDirectoryExist abspath
case (isFile, kindfile, isDir, kinddir) of
(Bool
_, MonitorKindFile
FileNotExists, Bool
_, MonitorKindDir
DirNotExists) ->
MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateFileStatus
MonitorStateNonExistent
(Bool
False, MonitorKindFile
_, Bool
False, MonitorKindDir
_) ->
MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateFileStatus
MonitorStateAlreadyChanged
(Bool
True, MonitorKindFile
FileExists, Bool
_, MonitorKindDir
_) ->
MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateFileStatus
MonitorStateFileExists
(Bool
True, MonitorKindFile
FileModTime, Bool
_, MonitorKindDir
_) ->
MonitorStateFileStatus
-> IO MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a -> IO a
handleIOException MonitorStateFileStatus
MonitorStateAlreadyChanged (IO MonitorStateFileStatus -> IO MonitorStateFileStatus)
-> IO MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a b. (a -> b) -> a -> b
$ do
mtime <- [Char] -> IO ModTime
getModTime [Char]
abspath
if changedDuringUpdate mstartTime mtime
then return MonitorStateAlreadyChanged
else return (MonitorStateFileModTime mtime)
(Bool
True, MonitorKindFile
FileHashed, Bool
_, MonitorKindDir
_) ->
MonitorStateFileStatus
-> IO MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a -> IO a
handleIOException MonitorStateFileStatus
MonitorStateAlreadyChanged (IO MonitorStateFileStatus -> IO MonitorStateFileStatus)
-> IO MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a b. (a -> b) -> a -> b
$ do
mtime <- [Char] -> IO ModTime
getModTime [Char]
abspath
if changedDuringUpdate mstartTime mtime
then return MonitorStateAlreadyChanged
else do
hash <- getFileHash hashcache abspath abspath mtime
return (MonitorStateFileHashed mtime hash)
(Bool
_, MonitorKindFile
_, Bool
True, MonitorKindDir
DirExists) ->
MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateFileStatus
MonitorStateDirExists
(Bool
_, MonitorKindFile
_, Bool
True, MonitorKindDir
DirModTime) ->
MonitorStateFileStatus
-> IO MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a -> IO a
handleIOException MonitorStateFileStatus
MonitorStateAlreadyChanged (IO MonitorStateFileStatus -> IO MonitorStateFileStatus)
-> IO MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a b. (a -> b) -> a -> b
$ do
mtime <- [Char] -> IO ModTime
getModTime [Char]
abspath
if changedDuringUpdate mstartTime mtime
then return MonitorStateAlreadyChanged
else return (MonitorStateDirModTime mtime)
(Bool
False, MonitorKindFile
_, Bool
True, MonitorKindDir
DirNotExists) -> MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateFileStatus
MonitorStateAlreadyChanged
(Bool
True, MonitorKindFile
FileNotExists, Bool
False, MonitorKindDir
_) -> MonitorStateFileStatus -> IO MonitorStateFileStatus
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateFileStatus
MonitorStateAlreadyChanged
changedDuringUpdate :: Maybe MonitorTimestamp -> ModTime -> Bool
changedDuringUpdate :: Maybe MonitorTimestamp -> ModTime -> Bool
changedDuringUpdate (Just (MonitorTimestamp ModTime
startTime)) ModTime
mtime =
ModTime
mtime ModTime -> ModTime -> Bool
forall a. Ord a => a -> a -> Bool
> ModTime
startTime
changedDuringUpdate Maybe MonitorTimestamp
_ ModTime
_ = Bool
False
buildMonitorStateGlob
:: Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> FilePath
-> RootedGlob
-> IO MonitorStateGlob
buildMonitorStateGlob :: Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> RootedGlob
-> IO MonitorStateGlob
buildMonitorStateGlob
Maybe MonitorTimestamp
mstartTime
FileHashCache
hashcache
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
relroot
(RootedGlob FilePathRoot
globroot Glob
globPath) = do
root <- IO [Char] -> IO [Char]
forall a. IO a -> IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Char] -> IO [Char]) -> IO [Char] -> IO [Char]
forall a b. (a -> b) -> a -> b
$ FilePathRoot -> [Char] -> IO [Char]
getFilePathRootDirectory FilePathRoot
globroot [Char]
relroot
MonitorStateGlob kindfile kinddir globroot
<$> buildMonitorStateGlobRel
mstartTime
hashcache
kindfile
kinddir
root
"."
globPath
buildMonitorStateGlobRel
:: Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> FilePath
-> FilePath
-> Glob
-> IO MonitorStateGlobRel
buildMonitorStateGlobRel :: Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> Glob
-> IO MonitorStateGlobRel
buildMonitorStateGlobRel
Maybe MonitorTimestamp
mstartTime
FileHashCache
hashcache
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
[Char]
dir
Glob
globPath = do
let absdir :: [Char]
absdir = [Char]
root [Char] -> ShowS
</> [Char]
dir
dirEntries <- [Char] -> IO [[Char]]
getDirectoryContents [Char]
absdir
dirMTime <- getModTime absdir
case globPath of
GlobDirRecursive{} -> [Char] -> IO MonitorStateGlobRel
forall a. HasCallStack => [Char] -> a
error [Char]
"Monitoring directory-recursive globs (i.e. ../**/...) is currently unsupported"
GlobDir [GlobPiece]
glob Glob
globPath' -> do
subdirs <-
([Char] -> IO Bool) -> [[Char]] -> IO [[Char]]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM (\[Char]
subdir -> [Char] -> IO Bool
doesDirectoryExist ([Char]
absdir [Char] -> ShowS
</> [Char]
subdir)) ([[Char]] -> IO [[Char]]) -> [[Char]] -> IO [[Char]]
forall a b. (a -> b) -> a -> b
$
([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter ([GlobPiece] -> [Char] -> Bool
matchGlobPieces [GlobPiece]
glob) [[Char]]
dirEntries
subdirStates <-
for (sort subdirs) $ \[Char]
subdir -> do
fstate <-
Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> Glob
-> IO MonitorStateGlobRel
buildMonitorStateGlobRel
Maybe MonitorTimestamp
mstartTime
FileHashCache
hashcache
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
([Char]
dir [Char] -> ShowS
</> [Char]
subdir)
Glob
globPath'
return (subdir, fstate)
return $! MonitorStateGlobDirs glob globPath' dirMTime subdirStates
GlobFile [GlobPiece]
glob -> do
let files :: [[Char]]
files = ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter ([GlobPiece] -> [Char] -> Bool
matchGlobPieces [GlobPiece]
glob) [[Char]]
dirEntries
filesStates <-
[[Char]]
-> ([Char] -> IO ([Char], MonitorStateFileStatus))
-> IO [([Char], MonitorStateFileStatus)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for ([[Char]] -> [[Char]]
forall a. Ord a => [a] -> [a]
sort [[Char]]
files) (([Char] -> IO ([Char], MonitorStateFileStatus))
-> IO [([Char], MonitorStateFileStatus)])
-> ([Char] -> IO ([Char], MonitorStateFileStatus))
-> IO [([Char], MonitorStateFileStatus)]
forall a b. (a -> b) -> a -> b
$ \[Char]
file -> do
fstate <-
Maybe MonitorTimestamp
-> FileHashCache
-> MonitorKindFile
-> MonitorKindDir
-> [Char]
-> [Char]
-> IO MonitorStateFileStatus
buildMonitorStateFile
Maybe MonitorTimestamp
mstartTime
FileHashCache
hashcache
MonitorKindFile
kindfile
MonitorKindDir
kinddir
[Char]
root
([Char]
dir [Char] -> ShowS
</> [Char]
file)
return (file, fstate)
return $! MonitorStateGlobFiles glob dirMTime filesStates
Glob
GlobDirTrailing ->
MonitorStateGlobRel -> IO MonitorStateGlobRel
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return MonitorStateGlobRel
MonitorStateGlobDirTrailing
type FileHashCache = Map FilePath (ModTime, HashValue)
lookupFileHashCache :: FileHashCache -> FilePath -> ModTime -> Maybe HashValue
lookupFileHashCache :: FileHashCache -> [Char] -> ModTime -> Maybe HashValue
lookupFileHashCache FileHashCache
hashcache [Char]
file ModTime
mtime = do
(mtime', hash) <- [Char] -> FileHashCache -> Maybe (ModTime, HashValue)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup [Char]
file FileHashCache
hashcache
guard (mtime' == mtime)
return hash
getFileHash :: FileHashCache -> FilePath -> FilePath -> ModTime -> IO HashValue
getFileHash :: FileHashCache -> [Char] -> [Char] -> ModTime -> IO HashValue
getFileHash FileHashCache
hashcache [Char]
relfile [Char]
absfile ModTime
mtime =
case FileHashCache -> [Char] -> ModTime -> Maybe HashValue
lookupFileHashCache FileHashCache
hashcache [Char]
relfile ModTime
mtime of
Just HashValue
hash -> HashValue -> IO HashValue
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return HashValue
hash
Maybe HashValue
Nothing -> [Char] -> IO HashValue
readFileHashValue [Char]
absfile
readCacheFileHashes
:: (Binary a, Structured a, Binary b, Structured b)
=> FileMonitor a b
-> IO FileHashCache
readCacheFileHashes :: forall a b.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b -> IO FileHashCache
readCacheFileHashes FileMonitor a b
monitor =
FileHashCache -> IO FileHashCache -> IO FileHashCache
forall a. a -> IO a -> IO a
handleDoesNotExist FileHashCache
forall k a. Map k a
Map.empty (IO FileHashCache -> IO FileHashCache)
-> IO FileHashCache -> IO FileHashCache
forall a b. (a -> b) -> a -> b
$
FileHashCache -> IO FileHashCache -> IO FileHashCache
forall a. a -> IO a -> IO a
handleErrorCall FileHashCache
forall k a. Map k a
Map.empty (IO FileHashCache -> IO FileHashCache)
-> IO FileHashCache -> IO FileHashCache
forall a b. (a -> b) -> a -> b
$
FileMonitor a b
-> (Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO FileHashCache)
-> IO FileHashCache
forall a b r.
(Binary a, Structured a, Binary b, Structured b) =>
FileMonitor a b
-> (Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO r)
-> IO r
withCacheFile FileMonitor a b
monitor ((Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO FileHashCache)
-> IO FileHashCache)
-> (Either [Char] (MonitorStateFileSet, a, Either [Char] b)
-> IO FileHashCache)
-> IO FileHashCache
forall a b. (a -> b) -> a -> b
$ \Either [Char] (MonitorStateFileSet, a, Either [Char] b)
res ->
case Either [Char] (MonitorStateFileSet, a, Either [Char] b)
res of
Left [Char]
_ -> FileHashCache -> IO FileHashCache
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return FileHashCache
forall k a. Map k a
Map.empty
Right (MonitorStateFileSet
msfs, a
_, Either [Char] b
_) -> FileHashCache -> IO FileHashCache
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (MonitorStateFileSet -> FileHashCache
mkFileHashCache MonitorStateFileSet
msfs)
where
mkFileHashCache :: MonitorStateFileSet -> FileHashCache
mkFileHashCache :: MonitorStateFileSet -> FileHashCache
mkFileHashCache (MonitorStateFileSet [MonitorStateFile]
singlePaths [MonitorStateGlob]
globPaths) =
[MonitorStateFile] -> FileHashCache
collectAllFileHashes [MonitorStateFile]
singlePaths
FileHashCache -> FileHashCache -> FileHashCache
forall k a. Ord k => Map k a -> Map k a -> Map k a
`Map.union` [MonitorStateGlob] -> FileHashCache
collectAllGlobHashes [MonitorStateGlob]
globPaths
collectAllFileHashes :: [MonitorStateFile] -> Map FilePath (ModTime, HashValue)
collectAllFileHashes :: [MonitorStateFile] -> FileHashCache
collectAllFileHashes [MonitorStateFile]
singlePaths =
[([Char], (ModTime, HashValue))] -> FileHashCache
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ ([Char]
fpath, (ModTime
mtime, HashValue
hash))
| MonitorStateFile
MonitorKindFile
_
MonitorKindDir
_
[Char]
fpath
(MonitorStateFileHashed ModTime
mtime HashValue
hash) <-
[MonitorStateFile]
singlePaths
]
collectAllGlobHashes :: [MonitorStateGlob] -> Map FilePath (ModTime, HashValue)
collectAllGlobHashes :: [MonitorStateGlob] -> FileHashCache
collectAllGlobHashes [MonitorStateGlob]
globPaths =
[([Char], (ModTime, HashValue))] -> FileHashCache
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ ([Char]
fpath, (ModTime
mtime, HashValue
hash))
| MonitorStateGlob MonitorKindFile
_ MonitorKindDir
_ FilePathRoot
_ MonitorStateGlobRel
gstate <- [MonitorStateGlob]
globPaths
, ([Char]
fpath, (ModTime
mtime, HashValue
hash)) <- [Char] -> MonitorStateGlobRel -> [([Char], (ModTime, HashValue))]
collectGlobHashes [Char]
"" MonitorStateGlobRel
gstate
]
collectGlobHashes :: FilePath -> MonitorStateGlobRel -> [(FilePath, (ModTime, HashValue))]
collectGlobHashes :: [Char] -> MonitorStateGlobRel -> [([Char], (ModTime, HashValue))]
collectGlobHashes [Char]
dir (MonitorStateGlobDirs [GlobPiece]
_ Glob
_ ModTime
_ [([Char], MonitorStateGlobRel)]
entries) =
[ ([Char], (ModTime, HashValue))
res
| ([Char]
subdir, MonitorStateGlobRel
fstate) <- [([Char], MonitorStateGlobRel)]
entries
, ([Char], (ModTime, HashValue))
res <- [Char] -> MonitorStateGlobRel -> [([Char], (ModTime, HashValue))]
collectGlobHashes ([Char]
dir [Char] -> ShowS
</> [Char]
subdir) MonitorStateGlobRel
fstate
]
collectGlobHashes [Char]
dir (MonitorStateGlobFiles [GlobPiece]
_ ModTime
_ [([Char], MonitorStateFileStatus)]
entries) =
[ ([Char]
dir [Char] -> ShowS
</> [Char]
fname, (ModTime
mtime, HashValue
hash))
| ([Char]
fname, MonitorStateFileHashed ModTime
mtime HashValue
hash) <- [([Char], MonitorStateFileStatus)]
entries
]
collectGlobHashes [Char]
_dir MonitorStateGlobRel
MonitorStateGlobDirTrailing =
[]
probeFileModificationTime :: FilePath -> FilePath -> ModTime -> ChangedM ()
probeFileModificationTime :: [Char] -> [Char] -> ModTime -> ChangedM ()
probeFileModificationTime [Char]
root [Char]
file ModTime
mtime = do
unchanged <- IO Bool -> ChangedM Bool
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ChangedM Bool) -> IO Bool -> ChangedM Bool
forall a b. (a -> b) -> a -> b
$ [Char] -> [Char] -> ModTime -> IO Bool
checkModificationTimeUnchanged [Char]
root [Char]
file ModTime
mtime
unless unchanged (somethingChanged file)
probeFileModificationTimeAndHash
:: FilePath
-> FilePath
-> ModTime
-> HashValue
-> ChangedM ()
probeFileModificationTimeAndHash :: [Char] -> [Char] -> ModTime -> HashValue -> ChangedM ()
probeFileModificationTimeAndHash [Char]
root [Char]
file ModTime
mtime HashValue
hash = do
unchanged <-
IO Bool -> ChangedM Bool
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ChangedM Bool) -> IO Bool -> ChangedM Bool
forall a b. (a -> b) -> a -> b
$
[Char] -> [Char] -> ModTime -> HashValue -> IO Bool
checkFileModificationTimeAndHashUnchanged [Char]
root [Char]
file ModTime
mtime HashValue
hash
unless unchanged (somethingChanged file)
probeFileExistence :: FilePath -> FilePath -> ChangedM ()
probeFileExistence :: [Char] -> [Char] -> ChangedM ()
probeFileExistence [Char]
root [Char]
file = do
existsFile <- IO Bool -> ChangedM Bool
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ChangedM Bool) -> IO Bool -> ChangedM Bool
forall a b. (a -> b) -> a -> b
$ [Char] -> IO Bool
doesFileExist ([Char]
root [Char] -> ShowS
</> [Char]
file)
unless existsFile (somethingChanged file)
probeDirExistence :: FilePath -> FilePath -> ChangedM ()
probeDirExistence :: [Char] -> [Char] -> ChangedM ()
probeDirExistence [Char]
root [Char]
dir = do
existsDir <- IO Bool -> ChangedM Bool
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ChangedM Bool) -> IO Bool -> ChangedM Bool
forall a b. (a -> b) -> a -> b
$ [Char] -> IO Bool
doesDirectoryExist ([Char]
root [Char] -> ShowS
</> [Char]
dir)
unless existsDir (somethingChanged dir)
probeFileNonExistence :: FilePath -> FilePath -> ChangedM ()
probeFileNonExistence :: [Char] -> [Char] -> ChangedM ()
probeFileNonExistence [Char]
root [Char]
file = do
existsFile <- IO Bool -> ChangedM Bool
forall a. IO a -> ChangedM a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> ChangedM Bool) -> IO Bool -> ChangedM Bool
forall a b. (a -> b) -> a -> b
$ [Char] -> IO Bool
doesFileExist ([Char]
root [Char] -> ShowS
</> [Char]
file)
existsDir <- liftIO $ doesDirectoryExist (root </> file)
when (existsFile || existsDir) (somethingChanged file)
checkModificationTimeUnchanged
:: FilePath
-> FilePath
-> ModTime
-> IO Bool
checkModificationTimeUnchanged :: [Char] -> [Char] -> ModTime -> IO Bool
checkModificationTimeUnchanged [Char]
root [Char]
file ModTime
mtime =
Bool -> IO Bool -> IO Bool
forall a. a -> IO a -> IO a
handleIOException Bool
False (IO Bool -> IO Bool) -> IO Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ do
mtime' <- [Char] -> IO ModTime
getModTime ([Char]
root [Char] -> ShowS
</> [Char]
file)
return (mtime == mtime')
checkFileModificationTimeAndHashUnchanged
:: FilePath
-> FilePath
-> ModTime
-> HashValue
-> IO Bool
checkFileModificationTimeAndHashUnchanged :: [Char] -> [Char] -> ModTime -> HashValue -> IO Bool
checkFileModificationTimeAndHashUnchanged [Char]
root [Char]
file ModTime
mtime HashValue
chash =
Bool -> IO Bool -> IO Bool
forall a. a -> IO a -> IO a
handleIOException Bool
False (IO Bool -> IO Bool) -> IO Bool -> IO Bool
forall a b. (a -> b) -> a -> b
$ do
mtime' <- [Char] -> IO ModTime
getModTime ([Char]
root [Char] -> ShowS
</> [Char]
file)
if mtime == mtime'
then return True
else do
chash' <- readFileHashValue (root </> file)
return (chash == chash')
checkDirectoryModificationTime :: FilePath -> ModTime -> IO (Maybe ModTime)
checkDirectoryModificationTime :: [Char] -> ModTime -> IO (Maybe ModTime)
checkDirectoryModificationTime [Char]
dir ModTime
mtime =
Maybe ModTime -> IO (Maybe ModTime) -> IO (Maybe ModTime)
forall a. a -> IO a -> IO a
handleIOException Maybe ModTime
forall a. Maybe a
Nothing (IO (Maybe ModTime) -> IO (Maybe ModTime))
-> IO (Maybe ModTime) -> IO (Maybe ModTime)
forall a b. (a -> b) -> a -> b
$ do
mtime' <- [Char] -> IO ModTime
getModTime [Char]
dir
if mtime == mtime'
then return Nothing
else return (Just mtime')
handleErrorCall :: a -> IO a -> IO a
handleErrorCall :: forall a. a -> IO a -> IO a
handleErrorCall a
e = (ErrorCall -> IO a) -> IO a -> IO a
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle ErrorCall -> IO a
forall {m :: * -> *}. Monad m => ErrorCall -> m a
handler
where
handler :: ErrorCall -> m a
handler (ErrorCall [Char]
_) = a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
e
handleIOException :: a -> IO a -> IO a
handleIOException :: forall a. a -> IO a -> IO a
handleIOException a
e =
(IOException -> IO a) -> IO a -> IO a
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle (a -> IOException -> IO a
forall a. a -> IOException -> IO a
anyIOException a
e)
where
anyIOException :: a -> IOException -> IO a
anyIOException :: forall a. a -> IOException -> IO a
anyIOException a
x IOException
_ = a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x