diff options
author | Joey Hess <joey@kitenet.net> | 2011-03-23 02:10:59 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2011-03-23 02:10:59 -0400 |
commit | c43e3b5c787050664089fa4498e660a475addcd9 (patch) | |
tree | 3da57db088d99ac43c81245c9fd605ddf2854eda /Backend.hs | |
parent | 12cdc96216e0c516ceeee922f9ca1568e9d4d592 (diff) |
check key size when available, no matter the backend
Now that SHA and other backends can have size info, fsck should check it
whenever available.
Diffstat (limited to 'Backend.hs')
-rw-r--r-- | Backend.hs | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/Backend.hs b/Backend.hs index cd14ce50e..d7334f144 100644 --- a/Backend.hs +++ b/Backend.hs @@ -35,6 +35,7 @@ import Control.Monad.State import System.IO.Error (try) import System.FilePath import System.Posix.Files +import System.Directory import Locations import qualified GitRepo as Git @@ -43,6 +44,8 @@ import Types import Key import qualified BackendClass as B import Messages +import Content +import DataUnits {- List of backends in the order to try them when storing a new key. -} list :: Annex [Backend Annex] @@ -120,9 +123,12 @@ hasKey key = do backend <- keyBackend key (B.hasKey backend) key -{- Checks a key's backend for problems. -} +{- Checks a key for problems. -} fsckKey :: Backend Annex -> Key -> Maybe FilePath -> Maybe Int -> Annex Bool -fsckKey backend key file numcopies = (B.fsckKey backend) key file numcopies +fsckKey backend key file numcopies = do + size_ok <- checkKeySize key + backend_ok <-(B.fsckKey backend) key file numcopies + return $ size_ok && backend_ok {- Looks up the key and backend corresponding to an annexed file, - by examining what the file symlinks to. -} @@ -168,3 +174,33 @@ keyBackend :: Key -> Annex (Backend Annex) keyBackend key = do bs <- Annex.getState Annex.supportedBackends return $ lookupBackendName bs $ keyBackendName key + +{- 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, 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 $ badsizeNote dest size size' + return False + +badsizeNote :: FilePath -> Integer -> Integer -> String +badsizeNote dest expected got = "Bad file size (" ++ aside ++ "); moved to " ++ dest + where + expected' = roughSize True expected + got' = roughSize True got + aside = + if expected' == got' + then show expected ++ " not " ++ show got + else expected' ++ " not " ++ got' |