git-annex/Backend.hs

79 lines
2.4 KiB
Haskell
Raw Normal View History

2010-10-10 13:47:04 -04: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 17:52:46 -04:00
module Backend (
storeFile,
dropFile,
2010-10-12 16:52:01 -04:00
retrieveFile,
2010-10-13 03:20:05 -04:00
lookupFile
2010-10-11 17:52:46 -04:00
) where
2010-10-10 13:47:04 -04:00
2010-10-13 21:28:47 -04:00
import Control.Monad.State
2010-10-13 03:20:05 -04:00
import Control.Exception
2010-10-10 13:47:04 -04:00
import System.Directory
import System.FilePath
2010-10-12 16:39:10 -04:00
import Data.String.Utils
import System.Posix.Files
import BackendList
import Locations
2010-10-10 15:04:18 -04:00
import GitRepo
import Utility
2010-10-12 16:06:10 -04:00
import Types
2010-10-10 15:04:18 -04:00
2010-10-12 20:04:36 -04:00
{- Attempts to store a file in one of the backends. -}
2010-10-13 21:28:47 -04:00
storeFile :: FilePath -> Annex (Maybe (Key, Backend))
storeFile file = do
g <- gitAnnex
let relfile = gitRelative g file
b <- backendsAnnex
storeFile' b file relfile
storeFile' [] _ _ = return Nothing
2010-10-13 21:28:47 -04:00
storeFile' (b:bs) file relfile = do
try <- (getKey b) relfile
2010-10-10 15:04:18 -04:00
case (try) of
2010-10-10 15:21:17 -04:00
Nothing -> nextbackend
2010-10-10 15:04:18 -04:00
Just key -> do
2010-10-13 21:28:47 -04:00
stored <- (storeFileKey b) file key
2010-10-10 15:21:17 -04:00
if (not stored)
then nextbackend
else do
2010-10-12 20:04:36 -04:00
return $ Just (key, b)
2010-10-10 15:21:17 -04:00
where
2010-10-13 21:28:47 -04:00
nextbackend = storeFile' bs file relfile
2010-10-10 15:04:18 -04:00
{- Attempts to retrieve an key from one of the backends, saving it to
2010-10-10 15:04:18 -04:00
- a specified location. -}
2010-10-13 21:28:47 -04:00
retrieveFile :: Backend -> Key -> FilePath -> Annex Bool
retrieveFile backend key dest = (retrieveKeyFile backend) key dest
2010-10-10 13:47:04 -04:00
2010-10-13 03:20:05 -04:00
{- Drops a key from a backend. -}
2010-10-13 21:28:47 -04:00
dropFile :: Backend -> Key -> Annex Bool
dropFile backend key = (removeKey backend) key
2010-10-12 16:39:10 -04:00
2010-10-13 03:20:05 -04:00
{- Looks up the key and backend corresponding to an annexed file,
- by examining what the file symlinks to. -}
2010-10-13 03:20:05 -04:00
lookupFile :: FilePath -> IO (Maybe (Key, Backend))
lookupFile file = do
result <- try (lookup)::IO (Either SomeException (Maybe (Key, Backend)))
case (result) of
Left err -> return Nothing
Right succ -> return succ
where
lookup = do
l <- readSymbolicLink file
return $ Just (k l, b l)
2010-10-13 03:41:12 -04:00
k l = fileKey $ takeFileName $ l
2010-10-13 03:20:05 -04:00
b l = lookupBackendName $ takeFileName $ parentDir $ l