git-annex/Backend/File.hs

198 lines
6.1 KiB
Haskell
Raw Normal View History

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; name, getKey and fsckKey have to be implemented
- to complete it.
2010-10-27 20:53:54 +00:00
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
2010-10-15 20:42:36 +00:00
-}
2010-10-10 17:47:04 +00:00
module Backend.File (backend, checkKey) where
2010-10-10 17:47:04 +00:00
2010-10-14 01:28:47 +00:00
import Control.Monad.State
import System.Directory
2011-01-26 21:44:40 +00:00
import Data.List
2010-10-16 20:20:49 +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
import Content
2010-10-14 21:37:20 +00:00
import qualified Annex
import Types
2010-10-14 21:37:20 +00:00
import UUID
2010-11-08 19:15:21 +00:00
import Messages
2011-01-26 21:44:40 +00:00
import Trust
2010-10-10 17:47:04 +00:00
backend :: Backend Annex
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,
retrieveKeyFile = copyKeyFile,
removeKey = checkRemoveKey,
2010-11-22 21:51:55 +00:00
hasKey = inAnnex,
fsckKey = mustProvide
2010-10-10 17:47:04 +00:00
}
2010-10-31 20:00:32 +00:00
mustProvide :: a
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-11-22 21:51:55 +00:00
dummyStore :: FilePath -> Key -> Annex Bool
2010-10-31 20:00:32 +00:00
dummyStore _ _ = return True
2010-10-14 18:14:19 +00:00
{- Try to find a copy of the file in one of the remotes,
- and copy it over to this one. -}
2010-11-22 21:51:55 +00:00
copyKeyFile :: Key -> FilePath -> Annex Bool
2010-10-14 01:28:47 +00:00
copyKeyFile key file = do
(remotes, _) <- Remotes.keyPossibilities key
2010-11-22 21:51:55 +00:00
if null remotes
2010-10-19 17:39:53 +00:00
then do
showNote "not available"
showLocations key []
2010-10-19 17:39:53 +00:00
return False
2010-10-17 17:13:49 +00:00
else trycopy remotes remotes
where
2010-10-17 17:13:49 +00:00
trycopy full [] = do
showTriedRemotes full
showLocations key []
2010-10-17 17:13:49 +00:00
return False
trycopy full (r:rs) = do
probablythere <- probablyPresent r
2010-11-22 21:51:55 +00:00
if probablythere
then docopy r (trycopy full rs)
2010-10-23 18:14:36 +00:00
else trycopy full rs
2010-11-22 21:51:55 +00:00
-- This check is to avoid an ugly message if a remote is a
-- drive that is not mounted. Avoid checking inAnnex for ssh
-- remotes because that is unnecessarily slow, and the
-- locationlog should be trusted. (If the ssh remote is down
-- or really lacks the file, it's ok to show an ugly message
-- before going on to the next remote.)
probablyPresent r =
if not $ Git.repoIsUrl r
then liftIO $ doesFileExist $ annexLocation r key
else return True
docopy r continue = do
showNote $ "copying from " ++ Git.repoDescribe r ++ "..."
copied <- Remotes.copyFromRemote r key file
if copied
then return True
else continue
{- 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 -> Maybe Int -> Annex Bool
checkRemoveKey key numcopiesM = do
force <- Annex.getState Annex.force
if force || numcopiesM == Just 0
then return True
else do
(remotes, trusteduuids) <- Remotes.keyPossibilities key
untrusted <- reposWithoutUUID remotes trusteduuids
numcopies <- getNumCopies numcopiesM
findcopies numcopies trusteduuids untrusted []
where
2010-11-22 21:51:55 +00:00
findcopies need have [] bad
| length have >= need = return True
| otherwise = notEnoughCopies need have bad
2010-11-22 21:51:55 +00:00
findcopies need have (r:rs) bad
| length have >= need = return True
| otherwise = do
u <- getUUID r
let dup = elem u have
haskey <- Remotes.inAnnex r key
case (dup, haskey) of
(False, Right True) -> findcopies need (u:have) rs bad
(False, Left _) -> findcopies need have rs (r:bad)
_ -> findcopies need have rs bad
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 " ++
show (length have) ++ " out of " ++ show need ++
2010-10-19 17:39:53 +00:00
" necessary copies"
2010-10-28 16:40:05 +00:00
showTriedRemotes bad
showLocations key have
2010-10-19 17:39:53 +00:00
hint
2010-10-17 17:13:49 +00:00
return False
2010-10-19 17:39:53 +00:00
unsafe = showNote "unsafe"
2010-11-22 21:51:55 +00:00
hint = showLongNote "(Use --force to override this check, or adjust annex.numcopies.)"
2010-10-22 19:56:57 +00:00
showLocations :: Key -> [UUID] -> Annex ()
showLocations key exclude = do
2010-10-22 19:56:57 +00:00
g <- Annex.gitRepo
u <- getUUID g
uuids <- liftIO $ keyLocations g key
let uuidsf = filter (\l -> l /= u && (not $ elem l exclude)) uuids
2010-10-22 19:56:57 +00:00
ppuuids <- prettyPrintUUIDs uuidsf
2010-11-22 21:51:55 +00:00
if null uuidsf
2010-10-23 00:47:14 +00:00
then showLongNote $ "No other repository is known to contain the file."
else showLongNote $ "Try making some of these repositories available:\n" ++ ppuuids
2010-10-31 20:00:32 +00:00
showTriedRemotes :: [Git.Repo] -> Annex ()
2010-10-28 16:40:05 +00:00
showTriedRemotes [] = return ()
2010-10-22 19:56:57 +00:00
showTriedRemotes remotes =
2011-01-06 00:28:50 +00:00
showLongNote $ "Unable to access these remotes: " ++
2010-11-22 21:51:55 +00:00
Remotes.list remotes
getNumCopies :: Maybe Int -> Annex Int
getNumCopies (Just n) = return n
getNumCopies Nothing = do
g <- Annex.gitRepo
return $ read $ Git.configGet g config "1"
where
config = "annex.numcopies"
{- This is used to check that numcopies is satisfied for the key on fsck.
2011-01-26 21:44:40 +00:00
- This trusts data in the the location log, and so can check all keys, even
- those with data not present in the current annex.
-
- The passed action is first run to allow backends deriving this one
- to do their own checks.
-}
checkKey :: (Key -> Annex Bool) -> Key -> Maybe Int -> Annex Bool
checkKey a key numcopies = do
a_ok <- a key
copies_ok <- checkKeyNumCopies key numcopies
return $ a_ok && copies_ok
checkKeyNumCopies :: Key -> Maybe Int -> Annex Bool
checkKeyNumCopies key numcopies = do
needed <- getNumCopies numcopies
g <- Annex.gitRepo
locations <- liftIO $ keyLocations g key
2011-01-26 21:44:40 +00:00
untrusted <- trustGet UnTrusted
let untrustedlocations = intersect untrusted locations
let safelocations = filter (\l -> not $ l `elem` untrusted) locations
let present = length safelocations
2010-11-22 21:51:55 +00:00
if present < needed
then do
2011-01-26 21:44:40 +00:00
ppuuids <- prettyPrintUUIDs untrustedlocations
2011-01-26 21:47:02 +00:00
showLongNote $ missingNote present needed ppuuids
return False
else return True
2010-11-13 19:24:36 +00:00
where
2011-01-26 21:44:40 +00:00
2011-01-26 21:47:02 +00:00
missingNote :: Int -> Int -> String -> String
missingNote 0 _ [] =
"** No known copies of this file exist!"
missingNote 0 _ untrusted =
2011-01-26 21:44:40 +00:00
"Only these untrusted locations may have copies of this file!" ++
"\n" ++ untrusted ++
"Back it up to trusted locations with git-annex copy."
2011-01-26 21:47:02 +00:00
missingNote present needed [] =
2011-01-26 21:44:40 +00:00
"Only " ++ show present ++ " of " ++ show needed ++
" trustworthy copies of this file exist." ++
"\nBack it up with git-annex copy."
2011-01-26 21:47:02 +00:00
missingNote present needed untrusted = missingNote present needed [] ++
"\nThe following untrusted copies may also exist: " ++
"\n" ++ untrusted