git-annex/Backend/File.hs

223 lines
7.1 KiB
Haskell
Raw Normal View History

2010-10-15 16:42:36 -04: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 16:53:54 -04:00
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
2010-10-15 16:42:36 -04:00
-}
2010-10-10 13:47:04 -04:00
module Backend.File (backend, checkKey) where
2010-10-10 13:47:04 -04:00
import Control.Monad.State (liftIO)
2011-01-26 17:44:40 -04:00
import Data.List
import Data.String.Utils
2010-10-16 16:20:49 -04:00
import Types.Backend
import LocationLog
import qualified Remote
2010-10-14 02:36:41 -04:00
import qualified GitRepo as Git
import Content
2010-10-14 17:37:20 -04:00
import qualified Annex
import Types
2010-10-14 17:37:20 -04:00
import UUID
2010-11-08 15:15:21 -04:00
import Messages
2011-01-26 17:44:40 -04:00
import Trust
import Types.Key
2010-10-10 13:47:04 -04:00
backend :: Backend Annex
2010-10-10 13:47:04 -04:00
backend = Backend {
2010-10-15 19:32:56 -04:00
name = mustProvide,
getKey = mustProvide,
2010-10-10 15:41:35 -04:00
storeFileKey = dummyStore,
retrieveKeyFile = copyKeyFile,
removeKey = checkRemoveKey,
2010-11-22 17:51:55 -04:00
hasKey = inAnnex,
fsckKey = checkKeyOnly,
upgradableKey = checkUpgradableKey
2010-10-10 13:47:04 -04:00
}
2010-10-31 16:00:32 -04:00
mustProvide :: a
2010-10-15 19:32:56 -04:00
mustProvide = error "must provide this field"
2010-10-15 16:42:36 -04:00
{- Storing a key is a no-op. -}
2010-11-22 17:51:55 -04:00
dummyStore :: FilePath -> Key -> Annex Bool
2010-10-31 16:00:32 -04:00
dummyStore _ _ = return True
2010-10-14 14:14:19 -04:00
{- Try to find a copy of the file in one of the remotes,
- and copy it to here. -}
2010-11-22 17:51:55 -04:00
copyKeyFile :: Key -> FilePath -> Annex Bool
2010-10-13 21:28:47 -04:00
copyKeyFile key file = do
(remotes, _) <- Remote.keyPossibilities key
2010-11-22 17:51:55 -04:00
if null remotes
2010-10-19 13:39:53 -04:00
then do
showNote "not available"
showLocations key []
2010-10-19 13:39:53 -04:00
return False
2010-10-17 13:13:49 -04:00
else trycopy remotes remotes
where
2010-10-17 13:13:49 -04:00
trycopy full [] = do
showTriedRemotes full
showLocations key []
2010-10-17 13:13:49 -04:00
return False
trycopy full (r:rs) = do
probablythere <- probablyPresent r
2010-11-22 17:51:55 -04:00
if probablythere
then docopy r (trycopy full rs)
2010-10-23 14:14:36 -04:00
else trycopy full rs
2010-11-22 17:51:55 -04:00
-- This check is to avoid an ugly message if a remote is a
-- drive that is not mounted.
2010-11-22 17:51:55 -04:00
probablyPresent r =
if Remote.hasKeyCheap r
then do
res <- Remote.hasKey r key
case res of
Right b -> return b
Left _ -> return False
else return True
docopy r continue = do
showNote $ "from " ++ Remote.name r ++ "..."
copied <- Remote.retrieveKeyFile 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) <- Remote.keyPossibilities key
2011-01-26 19:35:35 -04:00
untrusteduuids <- trustGet UnTrusted
let tocheck = Remote.remotesWithoutUUID remotes (trusteduuids++untrusteduuids)
numcopies <- getNumCopies numcopiesM
2011-01-26 19:35:35 -04:00
findcopies numcopies trusteduuids tocheck []
where
2010-11-22 17:51:55 -04:00
findcopies need have [] bad
| length have >= need = return True
| otherwise = notEnoughCopies need have bad
2010-11-22 17:51:55 -04:00
findcopies need have (r:rs) bad
| length have >= need = return True
| otherwise = do
let u = Remote.uuid r
2011-01-30 12:01:56 -04:00
let dup = u `elem` have
haskey <- Remote.hasKey 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 13:13:49 -04:00
unsafe
2010-10-19 13:39:53 -04:00
showLongNote $
"Could only verify the existence of " ++
show (length have) ++ " out of " ++ show need ++
2010-10-19 13:39:53 -04:00
" necessary copies"
2010-10-28 12:40:05 -04:00
showTriedRemotes bad
showLocations key have
2010-10-19 13:39:53 -04:00
hint
2010-10-17 13:13:49 -04:00
return False
2010-10-19 13:39:53 -04:00
unsafe = showNote "unsafe"
2010-11-22 17:51:55 -04:00
hint = showLongNote "(Use --force to override this check, or adjust annex.numcopies.)"
2010-10-22 15:56:57 -04:00
showLocations :: Key -> [UUID] -> Annex ()
showLocations key exclude = do
2010-10-22 15:56:57 -04:00
g <- Annex.gitRepo
u <- getUUID g
uuids <- liftIO $ keyLocations g key
2011-01-26 19:35:35 -04:00
untrusteduuids <- trustGet UnTrusted
let uuidswanted = filteruuids uuids (u:exclude++untrusteduuids)
let uuidsskipped = filteruuids uuids (u:exclude++uuidswanted)
ppuuidswanted <- prettyPrintUUIDs uuidswanted
ppuuidsskipped <- prettyPrintUUIDs uuidsskipped
showLongNote $ message ppuuidswanted ppuuidsskipped
where
2011-01-30 12:01:56 -04:00
filteruuids list x = filter (`notElem` x) list
2011-01-26 19:35:35 -04:00
message [] [] = "No other repository is known to contain the file."
message rs [] = "Try making some of these repositories available:\n" ++ rs
message [] us = "Also these untrusted repositories may contain the file:\n" ++ us
message rs us = message rs [] ++ message [] us
2010-10-31 16:00:32 -04:00
showTriedRemotes :: [Remote.Remote Annex] -> Annex ()
2010-10-28 12:40:05 -04:00
showTriedRemotes [] = return ()
2010-10-22 15:56:57 -04:00
showTriedRemotes remotes =
2011-01-05 20:28:50 -04:00
showLongNote $ "Unable to access these remotes: " ++
(join ", " $ map Remote.name remotes)
2011-06-01 16:49:17 -04:00
{- If a value is specified, it is used; otherwise the default is looked up
- in git config. forcenumcopies overrides everything. -}
getNumCopies :: Maybe Int -> Annex Int
2011-06-01 16:49:17 -04:00
getNumCopies v =
Annex.getState Annex.forcenumcopies >>= maybe (use v) (return . id)
where
2011-06-01 16:49:17 -04:00
use (Just n) = return n
use Nothing = do
g <- Annex.gitRepo
return $ read $ Git.configGet g config "1"
config = "annex.numcopies"
{- Ideally, all keys have file size metadata. Old keys may not. -}
checkUpgradableKey :: Key -> Annex Bool
checkUpgradableKey key
| keySize key == Nothing = return True
| otherwise = return False
{- This is used to check that numcopies is satisfied for the key on fsck.
2011-01-26 17:44:40 -04: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 FilePath -> Maybe Int -> Annex Bool
checkKey a key file numcopies = do
a_ok <- a key
copies_ok <- checkKeyNumCopies key file numcopies
return $ a_ok && copies_ok
checkKeyOnly :: Key -> Maybe FilePath -> Maybe Int -> Annex Bool
checkKeyOnly = checkKey (\_ -> return True)
checkKeyNumCopies :: Key -> Maybe FilePath -> Maybe Int -> Annex Bool
checkKeyNumCopies key file numcopies = do
needed <- getNumCopies numcopies
g <- Annex.gitRepo
locations <- liftIO $ keyLocations g key
2011-01-26 17:44:40 -04:00
untrusted <- trustGet UnTrusted
let untrustedlocations = intersect untrusted locations
2011-01-30 12:01:56 -04:00
let safelocations = filter (`notElem` untrusted) locations
2011-01-26 17:44:40 -04:00
let present = length safelocations
2010-11-22 17:51:55 -04:00
if present < needed
then do
2011-01-26 17:44:40 -04:00
ppuuids <- prettyPrintUUIDs untrustedlocations
warning $ missingNote (filename file key) present needed ppuuids
return False
else return True
2010-11-13 15:24:36 -04:00
where
filename Nothing k = show k
filename (Just f) _ = f
2011-01-26 17:44:40 -04:00
missingNote :: String -> Int -> Int -> String -> String
missingNote file 0 _ [] =
"** No known copies of " ++ file ++ " exist!"
missingNote file 0 _ untrusted =
"Only these untrusted locations may have copies of " ++ file ++
2011-01-26 17:44:40 -04:00
"\n" ++ untrusted ++
"Back it up to trusted locations with git-annex copy."
missingNote file present needed [] =
2011-01-26 17:44:40 -04:00
"Only " ++ show present ++ " of " ++ show needed ++
" trustworthy copies of " ++ file ++ " exist." ++
2011-01-26 17:44:40 -04:00
"\nBack it up with git-annex copy."
missingNote file present needed untrusted =
missingNote file present needed [] ++
"\nThe following untrusted locations may also have copies: " ++
2011-01-26 17:47:02 -04:00
"\n" ++ untrusted