2014-01-20 20:47:56 +00:00
|
|
|
{- git-annex numcopies log
|
|
|
|
-
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
module Logs.NumCopies (
|
|
|
|
module Types.NumCopies,
|
|
|
|
setGlobalNumCopies,
|
|
|
|
getGlobalNumCopies,
|
|
|
|
globalNumCopiesLoad,
|
|
|
|
getFileNumCopies,
|
|
|
|
numCopiesCheck,
|
|
|
|
getNumCopies,
|
|
|
|
deprecatedNumCopies,
|
|
|
|
) where
|
2014-01-20 20:47:56 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import qualified Annex
|
2014-01-21 20:08:19 +00:00
|
|
|
import Types.NumCopies
|
2014-01-20 20:47:56 +00:00
|
|
|
import Logs
|
|
|
|
import Logs.SingleValue
|
2014-01-21 20:08:19 +00:00
|
|
|
import Logs.Trust
|
|
|
|
import Annex.CheckAttr
|
|
|
|
import qualified Remote
|
2014-01-20 20:47:56 +00:00
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
instance SingleValueSerializable NumCopies where
|
|
|
|
serialize (NumCopies n) = show n
|
|
|
|
deserialize = NumCopies <$$> readish
|
2014-01-20 20:47:56 +00:00
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
setGlobalNumCopies :: NumCopies -> Annex ()
|
2014-01-20 20:47:56 +00:00
|
|
|
setGlobalNumCopies = setLog numcopiesLog
|
|
|
|
|
|
|
|
{- Cached for speed. -}
|
2014-01-21 20:08:19 +00:00
|
|
|
getGlobalNumCopies :: Annex (Maybe NumCopies)
|
|
|
|
getGlobalNumCopies = maybe globalNumCopiesLoad (return . Just)
|
2014-01-20 20:47:56 +00:00
|
|
|
=<< Annex.getState Annex.globalnumcopies
|
|
|
|
|
2014-01-21 20:08:19 +00:00
|
|
|
globalNumCopiesLoad :: Annex (Maybe NumCopies)
|
|
|
|
globalNumCopiesLoad = do
|
2014-01-20 20:47:56 +00:00
|
|
|
v <- getLog numcopiesLog
|
|
|
|
Annex.changeState $ \s -> s { Annex.globalnumcopies = v }
|
|
|
|
return v
|
2014-01-21 20:08:19 +00:00
|
|
|
|
|
|
|
{- Numcopies value for a file, from .gitattributes or global,
|
|
|
|
- but not the deprecated git config. -}
|
|
|
|
getFileNumCopies :: FilePath -> Annex (Maybe NumCopies)
|
|
|
|
getFileNumCopies file = do
|
|
|
|
global <- getGlobalNumCopies
|
|
|
|
case global of
|
|
|
|
Just n -> return $ Just n
|
|
|
|
Nothing -> (NumCopies <$$> readish)
|
|
|
|
<$> checkAttr "annex.numcopies" file
|
|
|
|
|
|
|
|
deprecatedNumCopies :: Annex NumCopies
|
|
|
|
deprecatedNumCopies = NumCopies . fromMaybe 1 . annexNumCopies
|
|
|
|
<$> Annex.getGitConfig
|
|
|
|
|
|
|
|
{- Checks if numcopies are satisfied by running a comparison
|
|
|
|
- between the number of (not untrusted) copies that are
|
|
|
|
- belived to exist, and the configured value.
|
|
|
|
-
|
|
|
|
- Includes the deprecated annex.numcopies git config if
|
|
|
|
- nothing else specifies a numcopies value. -}
|
|
|
|
numCopiesCheck :: FilePath -> Key -> (Int -> Int -> v) -> Annex v
|
|
|
|
numCopiesCheck file key vs = do
|
|
|
|
numcopiesattr <- getFileNumCopies file
|
|
|
|
NumCopies needed <- getNumCopies numcopiesattr
|
|
|
|
have <- trustExclude UnTrusted =<< Remote.keyLocations key
|
|
|
|
return $ length have `vs` needed
|
|
|
|
|
|
|
|
getNumCopies :: Maybe NumCopies -> Annex NumCopies
|
|
|
|
getNumCopies (Just v) = return v
|
|
|
|
getNumCopies Nothing = deprecatedNumCopies
|