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.
|
|
|
|
-
|
2010-11-13 18:59:27 +00:00
|
|
|
- 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
|
|
|
|
2010-11-13 18:59:27 +00:00
|
|
|
module Backend.File (backend, checkKey) where
|
2010-10-10 17:47:04 +00:00
|
|
|
|
2011-01-26 21:44:40 +00:00
|
|
|
import Data.List
|
2011-03-27 19:56:43 +00:00
|
|
|
import Data.String.Utils
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Backend
|
2010-10-13 19:55:18 +00:00
|
|
|
import LocationLog
|
2011-03-27 19:56:43 +00:00
|
|
|
import qualified Remote
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-01-16 20:05:05 +00:00
|
|
|
import Content
|
2010-10-14 21:37:20 +00:00
|
|
|
import qualified Annex
|
2011-01-26 01:49:04 +00:00
|
|
|
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
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Key
|
2010-10-10 17:47:04 +00:00
|
|
|
|
2011-01-26 01:02:34 +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,
|
2010-10-13 19:55:18 +00:00
|
|
|
retrieveKeyFile = copyKeyFile,
|
2010-10-17 15:47:36 +00:00
|
|
|
removeKey = checkRemoveKey,
|
2010-11-22 21:51:55 +00:00
|
|
|
hasKey = inAnnex,
|
2011-03-23 21:57:10 +00:00
|
|
|
fsckKey = checkKeyOnly,
|
|
|
|
upgradableKey = checkUpgradableKey
|
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
|
|
|
|
2010-10-13 19:55:18 +00:00
|
|
|
{- Try to find a copy of the file in one of the remotes,
|
2011-03-27 19:56:43 +00:00
|
|
|
- and copy it to here. -}
|
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
|
2011-06-23 17:39:04 +00:00
|
|
|
remotes <- Remote.keyPossibilities key
|
2010-11-22 21:51:55 +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-12-29 20:41:27 +00:00
|
|
|
showLocations key []
|
2010-10-19 17:39:53 +00:00
|
|
|
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
|
|
|
showTriedRemotes full
|
2010-12-29 20:41:27 +00:00
|
|
|
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-26 01:06:31 +00:00
|
|
|
probablythere <- probablyPresent r
|
2010-11-22 21:51:55 +00:00
|
|
|
if probablythere
|
2010-12-31 23:09:17 +00:00
|
|
|
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
|
2011-03-27 19:56:43 +00:00
|
|
|
-- drive that is not mounted.
|
2010-11-22 21:51:55 +00:00
|
|
|
probablyPresent r =
|
2011-03-27 21:12:32 +00:00
|
|
|
if Remote.hasKeyCheap r
|
2011-03-27 19:56:43 +00:00
|
|
|
then do
|
2011-03-27 21:12:32 +00:00
|
|
|
res <- Remote.hasKey r key
|
2011-03-27 19:56:43 +00:00
|
|
|
case res of
|
|
|
|
Right b -> return b
|
|
|
|
Left _ -> return False
|
2010-10-26 01:06:31 +00:00
|
|
|
else return True
|
2010-12-31 23:09:17 +00:00
|
|
|
docopy r continue = do
|
2011-06-09 22:54:49 +00:00
|
|
|
showNote $ "from " ++ Remote.name r ++ "..."
|
2011-03-27 21:12:32 +00:00
|
|
|
copied <- Remote.retrieveKeyFile r key file
|
2010-12-31 23:09:17 +00:00
|
|
|
if copied
|
|
|
|
then return True
|
|
|
|
else continue
|
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. -}
|
2010-11-28 19:28:20 +00:00
|
|
|
checkRemoveKey :: Key -> Maybe Int -> Annex Bool
|
|
|
|
checkRemoveKey key numcopiesM = do
|
2011-01-26 04:17:38 +00:00
|
|
|
force <- Annex.getState Annex.force
|
2010-11-28 19:28:20 +00:00
|
|
|
if force || numcopiesM == Just 0
|
2010-10-17 15:47:36 +00:00
|
|
|
then return True
|
|
|
|
else do
|
2011-06-23 17:39:04 +00:00
|
|
|
(remotes, trusteduuids) <- Remote.keyPossibilitiesTrusted key
|
2011-01-26 23:35:35 +00:00
|
|
|
untrusteduuids <- trustGet UnTrusted
|
2011-03-27 19:56:43 +00:00
|
|
|
let tocheck = Remote.remotesWithoutUUID remotes (trusteduuids++untrusteduuids)
|
2010-11-28 19:28:20 +00:00
|
|
|
numcopies <- getNumCopies numcopiesM
|
2011-01-26 23:35:35 +00:00
|
|
|
findcopies numcopies trusteduuids tocheck []
|
2010-10-17 15:47:36 +00:00
|
|
|
where
|
2010-11-22 21:51:55 +00:00
|
|
|
findcopies need have [] bad
|
2010-12-29 20:31:25 +00:00
|
|
|
| length have >= need = return True
|
2010-12-29 20:41:27 +00:00
|
|
|
| otherwise = notEnoughCopies need have bad
|
2010-11-22 21:51:55 +00:00
|
|
|
findcopies need have (r:rs) bad
|
2010-12-29 20:31:25 +00:00
|
|
|
| length have >= need = return True
|
|
|
|
| otherwise = do
|
2011-03-27 21:12:32 +00:00
|
|
|
let u = Remote.uuid r
|
2011-01-30 16:01:56 +00:00
|
|
|
let dup = u `elem` have
|
2011-03-27 21:12:32 +00:00
|
|
|
haskey <- Remote.hasKey r key
|
2010-12-29 20:41:27 +00:00
|
|
|
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 " ++
|
2010-12-29 20:41:27 +00:00
|
|
|
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
|
2010-12-29 20:41:27 +00:00
|
|
|
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
|
|
|
|
2010-12-29 20:41:27 +00:00
|
|
|
showLocations :: Key -> [UUID] -> Annex ()
|
|
|
|
showLocations key exclude = do
|
2010-10-22 19:56:57 +00:00
|
|
|
g <- Annex.gitRepo
|
|
|
|
u <- getUUID g
|
2011-06-22 20:13:43 +00:00
|
|
|
uuids <- keyLocations key
|
2011-01-26 23:35:35 +00:00
|
|
|
untrusteduuids <- trustGet UnTrusted
|
|
|
|
let uuidswanted = filteruuids uuids (u:exclude++untrusteduuids)
|
|
|
|
let uuidsskipped = filteruuids uuids (u:exclude++uuidswanted)
|
2011-07-01 19:24:07 +00:00
|
|
|
ppuuidswanted <- Remote.prettyPrintUUIDs uuidswanted
|
|
|
|
ppuuidsskipped <- Remote.prettyPrintUUIDs uuidsskipped
|
2011-01-26 23:35:35 +00:00
|
|
|
showLongNote $ message ppuuidswanted ppuuidsskipped
|
|
|
|
where
|
2011-01-30 16:01:56 +00:00
|
|
|
filteruuids list x = filter (`notElem` x) list
|
2011-01-26 23:35:35 +00: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 20:00:32 +00:00
|
|
|
|
2011-03-27 21:12:32 +00:00
|
|
|
showTriedRemotes :: [Remote.Remote Annex] -> 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: " ++
|
2011-03-27 21:12:32 +00:00
|
|
|
(join ", " $ map Remote.name remotes)
|
2010-11-13 18:59:27 +00:00
|
|
|
|
2011-06-01 20:49:17 +00:00
|
|
|
{- If a value is specified, it is used; otherwise the default is looked up
|
|
|
|
- in git config. forcenumcopies overrides everything. -}
|
2010-11-28 19:28:20 +00:00
|
|
|
getNumCopies :: Maybe Int -> Annex Int
|
2011-06-01 20:49:17 +00:00
|
|
|
getNumCopies v =
|
|
|
|
Annex.getState Annex.forcenumcopies >>= maybe (use v) (return . id)
|
2010-11-13 18:59:27 +00:00
|
|
|
where
|
2011-06-01 20:49:17 +00:00
|
|
|
use (Just n) = return n
|
|
|
|
use Nothing = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
return $ read $ Git.configGet g config "1"
|
2010-11-13 18:59:27 +00:00
|
|
|
config = "annex.numcopies"
|
|
|
|
|
2011-03-23 21:57:10 +00:00
|
|
|
{- 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
|
|
|
|
|
2010-11-13 18:59:27 +00:00
|
|
|
{- 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.
|
2010-11-13 18:59:27 +00:00
|
|
|
-
|
|
|
|
- The passed action is first run to allow backends deriving this one
|
|
|
|
- to do their own checks.
|
|
|
|
-}
|
2011-01-27 00:37:46 +00:00
|
|
|
checkKey :: (Key -> Annex Bool) -> Key -> Maybe FilePath -> Maybe Int -> Annex Bool
|
|
|
|
checkKey a key file numcopies = do
|
2010-11-13 18:59:27 +00:00
|
|
|
a_ok <- a key
|
2011-01-27 00:37:46 +00:00
|
|
|
copies_ok <- checkKeyNumCopies key file numcopies
|
2010-11-13 18:59:27 +00:00
|
|
|
return $ a_ok && copies_ok
|
|
|
|
|
2011-03-23 06:10:59 +00:00
|
|
|
checkKeyOnly :: Key -> Maybe FilePath -> Maybe Int -> Annex Bool
|
|
|
|
checkKeyOnly = checkKey (\_ -> return True)
|
|
|
|
|
2011-01-27 00:37:46 +00:00
|
|
|
checkKeyNumCopies :: Key -> Maybe FilePath -> Maybe Int -> Annex Bool
|
|
|
|
checkKeyNumCopies key file numcopies = do
|
2010-11-28 19:28:20 +00:00
|
|
|
needed <- getNumCopies numcopies
|
2011-06-22 20:13:43 +00:00
|
|
|
locations <- keyLocations key
|
2011-01-26 21:44:40 +00:00
|
|
|
untrusted <- trustGet UnTrusted
|
|
|
|
let untrustedlocations = intersect untrusted locations
|
2011-01-30 16:01:56 +00:00
|
|
|
let safelocations = filter (`notElem` untrusted) locations
|
2011-01-26 21:44:40 +00:00
|
|
|
let present = length safelocations
|
2010-11-22 21:51:55 +00:00
|
|
|
if present < needed
|
2010-11-13 18:59:27 +00:00
|
|
|
then do
|
2011-07-01 19:24:07 +00:00
|
|
|
ppuuids <- Remote.prettyPrintUUIDs untrustedlocations
|
2011-01-27 00:37:46 +00:00
|
|
|
warning $ missingNote (filename file key) present needed ppuuids
|
2010-11-13 18:59:27 +00:00
|
|
|
return False
|
|
|
|
else return True
|
2010-11-13 19:24:36 +00:00
|
|
|
where
|
2011-01-27 00:37:46 +00:00
|
|
|
filename Nothing k = show k
|
|
|
|
filename (Just f) _ = f
|
2011-01-26 21:44:40 +00:00
|
|
|
|
2011-01-27 00:08:37 +00:00
|
|
|
missingNote :: String -> Int -> Int -> String -> String
|
|
|
|
missingNote file 0 _ [] =
|
2011-06-23 23:56:24 +00:00
|
|
|
"** No known copies exist of " ++ file
|
2011-01-27 00:08:37 +00:00
|
|
|
missingNote file 0 _ untrusted =
|
2011-03-12 19:30:17 +00:00
|
|
|
"Only these untrusted locations may have copies of " ++ file ++
|
2011-01-26 21:44:40 +00:00
|
|
|
"\n" ++ untrusted ++
|
|
|
|
"Back it up to trusted locations with git-annex copy."
|
2011-01-27 00:08:37 +00:00
|
|
|
missingNote file present needed [] =
|
2011-01-26 21:44:40 +00:00
|
|
|
"Only " ++ show present ++ " of " ++ show needed ++
|
2011-06-23 23:56:24 +00:00
|
|
|
" trustworthy copies exist of " ++ file ++
|
2011-01-26 21:44:40 +00:00
|
|
|
"\nBack it up with git-annex copy."
|
2011-01-27 00:08:37 +00:00
|
|
|
missingNote file present needed untrusted =
|
|
|
|
missingNote file present needed [] ++
|
2011-01-27 00:37:46 +00:00
|
|
|
"\nThe following untrusted locations may also have copies: " ++
|
2011-01-26 21:47:02 +00:00
|
|
|
"\n" ++ untrusted
|