git-annex/BackendFile.hs

79 lines
2.4 KiB
Haskell
Raw Normal View History

2010-10-10 17:47:04 +00:00
{- git-annex "file" backend
- -}
module BackendFile (backend) where
2010-10-13 20:21:50 +00:00
import System.IO
import System.Cmd
import Control.Exception
2010-10-12 20:06:10 +00:00
import Types
import LocationLog
import Locations
import Remotes
import GitRepo
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-12 20:06:10 +00:00
keyValue :: State -> FilePath -> IO (Maybe Key)
keyValue state 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-12 20:06:10 +00:00
dummyStore :: State -> FilePath -> Key -> IO (Bool)
dummyStore state file key = return True
2010-10-12 20:10:15 +00:00
dummyRemove :: State -> Key -> IO Bool
dummyRemove state 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. -}
copyKeyFile :: State -> Key -> FilePath -> IO (Bool)
copyKeyFile state key file = do
remotes <- remotesWithKey state key
if (0 == length remotes)
then error $ "no known remotes have: " ++ (keyFile key) ++ "\n" ++
"(Perhaps you need to git remote add a repository?)"
else trycopy remotes remotes
where
trycopy full [] = error $ "unable to get: " ++ (keyFile key) ++ "\n" ++
"To get that file, need access to one of these remotes: " ++
(remotesList full)
trycopy full (r:rs) = do
2010-10-13 20:21:50 +00:00
result <- try (copyFromRemote r key file)::IO (Either SomeException ())
case (result) of
Left err -> do
2010-10-13 20:32:16 +00:00
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. -}
copyFromRemote :: GitRepo -> Key -> FilePath -> IO ()
copyFromRemote r key file = do
2010-10-13 20:32:16 +00:00
putStrLn $ "copy from " ++ (gitRepoDescribe r ) ++ " " ++ file
-- annexLocation needs the git config read for the remote first.
-- FIXME: Having this here means git-config is run repeatedly when
-- copying a series of files; need to use state monad to avoid
-- this.
r' <- gitConfigRead r
_ <- if (gitRepoIsLocal r')
then getlocal r'
else getremote r'
2010-10-13 20:21:50 +00:00
return ()
where
2010-10-13 20:32:16 +00:00
getlocal r = do
rawSystem "cp" ["-a", location r, file]
getremote r = do
2010-10-13 20:21:50 +00:00
error "get via network not yet implemented!"
2010-10-13 20:32:16 +00:00
location r = annexLocation r backend key