2010-10-16 20:15:31 +00:00
|
|
|
{- git-annex key-value storage backends
|
2010-10-10 17:47:04 +00:00
|
|
|
-
|
2010-10-16 20:15:31 +00:00
|
|
|
- git-annex uses a key-value abstraction layer to allow files contents to be
|
|
|
|
- stored in different ways. In theory, any key-value storage system could be
|
2010-10-10 17:47:04 +00:00
|
|
|
- used to store the file contents, and git-annex would then retrieve them
|
|
|
|
- as needed and put them in `.git/annex/`.
|
|
|
|
-
|
|
|
|
- When a file is annexed, a key is generated from its content and/or metadata.
|
|
|
|
- This key can later be used to retrieve the file's content (its value). This
|
|
|
|
- key generation must be stable for a given file content, name, and size.
|
|
|
|
-
|
|
|
|
- Multiple pluggable backends are supported, and more than one can be used
|
|
|
|
- to store different files' contents in a given repository.
|
2010-10-27 20:53:54 +00:00
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
2010-10-10 17:47:04 +00:00
|
|
|
|
2010-10-11 21:52:46 +00:00
|
|
|
module Backend (
|
2010-10-21 20:30:16 +00:00
|
|
|
list,
|
2010-10-14 18:14:19 +00:00
|
|
|
storeFileKey,
|
|
|
|
retrieveKeyFile,
|
2010-10-14 21:37:20 +00:00
|
|
|
removeKey,
|
|
|
|
hasKey,
|
2010-11-13 18:59:27 +00:00
|
|
|
fsckKey,
|
2011-03-23 21:57:10 +00:00
|
|
|
upgradableKey,
|
2010-11-01 21:50:37 +00:00
|
|
|
lookupFile,
|
2010-11-15 22:04:19 +00:00
|
|
|
chooseBackends,
|
2011-03-02 17:47:45 +00:00
|
|
|
keyBackend,
|
2011-03-16 05:23:20 +00:00
|
|
|
lookupBackendName,
|
|
|
|
maybeLookupBackendName
|
2010-10-11 21:52:46 +00:00
|
|
|
) where
|
2010-10-10 17:47:04 +00:00
|
|
|
|
2010-10-14 01:28:47 +00:00
|
|
|
import Control.Monad.State
|
2010-11-22 21:51:55 +00:00
|
|
|
import System.IO.Error (try)
|
2010-10-13 04:42:46 +00:00
|
|
|
import System.FilePath
|
|
|
|
import System.Posix.Files
|
2011-03-23 06:10:59 +00:00
|
|
|
import System.Directory
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2010-10-10 19:54:02 +00:00
|
|
|
import Locations
|
2010-10-14 06:36:41 +00:00
|
|
|
import qualified GitRepo as Git
|
2010-10-14 07:18:11 +00:00
|
|
|
import qualified Annex
|
|
|
|
import Types
|
2011-03-16 01:34:13 +00:00
|
|
|
import Key
|
2011-03-16 02:04:50 +00:00
|
|
|
import qualified BackendClass as B
|
2010-11-08 19:15:21 +00:00
|
|
|
import Messages
|
2011-03-23 06:10:59 +00:00
|
|
|
import Content
|
|
|
|
import DataUnits
|
2010-10-14 19:58:53 +00:00
|
|
|
|
|
|
|
{- List of backends in the order to try them when storing a new key. -}
|
2011-01-26 01:02:34 +00:00
|
|
|
list :: Annex [Backend Annex]
|
2010-10-21 20:30:16 +00:00
|
|
|
list = do
|
2011-01-26 01:49:04 +00:00
|
|
|
l <- Annex.getState Annex.backends -- list is cached here
|
2010-11-22 19:46:57 +00:00
|
|
|
if not $ null l
|
2010-10-14 19:58:53 +00:00
|
|
|
then return l
|
|
|
|
else do
|
2011-01-26 04:17:38 +00:00
|
|
|
s <- getstandard
|
|
|
|
d <- Annex.getState Annex.defaultbackend
|
|
|
|
handle d s
|
|
|
|
where
|
|
|
|
parseBackendList l [] = l
|
|
|
|
parseBackendList bs s = map (lookupBackendName bs) $ words s
|
|
|
|
handle Nothing s = return s
|
|
|
|
handle (Just "") s = return s
|
|
|
|
handle (Just name) s = do
|
2011-01-26 01:49:04 +00:00
|
|
|
bs <- Annex.getState Annex.supportedBackends
|
2011-01-26 04:17:38 +00:00
|
|
|
let l' = (lookupBackendName bs name):s
|
|
|
|
Annex.changeState $ \state -> state { Annex.backends = l' }
|
2010-10-21 20:30:16 +00:00
|
|
|
return l'
|
2011-01-26 04:17:38 +00:00
|
|
|
getstandard = do
|
|
|
|
bs <- Annex.getState Annex.supportedBackends
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
return $ parseBackendList bs $
|
|
|
|
Git.configGet g "annex.backends" ""
|
2010-10-17 15:47:36 +00:00
|
|
|
|
2010-10-31 22:04:34 +00:00
|
|
|
{- Looks up a backend in a list. May fail if unknown. -}
|
2011-01-26 01:02:34 +00:00
|
|
|
lookupBackendName :: [Backend Annex] -> String -> Backend Annex
|
2010-10-31 18:39:53 +00:00
|
|
|
lookupBackendName bs s =
|
2010-10-31 22:04:34 +00:00
|
|
|
case maybeLookupBackendName bs s of
|
|
|
|
Just b -> b
|
|
|
|
Nothing -> error $ "unknown backend " ++ s
|
2011-01-26 01:02:34 +00:00
|
|
|
maybeLookupBackendName :: [Backend Annex] -> String -> Maybe (Backend Annex)
|
2010-10-31 22:04:34 +00:00
|
|
|
maybeLookupBackendName bs s =
|
2010-11-22 19:46:57 +00:00
|
|
|
if 1 /= length matches
|
2010-10-31 22:04:34 +00:00
|
|
|
then Nothing
|
2010-11-06 21:07:11 +00:00
|
|
|
else Just $ head matches
|
2011-01-26 04:37:50 +00:00
|
|
|
where matches = filter (\b -> s == B.name b) bs
|
2010-10-10 19:04:18 +00:00
|
|
|
|
2010-10-13 00:04:36 +00:00
|
|
|
{- Attempts to store a file in one of the backends. -}
|
2011-01-26 01:02:34 +00:00
|
|
|
storeFileKey :: FilePath -> Maybe (Backend Annex) -> Annex (Maybe (Key, Backend Annex))
|
2010-11-01 21:50:37 +00:00
|
|
|
storeFileKey file trybackend = do
|
|
|
|
bs <- list
|
|
|
|
let bs' = case trybackend of
|
|
|
|
Nothing -> bs
|
|
|
|
Just backend -> backend:bs
|
2010-11-17 17:55:38 +00:00
|
|
|
storeFileKey' bs' file
|
2011-01-26 01:02:34 +00:00
|
|
|
storeFileKey' :: [Backend Annex] -> FilePath -> Annex (Maybe (Key, Backend Annex))
|
2010-11-17 17:55:38 +00:00
|
|
|
storeFileKey' [] _ = return Nothing
|
|
|
|
storeFileKey' (b:bs) file = do
|
2011-01-26 04:37:50 +00:00
|
|
|
result <- (B.getKey b) file
|
2010-11-06 21:07:11 +00:00
|
|
|
case result of
|
2010-10-10 19:21:17 +00:00
|
|
|
Nothing -> nextbackend
|
2010-10-10 19:04:18 +00:00
|
|
|
Just key -> do
|
2011-01-26 04:37:50 +00:00
|
|
|
stored <- (B.storeFileKey b) file key
|
2010-10-10 19:21:17 +00:00
|
|
|
if (not stored)
|
|
|
|
then nextbackend
|
2010-11-06 21:07:11 +00:00
|
|
|
else return $ Just (key, b)
|
2010-10-10 19:21:17 +00:00
|
|
|
where
|
2010-11-17 17:55:38 +00:00
|
|
|
nextbackend = storeFileKey' bs file
|
2010-10-10 19:04:18 +00:00
|
|
|
|
2010-10-13 06:31:24 +00:00
|
|
|
{- Attempts to retrieve an key from one of the backends, saving it to
|
2010-10-10 19:04:18 +00:00
|
|
|
- a specified location. -}
|
2011-01-26 01:02:34 +00:00
|
|
|
retrieveKeyFile :: Backend Annex -> Key -> FilePath -> Annex Bool
|
2011-01-26 04:37:50 +00:00
|
|
|
retrieveKeyFile backend key dest = (B.retrieveKeyFile backend) key dest
|
2010-10-10 17:47:04 +00:00
|
|
|
|
2010-10-14 18:14:19 +00:00
|
|
|
{- Removes a key from a backend. -}
|
2011-01-26 01:02:34 +00:00
|
|
|
removeKey :: Backend Annex -> Key -> Maybe Int -> Annex Bool
|
2011-01-26 04:37:50 +00:00
|
|
|
removeKey backend key numcopies = (B.removeKey backend) key numcopies
|
2010-10-12 20:39:10 +00:00
|
|
|
|
2010-11-07 22:22:25 +00:00
|
|
|
{- Checks if a key is present in its backend. -}
|
2010-10-14 21:37:20 +00:00
|
|
|
hasKey :: Key -> Annex Bool
|
2010-10-17 15:47:36 +00:00
|
|
|
hasKey key = do
|
2010-11-15 22:04:19 +00:00
|
|
|
backend <- keyBackend key
|
2011-01-26 04:37:50 +00:00
|
|
|
(B.hasKey backend) key
|
2010-10-14 21:37:20 +00:00
|
|
|
|
2011-03-23 06:10:59 +00:00
|
|
|
{- Checks a key for problems. -}
|
2011-01-27 00:37:46 +00:00
|
|
|
fsckKey :: Backend Annex -> Key -> Maybe FilePath -> Maybe Int -> Annex Bool
|
2011-03-23 06:10:59 +00:00
|
|
|
fsckKey backend key file numcopies = do
|
|
|
|
size_ok <- checkKeySize key
|
|
|
|
backend_ok <-(B.fsckKey backend) key file numcopies
|
|
|
|
return $ size_ok && backend_ok
|
2010-11-13 18:59:27 +00:00
|
|
|
|
2011-03-23 21:57:10 +00:00
|
|
|
{- Checks if a key is upgradable to a newer representation. -}
|
|
|
|
upgradableKey :: Backend Annex -> Key -> Annex Bool
|
|
|
|
upgradableKey backend key = (B.upgradableKey backend) key
|
|
|
|
|
2010-10-13 07:20:05 +00:00
|
|
|
{- Looks up the key and backend corresponding to an annexed file,
|
2010-10-13 04:42:46 +00:00
|
|
|
- by examining what the file symlinks to. -}
|
2011-01-26 01:02:34 +00:00
|
|
|
lookupFile :: FilePath -> Annex (Maybe (Key, Backend Annex))
|
2010-10-13 07:20:05 +00:00
|
|
|
lookupFile file = do
|
2011-01-26 01:49:04 +00:00
|
|
|
bs <- Annex.getState Annex.supportedBackends
|
2010-10-31 22:04:34 +00:00
|
|
|
tl <- liftIO $ try getsymlink
|
|
|
|
case tl of
|
2010-10-31 18:39:53 +00:00
|
|
|
Left _ -> return Nothing
|
2010-10-31 22:04:34 +00:00
|
|
|
Right l -> makekey bs l
|
|
|
|
where
|
|
|
|
getsymlink = do
|
2010-10-13 07:20:05 +00:00
|
|
|
l <- readSymbolicLink file
|
2010-10-31 22:04:34 +00:00
|
|
|
return $ takeFileName l
|
2011-03-16 01:34:13 +00:00
|
|
|
makekey bs l =
|
|
|
|
case fileKey l of
|
|
|
|
Just k -> makeret k l bs
|
|
|
|
Nothing -> return Nothing
|
|
|
|
makeret k l bs =
|
2010-11-06 21:07:11 +00:00
|
|
|
case maybeLookupBackendName bs bname of
|
2011-03-16 01:34:13 +00:00
|
|
|
Just backend -> return $ Just (k, backend)
|
|
|
|
Nothing -> do
|
|
|
|
when (isLinkToAnnex l) $
|
|
|
|
warning skip
|
|
|
|
return Nothing
|
2010-10-15 00:05:04 +00:00
|
|
|
where
|
2011-03-16 01:34:13 +00:00
|
|
|
bname = keyBackendName k
|
2010-10-31 22:04:34 +00:00
|
|
|
skip = "skipping " ++ file ++
|
|
|
|
" (unknown backend " ++ bname ++ ")"
|
2010-11-01 21:50:37 +00:00
|
|
|
|
|
|
|
{- Looks up the backends that should be used for each file in a list.
|
|
|
|
- That can be configured on a per-file basis in the gitattributes file.
|
|
|
|
-}
|
2011-01-26 01:02:34 +00:00
|
|
|
chooseBackends :: [FilePath] -> Annex [(FilePath, Maybe (Backend Annex))]
|
2010-11-01 21:50:37 +00:00
|
|
|
chooseBackends fs = do
|
2010-11-01 23:05:38 +00:00
|
|
|
g <- Annex.gitRepo
|
2011-01-26 01:49:04 +00:00
|
|
|
bs <- Annex.getState Annex.supportedBackends
|
2010-11-28 22:58:03 +00:00
|
|
|
pairs <- liftIO $ Git.checkAttr g "annex.backend" fs
|
2010-11-01 23:05:38 +00:00
|
|
|
return $ map (\(f,b) -> (f, maybeLookupBackendName bs b)) pairs
|
2010-11-15 22:04:19 +00:00
|
|
|
|
|
|
|
{- Returns the backend to use for a key. -}
|
2011-01-26 01:02:34 +00:00
|
|
|
keyBackend :: Key -> Annex (Backend Annex)
|
2010-11-15 22:04:19 +00:00
|
|
|
keyBackend key = do
|
2011-01-26 01:49:04 +00:00
|
|
|
bs <- Annex.getState Annex.supportedBackends
|
2011-03-16 01:34:13 +00:00
|
|
|
return $ lookupBackendName bs $ keyBackendName key
|
2011-03-23 06:10:59 +00:00
|
|
|
|
|
|
|
{- The size of the data for a key is checked against the size encoded in
|
|
|
|
- the key's metadata, if available. -}
|
|
|
|
checkKeySize :: Key -> Annex Bool
|
|
|
|
checkKeySize key = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
let file = gitAnnexLocation g key
|
|
|
|
present <- liftIO $ doesFileExist file
|
|
|
|
case (present, keySize key) of
|
|
|
|
(_, Nothing) -> return True
|
|
|
|
(False, _) -> return True
|
|
|
|
(True, Just size) -> do
|
|
|
|
stat <- liftIO $ getFileStatus file
|
|
|
|
let size' = fromIntegral (fileSize stat)
|
|
|
|
if size == size'
|
|
|
|
then return True
|
|
|
|
else do
|
|
|
|
dest <- moveBad key
|
2011-03-23 06:42:14 +00:00
|
|
|
warning $ "Bad file size (" ++
|
2011-03-26 18:37:39 +00:00
|
|
|
compareSizes storageUnits True size size' ++
|
2011-03-23 06:42:14 +00:00
|
|
|
"); moved to " ++ dest
|
2011-03-23 06:10:59 +00:00
|
|
|
return False
|