aboutsummaryrefslogtreecommitdiff
path: root/Logs/Unused.hs
blob: 4de5bc17a394f6303afd89df5848e8da1429bde1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{- git-annex unused log file
 -
 - Copyright 2010,2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Logs.Unused (
	UnusedMap,
	writeUnusedLog,
	readUnusedLog,
	unusedKeys,
) where

import qualified Data.Map as M

import Common.Annex
import Types.Key
import Utility.Tmp

type UnusedMap = M.Map Int Key

writeUnusedLog :: FilePath -> [(Int, Key)] -> Annex ()
writeUnusedLog prefix l = do
	logfile <- fromRepo $ gitAnnexUnusedLog prefix
	liftIO $ viaTmp writeFile logfile $
		unlines $ map (\(n, k) -> show n ++ " " ++ key2file k) l

readUnusedLog :: FilePath -> Annex UnusedMap
readUnusedLog prefix = do
	f <- fromRepo $ gitAnnexUnusedLog prefix
	ifM (liftIO $ doesFileExist f)
		( M.fromList . mapMaybe parse . lines
			<$> liftIO (readFile f)
		, return M.empty
		)
  where
	parse line = case (readish tag, file2key rest) of
		(Just num, Just key) -> Just (num, key)
		_ -> Nothing
	  where
		(tag, rest) = separate (== ' ') line

unusedKeys :: Annex [Key]
unusedKeys = M.elems <$> readUnusedLog ""