File: EquivilantKeys.hs

package info (click to toggle)
git-annex 10.20250416-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 73,572 kB
  • sloc: haskell: 90,656; javascript: 9,103; sh: 1,469; makefile: 211; perl: 137; ansic: 44
file content (70 lines) | stat: -rw-r--r-- 2,087 bytes parent folder | download | duplicates (3)
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
{- Logs listing keys that are equivalent to a key.
 -
 - Copyright 2024-2025 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

{-# LANGUAGE BangPatterns #-}

module Logs.EquivilantKeys (
	getEquivilantKeys,
	setEquivilantKey,
	updateEquivilantKeys,
	addEquivilantKey,
	generateEquivilantKey,
) where

import Annex.Common
import qualified Annex
import Logs
import Logs.Presence
import qualified Annex.Branch
import qualified Backend.Hash
import Types.KeySource
import Types.Backend
import Types.Remote (Verification(..))
import Utility.Metered

getEquivilantKeys :: Key -> Annex [Key]
getEquivilantKeys key = do
	config <- Annex.getGitConfig
	nub . mapMaybe (deserializeKey' . fromLogInfo)
		<$> presentLogInfo (equivilantKeysLogFile config key)

setEquivilantKey :: Key -> Key -> Annex ()
setEquivilantKey key equivkey = do
	config <- Annex.getGitConfig
	addLog (Annex.Branch.RegardingUUID []) (equivilantKeysLogFile config key)
		InfoPresent (LogInfo (serializeKey' equivkey))

-- This returns Verified when when an equivilant key has been added to the
-- log (or was already in the log). This is to avoid hashing the object
-- again later.
updateEquivilantKeys :: Backend -> OsPath -> Key -> [Key] -> Annex (Maybe Verification)
updateEquivilantKeys b obj key eks = generateEquivilantKey b obj >>= \case
	Nothing -> return Nothing
	Just ek -> do
		unless (ek `elem` eks) $
			setEquivilantKey key ek
		return (Just Verified)

addEquivilantKey :: Backend -> Key -> OsPath -> Annex (Maybe Verification)
addEquivilantKey b key obj = 
	updateEquivilantKeys b obj key
		=<< getEquivilantKeys key

-- The Backend must use a cryptographically secure hash.
generateEquivilantKey :: Backend -> OsPath -> Annex (Maybe Key)
generateEquivilantKey b obj =
	case genKey b of
		Just genkey -> do
			showSideAction (UnquotedString Backend.Hash.descChecksum)
			Just <$> genkey source nullMeterUpdate
		Nothing -> return Nothing
  where
	source = KeySource
		{ keyFilename = mempty -- avoid adding any extension
		, contentLocation = obj
		, inodeCache = Nothing
		}