summaryrefslogtreecommitdiff
path: root/Command/Unused.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-07-03 15:26:59 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-07-03 15:26:59 -0400
commit289a3f7749a5086a19a22b277a022e3b006da94f (patch)
tree10c4a4fe5ab4391c82cf91f4069bf0d7f4c7490b /Command/Unused.hs
parent0e4ed4dd9bf9b78c1723400e9c787a18430c5f57 (diff)
--unused: New switch that makes git-annex operate on all data found by the last run of git annex unused. Supported by fsck, get, move, copy.
Diffstat (limited to 'Command/Unused.hs')
-rw-r--r--Command/Unused.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/Command/Unused.hs b/Command/Unused.hs
index 6c4a61cd4..989faa9a3 100644
--- a/Command/Unused.hs
+++ b/Command/Unused.hs
@@ -15,6 +15,7 @@ import Data.BloomFilter
import Data.BloomFilter.Easy
import Data.BloomFilter.Hash
import Control.Monad.ST
+import qualified Data.Map as M
import Common.Annex
import Command
@@ -311,3 +312,49 @@ staleKeys dirspec = do
return $ mapMaybe (fileKey . takeFileName) files
, return []
)
+
+data UnusedMaps = UnusedMaps
+ { unusedMap :: UnusedMap
+ , unusedBadMap :: UnusedMap
+ , unusedTmpMap :: UnusedMap
+ }
+
+{- Read unused logs once, and pass the maps to each start action. -}
+withUnusedMaps :: (UnusedMaps -> Int -> CommandStart) -> CommandSeek
+withUnusedMaps a params = do
+ unused <- readUnusedLog ""
+ unusedbad <- readUnusedLog "bad"
+ unusedtmp <- readUnusedLog "tmp"
+ return $ map (a $ UnusedMaps unused unusedbad unusedtmp) $
+ concatMap unusedSpec params
+
+unusedSpec :: String -> [Int]
+unusedSpec spec
+ | "-" `isInfixOf` spec = range $ separate (== '-') spec
+ | otherwise = maybe badspec (: []) (readish spec)
+ where
+ range (a, b) = case (readish a, readish b) of
+ (Just x, Just y) -> [x..y]
+ _ -> badspec
+ badspec = error $ "Expected number or range, not \"" ++ spec ++ "\""
+
+{- Start action for unused content. Finds the number in the maps, and
+ - calls either of 3 actions, depending on the type of unused file. -}
+startUnused :: String
+ -> (Key -> CommandPerform)
+ -> (Key -> CommandPerform)
+ -> (Key -> CommandPerform)
+ -> UnusedMaps -> Int -> CommandStart
+startUnused message unused badunused tmpunused maps n = search
+ [ (unusedMap maps, unused)
+ , (unusedBadMap maps, badunused)
+ , (unusedTmpMap maps, tmpunused)
+ ]
+ where
+ search [] = stop
+ search ((m, a):rest) =
+ case M.lookup n m of
+ Nothing -> search rest
+ Just key -> do
+ showStart message (show n)
+ next $ a key