git-annex/BackendTypes.hs

70 lines
1.8 KiB
Haskell
Raw Normal View History

2010-10-14 06:52:17 +00:00
{- git-annex backend data types
-
- Mostly only backend implementations should need to import this.
-}
2010-10-12 19:52:18 +00:00
2010-10-14 06:52:17 +00:00
module BackendTypes 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-14 06:36:41 +00:00
import qualified GitRepo as Git
2010-10-12 19:52:18 +00:00
-- command-line flags
data Flag = Force
deriving (Eq, Read, Show)
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,
backends :: [Backend],
flags :: [Flag]
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
type KeyFrag = String
type BackendName = String
data Key = Key (BackendName, KeyFrag) deriving (Eq)
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
instance Show Key where
2010-10-14 23:36:11 +00:00
show (Key (b, k)) = b ++ ":" ++ k
instance Read Key where
readsPrec _ s = [((Key (b,k)) ,"")]
where
l = split ":" s
b = l !! 0
k = join ":" $ drop 1 l
2010-10-12 20:06:10 +00:00
2010-10-15 00:05:04 +00:00
-- pulls the backend name out
backendName :: Key -> BackendName
backendName (Key (b,k)) = b
-- pulls the key fragment out
keyFrag :: Key -> KeyFrag
keyFrag (Key (b,k)) = k
2010-10-12 20:06:10 +00:00
-- this structure represents a key/value backend
data Backend = Backend {
-- name of this backend
name :: String,
-- converts a filename to a key
2010-10-14 01:28:47 +00:00
getKey :: FilePath -> Annex (Maybe Key),
2010-10-12 20:06:10 +00:00
-- stores a file's contents to a key
2010-10-14 01:28:47 +00:00
storeFileKey :: FilePath -> Key -> Annex Bool,
2010-10-12 20:06:10 +00:00
-- retrieves a key's contents to a file
2010-10-14 01:28:47 +00:00
retrieveKeyFile :: Key -> FilePath -> Annex Bool,
2010-10-12 20:06:10 +00:00
-- removes a key
2010-10-14 19:31:44 +00:00
removeKey :: Key -> Annex Bool,
-- checks if a backend is storing the content of a key
hasKey :: Key -> Annex Bool
2010-10-12 19:52:18 +00:00
}
2010-10-12 20:06:10 +00:00
instance Show Backend where
show backend = "Backend { name =\"" ++ (name backend) ++ "\" }"