git-annex/Backend.hs

122 lines
3.8 KiB
Haskell
Raw Permalink Normal View History

{- git-annex key/value backends
2010-10-27 20:53:54 +00:00
-
- Copyright 2010-2024 Joey Hess <id@joeyh.name>
2010-10-27 20:53:54 +00:00
-
- Licensed under the GNU AGPL version 3 or higher.
2010-10-27 20:53:54 +00:00
-}
2010-10-10 17:47:04 +00:00
{-# LANGUAGE OverloadedStrings #-}
2010-10-11 21:52:46 +00:00
module Backend (
builtinList,
defaultBackend,
defaultHashBackend,
genKey,
getBackend,
chooseBackend,
lookupBackendVariety,
lookupBuiltinBackendVariety,
maybeLookupBackendVariety,
isStableKey,
isCryptographicallySecureKey,
isCryptographicallySecure,
2010-10-11 21:52:46 +00:00
) where
2010-10-10 17:47:04 +00:00
import qualified Data.Map as M
import Annex.Common
2010-10-14 07:18:11 +00:00
import qualified Annex
import Annex.CheckAttr
import Types.Key
import Types.KeySource
import qualified Types.Backend as B
import Utility.Metered
import Backend.Variety
import qualified Backend.VURL
2014-07-27 16:24:12 +00:00
2023-03-14 02:39:16 +00:00
{- Built-in backends. Does not include externals. -}
builtinList :: [Backend]
builtinList = regularBackendList ++ Backend.VURL.backends
2010-10-14 19:58:53 +00:00
{- Backend to use by default when generating a new key. Takes git config
- and --backend option into account. -}
defaultBackend :: Annex Backend
defaultBackend = maybe cache return =<< Annex.getState Annex.backend
2012-10-29 02:09:09 +00:00
where
cache = do
n <- maybe (annexBackend <$> Annex.getGitConfig) (return . Just)
=<< Annex.getRead Annex.forcebackend
b <- case n of
Just name | valid name -> lookupname name
_ -> pure defaultHashBackend
Annex.changeState $ \s -> s { Annex.backend = Just b }
return b
valid name = not (null name)
lookupname = lookupBackendVariety . parseKeyVariety . encodeBS
{- Generates a key for a file. -}
genKey :: KeySource -> MeterUpdate -> Backend -> Annex (Key, Backend)
genKey source meterupdate b = case B.genKey b of
Just a -> do
k <- a source meterupdate
return (k, b)
Nothing -> giveup $ "Cannot generate a key for backend " ++
decodeBS (formatKeyVariety (B.backendVariety b))
getBackend :: FilePath -> Key -> Annex (Maybe Backend)
getBackend file k = maybeLookupBackendVariety (fromKey keyVariety k) >>= \case
Just backend -> return $ Just backend
Nothing -> do
warning $ "skipping " <> QuotedPath (toRawFilePath file) <> " (" <>
UnquotedString (unknownBackendVarietyMessage (fromKey keyVariety k)) <> ")"
return Nothing
unknownBackendVarietyMessage :: KeyVariety -> String
unknownBackendVarietyMessage v =
"unknown backend " ++ decodeBS (formatKeyVariety v)
{- Looks up the backend that should be used for a file.
- That can be configured on a per-file basis in the gitattributes file,
- or forced with --backend. -}
chooseBackend :: RawFilePath -> Annex Backend
chooseBackend f = Annex.getRead Annex.forcebackend >>= go
2012-10-29 02:09:09 +00:00
where
go Nothing = do
mb <- maybeLookupBackendVariety . parseKeyVariety . encodeBS
=<< checkAttr "annex.backend" f
case mb of
Just b -> return b
Nothing -> defaultBackend
go (Just _) = defaultBackend
{- Looks up a backend by variety. May fail if unsupported or disabled. -}
lookupBackendVariety :: KeyVariety -> Annex Backend
lookupBackendVariety v = fromMaybe (giveup (unknownBackendVarietyMessage v))
<$> maybeLookupBackendVariety v
lookupBuiltinBackendVariety :: KeyVariety -> Backend
lookupBuiltinBackendVariety v = fromMaybe (giveup (unknownBackendVarietyMessage v)) $
maybeLookupBuiltinBackendVariety v
maybeLookupBuiltinBackendVariety :: KeyVariety -> Maybe Backend
maybeLookupBuiltinBackendVariety v = M.lookup v varietyMap
2014-07-27 16:24:12 +00:00
maybeLookupBackendVariety :: KeyVariety -> Annex (Maybe Backend)
maybeLookupBackendVariety v = maybeLookupBackendVarietyMap v varietyMap
varietyMap :: M.Map KeyVariety Backend
varietyMap = makeVarietyMap builtinList
isStableKey :: Key -> Annex Bool
isStableKey k = maybe False (`B.isStableKey` k)
<$> maybeLookupBackendVariety (fromKey keyVariety k)
isCryptographicallySecureKey :: Key -> Annex Bool
isCryptographicallySecureKey k = maybe
(pure False)
(\b -> B.isCryptographicallySecureKey b k)
=<< maybeLookupBackendVariety (fromKey keyVariety k)
isCryptographicallySecure :: Backend -> Bool
isCryptographicallySecure = B.isCryptographicallySecure