2010-10-18 06:06:27 +00:00
|
|
|
{- git-annex internal data types
|
2010-10-14 06:52:17 +00:00
|
|
|
-
|
2010-10-18 06:06:27 +00:00
|
|
|
- Most things should not need this, using Types and/or Annex instead.
|
2010-10-27 20:53:54 +00:00
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
2010-10-14 06:52:17 +00:00
|
|
|
-}
|
2010-10-12 19:52:18 +00:00
|
|
|
|
2010-10-18 06:06:27 +00:00
|
|
|
module TypeInternals where
|
2010-10-12 19:52:18 +00:00
|
|
|
|
2010-10-14 23:36:11 +00:00
|
|
|
import Control.Monad.State (StateT)
|
2010-10-13 00:04:36 +00:00
|
|
|
import Data.String.Utils
|
2010-10-21 20:30:16 +00:00
|
|
|
import qualified Data.Map as M
|
2011-01-05 01:05:31 +00:00
|
|
|
import Test.QuickCheck
|
2010-10-16 20:20:49 +00:00
|
|
|
|
2010-10-14 06:36:41 +00:00
|
|
|
import qualified GitRepo as Git
|
2010-10-26 19:59:50 +00:00
|
|
|
import qualified GitQueue
|
2010-10-12 19:52:18 +00:00
|
|
|
|
2010-10-21 20:30:16 +00:00
|
|
|
-- command-line flags
|
|
|
|
type FlagName = String
|
|
|
|
data Flag =
|
|
|
|
FlagBool Bool |
|
|
|
|
FlagString String
|
2010-10-19 17:08:05 +00:00
|
|
|
deriving (Eq, Read, Show)
|
2010-10-15 01:10:59 +00:00
|
|
|
|
2010-10-14 06:52:17 +00:00
|
|
|
-- git-annex's runtime state type doesn't really belong here,
|
|
|
|
-- but it uses Backend, so has to be here to avoid a depends loop.
|
2010-10-14 01:28:47 +00:00
|
|
|
data AnnexState = AnnexState {
|
2010-10-14 06:36:41 +00:00
|
|
|
repo :: Git.Repo,
|
2011-01-26 01:02:34 +00:00
|
|
|
backends :: [Backend Annex],
|
|
|
|
supportedBackends :: [Backend Annex],
|
2010-10-26 19:59:50 +00:00
|
|
|
flags :: M.Map FlagName Flag,
|
2011-01-26 01:02:34 +00:00
|
|
|
repoqueue :: GitQueue.Queue,
|
|
|
|
quiet :: Bool
|
2010-10-12 20:06:10 +00:00
|
|
|
} deriving (Show)
|
|
|
|
|
2010-10-14 01:28:47 +00:00
|
|
|
-- git-annex's monad
|
|
|
|
type Annex = StateT AnnexState IO
|
|
|
|
|
2010-10-14 23:36:11 +00:00
|
|
|
-- annexed filenames are mapped through a backend into keys
|
2010-10-25 22:32:29 +00:00
|
|
|
type KeyName = String
|
2010-10-14 23:36:11 +00:00
|
|
|
type BackendName = String
|
2010-11-07 21:26:21 +00:00
|
|
|
data Key = Key (BackendName, KeyName) deriving (Eq, Ord)
|
2010-10-13 06:31:24 +00:00
|
|
|
|
2010-10-21 20:30:16 +00:00
|
|
|
-- constructs a key in a backend
|
2011-01-26 01:02:34 +00:00
|
|
|
genKey :: Backend a -> KeyName -> Key
|
2010-10-21 20:30:16 +00:00
|
|
|
genKey b f = Key (name b,f)
|
|
|
|
|
2010-10-14 23:36:11 +00:00
|
|
|
-- show a key to convert it to a string; the string includes the
|
|
|
|
-- name of the backend to avoid collisions between key strings
|
2010-10-13 06:31:24 +00:00
|
|
|
instance Show Key where
|
2010-10-14 23:36:11 +00:00
|
|
|
show (Key (b, k)) = b ++ ":" ++ k
|
|
|
|
|
|
|
|
instance Read Key where
|
2010-11-22 19:46:57 +00:00
|
|
|
readsPrec _ s = [(Key (b,k), "")]
|
2010-10-14 23:36:11 +00:00
|
|
|
where
|
|
|
|
l = split ":" s
|
2011-01-09 14:48:04 +00:00
|
|
|
b = if null l then "" else head l
|
2010-10-14 23:36:11 +00:00
|
|
|
k = join ":" $ drop 1 l
|
2010-10-12 20:06:10 +00:00
|
|
|
|
2011-01-05 01:05:31 +00:00
|
|
|
-- for quickcheck
|
|
|
|
instance Arbitrary Key where
|
|
|
|
arbitrary = do
|
|
|
|
backendname <- arbitrary
|
|
|
|
keyname <- arbitrary
|
|
|
|
return $ Key (backendname, keyname)
|
|
|
|
|
|
|
|
prop_idempotent_key_read_show :: Key -> Bool
|
|
|
|
prop_idempotent_key_read_show k
|
2011-01-09 14:48:04 +00:00
|
|
|
-- backend names will never contain colons
|
|
|
|
| elem ':' (backendName k) = True
|
2011-01-05 01:05:31 +00:00
|
|
|
| otherwise = k == (read $ show k)
|
|
|
|
|
2010-10-15 00:05:04 +00:00
|
|
|
backendName :: Key -> BackendName
|
2010-10-31 20:00:32 +00:00
|
|
|
backendName (Key (b,_)) = b
|
2010-10-25 22:32:29 +00:00
|
|
|
keyName :: Key -> KeyName
|
2010-10-31 20:00:32 +00:00
|
|
|
keyName (Key (_,k)) = k
|
2010-10-15 00:05:04 +00:00
|
|
|
|
2010-10-16 20:15:31 +00:00
|
|
|
-- this structure represents a key-value backend
|
2011-01-26 01:02:34 +00:00
|
|
|
data Backend a = Backend {
|
2010-10-12 20:06:10 +00:00
|
|
|
-- name of this backend
|
|
|
|
name :: String,
|
|
|
|
-- converts a filename to a key
|
2011-01-26 01:02:34 +00:00
|
|
|
getKey :: FilePath -> a (Maybe Key),
|
2010-10-12 20:06:10 +00:00
|
|
|
-- stores a file's contents to a key
|
2011-01-26 01:02:34 +00:00
|
|
|
storeFileKey :: FilePath -> Key -> a Bool,
|
2010-10-12 20:06:10 +00:00
|
|
|
-- retrieves a key's contents to a file
|
2011-01-26 01:02:34 +00:00
|
|
|
retrieveKeyFile :: Key -> FilePath -> a Bool,
|
2010-11-28 19:28:20 +00:00
|
|
|
-- removes a key, optionally checking that enough copies are stored
|
|
|
|
-- elsewhere
|
2011-01-26 01:02:34 +00:00
|
|
|
removeKey :: Key -> Maybe Int -> a Bool,
|
2010-10-14 19:31:44 +00:00
|
|
|
-- checks if a backend is storing the content of a key
|
2011-01-26 01:02:34 +00:00
|
|
|
hasKey :: Key -> a Bool,
|
2010-11-13 18:59:27 +00:00
|
|
|
-- called during fsck to check a key
|
2010-11-28 19:28:20 +00:00
|
|
|
-- (second parameter may be the number of copies that there should
|
|
|
|
-- be of the key)
|
2011-01-26 01:02:34 +00:00
|
|
|
fsckKey :: Key -> Maybe Int -> a Bool
|
2010-10-12 19:52:18 +00:00
|
|
|
}
|
2010-10-12 20:06:10 +00:00
|
|
|
|
2011-01-26 01:02:34 +00:00
|
|
|
instance Show (Backend a) where
|
2010-11-22 19:46:57 +00:00
|
|
|
show backend = "Backend { name =\"" ++ name backend ++ "\" }"
|
2011-01-08 19:54:14 +00:00
|
|
|
|
2011-01-26 01:02:34 +00:00
|
|
|
instance Eq (Backend a) where
|
2011-01-08 19:54:14 +00:00
|
|
|
a == b = name a == name b
|