2010-11-15 20:35:06 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Unused where
|
|
|
|
|
2011-02-01 03:27:53 +00:00
|
|
|
import Control.Monad (filterM, unless, forM_)
|
2010-11-15 22:04:19 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
2011-01-30 05:41:15 +00:00
|
|
|
import qualified Data.Set as S
|
2011-01-16 20:05:05 +00:00
|
|
|
import Data.Maybe
|
2011-01-28 18:10:50 +00:00
|
|
|
import System.FilePath
|
|
|
|
import System.Directory
|
2010-11-15 20:35:06 +00:00
|
|
|
|
|
|
|
import Command
|
|
|
|
import Types
|
2011-01-16 20:05:05 +00:00
|
|
|
import Content
|
2010-11-15 20:35:06 +00:00
|
|
|
import Messages
|
2010-11-15 22:04:19 +00:00
|
|
|
import Locations
|
2011-01-28 16:35:51 +00:00
|
|
|
import Utility
|
2010-11-15 22:04:19 +00:00
|
|
|
import qualified Annex
|
2011-01-16 20:05:05 +00:00
|
|
|
import qualified GitRepo as Git
|
|
|
|
import qualified Backend
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
command :: [Command]
|
|
|
|
command = [Command "unused" paramNothing seek "look for unused file content"]
|
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
seek :: [CommandSeek]
|
2010-11-15 20:35:06 +00:00
|
|
|
seek = [withNothing start]
|
|
|
|
|
|
|
|
{- Finds unused content in the annex. -}
|
2010-12-30 18:19:16 +00:00
|
|
|
start :: CommandStartNothing
|
2011-03-03 20:40:55 +00:00
|
|
|
start = notBareRepo $ do
|
2010-11-15 20:35:06 +00:00
|
|
|
showStart "unused" ""
|
|
|
|
return $ Just perform
|
|
|
|
|
2010-12-30 18:19:16 +00:00
|
|
|
perform :: CommandPerform
|
2010-11-15 20:35:06 +00:00
|
|
|
perform = do
|
2010-11-15 22:37:49 +00:00
|
|
|
_ <- checkUnused
|
|
|
|
return $ Just $ return True
|
2010-11-15 20:35:06 +00:00
|
|
|
|
|
|
|
checkUnused :: Annex Bool
|
|
|
|
checkUnused = do
|
|
|
|
showNote "checking for unused data..."
|
2011-01-28 18:10:50 +00:00
|
|
|
(unused, staletmp) <- unusedKeys
|
|
|
|
let unusedlist = number 0 unused
|
|
|
|
let staletmplist = number (length unused) staletmp
|
|
|
|
let list = unusedlist ++ staletmplist
|
2011-01-12 05:57:32 +00:00
|
|
|
g <- Annex.gitRepo
|
2011-01-28 16:35:51 +00:00
|
|
|
liftIO $ safeWriteFile (gitAnnexUnusedLog g) $ unlines $
|
2011-01-12 05:57:32 +00:00
|
|
|
map (\(n, k) -> show n ++ " " ++ show k) list
|
2011-01-30 03:30:08 +00:00
|
|
|
unless (null unused) $ showLongNote $ unusedmsg unusedlist
|
|
|
|
unless (null staletmp) $ showLongNote $ staletmpmsg staletmplist
|
|
|
|
unless (null list) $ showLongNote $ "\n"
|
2011-01-28 18:10:50 +00:00
|
|
|
return $ null list
|
|
|
|
|
2010-11-15 20:35:06 +00:00
|
|
|
where
|
2011-01-28 18:10:50 +00:00
|
|
|
unusedmsg u = unlines $
|
|
|
|
["Some annexed data is no longer pointed to by any files in the repository:"]
|
|
|
|
++ table u ++
|
|
|
|
["(To see where data was previously used, try: git log --stat -S'KEY')"] ++
|
|
|
|
dropmsg
|
|
|
|
staletmpmsg t = unlines $
|
|
|
|
["Some partially transferred data exists in temporary files:"]
|
|
|
|
++ table t ++ dropmsg
|
|
|
|
dropmsg = ["(To remove unwanted data: git-annex dropunused NUMBER)"]
|
|
|
|
|
|
|
|
table l = [" NUMBER KEY"] ++ map cols l
|
2011-02-11 19:37:37 +00:00
|
|
|
cols (n,k) = " " ++ pad 6 (show n) ++ " " ++ (filePathToString . show) k
|
2010-11-15 22:04:19 +00:00
|
|
|
pad n s = s ++ replicate (n - length s) ' '
|
|
|
|
|
2011-01-28 18:10:50 +00:00
|
|
|
number :: Int -> [a] -> [(Int, a)]
|
2010-11-15 22:04:19 +00:00
|
|
|
number _ [] = []
|
2011-01-28 18:10:50 +00:00
|
|
|
number n (x:xs) = (n+1, x):(number (n+1) xs)
|
2010-11-15 20:35:06 +00:00
|
|
|
|
|
|
|
{- Finds keys whose content is present, but that do not seem to be used
|
2011-01-28 18:10:50 +00:00
|
|
|
- by any files in the git repo, or that are only present as tmp files. -}
|
|
|
|
unusedKeys :: Annex ([Key], [Key])
|
2010-11-15 20:35:06 +00:00
|
|
|
unusedKeys = do
|
2011-01-28 18:10:50 +00:00
|
|
|
g <- Annex.gitRepo
|
2010-11-15 20:35:06 +00:00
|
|
|
present <- getKeysPresent
|
|
|
|
referenced <- getKeysReferenced
|
2011-01-30 03:30:08 +00:00
|
|
|
tmps <- tmpKeys
|
2010-11-15 20:35:06 +00:00
|
|
|
|
2011-01-30 03:30:08 +00:00
|
|
|
let (unused, staletmp, duptmp) = calcUnusedKeys present referenced tmps
|
2011-01-28 18:10:50 +00:00
|
|
|
|
2011-01-30 03:30:08 +00:00
|
|
|
-- Tmp files that are dups of content already present can simply
|
|
|
|
-- be removed.
|
2011-02-01 03:27:53 +00:00
|
|
|
liftIO $ forM_ duptmp $ \t -> removeFile $ gitAnnexTmpLocation g t
|
2011-01-28 18:10:50 +00:00
|
|
|
|
|
|
|
return (unused, staletmp)
|
|
|
|
|
2011-01-30 03:30:08 +00:00
|
|
|
calcUnusedKeys :: [Key] -> [Key] -> [Key] -> ([Key], [Key], [Key])
|
|
|
|
calcUnusedKeys present referenced tmps = (unused, staletmp, duptmp)
|
2010-11-15 20:35:06 +00:00
|
|
|
where
|
2011-01-30 03:30:08 +00:00
|
|
|
unused = present `exclude` referenced
|
|
|
|
staletmp = tmps `exclude` present
|
|
|
|
duptmp = tmps `exclude` staletmp
|
|
|
|
|
2011-01-30 05:41:15 +00:00
|
|
|
-- Constructing a single set, of the list that tends to be
|
2011-01-28 18:10:50 +00:00
|
|
|
-- smaller, appears more efficient in both memory and CPU
|
2011-01-30 05:41:15 +00:00
|
|
|
-- than constructing and taking the S.difference of two sets.
|
2011-01-28 18:10:50 +00:00
|
|
|
exclude [] _ = [] -- optimisation
|
2011-01-30 05:41:15 +00:00
|
|
|
exclude smaller larger = S.toList $ remove larger $ S.fromList smaller
|
|
|
|
remove a b = foldl (flip S.delete) b a
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
{- List of keys referenced by symlinks in the git repo. -}
|
|
|
|
getKeysReferenced :: Annex [Key]
|
|
|
|
getKeysReferenced = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
files <- liftIO $ Git.inRepo g [Git.workTree g]
|
|
|
|
keypairs <- mapM Backend.lookupFile files
|
|
|
|
return $ map fst $ catMaybes keypairs
|
2011-01-28 18:10:50 +00:00
|
|
|
|
|
|
|
{- List of keys that have temp files in the git repo. -}
|
|
|
|
tmpKeys :: Annex [Key]
|
|
|
|
tmpKeys = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
let tmp = gitAnnexTmpDir g
|
|
|
|
exists <- liftIO $ doesDirectoryExist tmp
|
|
|
|
if (not exists)
|
|
|
|
then return []
|
|
|
|
else do
|
|
|
|
contents <- liftIO $ getDirectoryContents tmp
|
|
|
|
files <- liftIO $ filterM doesFileExist $
|
|
|
|
map (tmp </>) contents
|
|
|
|
return $ map (fileKey . takeFileName) files
|