reorganize numcopies code (no behavior changes)

Move stuff into Logs.NumCopies. Add a NumCopies newtype.

Better names for various serialization classes that are specific to one
thing or another.
This commit is contained in:
Joey Hess 2014-01-21 16:08:19 -04:00
parent e38a21a768
commit b40df4f0d0
20 changed files with 137 additions and 98 deletions

View file

@ -13,6 +13,7 @@ import GitAnnex.Options
import qualified Command.Move
import qualified Remote
import Annex.Wanted
import Logs.NumCopies
def :: [Command]
def = [withOptions Command.Move.moveOptions $ command "copy" paramPaths seek

View file

@ -14,8 +14,8 @@ import qualified Annex
import Annex.UUID
import Logs.Location
import Logs.Trust
import Logs.NumCopies
import Annex.Content
import Config
import qualified Option
import Annex.Wanted
import Types.Key
@ -43,17 +43,17 @@ start from file (key, _) = checkDropAuto from file key $ \numcopies ->
then startLocal (Just file) numcopies key Nothing
else startRemote (Just file) numcopies key remote
startLocal :: AssociatedFile -> Maybe Int -> Key -> Maybe Remote -> CommandStart
startLocal :: AssociatedFile -> Maybe NumCopies -> Key -> Maybe Remote -> CommandStart
startLocal afile numcopies key knownpresentremote = stopUnless (inAnnex key) $ do
showStart "drop" (fromMaybe (key2file key) afile)
next $ performLocal key numcopies knownpresentremote
startRemote :: AssociatedFile -> Maybe Int -> Key -> Remote -> CommandStart
startRemote :: AssociatedFile -> Maybe NumCopies -> Key -> Remote -> CommandStart
startRemote afile numcopies key remote = do
showStart ("drop " ++ Remote.name remote) (fromMaybe (key2file key) afile)
next $ performRemote key numcopies remote
performLocal :: Key -> Maybe Int -> Maybe Remote -> CommandPerform
performLocal :: Key -> Maybe NumCopies -> Maybe Remote -> CommandPerform
performLocal key numcopies knownpresentremote = lockContent key $ do
(remotes, trusteduuids) <- Remote.keyPossibilitiesTrusted key
let trusteduuids' = case knownpresentremote of
@ -65,7 +65,7 @@ performLocal key numcopies knownpresentremote = lockContent key $ do
removeAnnex key
next $ cleanupLocal key
performRemote :: Key -> Maybe Int -> Remote -> CommandPerform
performRemote :: Key -> Maybe NumCopies -> Remote -> CommandPerform
performRemote key numcopies remote = lockContent key $ do
-- Filter the remote it's being dropped from out of the lists of
-- places assumed to have the key, and places to check.
@ -98,23 +98,23 @@ cleanupRemote key remote ok = do
{- Checks specified remotes to verify that enough copies of a key exist to
- allow it to be safely removed (with no data loss). Can be provided with
- some locations where the key is known/assumed to be present. -}
canDropKey :: Key -> Maybe Int -> [UUID] -> [Remote] -> [UUID] -> Annex Bool
canDropKey :: Key -> Maybe NumCopies -> [UUID] -> [Remote] -> [UUID] -> Annex Bool
canDropKey key numcopiesM have check skip = do
force <- Annex.getState Annex.force
if force || numcopiesM == Just 0
if force || numcopiesM == Just (NumCopies 0)
then return True
else do
need <- getNumCopies numcopiesM
findCopies key need skip have check
findCopies :: Key -> Int -> [UUID] -> [UUID] -> [Remote] -> Annex Bool
findCopies :: Key -> NumCopies -> [UUID] -> [UUID] -> [Remote] -> Annex Bool
findCopies key need skip = helper [] []
where
helper bad missing have []
| length have >= need = return True
| NumCopies (length have) >= need = return True
| otherwise = notEnoughCopies key need have (skip++missing) bad
helper bad missing have (r:rs)
| length have >= need = return True
| NumCopies (length have) >= need = return True
| otherwise = do
let u = Remote.uuid r
let duplicate = u `elem` have
@ -125,12 +125,12 @@ findCopies key need skip = helper [] []
(False, Right False) -> helper bad (u:missing) have rs
_ -> helper bad missing have rs
notEnoughCopies :: Key -> Int -> [UUID] -> [UUID] -> [Remote] -> Annex Bool
notEnoughCopies :: Key -> NumCopies -> [UUID] -> [UUID] -> [Remote] -> Annex Bool
notEnoughCopies key need have skip bad = do
unsafe
showLongNote $
"Could only verify the existence of " ++
show (length have) ++ " out of " ++ show need ++
show (length have) ++ " out of " ++ show (fromNumCopies need) ++
" necessary copies"
Remote.showTriedRemotes bad
Remote.showLocations key (have++skip)
@ -146,9 +146,9 @@ notEnoughCopies key need have skip bad = do
-
- Passes any numcopies attribute of the file on to the action as an
- optimisation. -}
checkDropAuto :: Maybe Remote -> FilePath -> Key -> (Maybe Int -> CommandStart) -> CommandStart
checkDropAuto :: Maybe Remote -> FilePath -> Key -> (Maybe NumCopies -> CommandStart) -> CommandStart
checkDropAuto mremote file key a = do
numcopiesattr <- numCopies file
numcopiesattr <- getFileNumCopies file
Annex.getState Annex.auto >>= auto numcopiesattr
where
auto numcopiesattr False = a numcopiesattr
@ -158,6 +158,6 @@ checkDropAuto mremote file key a = do
uuid <- getUUID
let remoteuuid = fromMaybe uuid $ Remote.uuid <$> mremote
locs' <- trustExclude UnTrusted $ filter (/= remoteuuid) locs
if length locs' >= needed
if NumCopies (length locs') >= needed
then a numcopiesattr
else stop

View file

@ -25,6 +25,7 @@ import Annex.Perms
import Annex.Link
import Logs.Location
import Logs.Trust
import Logs.NumCopies
import Annex.UUID
import Utility.DataUnits
import Utility.FileMode
@ -111,14 +112,14 @@ getIncremental = do
start :: Maybe Remote -> Incremental -> FilePath -> (Key, Backend) -> CommandStart
start from inc file (key, backend) = do
numcopies <- numCopies file
numcopies <- getFileNumCopies file
case from of
Nothing -> go $ perform key file backend numcopies
Just r -> go $ performRemote key file backend numcopies r
where
go = runFsck inc file key
perform :: Key -> FilePath -> Backend -> Maybe Int -> Annex Bool
perform :: Key -> FilePath -> Backend -> Maybe NumCopies -> Annex Bool
perform key file backend numcopies = check
-- order matters
[ fixLink key file
@ -132,7 +133,7 @@ perform key file backend numcopies = check
{- To fsck a remote, the content is retrieved to a tmp file,
- and checked locally. -}
performRemote :: Key -> FilePath -> Backend -> Maybe Int -> Remote -> Annex Bool
performRemote :: Key -> FilePath -> Backend -> Maybe NumCopies -> Remote -> Annex Bool
performRemote key file backend numcopies remote =
dispatch =<< Remote.hasKey remote key
where
@ -368,11 +369,11 @@ checkBackendOr' bad backend key file postcheck =
, return True
)
checkKeyNumCopies :: Key -> FilePath -> Maybe Int -> Annex Bool
checkKeyNumCopies :: Key -> FilePath -> Maybe NumCopies -> Annex Bool
checkKeyNumCopies key file numcopies = do
needed <- getNumCopies numcopies
(untrustedlocations, safelocations) <- trustPartition UnTrusted =<< Remote.keyLocations key
let present = length safelocations
let present = NumCopies (length safelocations)
if present < needed
then do
ppuuids <- Remote.prettyPrintUUIDs "untrusted" untrustedlocations
@ -380,15 +381,15 @@ checkKeyNumCopies key file numcopies = do
return False
else return True
missingNote :: String -> Int -> Int -> String -> String
missingNote file 0 _ [] =
missingNote :: String -> NumCopies -> NumCopies -> String -> String
missingNote file (NumCopies 0) _ [] =
"** No known copies exist of " ++ file
missingNote file 0 _ untrusted =
missingNote file (NumCopies 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 ++
"Only " ++ show (fromNumCopies present) ++ " of " ++ show (fromNumCopies needed) ++
" trustworthy copies exist of " ++ file ++
"\nBack it up with git-annex copy."
missingNote file present needed untrusted =

View file

@ -12,6 +12,7 @@ import Command
import qualified Remote
import Annex.Content
import Logs.Transfer
import Logs.NumCopies
import Annex.Wanted
import GitAnnex.Options
import qualified Command.Move

View file

@ -29,6 +29,7 @@ import Annex.Content
import Types.Key
import Logs.UUID
import Logs.Trust
import Logs.NumCopies
import Remote
import Config
import Utility.Percentage

View file

@ -16,6 +16,7 @@ import qualified Command.Get
import qualified Remote
import Annex.Content
import qualified Annex
import Logs.NumCopies
def :: [Command]
def = [withOptions (fromToOptions ++ keyOptions) $
@ -33,10 +34,10 @@ seek ps = do
start :: Maybe Remote -> Maybe Remote -> FilePath -> (Key, Backend) -> CommandStart
start to from file (key, _backend) = do
numcopies <- numCopies file
numcopies <- getFileNumCopies file
startKey numcopies to from (Just file) key
startKey :: Maybe Int -> Maybe Remote -> Maybe Remote -> Maybe FilePath -> Key -> CommandStart
startKey :: Maybe NumCopies -> Maybe Remote -> Maybe Remote -> Maybe FilePath -> Key -> CommandStart
startKey numcopies to from afile key = do
noAuto
case (from, to) of

View file

@ -39,7 +39,7 @@ startGet = next $ next $ do
Annex.setOutput QuietOutput
v <- getGlobalNumCopies
case v of
Just n -> liftIO $ putStrLn $ show n
Just n -> liftIO $ putStrLn $ show $ fromNumCopies n
Nothing -> do
liftIO $ putStrLn $ "global numcopies is not set"
old <- annexNumCopies <$> Annex.getGitConfig
@ -52,5 +52,5 @@ startSet :: Int -> CommandStart
startSet n = do
showStart "numcopies" (show n)
next $ next $ do
setGlobalNumCopies n
setGlobalNumCopies $ NumCopies n
return True

View file

@ -106,34 +106,34 @@ readResponse h = fromMaybe False . deserialize <$> hGetLine h
fieldSep :: String
fieldSep = "\0"
class Serialized a where
class TCSerialized a where
serialize :: a -> String
deserialize :: String -> Maybe a
instance Serialized Bool where
instance TCSerialized Bool where
serialize True = "1"
serialize False = "0"
deserialize "1" = Just True
deserialize "0" = Just False
deserialize _ = Nothing
instance Serialized Direction where
instance TCSerialized Direction where
serialize Upload = "u"
serialize Download = "d"
deserialize "u" = Just Upload
deserialize "d" = Just Download
deserialize _ = Nothing
instance Serialized AssociatedFile where
instance TCSerialized AssociatedFile where
serialize (Just f) = f
serialize Nothing = ""
deserialize "" = Just Nothing
deserialize f = Just $ Just f
instance Serialized UUID where
instance TCSerialized UUID where
serialize = fromUUID
deserialize = Just . toUUID
instance Serialized Key where
instance TCSerialized Key where
serialize = key2file
deserialize = file2key