summaryrefslogtreecommitdiff
path: root/Logs/NumCopies.hs
blob: 2fd6f75f8cdf097383c58266a4ffefc8d1fd8abd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{- 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 #-}

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 SingleValueSerializable NumCopies where
	serialize (NumCopies n) = show n
	deserialize = NumCopies <$$> readish

setGlobalNumCopies :: NumCopies -> Annex ()
setGlobalNumCopies = setLog numcopiesLog

{- Cached for speed. -}
getGlobalNumCopies :: Annex (Maybe NumCopies)
getGlobalNumCopies = maybe globalNumCopiesLoad (return . Just)
	=<< Annex.getState Annex.globalnumcopies

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