2010-10-15 20:42:36 +00:00
|
|
|
{- git-annex pseudo-backend
|
|
|
|
-
|
|
|
|
- This backend does not really do any independant data storage,
|
|
|
|
- it relies on the file contents in .git/annex/ in this repo,
|
|
|
|
- and other accessible repos.
|
|
|
|
-
|
|
|
|
- This is an abstract backend; getKey has to be implemented to complete
|
|
|
|
- it.
|
|
|
|
-}
|
2010-10-10 17:47:04 +00:00
|
|
|
|
2010-10-14 07:50:28 +00:00
|
|
|
module Backend.File (backend) where
|
2010-10-10 17:47:04 +00:00
|
|
|
|
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
|
2010-10-14 17:17:43 +00:00
|
|
|
import System.Exit
|
2010-10-13 20:21:50 +00:00
|
|
|
import Control.Exception
|
2010-10-14 06:52:17 +00:00
|
|
|
import BackendTypes
|
2010-10-13 19:55:18 +00:00
|
|
|
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-14 19:31:44 +00:00
|
|
|
import Utility
|
2010-10-14 20:13:43 +00:00
|
|
|
import Core
|
2010-10-14 21:37:20 +00:00
|
|
|
import qualified Annex
|
|
|
|
import UUID
|
2010-10-10 17:47:04 +00:00
|
|
|
|
|
|
|
backend = Backend {
|
2010-10-15 23:32:56 +00:00
|
|
|
name = mustProvide,
|
|
|
|
getKey = mustProvide,
|
2010-10-10 19:41:35 +00:00
|
|
|
storeFileKey = dummyStore,
|
2010-10-13 19:55:18 +00:00
|
|
|
retrieveKeyFile = copyKeyFile,
|
2010-10-14 19:31:44 +00:00
|
|
|
removeKey = dummyRemove,
|
|
|
|
hasKey = checkKeyFile
|
2010-10-10 17:47:04 +00:00
|
|
|
}
|
|
|
|
|
2010-10-15 23:32:56 +00:00
|
|
|
mustProvide = error "must provide this field"
|
|
|
|
|
2010-10-15 20:42:36 +00:00
|
|
|
{- Storing a key is a no-op. -}
|
2010-10-14 01:28:47 +00:00
|
|
|
dummyStore :: FilePath -> Key -> Annex (Bool)
|
|
|
|
dummyStore file key = return True
|
2010-10-14 18:14:19 +00:00
|
|
|
|
|
|
|
{- Allow keys to be removed. -}
|
2010-10-14 01:28:47 +00:00
|
|
|
dummyRemove :: Key -> Annex Bool
|
2010-10-14 18:14:19 +00:00
|
|
|
dummyRemove url = return True
|
2010-10-10 19:04:18 +00:00
|
|
|
|
2010-10-14 19:31:44 +00:00
|
|
|
{- Just check if the .git/annex/ file for the key exists. -}
|
|
|
|
checkKeyFile :: Key -> Annex Bool
|
2010-10-14 23:36:11 +00:00
|
|
|
checkKeyFile k = inAnnex k
|
2010-10-14 19:31:44 +00:00
|
|
|
|
2010-10-13 19:55:18 +00:00
|
|
|
{- Try to find a copy of the file in one of the remotes,
|
2010-10-10 19:54:02 +00:00
|
|
|
- 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 21:37:20 +00:00
|
|
|
if (0 == length remotes)
|
|
|
|
then cantfind
|
|
|
|
else return ()
|
2010-10-14 03:18:58 +00:00
|
|
|
trycopy remotes remotes
|
2010-10-13 19:55:18 +00:00
|
|
|
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)
|
2010-10-13 19:55:18 +00:00
|
|
|
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 17:11:42 +00:00
|
|
|
result <- Remotes.tryGitConfigRead r
|
|
|
|
case (result) of
|
|
|
|
Nothing -> trycopy full rs
|
|
|
|
Just r' -> do
|
|
|
|
result <- liftIO $ (try (copyFromRemote r' key file)::IO (Either SomeException ()))
|
|
|
|
case (result) of
|
|
|
|
Left err -> do
|
|
|
|
liftIO $ hPutStrLn stderr (show err)
|
|
|
|
trycopy full rs
|
|
|
|
Right succ -> return True
|
2010-10-14 21:37:20 +00:00
|
|
|
cantfind = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
uuids <- liftIO $ keyLocations g key
|
2010-10-15 23:32:56 +00:00
|
|
|
ppuuids <- prettyPrintUUIDs uuids
|
2010-10-14 21:37:20 +00:00
|
|
|
error $ "no available git remotes have: " ++
|
2010-10-15 23:32:56 +00:00
|
|
|
(keyFile key) ++
|
|
|
|
if (0 < length uuids)
|
|
|
|
then "\nIt has been seen before in these repositories:\n" ++ ppuuids
|
|
|
|
else ""
|
2010-10-13 19:55:18 +00:00
|
|
|
|
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 ()
|
2010-10-13 19:55:18 +00:00
|
|
|
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
|
|
|
where
|
2010-10-14 17:11:42 +00:00
|
|
|
getlocal = do
|
2010-10-14 17:17:43 +00:00
|
|
|
res <-rawSystem "cp" ["-a", location, file]
|
|
|
|
if (res == ExitSuccess)
|
|
|
|
then return ()
|
|
|
|
else error "cp failed"
|
2010-10-14 02:59:43 +00:00
|
|
|
getremote = error "get via network not yet implemented!"
|
2010-10-14 23:36:11 +00:00
|
|
|
location = annexLocation r key
|