2011-04-28 00:06:07 +00:00
|
|
|
|
{- A remote that is only accessible by rsync.
|
|
|
|
|
-
|
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Remote.Rsync (remote) where
|
|
|
|
|
|
2012-06-20 17:13:40 +00:00
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
2011-04-28 00:06:07 +00:00
|
|
|
|
import qualified Data.Map as M
|
2012-07-18 19:30:26 +00:00
|
|
|
|
import System.Posix.Process (getProcessID)
|
2011-10-04 02:24:57 +00:00
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
|
import Common.Annex
|
2011-06-02 01:56:04 +00:00
|
|
|
|
import Types.Remote
|
2011-06-30 17:16:57 +00:00
|
|
|
|
import qualified Git
|
2011-04-28 00:06:07 +00:00
|
|
|
|
import Config
|
2011-10-04 04:40:47 +00:00
|
|
|
|
import Annex.Content
|
2011-08-17 00:49:54 +00:00
|
|
|
|
import Remote.Helper.Special
|
|
|
|
|
import Remote.Helper.Encryptable
|
2011-04-28 00:06:07 +00:00
|
|
|
|
import Crypto
|
2012-09-19 18:28:32 +00:00
|
|
|
|
import Utility.Rsync
|
2012-06-06 00:25:32 +00:00
|
|
|
|
import Annex.Perms
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
|
|
|
|
type RsyncUrl = String
|
|
|
|
|
|
2012-05-02 17:08:31 +00:00
|
|
|
|
data RsyncOpts = RsyncOpts
|
|
|
|
|
{ rsyncUrl :: RsyncUrl
|
|
|
|
|
, rsyncOptions :: [CommandParam]
|
|
|
|
|
, rsyncShellEscape :: Bool
|
2011-04-28 00:06:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
|
remote :: RemoteType
|
2011-04-28 00:06:07 +00:00
|
|
|
|
remote = RemoteType {
|
|
|
|
|
typename = "rsync",
|
|
|
|
|
enumerate = findSpecialRemotes "rsyncurl",
|
|
|
|
|
generate = gen,
|
|
|
|
|
setup = rsyncSetup
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
|
gen :: Git.Repo -> UUID -> Maybe RemoteConfig -> Annex Remote
|
2011-04-28 00:06:07 +00:00
|
|
|
|
gen r u c = do
|
2012-05-02 17:08:31 +00:00
|
|
|
|
o <- genRsyncOpts r c
|
2011-04-28 00:06:07 +00:00
|
|
|
|
cst <- remoteCost r expensiveRemoteCost
|
|
|
|
|
return $ encryptableRemote c
|
|
|
|
|
(storeEncrypted o)
|
|
|
|
|
(retrieveEncrypted o)
|
2012-07-22 17:48:50 +00:00
|
|
|
|
Remote
|
|
|
|
|
{ uuid = u
|
|
|
|
|
, cost = cst
|
|
|
|
|
, name = Git.repoDescribe r
|
|
|
|
|
, storeKey = store o
|
|
|
|
|
, retrieveKeyFile = retrieve o
|
|
|
|
|
, retrieveKeyFileCheap = retrieveCheap o
|
|
|
|
|
, removeKey = remove o
|
|
|
|
|
, hasKey = checkPresent r o
|
|
|
|
|
, hasKeyCheap = False
|
|
|
|
|
, whereisKey = Nothing
|
|
|
|
|
, config = Nothing
|
|
|
|
|
, repo = r
|
2012-08-26 18:26:43 +00:00
|
|
|
|
, localpath = if rsyncUrlIsPath $ rsyncUrl o
|
2012-07-22 17:48:50 +00:00
|
|
|
|
then Just $ rsyncUrl o
|
|
|
|
|
else Nothing
|
2012-08-26 19:39:02 +00:00
|
|
|
|
, readonly = False
|
2012-07-22 17:48:50 +00:00
|
|
|
|
, remotetype = remote
|
|
|
|
|
}
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
2012-05-02 17:08:31 +00:00
|
|
|
|
genRsyncOpts :: Git.Repo -> Maybe RemoteConfig -> Annex RsyncOpts
|
|
|
|
|
genRsyncOpts r c = do
|
2012-03-22 03:41:01 +00:00
|
|
|
|
url <- getRemoteConfig r "rsyncurl" (error "missing rsyncurl")
|
2012-05-02 17:08:31 +00:00
|
|
|
|
opts <- map Param . filter safe . words
|
|
|
|
|
<$> getRemoteConfig r "rsync-options" ""
|
|
|
|
|
let escape = maybe True (\m -> M.lookup "shellescape" m /= Just "no") c
|
|
|
|
|
return $ RsyncOpts url opts escape
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
safe o
|
|
|
|
|
-- Don't allow user to pass --delete to rsync;
|
|
|
|
|
-- that could cause it to delete other keys
|
|
|
|
|
-- in the same hash bucket as a key it sends.
|
|
|
|
|
| o == "--delete" = False
|
|
|
|
|
| o == "--delete-excluded" = False
|
|
|
|
|
| otherwise = True
|
2011-04-28 00:30:43 +00:00
|
|
|
|
|
2011-04-28 00:06:07 +00:00
|
|
|
|
rsyncSetup :: UUID -> RemoteConfig -> Annex RemoteConfig
|
|
|
|
|
rsyncSetup u c = do
|
|
|
|
|
-- verify configuration is sane
|
2011-07-15 16:47:14 +00:00
|
|
|
|
let url = fromMaybe (error "Specify rsyncurl=") $
|
2011-05-15 06:49:43 +00:00
|
|
|
|
M.lookup "rsyncurl" c
|
2011-04-28 00:06:07 +00:00
|
|
|
|
c' <- encryptionSetup c
|
|
|
|
|
|
|
|
|
|
-- The rsyncurl is stored in git config, not only in this remote's
|
|
|
|
|
-- persistant state, so it can vary between hosts.
|
|
|
|
|
gitConfigSpecialRemote u c' "rsyncurl" url
|
|
|
|
|
return c'
|
|
|
|
|
|
2011-11-18 16:53:48 +00:00
|
|
|
|
rsyncEscape :: RsyncOpts -> String -> String
|
|
|
|
|
rsyncEscape o s
|
2012-05-02 17:08:31 +00:00
|
|
|
|
| rsyncShellEscape o && rsyncUrlIsShell (rsyncUrl o) = shellEscape s
|
2011-11-18 16:53:48 +00:00
|
|
|
|
| otherwise = s
|
|
|
|
|
|
2011-12-02 19:50:27 +00:00
|
|
|
|
rsyncUrls :: RsyncOpts -> Key -> [String]
|
|
|
|
|
rsyncUrls o k = map use annexHashes
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
use h = rsyncUrl o </> h k </> rsyncEscape o (f </> f)
|
|
|
|
|
f = keyFile k
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
2012-09-21 18:50:14 +00:00
|
|
|
|
store :: RsyncOpts -> Key -> AssociatedFile -> MeterUpdate -> Annex Bool
|
2012-09-20 17:50:21 +00:00
|
|
|
|
store o k _f p = rsyncSend o p k <=< inRepo $ gitAnnexLocation k
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
2012-09-21 18:50:14 +00:00
|
|
|
|
storeEncrypted :: RsyncOpts -> (Cipher, Key) -> Key -> MeterUpdate -> Annex Bool
|
2012-09-19 20:08:37 +00:00
|
|
|
|
storeEncrypted o (cipher, enck) k p = withTmp enck $ \tmp -> do
|
2011-11-29 02:43:51 +00:00
|
|
|
|
src <- inRepo $ gitAnnexLocation k
|
2011-11-08 19:34:10 +00:00
|
|
|
|
liftIO $ withEncryptedContent cipher (L.readFile src) $ L.writeFile tmp
|
2012-09-20 17:50:21 +00:00
|
|
|
|
rsyncSend o p enck tmp
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
2012-07-01 20:59:54 +00:00
|
|
|
|
retrieve :: RsyncOpts -> Key -> AssociatedFile -> FilePath -> Annex Bool
|
2012-09-20 17:50:21 +00:00
|
|
|
|
retrieve o k _ f = untilTrue (rsyncUrls o k) $ \u -> rsyncRemote o Nothing
|
2012-01-20 17:23:11 +00:00
|
|
|
|
-- use inplace when retrieving to support resuming
|
|
|
|
|
[ Param "--inplace"
|
|
|
|
|
, Param u
|
|
|
|
|
, Param f
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
retrieveCheap :: RsyncOpts -> Key -> FilePath -> Annex Bool
|
2012-07-01 20:59:54 +00:00
|
|
|
|
retrieveCheap o k f = ifM (preseedTmp k f) ( retrieve o k undefined f , return False )
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
2012-03-04 07:36:39 +00:00
|
|
|
|
retrieveEncrypted :: RsyncOpts -> (Cipher, Key) -> Key -> FilePath -> Annex Bool
|
|
|
|
|
retrieveEncrypted o (cipher, enck) _ f = withTmp enck $ \tmp -> do
|
2012-07-01 20:59:54 +00:00
|
|
|
|
ifM (retrieve o enck undefined tmp)
|
2012-03-16 00:39:25 +00:00
|
|
|
|
( liftIO $ catchBoolIO $ do
|
2011-04-28 00:06:07 +00:00
|
|
|
|
withDecryptedContent cipher (L.readFile tmp) $ L.writeFile f
|
|
|
|
|
return True
|
2012-03-16 00:39:25 +00:00
|
|
|
|
, return False
|
|
|
|
|
)
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
|
|
|
|
remove :: RsyncOpts -> Key -> Annex Bool
|
2011-12-21 20:56:48 +00:00
|
|
|
|
remove o k = withRsyncScratchDir $ \tmp -> liftIO $ do
|
|
|
|
|
{- Send an empty directory to rysnc to make it delete. -}
|
|
|
|
|
let dummy = tmp </> keyFile k
|
|
|
|
|
createDirectoryIfMissing True dummy
|
|
|
|
|
rsync $ rsyncOptions o ++
|
|
|
|
|
map (\s -> Param $ "--include=" ++ s) includes ++
|
|
|
|
|
[ Param "--exclude=*" -- exclude everything else
|
|
|
|
|
, Params "--quiet --delete --recursive"
|
|
|
|
|
, partialParams
|
|
|
|
|
, Param $ addTrailingPathSeparator dummy
|
|
|
|
|
, Param $ rsyncUrl o
|
|
|
|
|
]
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
{- Specify include rules to match the directories where the
|
|
|
|
|
- content could be. Note that the parent directories have
|
|
|
|
|
- to also be explicitly included, due to how rsync
|
|
|
|
|
- traverses directories. -}
|
|
|
|
|
includes = concatMap use annexHashes
|
|
|
|
|
use h = let dir = h k in
|
|
|
|
|
[ parentDir dir
|
|
|
|
|
, dir
|
|
|
|
|
-- match content directory and anything in it
|
|
|
|
|
, dir </> keyFile k </> "***"
|
|
|
|
|
]
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
2011-11-09 22:33:15 +00:00
|
|
|
|
checkPresent :: Git.Repo -> RsyncOpts -> Key -> Annex (Either String Bool)
|
2011-04-28 00:06:07 +00:00
|
|
|
|
checkPresent r o k = do
|
2011-07-19 18:07:23 +00:00
|
|
|
|
showAction $ "checking " ++ Git.repoDescribe r
|
2011-12-02 19:50:27 +00:00
|
|
|
|
-- note: Does not currently differentiate between rsync failing
|
2011-04-28 00:06:07 +00:00
|
|
|
|
-- to connect, and the file not being present.
|
2011-12-02 19:50:27 +00:00
|
|
|
|
Right <$> check
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
check = untilTrue (rsyncUrls o k) $ \u ->
|
|
|
|
|
liftIO $ catchBoolIO $ do
|
|
|
|
|
withQuietOutput createProcessSuccess $
|
|
|
|
|
proc "rsync" $ toCommand $
|
|
|
|
|
rsyncOptions o ++ [Param u]
|
|
|
|
|
return True
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
|
|
|
|
{- Rsync params to enable resumes of sending files safely,
|
|
|
|
|
- ensure that files are only moved into place once complete
|
|
|
|
|
-}
|
|
|
|
|
partialParams :: CommandParam
|
2012-07-10 18:40:31 +00:00
|
|
|
|
partialParams = Params "--partial --partial-dir=.rsync-partial"
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
|
|
|
|
{- Runs an action in an empty scratch directory that can be used to build
|
|
|
|
|
- up trees for rsync. -}
|
|
|
|
|
withRsyncScratchDir :: (FilePath -> Annex Bool) -> Annex Bool
|
|
|
|
|
withRsyncScratchDir a = do
|
2011-07-15 16:47:14 +00:00
|
|
|
|
pid <- liftIO getProcessID
|
2011-11-08 19:34:10 +00:00
|
|
|
|
t <- fromRepo gitAnnexTmpDir
|
2012-06-06 00:25:32 +00:00
|
|
|
|
createAnnexDirectory t
|
2011-11-08 19:34:10 +00:00
|
|
|
|
let tmp = t </> "rsynctmp" </> show pid
|
2011-04-28 00:06:07 +00:00
|
|
|
|
nuke tmp
|
2011-07-15 16:47:14 +00:00
|
|
|
|
liftIO $ createDirectoryIfMissing True tmp
|
2012-01-03 04:29:27 +00:00
|
|
|
|
nuke tmp `after` a tmp
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
nuke d = liftIO $ whenM (doesDirectoryExist d) $
|
|
|
|
|
removeDirectoryRecursive d
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
2012-09-21 18:50:14 +00:00
|
|
|
|
rsyncRemote :: RsyncOpts -> (Maybe MeterUpdate) -> [CommandParam] -> Annex Bool
|
2012-09-20 17:50:21 +00:00
|
|
|
|
rsyncRemote o callback params = do
|
2011-07-19 18:07:23 +00:00
|
|
|
|
showOutput -- make way for progress bar
|
2012-09-20 17:50:21 +00:00
|
|
|
|
ifM (liftIO $ (maybe rsync rsyncProgress callback) ps)
|
2012-03-16 00:39:25 +00:00
|
|
|
|
( return True
|
|
|
|
|
, do
|
2011-04-28 00:06:07 +00:00
|
|
|
|
showLongNote "rsync failed -- run git annex again to resume file transfer"
|
2012-03-16 00:39:25 +00:00
|
|
|
|
return False
|
|
|
|
|
)
|
2012-11-11 04:51:07 +00:00
|
|
|
|
where
|
|
|
|
|
defaultParams = [Params "--progress"]
|
|
|
|
|
ps = rsyncOptions o ++ defaultParams ++ params
|
2011-04-28 00:06:07 +00:00
|
|
|
|
|
|
|
|
|
{- To send a single key is slightly tricky; need to build up a temporary
|
|
|
|
|
directory structure to pass to rsync so it can create the hash
|
|
|
|
|
directories. -}
|
2012-09-21 18:50:14 +00:00
|
|
|
|
rsyncSend :: RsyncOpts -> MeterUpdate -> Key -> FilePath -> Annex Bool
|
2012-09-20 17:50:21 +00:00
|
|
|
|
rsyncSend o callback k src = withRsyncScratchDir $ \tmp -> do
|
2011-12-15 22:11:42 +00:00
|
|
|
|
let dest = tmp </> Prelude.head (keyPaths k)
|
2011-07-15 16:47:14 +00:00
|
|
|
|
liftIO $ createDirectoryIfMissing True $ parentDir dest
|
2011-04-28 00:06:07 +00:00
|
|
|
|
liftIO $ createLink src dest
|
2012-09-20 17:50:21 +00:00
|
|
|
|
rsyncRemote o (Just callback)
|
2011-04-28 00:06:07 +00:00
|
|
|
|
[ Param "--recursive"
|
|
|
|
|
, partialParams
|
|
|
|
|
-- tmp/ to send contents of tmp dir
|
|
|
|
|
, Param $ addTrailingPathSeparator tmp
|
|
|
|
|
, Param $ rsyncUrl o
|
|
|
|
|
]
|