add loggedKeys

This commit is contained in:
Joey Hess 2011-04-02 15:50:51 -04:00
parent 00b9a9a25d
commit f005a84e56
4 changed files with 50 additions and 13 deletions

View file

@ -219,9 +219,9 @@ getKeysPresent' dir = do
then return [] then return []
else do else do
-- 2 levels of hashing -- 2 levels of hashing
levela <- liftIO $ subdirContent dir levela <- liftIO $ dirContents dir
levelb <- liftIO $ mapM subdirContent levela levelb <- liftIO $ mapM dirContents levela
contents <- liftIO $ mapM subdirContent (concat levelb) contents <- liftIO $ mapM dirContents (concat levelb)
files <- liftIO $ filterM present (concat contents) files <- liftIO $ filterM present (concat contents)
return $ catMaybes $ map (fileKey . takeFileName) files return $ catMaybes $ map (fileKey . takeFileName) files
where where
@ -231,7 +231,3 @@ getKeysPresent' dir = do
case result of case result of
Right s -> return $ isRegularFile s Right s -> return $ isRegularFile s
Left _ -> return False Left _ -> return False
subdirContent d = do
c <- getDirectoryContents d
return $ map (d </>) $ filter notcruft c
notcruft f = f /= "." && f /= ".."

View file

@ -1,9 +1,7 @@
{- git-annex location log {- git-annex location log
- -
- git-annex keeps track of on which repository it last saw a value. - git-annex keeps track of which repositories have the contents of annexed
- This can be useful when using it for archiving with offline storage. - files.
- When you indicate you --want a file, git-annex will tell you which
- repositories have the value.
- -
- Location tracking information is stored in `.git-annex/key.log`. - Location tracking information is stored in `.git-annex/key.log`.
- Repositories record their UUID and the date when they --get or --drop - Repositories record their UUID and the date when they --get or --drop
@ -15,7 +13,7 @@
- Git is configured to use a union merge for this file, - Git is configured to use a union merge for this file,
- so the lines may be in arbitrary order, but it will never conflict. - so the lines may be in arbitrary order, but it will never conflict.
- -
- Copyright 2010 Joey Hess <joey@kitenet.net> - Copyright 2010-2011 Joey Hess <joey@kitenet.net>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -25,14 +23,19 @@ module LocationLog (
logChange, logChange,
readLog, readLog,
writeLog, writeLog,
keyLocations keyLocations,
loggedKeys,
logFile
) where ) where
import Data.Time.Clock.POSIX import Data.Time.Clock.POSIX
import Data.Time import Data.Time
import System.Locale import System.Locale
import System.Directory
import System.FilePath
import qualified Data.Map as Map import qualified Data.Map as Map
import Control.Monad (when) import Control.Monad (when)
import Data.Maybe
import qualified GitRepo as Git import qualified GitRepo as Git
import Utility import Utility
@ -153,3 +156,20 @@ mapLog m l =
Just l' -> (date l' <= date l) Just l' -> (date l' <= date l)
Nothing -> True Nothing -> True
u = uuid l u = uuid l
{- Finds all keys that have location log information. -}
loggedKeys :: Git.Repo -> IO [Key]
loggedKeys repo = do
let dir = gitStateDir repo
exists <- doesDirectoryExist dir
if exists
then do
-- 2 levels of hashing
levela <- dirContents dir
levelb <- mapM tryDirContents levela
files <- mapM tryDirContents (concat levelb)
return $ catMaybes $
map (logFileKey . takeFileName) (concat files)
else return []
where
tryDirContents d = catch (dirContents d) (return . const [])

View file

@ -21,6 +21,7 @@ module Locations (
isLinkToAnnex, isLinkToAnnex,
logFile, logFile,
logFileOld, logFileOld,
logFileKey,
hashDirMixed, hashDirMixed,
prop_idempotent_fileKey prop_idempotent_fileKey
@ -127,6 +128,14 @@ logFile' :: (Key -> FilePath) -> Git.Repo -> Key -> String
logFile' hasher repo key = logFile' hasher repo key =
gitStateDir repo ++ hasher key ++ keyFile key ++ ".log" gitStateDir repo ++ hasher key ++ keyFile key ++ ".log"
{- Converts a log filename into a key. -}
logFileKey :: FilePath -> Maybe Key
logFileKey file
| end == ".log" = readKey beginning
| otherwise = Nothing
where
(beginning, end) = splitAt (length file - 4) file
{- Converts a key into a filename fragment. {- Converts a key into a filename fragment.
- -
- Escape "/" in the key name, to keep a flat tree of files and avoid - Escape "/" in the key name, to keep a flat tree of files and avoid

View file

@ -22,6 +22,7 @@ module Utility (
readMaybe, readMaybe,
safeWriteFile, safeWriteFile,
dirContains, dirContains,
dirContents,
prop_idempotent_shellEscape, prop_idempotent_shellEscape,
prop_idempotent_shellEscape_multiword, prop_idempotent_shellEscape_multiword,
@ -235,3 +236,14 @@ safeWriteFile file content = do
createDirectoryIfMissing True (parentDir file) createDirectoryIfMissing True (parentDir file)
writeFile tmpfile content writeFile tmpfile content
renameFile tmpfile file renameFile tmpfile file
{- Lists the contents of a directory.
- Unlike getDirectoryContents, paths are not relative to the directory. -}
dirContents :: FilePath -> IO [FilePath]
dirContents d = do
c <- getDirectoryContents d
return $ map (d </>) $ filter notcruft c
where
notcruft "." = False
notcruft ".." = False
notcruft _ = True