2010-10-10 17:47:04 +00:00
|
|
|
{- git-annex key/value storage backends
|
|
|
|
-
|
|
|
|
- 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
|
|
|
|
- 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.
|
|
|
|
-
|
|
|
|
- The mapping from filename to its key is stored in the .git-annex directory,
|
|
|
|
- in a file named `$filename.$backend`
|
|
|
|
-
|
|
|
|
- Multiple pluggable backends are supported, and more than one can be used
|
|
|
|
- to store different files' contents in a given repository.
|
|
|
|
- -}
|
|
|
|
|
2010-10-11 21:52:46 +00:00
|
|
|
module Backend (
|
|
|
|
Key,
|
|
|
|
Backend, -- note only data type is exported, not destructors
|
|
|
|
lookupBackend,
|
|
|
|
storeFile,
|
|
|
|
dropFile
|
|
|
|
) where
|
2010-10-10 17:47:04 +00:00
|
|
|
|
|
|
|
import System.Directory
|
2010-10-10 19:54:02 +00:00
|
|
|
import Locations
|
2010-10-10 19:04:18 +00:00
|
|
|
import GitRepo
|
|
|
|
import Utility
|
2010-10-11 21:52:46 +00:00
|
|
|
import BackendType
|
2010-10-10 19:04:18 +00:00
|
|
|
|
2010-10-10 17:47:04 +00:00
|
|
|
{- Name of state file that holds the key for an annexed file,
|
|
|
|
- using a given backend. -}
|
|
|
|
backendFile :: Backend -> GitRepo -> FilePath -> String
|
|
|
|
backendFile backend repo file = gitStateDir repo ++
|
|
|
|
(gitRelative repo file) ++ "." ++ (name backend)
|
|
|
|
|
2010-10-10 19:21:17 +00:00
|
|
|
{- Attempts to store a file in one of the backends, and returns
|
|
|
|
- its key. -}
|
|
|
|
storeFile :: [Backend] -> GitRepo -> FilePath -> IO (Maybe Key)
|
|
|
|
storeFile [] _ _ = return Nothing
|
2010-10-10 19:04:18 +00:00
|
|
|
storeFile (b:bs) repo file = do
|
2010-10-10 19:41:35 +00:00
|
|
|
try <- (getKey b) repo (gitRelative repo file)
|
2010-10-10 19:04:18 +00:00
|
|
|
case (try) of
|
2010-10-10 19:21:17 +00:00
|
|
|
Nothing -> nextbackend
|
2010-10-10 19:04:18 +00:00
|
|
|
Just key -> do
|
2010-10-10 19:41:35 +00:00
|
|
|
stored <- (storeFileKey b) repo file key
|
2010-10-10 19:21:17 +00:00
|
|
|
if (not stored)
|
|
|
|
then nextbackend
|
|
|
|
else do
|
|
|
|
bookkeeping key
|
|
|
|
return $ Just key
|
|
|
|
where
|
|
|
|
nextbackend = storeFile bs repo file
|
|
|
|
backendfile = backendFile b repo file
|
|
|
|
bookkeeping key = do
|
2010-10-10 19:04:18 +00:00
|
|
|
createDirectoryIfMissing True (parentDir backendfile)
|
|
|
|
writeFile backendfile key
|
|
|
|
|
2010-10-10 19:21:17 +00:00
|
|
|
{- Attempts to retrieve an file from one of the backends, saving it to
|
2010-10-10 19:04:18 +00:00
|
|
|
- a specified location. -}
|
2010-10-10 23:53:31 +00:00
|
|
|
retrieveFile :: [Backend] -> GitRepo -> FilePath -> FilePath -> IO Bool
|
2010-10-10 19:04:18 +00:00
|
|
|
retrieveFile backends repo file dest = do
|
|
|
|
result <- lookupBackend backends repo file
|
|
|
|
case (result) of
|
|
|
|
Nothing -> return False
|
2010-10-10 23:53:31 +00:00
|
|
|
Just b -> do
|
|
|
|
key <- lookupKey b repo file
|
|
|
|
(retrieveKeyFile b) key dest
|
|
|
|
|
|
|
|
{- Drops the key for a file from the backend that has it. -}
|
|
|
|
dropFile :: [Backend] -> GitRepo -> FilePath -> IO (Maybe Key)
|
|
|
|
dropFile backends repo file = do
|
|
|
|
result <- lookupBackend backends repo file
|
|
|
|
case (result) of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just b -> do
|
|
|
|
key <- lookupKey b repo file
|
|
|
|
(removeKey b) key
|
2010-10-11 01:04:25 +00:00
|
|
|
removeFile $ backendFile b repo file
|
2010-10-10 23:53:31 +00:00
|
|
|
return $ Just key
|
|
|
|
|
|
|
|
{- Looks up the key a backend uses for an already annexed file. -}
|
|
|
|
lookupKey :: Backend -> GitRepo -> FilePath -> IO Key
|
|
|
|
lookupKey backend repo file = readFile (backendFile backend repo file)
|
2010-10-10 19:04:18 +00:00
|
|
|
|
2010-10-10 17:47:04 +00:00
|
|
|
{- Looks up the backend used for an already annexed file. -}
|
|
|
|
lookupBackend :: [Backend] -> GitRepo -> FilePath -> IO (Maybe Backend)
|
2010-10-10 19:04:18 +00:00
|
|
|
lookupBackend [] _ _ = return Nothing
|
2010-10-10 17:47:04 +00:00
|
|
|
lookupBackend (b:bs) repo file = do
|
|
|
|
present <- checkBackend b repo file
|
|
|
|
if present
|
|
|
|
then
|
|
|
|
return $ Just b
|
|
|
|
else
|
|
|
|
lookupBackend bs repo file
|
|
|
|
|
|
|
|
{- Checks if a file is available via a given backend. -}
|
|
|
|
checkBackend :: Backend -> GitRepo -> FilePath -> IO (Bool)
|
|
|
|
checkBackend backend repo file = doesFileExist $ backendFile backend repo file
|