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