git-annex/Backend.hs

101 lines
3.1 KiB
Haskell
Raw Normal View History

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.
-
- 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 (
lookupBackend,
storeFile,
dropFile,
2010-10-12 20:52:01 +00:00
retrieveFile,
fileKey,
fileBackend
2010-10-11 21:52:46 +00:00
) where
2010-10-10 17:47:04 +00:00
import System.Directory
import System.FilePath
2010-10-12 20:39:10 +00:00
import Data.String.Utils
import System.Posix.Files
import BackendList
import Locations
2010-10-10 19:04:18 +00:00
import GitRepo
import Utility
2010-10-12 20:06:10 +00:00
import Types
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. -}
storeFile :: State -> FilePath -> IO (Maybe (Key, Backend))
storeFile state file = storeFile' (backends state) state file
storeFile' [] _ _ = return Nothing
storeFile' (b:bs) state file = do
2010-10-12 20:06:10 +00:00
try <- (getKey b) state (gitRelative (repo state) 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-12 20:06:10 +00:00
stored <- (storeFileKey b) state file key
2010-10-10 19:21:17 +00:00
if (not stored)
then nextbackend
else do
2010-10-13 00:04:36 +00:00
return $ Just (key, b)
2010-10-10 19:21:17 +00:00
where
nextbackend = storeFile' bs state file
2010-10-10 19:04:18 +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. -}
retrieveFile :: State -> Key -> FilePath -> IO Bool
retrieveFile state key dest = do
result <- lookupBackend state key
2010-10-10 19:04:18 +00:00
case (result) of
Nothing -> return False
Just backend -> (retrieveKeyFile backend) state key dest
2010-10-10 23:53:31 +00:00
{- Drops a key from the backend that has it. -}
dropFile :: State -> Key -> IO (Maybe (Key, Backend))
dropFile state key = do
result <- lookupBackend state key
2010-10-10 23:53:31 +00:00
case (result) of
Nothing -> return Nothing
Just backend -> do
(removeKey backend) state key
return $ Just (key, backend)
2010-10-10 23:53:31 +00:00
{- Looks up the backend that has a key. -}
lookupBackend :: State -> Key -> IO (Maybe Backend)
lookupBackend state key = lookupBackend' (backends state) state key
lookupBackend' [] _ _ = return Nothing
lookupBackend' (b:bs) state key = do
present <- checkBackend b state key
2010-10-10 17:47:04 +00:00
if present
then
return $ Just b
else
lookupBackend' bs state key
2010-10-10 17:47:04 +00:00
{- Checks if a key is available via a given backend. -}
checkBackend :: Backend -> State -> Key -> IO (Bool)
checkBackend backend state key =
doesFileExist $ annexLocation state backend key
2010-10-12 20:39:10 +00:00
{- Looks up the key corresponding to an annexed file,
- by examining what the file symlinks to. -}
fileKey :: FilePath -> IO Key
fileKey file = do
l <- readSymbolicLink (file)
return $ Key $ takeFileName $ l
{- Looks up the backend corresponding to an annexed file,
- by examining what the file symlinks to. -}
fileBackend :: FilePath -> IO Backend
fileBackend file = do
l <- readSymbolicLink (file)
return $ lookupBackendName $ takeFileName $ parentDir $ l