2011-03-15 21:47:29 +00:00
|
|
|
{- git-annex Key data type
|
2011-06-02 01:56:04 +00:00
|
|
|
-
|
|
|
|
- Most things should not need this, using Types instead
|
2011-03-15 21:47:29 +00:00
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-06-02 01:56:04 +00:00
|
|
|
module Types.Key (
|
2011-03-16 01:34:13 +00:00
|
|
|
Key(..),
|
|
|
|
stubKey,
|
|
|
|
readKey,
|
|
|
|
|
|
|
|
prop_idempotent_key_read_show
|
|
|
|
) where
|
2011-03-15 21:47:29 +00:00
|
|
|
|
2011-03-16 01:34:13 +00:00
|
|
|
import System.Posix.Types
|
2011-03-15 21:47:29 +00:00
|
|
|
|
2011-10-04 02:24:57 +00:00
|
|
|
import Common
|
|
|
|
|
2011-03-16 01:34:13 +00:00
|
|
|
{- A Key has a unique name, is associated with a key/value backend,
|
|
|
|
- and may contain other optional metadata. -}
|
2011-03-15 23:11:21 +00:00
|
|
|
data Key = Key {
|
|
|
|
keyName :: String,
|
2011-03-16 01:34:13 +00:00
|
|
|
keyBackendName :: String,
|
|
|
|
keySize :: Maybe Integer,
|
|
|
|
keyMtime :: Maybe EpochTime
|
2011-03-15 23:11:21 +00:00
|
|
|
} deriving (Eq, Ord)
|
2011-03-15 21:47:29 +00:00
|
|
|
|
2011-03-16 01:34:13 +00:00
|
|
|
stubKey :: Key
|
|
|
|
stubKey = Key {
|
|
|
|
keyName = "",
|
|
|
|
keyBackendName = "",
|
|
|
|
keySize = Nothing,
|
|
|
|
keyMtime = Nothing
|
|
|
|
}
|
|
|
|
|
2011-03-15 21:47:29 +00:00
|
|
|
fieldSep :: Char
|
2011-03-16 01:54:38 +00:00
|
|
|
fieldSep = '-'
|
2011-03-15 21:47:29 +00:00
|
|
|
|
|
|
|
{- Keys show as strings that are suitable for use as filenames.
|
2011-03-16 01:54:38 +00:00
|
|
|
- The name field is always shown last, separated by doubled fieldSeps,
|
|
|
|
- and is the only field allowed to contain the fieldSep. -}
|
2011-03-15 21:47:29 +00:00
|
|
|
instance Show Key where
|
2011-03-16 01:34:13 +00:00
|
|
|
show Key { keyBackendName = b, keySize = s, keyMtime = m, keyName = n } =
|
2011-03-16 01:54:38 +00:00
|
|
|
b +++ ('s' ?: s) +++ ('m' ?: m) +++ (fieldSep : n)
|
2011-03-15 21:47:29 +00:00
|
|
|
where
|
2011-03-15 23:11:21 +00:00
|
|
|
"" +++ y = y
|
|
|
|
x +++ "" = x
|
|
|
|
x +++ y = x ++ fieldSep:y
|
2011-07-15 07:12:05 +00:00
|
|
|
c ?: (Just v) = c : show v
|
2011-03-15 23:11:21 +00:00
|
|
|
_ ?: _ = ""
|
|
|
|
|
|
|
|
readKey :: String -> Maybe Key
|
2011-03-16 01:34:13 +00:00
|
|
|
readKey s = if key == Just stubKey then Nothing else key
|
2011-03-15 23:11:21 +00:00
|
|
|
where
|
2011-03-16 01:54:38 +00:00
|
|
|
key = startbackend stubKey s
|
2011-03-15 21:47:29 +00:00
|
|
|
|
2011-03-16 01:54:38 +00:00
|
|
|
startbackend k v = sepfield k v addbackend
|
2011-03-15 23:11:21 +00:00
|
|
|
|
2011-03-16 01:54:38 +00:00
|
|
|
sepfield k v a = case span (/= fieldSep) v of
|
|
|
|
(v', _:r) -> findfields r $ a k v'
|
|
|
|
_ -> Nothing
|
|
|
|
|
|
|
|
findfields (c:v) (Just k)
|
|
|
|
| c == fieldSep = Just $ k { keyName = v }
|
|
|
|
| otherwise = sepfield k v $ addfield c
|
|
|
|
findfields _ v = v
|
|
|
|
|
|
|
|
addbackend k v = Just k { keyBackendName = v }
|
2012-01-23 21:00:10 +00:00
|
|
|
addfield 's' k v = Just k { keySize = readish v }
|
|
|
|
addfield 'm' k v = Just k { keyMtime = readish v }
|
2011-03-15 23:11:21 +00:00
|
|
|
addfield _ _ _ = Nothing
|
2011-03-15 21:47:29 +00:00
|
|
|
|
|
|
|
prop_idempotent_key_read_show :: Key -> Bool
|
2011-07-15 07:12:05 +00:00
|
|
|
prop_idempotent_key_read_show k = Just k == (readKey . show) k
|