summaryrefslogtreecommitdiff
path: root/Annex/Content/Direct.hs
blob: e23c6512cda12fb4a3983351b4b3797037fd4d86 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{- git-annex file content managing for direct mode
 -
 - Copyright 2010,2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Annex.Content.Direct (
	associatedFiles,
	unmodifed,
	getCache,
	showCache,
) where

import Common.Annex
import qualified Git

import System.Posix.Types

{- Files in the tree that are associated with a key.
 -
 - When no known associated files exist, returns the gitAnnexLocation. -}
associatedFiles :: Key -> Annex [FilePath]
associatedFiles key = do
	mapping <- inRepo $ gitAnnexMapping key
	files <- liftIO $ catchDefaultIO [] $ lines <$> readFile mapping
	if null files
		then do
			l <- inRepo $ gitAnnexLocation key
			return [l]
		else do
			top <- fromRepo Git.repoPath
			return $ map (top </>) files

{- Checks if a file in the tree, associated with a key, has not been modified.
 -
 - To avoid needing to fsck the file's content, which can involve an
 - expensive checksum, this relies on a cache that contains the file's
 - expected mtime and inode.
 -}
unmodifed :: Key -> FilePath -> Annex Bool
unmodifed key file = do
	cachefile <- inRepo $ gitAnnexCache key
	liftIO $ do
		curr <- getCache file
		old <- catchDefaultIO Nothing $ readCache <$> readFile cachefile
		return $ isJust curr && curr == old

{- Cache a file's inode, size, and modification time to determine if it's
 - been changed. -}
data Cache = Cache FileID FileOffset EpochTime
  deriving (Eq)

showCache :: Cache -> String
showCache (Cache inode size mtime) = unwords
	[ show inode
	, show size
	, show mtime
	]

readCache :: String -> Maybe Cache
readCache s = case words s of
	(inode:size:mtime:_) -> Cache
		<$> readish inode
		<*> readish size
		<*> readish mtime
	_ -> Nothing

getCache :: FilePath -> IO (Maybe Cache)
getCache f = catchDefaultIO Nothing $ toCache <$> getFileStatus f

toCache :: FileStatus -> Maybe Cache
toCache s
	| isRegularFile s = Just $ Cache
		(fileID s)
		(fileSize s)
		(modificationTime s)
	| otherwise = Nothing