2011-04-08 20:44:43 +00:00
|
|
|
|
{- Using bup as a remote.
|
|
|
|
|
-
|
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Remote.Bup (remote) where
|
|
|
|
|
|
2011-04-17 03:01:29 +00:00
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
2011-04-08 20:44:43 +00:00
|
|
|
|
import IO
|
|
|
|
|
import Control.Exception.Extensible (IOException)
|
|
|
|
|
import qualified Data.Map as M
|
2011-04-09 16:34:49 +00:00
|
|
|
|
import Control.Monad (unless, when)
|
2011-04-08 20:44:43 +00:00
|
|
|
|
import Control.Monad.State (liftIO)
|
|
|
|
|
import System.Process
|
|
|
|
|
import System.Exit
|
2011-04-09 16:34:49 +00:00
|
|
|
|
import System.FilePath
|
|
|
|
|
import Data.List.Utils
|
2011-04-17 04:57:11 +00:00
|
|
|
|
import System.Cmd.Utils
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
|
|
|
|
import RemoteClass
|
|
|
|
|
import Types
|
|
|
|
|
import qualified GitRepo as Git
|
|
|
|
|
import qualified Annex
|
|
|
|
|
import UUID
|
|
|
|
|
import Locations
|
|
|
|
|
import Config
|
|
|
|
|
import Utility
|
|
|
|
|
import Messages
|
2011-04-09 16:34:49 +00:00
|
|
|
|
import Ssh
|
2011-04-16 17:25:27 +00:00
|
|
|
|
import Remote.Special
|
2011-04-17 04:40:23 +00:00
|
|
|
|
import Remote.Encryptable
|
2011-04-17 03:01:29 +00:00
|
|
|
|
import Crypto
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
2011-04-09 19:36:54 +00:00
|
|
|
|
type BupRepo = String
|
|
|
|
|
|
2011-04-08 20:44:43 +00:00
|
|
|
|
remote :: RemoteType Annex
|
|
|
|
|
remote = RemoteType {
|
|
|
|
|
typename = "bup",
|
2011-04-09 16:41:17 +00:00
|
|
|
|
enumerate = findSpecialRemotes "buprepo",
|
2011-04-08 20:44:43 +00:00
|
|
|
|
generate = gen,
|
|
|
|
|
setup = bupSetup
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 19:09:36 +00:00
|
|
|
|
gen :: Git.Repo -> UUID -> Maybe RemoteConfig -> Annex (Remote Annex)
|
2011-04-08 20:44:43 +00:00
|
|
|
|
gen r u c = do
|
2011-04-09 16:41:17 +00:00
|
|
|
|
buprepo <- getConfig r "buprepo" (error "missing buprepo")
|
|
|
|
|
cst <- remoteCost r (if bupLocal buprepo then semiCheapRemoteCost else expensiveRemoteCost)
|
2011-04-09 19:36:54 +00:00
|
|
|
|
bupr <- liftIO $ bup2GitRemote buprepo
|
|
|
|
|
(u', bupr') <- getBupUUID bupr u
|
2011-04-09 01:37:59 +00:00
|
|
|
|
|
2011-04-17 04:40:23 +00:00
|
|
|
|
return $ encryptableRemote c
|
2011-04-17 03:01:29 +00:00
|
|
|
|
(storeEncrypted r buprepo)
|
|
|
|
|
(retrieveEncrypted buprepo)
|
|
|
|
|
Remote {
|
2011-04-09 16:41:17 +00:00
|
|
|
|
uuid = u',
|
2011-04-08 20:44:43 +00:00
|
|
|
|
cost = cst,
|
|
|
|
|
name = Git.repoDescribe r,
|
2011-04-09 16:41:17 +00:00
|
|
|
|
storeKey = store r buprepo,
|
|
|
|
|
retrieveKeyFile = retrieve buprepo,
|
2011-04-08 20:44:43 +00:00
|
|
|
|
removeKey = remove,
|
2011-04-17 03:01:29 +00:00
|
|
|
|
hasKey = checkPresent r bupr',
|
2011-04-08 20:44:43 +00:00
|
|
|
|
hasKeyCheap = True,
|
|
|
|
|
config = c
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-15 19:09:36 +00:00
|
|
|
|
bupSetup :: UUID -> RemoteConfig -> Annex RemoteConfig
|
2011-04-08 20:44:43 +00:00
|
|
|
|
bupSetup u c = do
|
|
|
|
|
-- verify configuration is sane
|
2011-04-09 16:45:30 +00:00
|
|
|
|
let buprepo = case M.lookup "buprepo" c of
|
|
|
|
|
Nothing -> error "Specify buprepo="
|
2011-04-08 20:44:43 +00:00
|
|
|
|
Just r -> r
|
2011-04-16 17:25:27 +00:00
|
|
|
|
c' <- encryptionSetup c
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
|
|
|
|
-- bup init will create the repository.
|
|
|
|
|
-- (If the repository already exists, bup init again appears safe.)
|
|
|
|
|
showNote "bup init"
|
2011-04-09 16:41:17 +00:00
|
|
|
|
ok <- bup "init" buprepo []
|
2011-04-08 20:44:43 +00:00
|
|
|
|
unless ok $ error "bup init failed"
|
|
|
|
|
|
2011-04-09 16:41:17 +00:00
|
|
|
|
storeBupUUID u buprepo
|
2011-04-09 16:34:49 +00:00
|
|
|
|
|
2011-04-09 16:41:17 +00:00
|
|
|
|
-- The buprepo is stored in git config, as well as this repo's
|
2011-04-08 20:44:43 +00:00
|
|
|
|
-- persistant state, so it can vary between hosts.
|
2011-04-16 17:25:27 +00:00
|
|
|
|
gitConfigSpecialRemote u c' "buprepo" buprepo
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
2011-04-16 17:25:27 +00:00
|
|
|
|
return c'
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
2011-04-09 19:36:54 +00:00
|
|
|
|
bupParams :: String -> BupRepo -> [CommandParam] -> [CommandParam]
|
2011-04-09 16:41:17 +00:00
|
|
|
|
bupParams command buprepo params =
|
|
|
|
|
(Param command) : [Param "-r", Param buprepo] ++ params
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
2011-04-09 19:36:54 +00:00
|
|
|
|
bup :: String -> BupRepo -> [CommandParam] -> Annex Bool
|
2011-04-09 16:41:17 +00:00
|
|
|
|
bup command buprepo params = do
|
2011-04-08 20:44:43 +00:00
|
|
|
|
showProgress -- make way for bup output
|
2011-04-09 16:41:17 +00:00
|
|
|
|
liftIO $ boolSystem "bup" $ bupParams command buprepo params
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
2011-04-17 04:34:38 +00:00
|
|
|
|
pipeBup :: [CommandParam] -> Maybe Handle -> Maybe Handle -> IO Bool
|
|
|
|
|
pipeBup params inh outh = do
|
|
|
|
|
p <- runProcess "bup" (toCommand params)
|
|
|
|
|
Nothing Nothing inh outh Nothing
|
|
|
|
|
ok <- waitForProcess p
|
|
|
|
|
case ok of
|
|
|
|
|
ExitSuccess -> return True
|
|
|
|
|
_ -> return False
|
|
|
|
|
|
2011-04-17 03:01:29 +00:00
|
|
|
|
bupSplitParams :: Git.Repo -> BupRepo -> Key -> CommandParam -> Annex [CommandParam]
|
|
|
|
|
bupSplitParams r buprepo k src = do
|
|
|
|
|
o <- getConfig r "bup-split-options" ""
|
|
|
|
|
let os = map Param $ words o
|
|
|
|
|
showProgress -- make way for bup output
|
|
|
|
|
return $ bupParams "split" buprepo
|
|
|
|
|
(os ++ [Param "-n", Param (show k), src])
|
|
|
|
|
|
2011-04-09 19:36:54 +00:00
|
|
|
|
store :: Git.Repo -> BupRepo -> Key -> Annex Bool
|
2011-04-09 16:41:17 +00:00
|
|
|
|
store r buprepo k = do
|
2011-04-08 20:44:43 +00:00
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
|
let src = gitAnnexLocation g k
|
2011-04-17 03:01:29 +00:00
|
|
|
|
params <- bupSplitParams r buprepo k (File src)
|
|
|
|
|
liftIO $ boolSystem "bup" params
|
|
|
|
|
|
|
|
|
|
storeEncrypted :: Git.Repo -> BupRepo -> (Cipher, Key) -> Key -> Annex Bool
|
|
|
|
|
storeEncrypted r buprepo (cipher, enck) k = do
|
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
|
let src = gitAnnexLocation g k
|
|
|
|
|
params <- bupSplitParams r buprepo enck (Param "-")
|
2011-04-17 04:57:11 +00:00
|
|
|
|
liftIO $ catchBool $ do
|
2011-04-17 03:01:29 +00:00
|
|
|
|
content <- L.readFile src
|
2011-04-17 17:11:38 +00:00
|
|
|
|
withEncryptedHandle cipher content $ \h -> do
|
2011-04-17 04:34:38 +00:00
|
|
|
|
pipeBup params (Just h) Nothing
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
2011-04-09 19:36:54 +00:00
|
|
|
|
retrieve :: BupRepo -> Key -> FilePath -> Annex Bool
|
2011-04-09 16:41:17 +00:00
|
|
|
|
retrieve buprepo k f = do
|
|
|
|
|
let params = bupParams "join" buprepo [Param $ show k]
|
2011-04-17 04:57:11 +00:00
|
|
|
|
liftIO $ catchBool $ do
|
2011-04-08 20:44:43 +00:00
|
|
|
|
tofile <- openFile f WriteMode
|
2011-04-17 04:34:38 +00:00
|
|
|
|
pipeBup params Nothing (Just tofile)
|
2011-04-08 20:44:43 +00:00
|
|
|
|
|
2011-04-17 03:01:29 +00:00
|
|
|
|
retrieveEncrypted :: BupRepo -> (Cipher, Key) -> FilePath -> Annex Bool
|
2011-04-17 04:57:11 +00:00
|
|
|
|
retrieveEncrypted buprepo (cipher, enck) f = do
|
|
|
|
|
let params = bupParams "join" buprepo [Param $ show enck]
|
|
|
|
|
liftIO $ catchBool $ do
|
|
|
|
|
(pid, h) <- hPipeFrom "bup" $ toCommand params
|
|
|
|
|
content <- L.hGetContents h
|
|
|
|
|
withDecryptedContent cipher content $ L.writeFile f
|
|
|
|
|
forceSuccess pid
|
|
|
|
|
return True
|
2011-04-17 03:01:29 +00:00
|
|
|
|
|
2011-04-08 20:44:43 +00:00
|
|
|
|
remove :: Key -> Annex Bool
|
|
|
|
|
remove _ = do
|
|
|
|
|
warning "content cannot be removed from bup remote"
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
{- Bup does not provide a way to tell if a given dataset is present
|
|
|
|
|
- in a bup repository. One way it to check if the git repository has
|
|
|
|
|
- a branch matching the name (as created by bup split -n).
|
|
|
|
|
-}
|
2011-04-09 19:36:54 +00:00
|
|
|
|
checkPresent :: Git.Repo -> Git.Repo -> Key -> Annex (Either IOException Bool)
|
|
|
|
|
checkPresent r bupr k
|
|
|
|
|
| Git.repoIsUrl bupr = do
|
|
|
|
|
showNote ("checking " ++ Git.repoDescribe r ++ "...")
|
|
|
|
|
ok <- onBupRemote bupr boolSystem "git" params
|
|
|
|
|
return $ Right ok
|
|
|
|
|
| otherwise = liftIO $ try $ boolSystem "git" $ Git.gitCommandLine bupr params
|
|
|
|
|
where
|
|
|
|
|
params =
|
|
|
|
|
[ Params "show-ref --quiet --verify"
|
|
|
|
|
, Param $ "refs/heads/" ++ show k]
|
2011-04-09 16:34:49 +00:00
|
|
|
|
|
|
|
|
|
{- Store UUID in the annex.uuid setting of the bup repository. -}
|
2011-04-09 19:36:54 +00:00
|
|
|
|
storeBupUUID :: UUID -> BupRepo -> Annex ()
|
2011-04-09 16:41:17 +00:00
|
|
|
|
storeBupUUID u buprepo = do
|
|
|
|
|
r <- liftIO $ bup2GitRemote buprepo
|
2011-04-09 16:34:49 +00:00
|
|
|
|
if Git.repoIsUrl r
|
|
|
|
|
then do
|
|
|
|
|
showNote "storing uuid"
|
2011-04-09 19:36:54 +00:00
|
|
|
|
ok <- onBupRemote r boolSystem "git"
|
|
|
|
|
[Params $ "config annex.uuid " ++ u]
|
2011-04-09 16:34:49 +00:00
|
|
|
|
unless ok $ do error "ssh failed"
|
|
|
|
|
else liftIO $ do
|
|
|
|
|
r' <- Git.configRead r
|
|
|
|
|
let olduuid = Git.configGet r' "annex.uuid" ""
|
|
|
|
|
when (olduuid == "") $
|
|
|
|
|
Git.run r' "config" [Param "annex.uuid", Param u]
|
|
|
|
|
|
2011-04-09 19:36:54 +00:00
|
|
|
|
onBupRemote :: Git.Repo -> (FilePath -> [CommandParam] -> IO a) -> FilePath -> [CommandParam] -> Annex a
|
|
|
|
|
onBupRemote r a command params = do
|
|
|
|
|
let dir = shellEscape (Git.workTree r)
|
|
|
|
|
sshparams <- sshToRepo r [Param $
|
|
|
|
|
"cd " ++ dir ++ " && " ++ (unwords $ command : toCommand params)]
|
|
|
|
|
liftIO $ a "ssh" sshparams
|
|
|
|
|
|
2011-04-09 16:41:17 +00:00
|
|
|
|
{- Allow for bup repositories on removable media by checking
|
2011-04-09 16:59:18 +00:00
|
|
|
|
- local bup repositories to see if they are available, and getting their
|
|
|
|
|
- uuid (which may be different from the stored uuid for the bup remote).
|
|
|
|
|
-
|
|
|
|
|
- If a bup repository is not available, returns a dummy uuid of "".
|
|
|
|
|
- This will cause checkPresent to indicate nothing from the bup remote
|
|
|
|
|
- is known to be present.
|
2011-04-09 19:36:54 +00:00
|
|
|
|
-
|
|
|
|
|
- Also, returns a version of the repo with config read, if it is local.
|
2011-04-09 16:59:18 +00:00
|
|
|
|
-}
|
2011-04-09 19:36:54 +00:00
|
|
|
|
getBupUUID :: Git.Repo -> UUID -> Annex (UUID, Git.Repo)
|
|
|
|
|
getBupUUID r u
|
|
|
|
|
| Git.repoIsUrl r = return (u, r)
|
|
|
|
|
| otherwise = liftIO $ do
|
|
|
|
|
ret <- try $ Git.configRead r
|
|
|
|
|
case ret of
|
|
|
|
|
Right r' -> return (Git.configGet r' "annex.uuid" "", r')
|
|
|
|
|
Left _ -> return ("", r)
|
2011-04-09 16:41:17 +00:00
|
|
|
|
|
2011-04-09 16:34:49 +00:00
|
|
|
|
{- Converts a bup remote path spec into a Git.Repo. There are some
|
|
|
|
|
- differences in path representation between git and bup. -}
|
2011-04-09 19:36:54 +00:00
|
|
|
|
bup2GitRemote :: BupRepo -> IO Git.Repo
|
2011-04-09 16:34:49 +00:00
|
|
|
|
bup2GitRemote "" = do
|
|
|
|
|
-- bup -r "" operates on ~/.bup
|
|
|
|
|
h <- myHomeDir
|
|
|
|
|
Git.repoFromAbsPath $ h </> ".bup"
|
|
|
|
|
bup2GitRemote r
|
|
|
|
|
| bupLocal r =
|
|
|
|
|
if r !! 0 == '/'
|
|
|
|
|
then Git.repoFromAbsPath r
|
|
|
|
|
else error "please specify an absolute path"
|
|
|
|
|
| otherwise = Git.repoFromUrl $ "ssh://" ++ host ++ slash dir
|
|
|
|
|
where
|
|
|
|
|
bits = split ":" r
|
|
|
|
|
host = bits !! 0
|
|
|
|
|
dir = join ":" $ drop 1 bits
|
|
|
|
|
-- "host:~user/dir" is not supported specially by bup;
|
|
|
|
|
-- "host:dir" is relative to the home directory;
|
|
|
|
|
-- "host:" goes in ~/.bup
|
|
|
|
|
slash d
|
|
|
|
|
| d == "" = "/~/.bup"
|
|
|
|
|
| d !! 0 == '/' = d
|
|
|
|
|
| otherwise = "/~/" ++ d
|
|
|
|
|
|
2011-04-09 19:36:54 +00:00
|
|
|
|
bupLocal :: BupRepo -> Bool
|
2011-04-09 16:34:49 +00:00
|
|
|
|
bupLocal = notElem ':'
|