git-annex/BackendFile.hs

73 lines
2.2 KiB
Haskell
Raw Normal View History

2010-10-10 17:47:04 +00:00
{- git-annex "file" backend
- -}
module BackendFile (backend) where
2010-10-14 01:28:47 +00:00
import Control.Monad.State
2010-10-13 20:21:50 +00:00
import System.IO
import System.Cmd
import Control.Exception
2010-10-14 06:52:17 +00:00
import BackendTypes
import LocationLog
import Locations
2010-10-14 06:41:54 +00:00
import qualified Remotes
2010-10-14 06:36:41 +00:00
import qualified GitRepo as Git
2010-10-10 17:47:04 +00:00
backend = Backend {
name = "file",
2010-10-10 19:04:18 +00:00
getKey = keyValue,
2010-10-10 19:41:35 +00:00
storeFileKey = dummyStore,
retrieveKeyFile = copyKeyFile,
2010-10-10 23:53:31 +00:00
removeKey = dummyRemove
2010-10-10 17:47:04 +00:00
}
-- direct mapping from filename to key
2010-10-14 01:28:47 +00:00
keyValue :: FilePath -> Annex (Maybe Key)
keyValue file = return $ Just $ Key file
2010-10-10 19:04:18 +00:00
{- This backend does not really do any independant data storage,
- it relies on the file contents in .git/annex/ in this repo,
2010-10-10 23:53:31 +00:00
- and other accessible repos. So storing or removing a key is
- a no-op. TODO until support is added for git annex --push otherrepo,
- then these could implement that.. -}
2010-10-14 01:28:47 +00:00
dummyStore :: FilePath -> Key -> Annex (Bool)
dummyStore file key = return True
dummyRemove :: Key -> Annex Bool
dummyRemove url = return False
2010-10-10 19:04:18 +00:00
{- Try to find a copy of the file in one of the remotes,
- and copy it over to this one. -}
2010-10-14 01:28:47 +00:00
copyKeyFile :: Key -> FilePath -> Annex (Bool)
copyKeyFile key file = do
2010-10-14 06:41:54 +00:00
remotes <- Remotes.withKey key
2010-10-14 03:18:58 +00:00
trycopy remotes remotes
where
trycopy full [] = error $ "unable to get: " ++ (keyFile key) ++ "\n" ++
"To get that file, need access to one of these remotes: " ++
2010-10-14 06:41:54 +00:00
(Remotes.list full)
trycopy full (r:rs) = do
2010-10-14 02:59:43 +00:00
-- annexLocation needs the git config to have been
-- read for a remote, so do that now,
-- if it hasn't been already
2010-10-14 06:41:54 +00:00
r' <- Remotes.ensureGitConfigRead r
2010-10-14 02:59:43 +00:00
result <- liftIO $ (try (copyFromRemote r' key file)::IO (Either SomeException ()))
2010-10-13 20:21:50 +00:00
case (result) of
Left err -> do
2010-10-14 02:59:43 +00:00
liftIO $ hPutStrLn stderr (show err)
2010-10-13 20:21:50 +00:00
trycopy full rs
Right succ -> return True
2010-10-13 20:21:50 +00:00
{- Tries to copy a file from a remote, exception on error. -}
2010-10-14 06:36:41 +00:00
copyFromRemote :: Git.Repo -> Key -> FilePath -> IO ()
copyFromRemote r key file = do
2010-10-14 06:36:41 +00:00
putStrLn $ "copy from " ++ (Git.repoDescribe r ) ++ " " ++ file
2010-10-13 20:32:16 +00:00
2010-10-14 06:36:41 +00:00
if (Git.repoIsLocal r)
2010-10-14 02:59:43 +00:00
then getlocal
else getremote
2010-10-13 20:21:50 +00:00
return ()
where
2010-10-14 02:59:43 +00:00
getlocal = rawSystem "cp" ["-a", location, file]
getremote = error "get via network not yet implemented!"
location = annexLocation r backend key