aboutsummaryrefslogtreecommitdiff
path: root/Logs/NumCopies.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2014-01-21 16:08:19 -0400
committerGravatar Joey Hess <joey@kitenet.net>2014-01-21 16:08:59 -0400
commitd5f7fb27aad3e2e9c4bebb9ccd5577af8deb25c7 (patch)
tree838837e3112942fcf0f82cfc7f68e62a6f4e7a6e /Logs/NumCopies.hs
parent9a8709f064c7608859b3155a752093b29cd8ab98 (diff)
reorganize numcopies code (no behavior changes)
Move stuff into Logs.NumCopies. Add a NumCopies newtype. Better names for various serialization classes that are specific to one thing or another.
Diffstat (limited to 'Logs/NumCopies.hs')
-rw-r--r--Logs/NumCopies.hs62
1 files changed, 53 insertions, 9 deletions
diff --git a/Logs/NumCopies.hs b/Logs/NumCopies.hs
index dc345dd0a..2fd6f75f8 100644
--- a/Logs/NumCopies.hs
+++ b/Logs/NumCopies.hs
@@ -7,27 +7,71 @@
{-# OPTIONS_GHC -fno-warn-orphans #-}
-module Logs.NumCopies where
+module Logs.NumCopies (
+ module Types.NumCopies,
+ setGlobalNumCopies,
+ getGlobalNumCopies,
+ globalNumCopiesLoad,
+ getFileNumCopies,
+ numCopiesCheck,
+ getNumCopies,
+ deprecatedNumCopies,
+) where
import Common.Annex
import qualified Annex
+import Types.NumCopies
import Logs
import Logs.SingleValue
+import Logs.Trust
+import Annex.CheckAttr
+import qualified Remote
-instance Serializable Int where
- serialize = show
- deserialize = readish
+instance SingleValueSerializable NumCopies where
+ serialize (NumCopies n) = show n
+ deserialize = NumCopies <$$> readish
-setGlobalNumCopies :: Int -> Annex ()
+setGlobalNumCopies :: NumCopies -> Annex ()
setGlobalNumCopies = setLog numcopiesLog
{- Cached for speed. -}
-getGlobalNumCopies :: Annex (Maybe Int)
-getGlobalNumCopies = maybe numCopiesLoad (return . Just)
+getGlobalNumCopies :: Annex (Maybe NumCopies)
+getGlobalNumCopies = maybe globalNumCopiesLoad (return . Just)
=<< Annex.getState Annex.globalnumcopies
-numCopiesLoad :: Annex (Maybe Int)
-numCopiesLoad = do
+globalNumCopiesLoad :: Annex (Maybe NumCopies)
+globalNumCopiesLoad = do
v <- getLog numcopiesLog
Annex.changeState $ \s -> s { Annex.globalnumcopies = v }
return v
+
+{- 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