d8fb97806c
Under ghc 7.4, this seems to be able to handle all filename encodings again. Including filename encodings that do not match the LANG setting. I think this will not work with earlier versions of ghc, it uses some ghc internals. Turns out that ghc 7.4 has a special filesystem encoding that it uses when reading/writing filenames (as FilePaths). This encoding is documented to allow "arbitrary undecodable bytes to be round-tripped through it". So, to get FilePaths from eg, git ls-files, set the Handle that is reading from git to use this encoding. Then things basically just work. However, I have not found a way to make Text read using this encoding. Text really does assume unicode. So I had to switch back to using String when reading/writing data to git. Which is a pity, because it's some percent slower, but at least it works. Note that stdout and stderr also have to be set to this encoding, or printing out filenames that contain undecodable bytes causes a crash. IMHO this is a misfeature in ghc, that the user can pass you a filename, which you can readFile, etc, but that default, putStr of filename may cause a crash! Git.CheckAttr gave me special trouble, because the filenames I got back from git, after feeding them in, had further encoding breakage. Rather than try to deal with that, I just zip up the input filenames with the attributes. Which must be returned in the same order queried for this to work. Also of note is an apparent GHC bug I worked around in Git.CheckAttr. It used to forkProcess and feed git from the child process. Unfortunatly, after this forkProcess, accessing the `files` variable from the parent returns []. Not the value that was passed into the function. This screams of a bad bug, that's clobbering a variable, but for now I just avoid forkProcess there to work around it. That forkProcess was itself only added because of a ghc bug, #624389. I've confirmed that the test case for that bug doesn't reproduce it with ghc 7.4. So that's ok, except for the new ghc bug I have not isolated and reported. Why does this simple bit of code magnet the ghc bugs? :) Also, the symlink touching code is currently broken, when used on utf-8 filenames in a non-utf-8 locale, or probably on any filename containing undecodable bytes, and I temporarily commented it out.
240 lines
7.4 KiB
Haskell
240 lines
7.4 KiB
Haskell
{- git-annex command
|
|
-
|
|
- Copyright 2010-2011 Joey Hess <joey@kitenet.net>
|
|
-
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
-}
|
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
module Command.Unused where
|
|
|
|
import qualified Data.Set as S
|
|
import qualified Data.Text.Lazy as L
|
|
import qualified Data.Text.Lazy.Encoding as L
|
|
|
|
import Common.Annex
|
|
import Command
|
|
import Annex.Content
|
|
import Utility.FileMode
|
|
import Utility.TempFile
|
|
import Logs.Location
|
|
import qualified Annex
|
|
import qualified Git
|
|
import qualified Git.Command
|
|
import qualified Git.Ref
|
|
import qualified Git.LsFiles as LsFiles
|
|
import qualified Git.LsTree as LsTree
|
|
import qualified Backend
|
|
import qualified Remote
|
|
import qualified Annex.Branch
|
|
import qualified Option
|
|
import Annex.CatFile
|
|
|
|
def :: [Command]
|
|
def = [withOptions [fromOption] $ command "unused" paramNothing seek
|
|
"look for unused file content"]
|
|
|
|
fromOption :: Option
|
|
fromOption = Option.field ['f'] "from" paramRemote "remote to check for unused content"
|
|
|
|
seek :: [CommandSeek]
|
|
seek = [withNothing $ start]
|
|
|
|
{- Finds unused content in the annex. -}
|
|
start :: CommandStart
|
|
start = do
|
|
from <- Annex.getField $ Option.name fromOption
|
|
let (name, action) = case from of
|
|
Nothing -> (".", checkUnused)
|
|
Just "." -> (".", checkUnused)
|
|
Just n -> (n, checkRemoteUnused n)
|
|
showStart "unused" name
|
|
next action
|
|
|
|
checkUnused :: CommandPerform
|
|
checkUnused = do
|
|
(unused, stalebad, staletmp) <- unusedKeys
|
|
_ <- list "" unusedMsg unused 0 >>=
|
|
list "bad" staleBadMsg stalebad >>=
|
|
list "tmp" staleTmpMsg staletmp
|
|
next $ return True
|
|
where
|
|
list file msg l c = do
|
|
let unusedlist = number c l
|
|
unless (null l) $ showLongNote $ msg unusedlist
|
|
writeUnusedFile file unusedlist
|
|
return $ c + length l
|
|
|
|
checkRemoteUnused :: String -> CommandPerform
|
|
checkRemoteUnused name = do
|
|
checkRemoteUnused' =<< fromJust <$> Remote.byName (Just name)
|
|
next $ return True
|
|
|
|
checkRemoteUnused' :: Remote -> Annex ()
|
|
checkRemoteUnused' r = do
|
|
showAction "checking for unused data"
|
|
remotehas <- loggedKeysFor (Remote.uuid r)
|
|
remoteunused <- excludeReferenced remotehas
|
|
let list = number 0 remoteunused
|
|
writeUnusedFile "" list
|
|
unless (null remoteunused) $ showLongNote $ remoteUnusedMsg r list
|
|
|
|
writeUnusedFile :: FilePath -> [(Int, Key)] -> Annex ()
|
|
writeUnusedFile prefix l = do
|
|
logfile <- fromRepo $ gitAnnexUnusedLog prefix
|
|
liftIO $ viaTmp writeFile logfile $
|
|
unlines $ map (\(n, k) -> show n ++ " " ++ show k) l
|
|
|
|
table :: [(Int, Key)] -> [String]
|
|
table l = " NUMBER KEY" : map cols l
|
|
where
|
|
cols (n,k) = " " ++ pad 6 (show n) ++ " " ++ show k
|
|
pad n s = s ++ replicate (n - length s) ' '
|
|
|
|
number :: Int -> [a] -> [(Int, a)]
|
|
number _ [] = []
|
|
number n (x:xs) = (n+1, x) : number (n+1) xs
|
|
|
|
staleTmpMsg :: [(Int, Key)] -> String
|
|
staleTmpMsg t = unlines $
|
|
["Some partially transferred data exists in temporary files:"]
|
|
++ table t ++ [dropMsg Nothing]
|
|
|
|
staleBadMsg :: [(Int, Key)] -> String
|
|
staleBadMsg t = unlines $
|
|
["Some corrupted files have been preserved by fsck, just in case:"]
|
|
++ table t ++ [dropMsg Nothing]
|
|
|
|
unusedMsg :: [(Int, Key)] -> String
|
|
unusedMsg u = unusedMsg' u
|
|
["Some annexed data is no longer used by any files:"]
|
|
[dropMsg Nothing]
|
|
unusedMsg' :: [(Int, Key)] -> [String] -> [String] -> String
|
|
unusedMsg' u header trailer = unlines $
|
|
header ++
|
|
table u ++
|
|
["(To see where data was previously used, try: git log --stat -S'KEY')"] ++
|
|
trailer
|
|
|
|
remoteUnusedMsg :: Remote -> [(Int, Key)] -> String
|
|
remoteUnusedMsg r u = unusedMsg' u
|
|
["Some annexed data on " ++ name ++ " is not used by any files:"]
|
|
[dropMsg $ Just r]
|
|
where
|
|
name = Remote.name r
|
|
|
|
dropMsg :: Maybe Remote -> String
|
|
dropMsg Nothing = dropMsg' ""
|
|
dropMsg (Just r) = dropMsg' $ " --from " ++ Remote.name r
|
|
dropMsg' :: String -> String
|
|
dropMsg' s = "\nTo remove unwanted data: git-annex dropunused" ++ s ++ " NUMBER\n"
|
|
|
|
{- Finds keys whose content is present, but that do not seem to be used
|
|
- by any files in the git repo, or that are only present as bad or tmp
|
|
- files. -}
|
|
unusedKeys :: Annex ([Key], [Key], [Key])
|
|
unusedKeys = do
|
|
fast <- Annex.getState Annex.fast
|
|
if fast
|
|
then do
|
|
showNote "fast mode enabled; only finding stale files"
|
|
tmp <- staleKeys gitAnnexTmpDir
|
|
bad <- staleKeys gitAnnexBadDir
|
|
return ([], bad, tmp)
|
|
else do
|
|
showAction "checking for unused data"
|
|
present <- getKeysPresent
|
|
unused <- excludeReferenced present
|
|
staletmp <- staleKeysPrune gitAnnexTmpDir present
|
|
stalebad <- staleKeysPrune gitAnnexBadDir present
|
|
return (unused, stalebad, staletmp)
|
|
|
|
{- Finds keys in the list that are not referenced in the git repository. -}
|
|
excludeReferenced :: [Key] -> Annex [Key]
|
|
excludeReferenced [] = return [] -- optimisation
|
|
excludeReferenced l = do
|
|
c <- inRepo $ Git.Command.pipeRead [Param "show-ref"]
|
|
removewith (getKeysReferenced : map getKeysReferencedInGit (refs c))
|
|
(S.fromList l)
|
|
where
|
|
-- Skip the git-annex branches, and get all other unique refs.
|
|
refs = map (Git.Ref . snd) .
|
|
nubBy uniqref .
|
|
filter ourbranches .
|
|
map (separate (== ' ')) . lines
|
|
uniqref (a, _) (b, _) = a == b
|
|
ourbranchend = '/' : show Annex.Branch.name
|
|
ourbranches (_, b) = not $ ourbranchend `isSuffixOf` b
|
|
removewith [] s = return $ S.toList s
|
|
removewith (a:as) s
|
|
| s == S.empty = return [] -- optimisation
|
|
| otherwise = do
|
|
referenced <- a
|
|
let !s' = s `S.difference` S.fromList referenced
|
|
removewith as s'
|
|
|
|
{- Finds items in the first, smaller list, that are not
|
|
- present in the second, larger list.
|
|
-
|
|
- Constructing a single set, of the list that tends to be
|
|
- smaller, appears more efficient in both memory and CPU
|
|
- than constructing and taking the S.difference of two sets. -}
|
|
exclude :: Ord a => [a] -> [a] -> [a]
|
|
exclude [] _ = [] -- optimisation
|
|
exclude smaller larger = S.toList $ remove larger $ S.fromList smaller
|
|
where
|
|
remove a b = foldl (flip S.delete) b a
|
|
|
|
{- List of keys referenced by symlinks in the git repo. -}
|
|
getKeysReferenced :: Annex [Key]
|
|
getKeysReferenced = do
|
|
top <- fromRepo Git.workTree
|
|
files <- inRepo $ LsFiles.inRepo [top]
|
|
keypairs <- mapM Backend.lookupFile files
|
|
return $ map fst $ catMaybes keypairs
|
|
|
|
{- List of keys referenced by symlinks in a git ref. -}
|
|
getKeysReferencedInGit :: Git.Ref -> Annex [Key]
|
|
getKeysReferencedInGit ref = do
|
|
showAction $ "checking " ++ Git.Ref.describe ref
|
|
findkeys [] =<< inRepo (LsTree.lsTree ref)
|
|
where
|
|
findkeys c [] = return c
|
|
findkeys c (l:ls)
|
|
| isSymLink (LsTree.mode l) = do
|
|
content <- L.decodeUtf8 <$> catFile ref (LsTree.file l)
|
|
case fileKey (takeFileName $ L.unpack content) of
|
|
Nothing -> findkeys c ls
|
|
Just k -> findkeys (k:c) ls
|
|
| otherwise = findkeys c ls
|
|
|
|
{- Looks in the specified directory for bad/tmp keys, and returns a list
|
|
- of those that might still have value, or might be stale and removable.
|
|
-
|
|
- When a list of presently available keys is provided, stale keys
|
|
- that no longer have value are deleted.
|
|
-}
|
|
staleKeysPrune :: (Git.Repo -> FilePath) -> [Key] -> Annex [Key]
|
|
staleKeysPrune dirspec present = do
|
|
contents <- staleKeys dirspec
|
|
|
|
let stale = contents `exclude` present
|
|
let dups = contents `exclude` stale
|
|
|
|
dir <- fromRepo dirspec
|
|
liftIO $ forM_ dups $ \t -> removeFile $ dir </> keyFile t
|
|
|
|
return stale
|
|
|
|
staleKeys :: (Git.Repo -> FilePath) -> Annex [Key]
|
|
staleKeys dirspec = do
|
|
dir <- fromRepo dirspec
|
|
exists <- liftIO $ doesDirectoryExist dir
|
|
if not exists
|
|
then return []
|
|
else do
|
|
contents <- liftIO $ getDirectoryContents dir
|
|
files <- liftIO $ filterM doesFileExist $
|
|
map (dir </>) contents
|
|
return $ mapMaybe (fileKey . takeFileName) files
|