2010-11-06 21:06:19 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Fsck where
|
|
|
|
|
2011-03-28 20:19:20 +00:00
|
|
|
import Control.Monad (when)
|
2011-03-02 18:30:36 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
2011-07-05 22:31:46 +00:00
|
|
|
import System.Directory
|
|
|
|
import System.Posix.Files
|
2011-03-02 18:30:36 +00:00
|
|
|
|
2010-11-06 21:06:19 +00:00
|
|
|
import Command
|
2011-03-02 18:30:36 +00:00
|
|
|
import qualified Annex
|
2011-07-05 22:31:46 +00:00
|
|
|
import qualified Remote
|
|
|
|
import qualified Types.Backend
|
|
|
|
import qualified Types.Key
|
2011-03-02 18:30:36 +00:00
|
|
|
import UUID
|
2010-11-15 22:22:50 +00:00
|
|
|
import Types
|
|
|
|
import Messages
|
2011-03-02 18:30:36 +00:00
|
|
|
import Content
|
|
|
|
import LocationLog
|
2011-03-28 20:19:20 +00:00
|
|
|
import Locations
|
2011-07-05 22:31:46 +00:00
|
|
|
import Trust
|
2011-07-06 00:36:43 +00:00
|
|
|
import Utility.DataUnits
|
2011-08-22 20:14:12 +00:00
|
|
|
import Utility.Path
|
2011-09-23 22:13:24 +00:00
|
|
|
import Utility.FileMode
|
2011-07-05 22:31:46 +00:00
|
|
|
import Config
|
2010-11-06 21:06:19 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
command :: [Command]
|
2011-09-15 18:33:37 +00:00
|
|
|
command = [repoCommand "fsck" paramPaths seek "check for problems"]
|
2010-12-30 19:06:26 +00:00
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2011-09-14 17:47:22 +00:00
|
|
|
seek = [withNumCopies start]
|
2010-11-15 22:22:50 +00:00
|
|
|
|
2011-09-15 20:24:47 +00:00
|
|
|
start :: FilePath -> Maybe Int -> CommandStart
|
|
|
|
start file numcopies = notBareRepo $ isAnnexed file $ \(key, backend) -> do
|
2010-11-15 22:22:50 +00:00
|
|
|
showStart "fsck" file
|
2011-09-15 20:24:47 +00:00
|
|
|
next $ perform key file backend numcopies
|
2010-11-15 22:22:50 +00:00
|
|
|
|
2011-01-27 00:37:46 +00:00
|
|
|
perform :: Key -> FilePath -> Backend Annex -> Maybe Int -> CommandPerform
|
|
|
|
perform key file backend numcopies = do
|
2011-03-02 18:30:36 +00:00
|
|
|
-- the location log is checked first, so that if it has bad data
|
|
|
|
-- that gets corrected
|
|
|
|
locationlogok <- verifyLocationLog key file
|
2011-07-05 22:31:46 +00:00
|
|
|
backendok <- fsckKey backend key (Just file) numcopies
|
2011-03-02 18:30:36 +00:00
|
|
|
if locationlogok && backendok
|
2011-05-15 06:02:46 +00:00
|
|
|
then next $ return True
|
|
|
|
else stop
|
2011-03-02 18:30:36 +00:00
|
|
|
|
|
|
|
{- Checks that the location log reflects the current status of the key,
|
|
|
|
in this repository only. -}
|
|
|
|
verifyLocationLog :: Key -> FilePath -> Annex Bool
|
|
|
|
verifyLocationLog key file = do
|
2011-03-28 20:19:20 +00:00
|
|
|
g <- Annex.gitRepo
|
2011-03-02 18:30:36 +00:00
|
|
|
present <- inAnnex key
|
|
|
|
|
2011-03-28 20:19:20 +00:00
|
|
|
-- Since we're checking that a key's file is present, throw
|
|
|
|
-- in a permission fixup here too.
|
|
|
|
when present $ liftIO $ do
|
|
|
|
let f = gitAnnexLocation g key
|
|
|
|
preventWrite f
|
|
|
|
preventWrite (parentDir f)
|
|
|
|
|
2011-03-02 18:30:36 +00:00
|
|
|
u <- getUUID g
|
2011-06-22 20:13:43 +00:00
|
|
|
uuids <- keyLocations key
|
2011-03-02 18:30:36 +00:00
|
|
|
|
|
|
|
case (present, u `elem` uuids) of
|
|
|
|
(True, False) -> do
|
2011-07-01 19:24:07 +00:00
|
|
|
fix g u InfoPresent
|
2011-03-02 18:30:36 +00:00
|
|
|
-- There is no data loss, so do not fail.
|
|
|
|
return True
|
|
|
|
(False, True) -> do
|
2011-07-01 19:24:07 +00:00
|
|
|
fix g u InfoMissing
|
2011-03-02 18:30:36 +00:00
|
|
|
warning $
|
|
|
|
"** Based on the location log, " ++ file
|
|
|
|
++ "\n** was expected to be present, " ++
|
|
|
|
"but its content is missing."
|
|
|
|
return False
|
|
|
|
_ -> return True
|
|
|
|
|
|
|
|
where
|
2011-06-22 22:32:41 +00:00
|
|
|
fix g u s = do
|
2011-03-02 18:30:36 +00:00
|
|
|
showNote "fixing location log"
|
2011-06-22 22:32:41 +00:00
|
|
|
logChange g key u s
|
2011-07-05 22:31:46 +00:00
|
|
|
|
|
|
|
{- Checks a key for problems. -}
|
|
|
|
fsckKey :: Backend Annex -> Key -> Maybe FilePath -> Maybe Int -> Annex Bool
|
|
|
|
fsckKey backend key file numcopies = do
|
|
|
|
size_ok <- checkKeySize key
|
|
|
|
copies_ok <- checkKeyNumCopies key file numcopies
|
2011-07-15 16:47:14 +00:00
|
|
|
backend_ok <- (Types.Backend.fsckKey backend) key
|
2011-07-05 22:31:46 +00:00
|
|
|
return $ size_ok && copies_ok && backend_ok
|
|
|
|
|
|
|
|
{- The size of the data for a key is checked against the size encoded in
|
|
|
|
- the key's metadata, if available. -}
|
|
|
|
checkKeySize :: Key -> Annex Bool
|
|
|
|
checkKeySize key = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
let file = gitAnnexLocation g key
|
|
|
|
present <- liftIO $ doesFileExist file
|
|
|
|
case (present, Types.Key.keySize key) of
|
|
|
|
(_, Nothing) -> return True
|
|
|
|
(False, _) -> return True
|
|
|
|
(True, Just size) -> do
|
|
|
|
stat <- liftIO $ getFileStatus file
|
|
|
|
let size' = fromIntegral (fileSize stat)
|
|
|
|
if size == size'
|
|
|
|
then return True
|
|
|
|
else do
|
|
|
|
dest <- moveBad key
|
|
|
|
warning $ "Bad file size (" ++
|
|
|
|
compareSizes storageUnits True size size' ++
|
|
|
|
"); moved to " ++ dest
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
checkKeyNumCopies :: Key -> Maybe FilePath -> Maybe Int -> Annex Bool
|
|
|
|
checkKeyNumCopies key file numcopies = do
|
|
|
|
needed <- getNumCopies numcopies
|
2011-09-06 21:19:29 +00:00
|
|
|
(untrustedlocations, safelocations) <- trustPartition UnTrusted =<< keyLocations key
|
2011-07-05 22:31:46 +00:00
|
|
|
let present = length safelocations
|
|
|
|
if present < needed
|
|
|
|
then do
|
2011-09-01 20:02:01 +00:00
|
|
|
ppuuids <- Remote.prettyPrintUUIDs "untrusted" untrustedlocations
|
2011-07-05 22:31:46 +00:00
|
|
|
warning $ missingNote (filename file key) present needed ppuuids
|
|
|
|
return False
|
|
|
|
else return True
|
|
|
|
where
|
|
|
|
filename Nothing k = show k
|
|
|
|
filename (Just f) _ = f
|
|
|
|
|
|
|
|
missingNote :: String -> Int -> Int -> String -> String
|
|
|
|
missingNote file 0 _ [] =
|
|
|
|
"** No known copies exist of " ++ file
|
|
|
|
missingNote file 0 _ untrusted =
|
|
|
|
"Only these untrusted locations may have copies of " ++ file ++
|
|
|
|
"\n" ++ untrusted ++
|
|
|
|
"Back it up to trusted locations with git-annex copy."
|
|
|
|
missingNote file present needed [] =
|
|
|
|
"Only " ++ show present ++ " of " ++ show needed ++
|
|
|
|
" trustworthy copies exist of " ++ file ++
|
|
|
|
"\nBack it up with git-annex copy."
|
|
|
|
missingNote file present needed untrusted =
|
|
|
|
missingNote file present needed [] ++
|
|
|
|
"\nThe following untrusted locations may also have copies: " ++
|
|
|
|
"\n" ++ untrusted
|