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-19 05:19:56 +00:00
|
|
|
import System.Cmd.Utils
|
2010-10-13 20:21:50 +00:00
|
|
|
import Control.Exception
|
2010-10-19 17:39:53 +00:00
|
|
|
import List
|
|
|
|
import Maybe
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2010-10-18 06:06:27 +00:00
|
|
|
import TypeInternals
|
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-17 15:47:36 +00:00
|
|
|
import qualified Backend
|
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-17 15:47:36 +00:00
|
|
|
removeKey = checkRemoveKey,
|
2010-10-14 19:31:44 +00:00
|
|
|
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
|
|
|
|
2010-10-22 19:06:14 +00:00
|
|
|
{- Just check if the .git/annex/ file for the key exists.
|
|
|
|
-
|
|
|
|
- But, if running against a remote annex, need to use ssh to do it. -}
|
2010-10-14 19:31:44 +00:00
|
|
|
checkKeyFile :: Key -> Annex Bool
|
2010-10-22 19:06:14 +00:00
|
|
|
checkKeyFile k = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
if (not $ Git.repoIsUrl g)
|
|
|
|
then inAnnex k
|
|
|
|
else do
|
|
|
|
showNote ("checking " ++ Git.repoDescribe g ++ "...")
|
|
|
|
liftIO $ boolSystem "ssh" [Git.urlHost g,
|
|
|
|
"test -e " ++ (shellEscape $ annexLocation g 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-23 00:47:14 +00:00
|
|
|
if (null remotes)
|
2010-10-19 17:39:53 +00:00
|
|
|
then do
|
2010-10-19 18:13:48 +00:00
|
|
|
showNote "not available"
|
2010-10-19 17:39:53 +00:00
|
|
|
showLocations key
|
|
|
|
return False
|
2010-10-17 17:13:49 +00:00
|
|
|
else trycopy remotes remotes
|
2010-10-13 19:55:18 +00:00
|
|
|
where
|
2010-10-17 17:13:49 +00:00
|
|
|
trycopy full [] = do
|
2010-10-19 18:13:48 +00:00
|
|
|
showNote "not available"
|
|
|
|
showTriedRemotes full
|
|
|
|
showLocations key
|
2010-10-17 17:13:49 +00:00
|
|
|
return False
|
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
|
2010-10-19 18:13:48 +00:00
|
|
|
Left err -> trycopy full rs
|
|
|
|
Right r' -> do
|
2010-10-22 17:21:43 +00:00
|
|
|
showNote $ "copying from " ++ (Git.repoDescribe r) ++ "..."
|
2010-10-19 05:46:07 +00:00
|
|
|
liftIO $ copyFromRemote r' key file
|
2010-10-13 19:55:18 +00:00
|
|
|
|
2010-10-19 05:46:07 +00:00
|
|
|
{- Tries to copy a file from a remote. -}
|
|
|
|
copyFromRemote :: Git.Repo -> Key -> FilePath -> IO Bool
|
2010-10-13 19:55:18 +00:00
|
|
|
copyFromRemote r key file = do
|
2010-10-22 18:28:47 +00:00
|
|
|
if (not $ Git.repoIsUrl r)
|
2010-10-14 02:59:43 +00:00
|
|
|
then getlocal
|
2010-10-22 17:21:43 +00:00
|
|
|
else if (Git.repoIsSsh r)
|
|
|
|
then getssh
|
|
|
|
else error "copying from non-ssh repo not supported"
|
2010-10-13 20:21:50 +00:00
|
|
|
where
|
2010-10-22 17:21:43 +00:00
|
|
|
getlocal = boolSystem "cp" ["-a", location, file]
|
|
|
|
getssh = do
|
|
|
|
liftIO $ putStrLn "" -- make way for scp progress bar
|
2010-10-22 19:06:14 +00:00
|
|
|
-- TODO double-shell-quote path for scp
|
|
|
|
boolSystem "scp" [sshlocation, file]
|
|
|
|
location = annexLocation r key
|
|
|
|
sshlocation = (Git.urlHost r) ++ ":" ++ location
|
2010-10-19 18:13:48 +00:00
|
|
|
|
2010-10-17 15:47:36 +00:00
|
|
|
{- Checks remotes to verify that enough copies of a key exist to allow
|
|
|
|
- for a key to be safely removed (with no data loss), and fails with an
|
|
|
|
- error if not. -}
|
|
|
|
checkRemoveKey :: Key -> Annex (Bool)
|
|
|
|
checkRemoveKey key = do
|
2010-10-21 20:30:16 +00:00
|
|
|
force <- Annex.flagIsSet "force"
|
2010-10-17 15:47:36 +00:00
|
|
|
if (force)
|
|
|
|
then return True
|
|
|
|
else do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
remotes <- Remotes.withKey key
|
2010-10-19 18:13:48 +00:00
|
|
|
let numcopies = read $ Git.configGet g config "1"
|
2010-10-17 15:47:36 +00:00
|
|
|
if (numcopies > length remotes)
|
2010-10-19 18:13:48 +00:00
|
|
|
then notEnoughCopies numcopies (length remotes) []
|
|
|
|
else findcopies numcopies 0 remotes []
|
2010-10-17 15:47:36 +00:00
|
|
|
where
|
2010-10-17 22:52:09 +00:00
|
|
|
config = "annex.numcopies"
|
2010-10-19 18:13:48 +00:00
|
|
|
findcopies need have [] bad =
|
|
|
|
if (have >= need)
|
|
|
|
then return True
|
|
|
|
else notEnoughCopies need have bad
|
|
|
|
findcopies need have (r:rs) bad = do
|
2010-10-17 15:47:36 +00:00
|
|
|
all <- Annex.supportedBackends
|
|
|
|
result <- liftIO $ ((try $ remoteHasKey r all)::IO (Either SomeException Bool))
|
|
|
|
case (result) of
|
2010-10-19 18:13:48 +00:00
|
|
|
Right True -> findcopies need (have+1) rs bad
|
|
|
|
Right False -> findcopies need have rs bad
|
|
|
|
Left _ -> findcopies need have rs (r:bad)
|
2010-10-22 17:21:43 +00:00
|
|
|
remoteHasKey remote all = do
|
2010-10-17 15:47:36 +00:00
|
|
|
-- To check if a remote has a key, construct a new
|
|
|
|
-- Annex monad and query its backend.
|
2010-10-22 17:21:43 +00:00
|
|
|
a <- Annex.new remote all
|
2010-10-17 15:47:36 +00:00
|
|
|
(result, _) <- Annex.run a (Backend.hasKey key)
|
|
|
|
return result
|
2010-10-19 18:13:48 +00:00
|
|
|
notEnoughCopies need have bad = do
|
2010-10-17 17:13:49 +00:00
|
|
|
unsafe
|
2010-10-19 17:39:53 +00:00
|
|
|
showLongNote $
|
|
|
|
"Could only verify the existence of " ++
|
2010-10-19 18:13:48 +00:00
|
|
|
(show have) ++ " out of " ++ (show need) ++
|
2010-10-19 17:39:53 +00:00
|
|
|
" necessary copies"
|
2010-10-23 00:47:14 +00:00
|
|
|
if (not $ null bad) then showTriedRemotes bad else return ()
|
2010-10-19 17:39:53 +00:00
|
|
|
showLocations key
|
|
|
|
hint
|
2010-10-17 17:13:49 +00:00
|
|
|
return False
|
2010-10-19 17:39:53 +00:00
|
|
|
unsafe = showNote "unsafe"
|
|
|
|
hint = showLongNote $ "(Use --force to override this check, or adjust annex.numcopies.)"
|
2010-10-22 19:56:57 +00:00
|
|
|
|
|
|
|
showLocations :: Key -> Annex ()
|
|
|
|
showLocations key = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
u <- getUUID g
|
|
|
|
uuids <- liftIO $ keyLocations g key
|
|
|
|
let uuidsf = filter (\v -> v /= u) uuids
|
|
|
|
ppuuids <- prettyPrintUUIDs uuidsf
|
2010-10-23 00:47:14 +00:00
|
|
|
if (null uuidsf)
|
|
|
|
then showLongNote $ "No other repository is known to contain the file."
|
|
|
|
else showLongNote $ "Try making some of these repositories available:\n" ++ ppuuids
|
2010-10-22 19:56:57 +00:00
|
|
|
|
|
|
|
showTriedRemotes remotes =
|
|
|
|
showLongNote $ "I was unable to access these remotes: " ++
|
|
|
|
(Remotes.list remotes)
|