2016-12-06 16:19:47 +00:00
|
|
|
{- git remotes using the git-annex P2P protocol
|
|
|
|
-
|
|
|
|
- Copyright 2016 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Remote.P2P (
|
|
|
|
remote,
|
|
|
|
chainGen
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Annex.Common
|
2016-12-06 19:08:00 +00:00
|
|
|
import qualified Annex
|
|
|
|
import qualified P2P.Protocol as P2P
|
2016-12-06 16:19:47 +00:00
|
|
|
import P2P.Address
|
2016-12-06 19:08:00 +00:00
|
|
|
import P2P.Annex
|
2016-12-06 19:40:31 +00:00
|
|
|
import P2P.IO
|
2016-12-06 19:49:39 +00:00
|
|
|
import P2P.Auth
|
2016-12-06 16:19:47 +00:00
|
|
|
import Types.Remote
|
|
|
|
import Types.GitConfig
|
|
|
|
import qualified Git
|
2016-12-06 19:49:39 +00:00
|
|
|
import Annex.UUID
|
2016-12-06 16:19:47 +00:00
|
|
|
import Config
|
|
|
|
import Config.Cost
|
|
|
|
import Remote.Helper.Git
|
2016-12-07 18:25:01 +00:00
|
|
|
import Messages.Progress
|
2016-12-06 19:08:00 +00:00
|
|
|
import Utility.Metered
|
2016-12-06 19:49:39 +00:00
|
|
|
import Utility.AuthToken
|
2016-12-06 19:08:00 +00:00
|
|
|
import Types.NumCopies
|
|
|
|
|
|
|
|
import Control.Concurrent
|
|
|
|
import Control.Concurrent.STM
|
2016-12-06 16:19:47 +00:00
|
|
|
|
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
|
|
|
typename = "p2p",
|
|
|
|
-- Remote.Git takes care of enumerating P2P remotes,
|
|
|
|
-- and will call chainGen on them.
|
|
|
|
enumerate = const (return []),
|
|
|
|
generate = \_ _ _ _ -> return Nothing,
|
|
|
|
setup = error "P2P remotes are set up using git-annex p2p"
|
|
|
|
}
|
|
|
|
|
|
|
|
chainGen :: P2PAddress -> Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
|
|
|
chainGen addr r u c gc = do
|
2016-12-06 19:08:00 +00:00
|
|
|
connpool <- mkConnectionPool
|
2017-01-06 19:23:30 +00:00
|
|
|
cst <- remoteCost gc veryExpensiveRemoteCost
|
2016-12-06 16:19:47 +00:00
|
|
|
let this = Remote
|
|
|
|
{ uuid = u
|
|
|
|
, cost = cst
|
|
|
|
, name = Git.repoDescribe r
|
2016-12-07 16:39:28 +00:00
|
|
|
, storeKey = store u addr connpool
|
|
|
|
, retrieveKeyFile = retrieve u addr connpool
|
2016-12-06 16:19:47 +00:00
|
|
|
, retrieveKeyFileCheap = \_ _ _ -> return False
|
2016-12-07 16:39:28 +00:00
|
|
|
, removeKey = remove u addr connpool
|
2016-12-06 19:08:00 +00:00
|
|
|
, lockContent = Just (lock u addr connpool)
|
2016-12-07 16:39:28 +00:00
|
|
|
, checkPresent = checkpresent u addr connpool
|
2016-12-06 16:19:47 +00:00
|
|
|
, checkPresentCheap = False
|
|
|
|
, whereisKey = Nothing
|
|
|
|
, remoteFsck = Nothing
|
|
|
|
, repairRepo = Nothing
|
|
|
|
, config = c
|
|
|
|
, localpath = Nothing
|
|
|
|
, repo = r
|
2017-05-11 20:03:50 +00:00
|
|
|
, gitconfig = gc { remoteGitConfig = extractGitConfig r }
|
2016-12-06 16:19:47 +00:00
|
|
|
, readonly = False
|
|
|
|
, availability = GloballyAvailable
|
|
|
|
, remotetype = remote
|
|
|
|
, mkUnavailable = return Nothing
|
|
|
|
, getInfo = gitRepoInfo this
|
|
|
|
, claimUrl = Nothing
|
|
|
|
, checkUrl = Nothing
|
|
|
|
}
|
2016-12-06 19:08:00 +00:00
|
|
|
return (Just this)
|
|
|
|
|
2016-12-07 16:39:28 +00:00
|
|
|
store :: UUID -> P2PAddress -> ConnectionPool -> Key -> AssociatedFile -> MeterUpdate -> Annex Bool
|
2016-12-07 18:25:01 +00:00
|
|
|
store u addr connpool k af p =
|
|
|
|
metered (Just p) k $ \p' -> fromMaybe False
|
|
|
|
<$> runProto u addr connpool (P2P.put k af p')
|
2016-12-06 19:08:00 +00:00
|
|
|
|
2016-12-07 16:39:28 +00:00
|
|
|
retrieve :: UUID -> P2PAddress -> ConnectionPool -> Key -> AssociatedFile -> FilePath -> MeterUpdate -> Annex (Bool, Verification)
|
2016-12-07 18:25:01 +00:00
|
|
|
retrieve u addr connpool k af dest p = unVerified $
|
|
|
|
metered (Just p) k $ \p' -> fromMaybe False
|
|
|
|
<$> runProto u addr connpool (P2P.get dest k af p')
|
2016-12-06 19:08:00 +00:00
|
|
|
|
2016-12-07 16:39:28 +00:00
|
|
|
remove :: UUID -> P2PAddress -> ConnectionPool -> Key -> Annex Bool
|
|
|
|
remove u addr connpool k = fromMaybe False
|
|
|
|
<$> runProto u addr connpool (P2P.remove k)
|
2016-12-06 19:08:00 +00:00
|
|
|
|
2016-12-07 16:39:28 +00:00
|
|
|
checkpresent :: UUID -> P2PAddress -> ConnectionPool -> Key -> Annex Bool
|
|
|
|
checkpresent u addr connpool k = maybe unavail return
|
|
|
|
=<< runProto u addr connpool (P2P.checkPresent k)
|
2016-12-06 19:08:00 +00:00
|
|
|
where
|
|
|
|
unavail = giveup "can't connect to peer"
|
|
|
|
|
|
|
|
lock :: UUID -> P2PAddress -> ConnectionPool -> Key -> (VerifiedCopy -> Annex r) -> Annex r
|
2016-12-07 16:39:28 +00:00
|
|
|
lock u addr connpool k callback =
|
|
|
|
withConnection u addr connpool $ \conn -> do
|
2016-12-06 19:08:00 +00:00
|
|
|
connv <- liftIO $ newMVar conn
|
|
|
|
let runproto d p = do
|
|
|
|
c <- liftIO $ takeMVar connv
|
|
|
|
(c', mr) <- runProto' p c
|
|
|
|
liftIO $ putMVar connv c'
|
|
|
|
return (fromMaybe d mr)
|
|
|
|
r <- P2P.lockContentWhile runproto k go
|
|
|
|
conn' <- liftIO $ takeMVar connv
|
|
|
|
return (conn', r)
|
|
|
|
where
|
|
|
|
go False = giveup "can't lock content"
|
2016-12-07 16:39:28 +00:00
|
|
|
go True = withVerifiedCopy LockedCopy u (return True) callback
|
2016-12-06 19:08:00 +00:00
|
|
|
|
|
|
|
-- | A connection to the peer.
|
|
|
|
data Connection
|
2016-12-06 19:40:31 +00:00
|
|
|
= OpenConnection P2PConnection
|
2016-12-06 19:08:00 +00:00
|
|
|
| ClosedConnection
|
|
|
|
|
|
|
|
type ConnectionPool = TVar [Connection]
|
2016-12-06 16:19:47 +00:00
|
|
|
|
2016-12-06 19:08:00 +00:00
|
|
|
mkConnectionPool :: Annex ConnectionPool
|
|
|
|
mkConnectionPool = liftIO $ newTVarIO []
|
2016-12-06 16:19:47 +00:00
|
|
|
|
2016-12-06 19:08:00 +00:00
|
|
|
-- Runs the Proto action.
|
2016-12-07 16:39:28 +00:00
|
|
|
runProto :: UUID -> P2PAddress -> ConnectionPool -> P2P.Proto a -> Annex (Maybe a)
|
|
|
|
runProto u addr connpool a = withConnection u addr connpool (runProto' a)
|
2016-12-06 16:19:47 +00:00
|
|
|
|
2016-12-06 19:08:00 +00:00
|
|
|
runProto' :: P2P.Proto a -> Connection -> Annex (Connection, Maybe a)
|
|
|
|
runProto' _ ClosedConnection = return (ClosedConnection, Nothing)
|
2016-12-06 19:40:31 +00:00
|
|
|
runProto' a (OpenConnection conn) = do
|
2016-12-08 19:47:49 +00:00
|
|
|
v <- runFullProto Client conn a
|
2016-12-06 19:08:00 +00:00
|
|
|
-- When runFullProto fails, the connection is no longer usable,
|
|
|
|
-- so close it.
|
2016-12-08 19:47:49 +00:00
|
|
|
case v of
|
|
|
|
Left e -> do
|
2016-12-09 16:35:45 +00:00
|
|
|
warning $ "Lost connection to peer (" ++ e ++ ")"
|
2016-12-06 19:49:39 +00:00
|
|
|
liftIO $ closeConnection conn
|
2016-12-08 19:47:49 +00:00
|
|
|
return (ClosedConnection, Nothing)
|
|
|
|
Right r -> return (OpenConnection conn, Just r)
|
2016-12-06 16:19:47 +00:00
|
|
|
|
2016-12-06 19:08:00 +00:00
|
|
|
-- Uses an open connection if one is available in the ConnectionPool;
|
|
|
|
-- otherwise opens a new connection.
|
|
|
|
--
|
|
|
|
-- Once the action is done, the connection is added back to the
|
|
|
|
-- ConnectionPool, unless it's no longer open.
|
2016-12-07 16:39:28 +00:00
|
|
|
withConnection :: UUID -> P2PAddress -> ConnectionPool -> (Connection -> Annex (Connection, a)) -> Annex a
|
|
|
|
withConnection u addr connpool a = bracketOnError get cache go
|
2016-12-06 19:08:00 +00:00
|
|
|
where
|
|
|
|
get = do
|
|
|
|
mc <- liftIO $ atomically $ do
|
|
|
|
l <- readTVar connpool
|
|
|
|
case l of
|
|
|
|
[] -> do
|
|
|
|
writeTVar connpool []
|
|
|
|
return Nothing
|
|
|
|
(c:cs) -> do
|
|
|
|
writeTVar connpool cs
|
|
|
|
return (Just c)
|
2016-12-07 16:39:28 +00:00
|
|
|
maybe (openConnection u addr) return mc
|
2016-12-06 19:08:00 +00:00
|
|
|
|
|
|
|
cache ClosedConnection = return ()
|
|
|
|
cache conn = liftIO $ atomically $ modifyTVar' connpool (conn:)
|
2016-12-06 16:19:47 +00:00
|
|
|
|
2016-12-06 19:08:00 +00:00
|
|
|
go conn = do
|
|
|
|
(conn', r) <- a conn
|
|
|
|
cache conn'
|
|
|
|
return r
|
2016-12-06 16:19:47 +00:00
|
|
|
|
2016-12-07 16:39:28 +00:00
|
|
|
openConnection :: UUID -> P2PAddress -> Annex Connection
|
|
|
|
openConnection u addr = do
|
2016-12-06 19:40:31 +00:00
|
|
|
g <- Annex.gitRepo
|
|
|
|
v <- liftIO $ tryNonAsync $ connectPeer g addr
|
2016-12-06 19:08:00 +00:00
|
|
|
case v of
|
2016-12-06 19:49:39 +00:00
|
|
|
Right conn -> do
|
|
|
|
myuuid <- getUUID
|
|
|
|
authtoken <- fromMaybe nullAuthToken
|
|
|
|
<$> loadP2PRemoteAuthToken addr
|
|
|
|
res <- liftIO $ runNetProto conn $
|
|
|
|
P2P.auth myuuid authtoken
|
|
|
|
case res of
|
2016-12-08 19:47:49 +00:00
|
|
|
Right (Just theiruuid)
|
2016-12-07 16:39:28 +00:00
|
|
|
| u == theiruuid -> return (OpenConnection conn)
|
|
|
|
| otherwise -> do
|
|
|
|
liftIO $ closeConnection conn
|
|
|
|
warning "Remote peer uuid seems to have changed."
|
|
|
|
return ClosedConnection
|
2016-12-08 19:47:49 +00:00
|
|
|
Right Nothing -> do
|
2016-12-07 16:39:28 +00:00
|
|
|
warning "Unable to authenticate with peer."
|
2016-12-08 19:47:49 +00:00
|
|
|
liftIO $ closeConnection conn
|
|
|
|
return ClosedConnection
|
|
|
|
Left e -> do
|
2016-12-08 20:02:26 +00:00
|
|
|
warning $ "Problem communicating with peer. (" ++ e ++ ")"
|
2016-12-08 19:47:49 +00:00
|
|
|
liftIO $ closeConnection conn
|
2016-12-06 19:49:39 +00:00
|
|
|
return ClosedConnection
|
2016-12-08 19:47:49 +00:00
|
|
|
Left e -> do
|
|
|
|
warning $ "Unable to connect to peer. (" ++ show e ++ ")"
|
2016-12-07 16:39:28 +00:00
|
|
|
return ClosedConnection
|