refactor
This commit is contained in:
parent
7f5958eec2
commit
f57ac29be1
2 changed files with 27 additions and 13 deletions
|
@ -183,10 +183,7 @@ contentLockFile key = Just <$> calcRepo (gitAnnexContentLock key)
|
||||||
lockContentShared :: Key -> (VerifiedCopy -> Annex a) -> Annex a
|
lockContentShared :: Key -> (VerifiedCopy -> Annex a) -> Annex a
|
||||||
lockContentShared key a = lockContentUsing lock key $ do
|
lockContentShared key a = lockContentUsing lock key $ do
|
||||||
u <- getUUID
|
u <- getUUID
|
||||||
bracketIO
|
withVerifiedCopy VerifiedCopyLock u a
|
||||||
(invalidatableVerifiedCopy VerifiedCopyLock u)
|
|
||||||
invalidateVerifiedCopy
|
|
||||||
a
|
|
||||||
where
|
where
|
||||||
#ifndef mingw32_HOST_OS
|
#ifndef mingw32_HOST_OS
|
||||||
lock contentfile Nothing = liftIO $ tryLockShared Nothing contentfile
|
lock contentfile Nothing = liftIO $ tryLockShared Nothing contentfile
|
||||||
|
|
|
@ -15,12 +15,16 @@ module Types.NumCopies (
|
||||||
deDupVerifiedCopies,
|
deDupVerifiedCopies,
|
||||||
mkVerifiedCopy,
|
mkVerifiedCopy,
|
||||||
invalidatableVerifiedCopy,
|
invalidatableVerifiedCopy,
|
||||||
|
withVerifiedCopy,
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Types.UUID
|
import Types.UUID
|
||||||
|
import Utility.Exception (bracketIO)
|
||||||
|
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
import Control.Concurrent.MVar
|
import Control.Concurrent.MVar
|
||||||
|
import Control.Monad.Catch (MonadMask)
|
||||||
|
import Control.Monad.IO.Class (MonadIO)
|
||||||
|
|
||||||
newtype NumCopies = NumCopies Int
|
newtype NumCopies = NumCopies Int
|
||||||
deriving (Ord, Eq)
|
deriving (Ord, Eq)
|
||||||
|
@ -44,6 +48,15 @@ data VerifiedCopy
|
||||||
| VerifiedCopyLock V
|
| VerifiedCopyLock V
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
|
data V = V
|
||||||
|
{ _getUUID :: UUID
|
||||||
|
, _checkVerifiedCopy :: IO Bool
|
||||||
|
, _invalidateVerifiedCopy :: IO ()
|
||||||
|
}
|
||||||
|
|
||||||
|
instance Show V where
|
||||||
|
show v = show (_getUUID v)
|
||||||
|
|
||||||
instance ToUUID VerifiedCopy where
|
instance ToUUID VerifiedCopy where
|
||||||
toUUID = _getUUID . toV
|
toUUID = _getUUID . toV
|
||||||
|
|
||||||
|
@ -59,15 +72,6 @@ checkVerifiedCopy = _checkVerifiedCopy . toV
|
||||||
invalidateVerifiedCopy :: VerifiedCopy -> IO ()
|
invalidateVerifiedCopy :: VerifiedCopy -> IO ()
|
||||||
invalidateVerifiedCopy = _invalidateVerifiedCopy . toV
|
invalidateVerifiedCopy = _invalidateVerifiedCopy . toV
|
||||||
|
|
||||||
data V = V
|
|
||||||
{ _getUUID :: UUID
|
|
||||||
, _checkVerifiedCopy :: IO Bool
|
|
||||||
, _invalidateVerifiedCopy :: IO ()
|
|
||||||
}
|
|
||||||
|
|
||||||
instance Show V where
|
|
||||||
show v = show (_getUUID v)
|
|
||||||
|
|
||||||
strongestVerifiedCopy :: VerifiedCopy -> VerifiedCopy -> VerifiedCopy
|
strongestVerifiedCopy :: VerifiedCopy -> VerifiedCopy -> VerifiedCopy
|
||||||
strongestVerifiedCopy a@(VerifiedCopyLock _) _ = a
|
strongestVerifiedCopy a@(VerifiedCopyLock _) _ = a
|
||||||
strongestVerifiedCopy _ b@(VerifiedCopyLock _) = b
|
strongestVerifiedCopy _ b@(VerifiedCopyLock _) = b
|
||||||
|
@ -91,3 +95,16 @@ invalidatableVerifiedCopy mk u = do
|
||||||
return ()
|
return ()
|
||||||
let check = isEmptyMVar v
|
let check = isEmptyMVar v
|
||||||
return $ mk $ V (toUUID u) check invalidate
|
return $ mk $ V (toUUID u) check invalidate
|
||||||
|
|
||||||
|
-- Constructs a VerifiedCopy, and runs the action, ensuring that the
|
||||||
|
-- verified copy is invalidated when the action returns, or on error.
|
||||||
|
withVerifiedCopy
|
||||||
|
:: (Monad m, MonadMask m, MonadIO m, ToUUID u)
|
||||||
|
=> (V -> VerifiedCopy)
|
||||||
|
-> u
|
||||||
|
-> (VerifiedCopy -> m a)
|
||||||
|
-> m a
|
||||||
|
withVerifiedCopy mk u = bracketIO setup cleanup
|
||||||
|
where
|
||||||
|
setup = invalidatableVerifiedCopy mk u
|
||||||
|
cleanup = invalidateVerifiedCopy
|
||||||
|
|
Loading…
Reference in a new issue