2020-09-02 14:41:27 +00:00
|
|
|
{- HttpAlso remote (readonly).
|
2020-09-01 19:16:35 +00:00
|
|
|
-
|
2023-06-20 17:20:08 +00:00
|
|
|
- Copyright 2020-2023 Joey Hess <id@joeyh.name>
|
2020-09-01 19:16:35 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2023-06-20 17:20:08 +00:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
|
2020-09-02 14:41:27 +00:00
|
|
|
module Remote.HttpAlso (remote) where
|
2020-09-01 19:16:35 +00:00
|
|
|
|
|
|
|
import Annex.Common
|
|
|
|
import Types.Remote
|
|
|
|
import Types.ProposedAccepted
|
2020-09-02 15:14:50 +00:00
|
|
|
import Types.Export
|
2020-09-01 19:16:35 +00:00
|
|
|
import Remote.Helper.ExportImport
|
|
|
|
import Remote.Helper.Special
|
|
|
|
import qualified Git
|
|
|
|
import Config.Cost
|
|
|
|
import Config
|
|
|
|
import Logs.Web
|
|
|
|
import Creds
|
|
|
|
import Utility.Metered
|
2021-08-18 19:13:14 +00:00
|
|
|
import Annex.Verify
|
2020-09-01 19:16:35 +00:00
|
|
|
import qualified Annex.Url as Url
|
|
|
|
import Annex.SpecialRemote.Config
|
|
|
|
|
2020-09-02 16:01:50 +00:00
|
|
|
import Data.Either
|
2020-09-01 19:16:35 +00:00
|
|
|
import qualified Data.Map as M
|
|
|
|
import System.FilePath.Posix as P
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
|
|
|
|
remote :: RemoteType
|
2023-06-20 17:20:08 +00:00
|
|
|
remote = specialRemoteType $ RemoteType
|
2020-09-02 14:41:27 +00:00
|
|
|
{ typename = "httpalso"
|
|
|
|
, enumerate = const (findSpecialRemotes "httpalso")
|
2020-09-01 19:16:35 +00:00
|
|
|
, generate = gen
|
|
|
|
, configParser = mkRemoteConfigParser
|
|
|
|
[ optionalStringParser urlField
|
|
|
|
(FieldDesc "(required) url to the remote content")
|
|
|
|
]
|
2020-09-02 14:41:27 +00:00
|
|
|
, setup = httpAlsoSetup
|
2020-09-02 15:14:50 +00:00
|
|
|
, exportSupported = exportIsSupported
|
2020-09-01 19:16:35 +00:00
|
|
|
, importSupported = importUnsupported
|
add thirdPartyPopulated interface
This is to support, eg a borg repo as a special remote, which is
populated not by running git-annex commands, but by using borg. Then
git-annex sync lists the content of the remote, learns which files are
annex objects, and treats those as present in the remote.
So, most of the import machinery is reused, to a new purpose. While
normally importtree maintains a remote tracking branch, this does not,
because the files stored in the remote are annex object files, not
user-visible filenames. But, internally, a git tree is still generated,
of the files on the remote that are annex objects. This tree is used
by retrieveExportWithContentIdentifier, etc. As with other import/export
remotes, that the tree is recorded in the export log, and gets grafted
into the git-annex branch.
importKey changed to be able to return Nothing, to indicate when an
ImportLocation is not an annex object and so should be skipped from
being included in the tree.
It did not seem to make sense to have git-annex import do this, since
from the user's perspective, it's not like other imports. So only
git-annex sync does it.
Note that, git-annex sync does not yet download objects from such
remotes that are preferred content. importKeys is run with
content downloading disabled, to avoid getting the content of all
objects. Perhaps what's needed is for seekSyncContent to be run with these
remotes, but I don't know if it will just work (in particular, it needs
to avoid trying to transfer objects to them), so I skipped that for now.
(Untested and unused as of yet.)
This commit was sponsored by Jochen Bartl on Patreon.
2020-12-18 18:52:57 +00:00
|
|
|
, thirdPartyPopulated = False
|
2020-09-01 19:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
urlField :: RemoteConfigField
|
|
|
|
urlField = Accepted "url"
|
|
|
|
|
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> RemoteStateHandle -> Annex (Maybe Remote)
|
|
|
|
gen r u rc gc rs = do
|
|
|
|
c <- parsedRemoteConfig remote rc
|
2023-01-12 17:42:28 +00:00
|
|
|
cst <- remoteCost gc c expensiveRemoteCost
|
2020-09-01 19:16:35 +00:00
|
|
|
let url = getRemoteConfigValue urlField c
|
|
|
|
ll <- liftIO newLearnedLayout
|
2023-06-20 17:20:08 +00:00
|
|
|
return $ Just $ specialRemote c
|
|
|
|
cannotModify
|
|
|
|
(downloadKey url ll)
|
|
|
|
cannotModify
|
|
|
|
(checkKey url ll)
|
|
|
|
(this url c cst)
|
2020-09-01 19:16:35 +00:00
|
|
|
where
|
2023-06-20 17:20:08 +00:00
|
|
|
this url c cst = Remote
|
2020-09-01 19:16:35 +00:00
|
|
|
{ uuid = u
|
|
|
|
, cost = cst
|
|
|
|
, name = Git.repoDescribe r
|
2020-09-02 15:14:50 +00:00
|
|
|
, storeKey = cannotModify
|
2023-06-20 17:20:08 +00:00
|
|
|
, retrieveKeyFile = retrieveKeyFileDummy
|
2020-09-01 19:16:35 +00:00
|
|
|
, retrieveKeyFileCheap = Nothing
|
|
|
|
-- HttpManagerRestricted is used here, so this is
|
|
|
|
-- secure.
|
|
|
|
, retrievalSecurityPolicy = RetrievalAllKeysSecure
|
2020-09-02 15:14:50 +00:00
|
|
|
, removeKey = cannotModify
|
2020-09-01 19:16:35 +00:00
|
|
|
, lockContent = Nothing
|
2023-06-20 17:20:08 +00:00
|
|
|
, checkPresent = checkPresentDummy
|
2020-09-01 19:16:35 +00:00
|
|
|
, checkPresentCheap = False
|
2020-09-02 15:14:50 +00:00
|
|
|
, exportActions = ExportActions
|
|
|
|
{ storeExport = cannotModify
|
|
|
|
, retrieveExport = retriveExportHttpAlso url
|
|
|
|
, removeExport = cannotModify
|
2020-12-28 18:37:15 +00:00
|
|
|
, versionedExport = False
|
2020-09-02 15:14:50 +00:00
|
|
|
, checkPresentExport = checkPresentExportHttpAlso url
|
|
|
|
, removeExportDirectory = Nothing
|
|
|
|
, renameExport = cannotModify
|
|
|
|
}
|
2020-09-01 19:16:35 +00:00
|
|
|
, importActions = importUnsupported
|
|
|
|
, whereisKey = Nothing
|
|
|
|
, remoteFsck = Nothing
|
|
|
|
, repairRepo = Nothing
|
|
|
|
, config = c
|
|
|
|
, gitconfig = gc
|
|
|
|
, localpath = Nothing
|
|
|
|
, getRepo = return r
|
|
|
|
, readonly = True
|
|
|
|
, appendonly = False
|
2020-12-28 19:08:53 +00:00
|
|
|
, untrustworthy = False
|
2023-08-16 18:31:31 +00:00
|
|
|
, availability = pure GloballyAvailable
|
2020-09-01 19:16:35 +00:00
|
|
|
, remotetype = remote
|
|
|
|
, mkUnavailable = return Nothing
|
|
|
|
, getInfo = return []
|
|
|
|
, claimUrl = Nothing
|
|
|
|
, checkUrl = Nothing
|
|
|
|
, remoteStateHandle = rs
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:14:50 +00:00
|
|
|
cannotModify :: a
|
|
|
|
cannotModify = giveup "httpalso special remote is read only"
|
|
|
|
|
2020-09-02 14:41:27 +00:00
|
|
|
httpAlsoSetup :: SetupStage -> Maybe UUID -> Maybe CredPair -> RemoteConfig -> RemoteGitConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
httpAlsoSetup _ Nothing _ _ _ =
|
2023-04-10 17:38:14 +00:00
|
|
|
giveup "Must use --sameas when initializing a httpalso remote."
|
2020-09-02 14:41:27 +00:00
|
|
|
httpAlsoSetup _ (Just u) _ c gc = do
|
2020-09-01 19:16:35 +00:00
|
|
|
_url <- maybe (giveup "Specify url=")
|
|
|
|
(return . fromProposedAccepted)
|
|
|
|
(M.lookup urlField c)
|
2020-09-29 17:56:27 +00:00
|
|
|
c' <- if isJust (M.lookup encryptionField c)
|
|
|
|
then fst <$> encryptionSetup c gc
|
|
|
|
else pure c
|
2020-09-02 14:41:27 +00:00
|
|
|
gitConfigSpecialRemote u c' [("httpalso", "true")]
|
2020-09-01 19:16:35 +00:00
|
|
|
return (c', u)
|
|
|
|
|
2023-06-20 17:20:08 +00:00
|
|
|
downloadKey :: Maybe URLString -> LearnedLayout -> Retriever
|
|
|
|
downloadKey baseurl ll = fileRetriever' $ \dest key p iv ->
|
2023-06-21 17:06:11 +00:00
|
|
|
downloadAction (fromRawFilePath dest) p iv (keyUrlAction baseurl ll key)
|
2020-09-01 19:16:35 +00:00
|
|
|
|
2022-05-09 16:25:04 +00:00
|
|
|
retriveExportHttpAlso :: Maybe URLString -> Key -> ExportLocation -> FilePath -> MeterUpdate -> Annex Verification
|
|
|
|
retriveExportHttpAlso baseurl key loc dest p = do
|
2022-05-09 17:18:47 +00:00
|
|
|
verifyKeyContentIncrementally AlwaysVerify key $ \iv ->
|
2023-06-21 17:06:11 +00:00
|
|
|
downloadAction dest p iv (exportLocationUrlAction baseurl loc)
|
2020-09-02 16:01:50 +00:00
|
|
|
|
2023-06-21 17:06:11 +00:00
|
|
|
downloadAction :: FilePath -> MeterUpdate -> Maybe IncrementalVerifier -> ((URLString -> Annex (Either String ())) -> Annex (Either String ())) -> Annex ()
|
|
|
|
downloadAction dest p iv run =
|
2020-09-02 16:21:10 +00:00
|
|
|
Url.withUrlOptions $ \uo ->
|
2023-06-20 17:20:08 +00:00
|
|
|
run (\url -> Url.download' p iv url dest uo)
|
|
|
|
>>= either giveup (const (return ()))
|
2020-09-01 19:16:35 +00:00
|
|
|
|
2023-06-20 17:20:08 +00:00
|
|
|
checkKey :: Maybe URLString -> LearnedLayout -> CheckPresent
|
remove "checking remotename" message
This fixes fsck of a remote that uses chunking displaying
(checking remotename) (checking remotename)" for every chunk.
Also, some remotes displayed the message, and others did not, with no
consistency. It was originally displayed only when accessing remotes
that were expensive or might involve a password prompt, I think, but
nothing in the API said when to do it so it became an inconsistent mess.
Originally I thought fsck should always display it. But it only displays
in fsck --from remote, so the user knows the remote is being accessed,
so there is no reason to tell them it's accessing it over and over.
It was also possible for git-annex move to sometimes display it twice,
due to checking if content is present twice. But, the user of move
specifies --from/--to, so it does not need to display when it's
accessing the remote, as the user expects it to access the remote.
git-annex get might display it, but only if the remote also supports
hasKeyCheap, which is really only local git remotes, which didn't
display it always; and in any case nothing displayed it before hasKeyCheap,
which is checked first, so I don't think this needs to display it ever.
mirror is like move. And that's all the main places it would have been
displayed.
This commit was sponsored by Jochen Bartl on Patreon.
2021-04-27 16:50:45 +00:00
|
|
|
checkKey baseurl ll key =
|
2020-09-02 16:21:10 +00:00
|
|
|
isRight <$> keyUrlAction baseurl ll key (checkKey' key)
|
2020-09-02 15:14:50 +00:00
|
|
|
|
2020-09-02 16:21:10 +00:00
|
|
|
checkKey' :: Key -> URLString -> Annex (Either String ())
|
|
|
|
checkKey' key url = ifM (Url.withUrlOptions $ Url.checkBoth url (fromKey keySize key))
|
|
|
|
( return (Right ())
|
|
|
|
, return (Left "content not found")
|
|
|
|
)
|
2020-09-02 15:14:50 +00:00
|
|
|
|
|
|
|
checkPresentExportHttpAlso :: Maybe URLString -> Key -> ExportLocation -> Annex Bool
|
|
|
|
checkPresentExportHttpAlso baseurl key loc =
|
2020-09-02 16:21:10 +00:00
|
|
|
isRight <$> exportLocationUrlAction baseurl loc (checkKey' key)
|
2020-09-01 19:16:35 +00:00
|
|
|
|
|
|
|
type LearnedLayout = TVar (Maybe [Key -> URLString])
|
|
|
|
|
|
|
|
newLearnedLayout :: IO LearnedLayout
|
|
|
|
newLearnedLayout = newTVarIO Nothing
|
|
|
|
|
2023-06-20 17:20:08 +00:00
|
|
|
-- Learns which layout the special remote uses, so once any action on an
|
|
|
|
-- url succeeds, subsequent calls will continue to use that layout
|
|
|
|
-- (or related layouts).
|
2020-09-02 16:21:10 +00:00
|
|
|
keyUrlAction
|
|
|
|
:: Maybe URLString
|
|
|
|
-> LearnedLayout
|
|
|
|
-> Key
|
|
|
|
-> (URLString -> Annex (Either String ()))
|
|
|
|
-> Annex (Either String ())
|
|
|
|
keyUrlAction (Just baseurl) ll key downloader =
|
|
|
|
liftIO (readTVarIO ll) >>= \case
|
|
|
|
Just learned -> go Nothing False [learned]
|
|
|
|
Nothing -> go Nothing True (supportedLayouts baseurl)
|
2020-09-01 19:16:35 +00:00
|
|
|
where
|
2020-09-02 16:21:10 +00:00
|
|
|
go err learn [] = go' err learn [] []
|
|
|
|
go err learn (layouts:rest) = go' err learn layouts [] >>= \case
|
|
|
|
Right () -> return (Right ())
|
|
|
|
Left err' -> go (Just err') learn rest
|
2020-09-01 19:16:35 +00:00
|
|
|
|
2020-09-02 16:21:10 +00:00
|
|
|
go' (Just err) _ [] _ = pure (Left err)
|
|
|
|
go' Nothing _ [] _ = error "internal"
|
|
|
|
go' _err learn (layout:rest) prevs =
|
|
|
|
downloader (layout key) >>= \case
|
|
|
|
Right () -> do
|
2020-09-01 19:16:35 +00:00
|
|
|
when learn $ do
|
|
|
|
let learned = layout:prevs++rest
|
|
|
|
liftIO $ atomically $
|
|
|
|
writeTVar ll (Just learned)
|
2020-09-02 16:21:10 +00:00
|
|
|
return (Right ())
|
|
|
|
Left err -> go' (Just err) learn rest (layout:prevs)
|
2020-09-02 15:14:50 +00:00
|
|
|
keyUrlAction Nothing _ _ _ = noBaseUrlError
|
|
|
|
|
2020-09-02 16:21:10 +00:00
|
|
|
exportLocationUrlAction
|
|
|
|
:: Maybe URLString
|
|
|
|
-> ExportLocation
|
|
|
|
-> (URLString -> Annex (Either String ()))
|
|
|
|
-> Annex (Either String ())
|
2020-09-02 15:14:50 +00:00
|
|
|
exportLocationUrlAction (Just baseurl) loc a =
|
|
|
|
a (baseurl P.</> fromRawFilePath (fromExportLocation loc))
|
|
|
|
exportLocationUrlAction Nothing _ _ = noBaseUrlError
|
|
|
|
|
2020-09-01 19:16:35 +00:00
|
|
|
-- cannot normally happen
|
2020-09-02 15:14:50 +00:00
|
|
|
noBaseUrlError :: Annex a
|
|
|
|
noBaseUrlError = giveup "no url configured for httpalso special remote"
|
2020-09-01 19:16:35 +00:00
|
|
|
|
|
|
|
-- Different ways that keys can be laid out in the special remote,
|
|
|
|
-- with the more common first.
|
|
|
|
--
|
|
|
|
-- This is a nested list, because a single remote may use more than one
|
|
|
|
-- layout. In particular, old versions of git-annex used hashDirMixed
|
|
|
|
-- for some special remotes, before switching to hashDirLower for new data.
|
|
|
|
-- So, when learning the layout, both need to be tried.
|
|
|
|
supportedLayouts :: URLString -> [[Key -> URLString]]
|
|
|
|
supportedLayouts baseurl =
|
|
|
|
-- Layout used for bare git-annex repos, and for many
|
|
|
|
-- special remotes like directory.
|
|
|
|
[ [ \k -> mkurl k (hashDirLower (HashLevels 2)) P.</> kf k
|
|
|
|
-- Layout used for non-bare git-annex repos, and for some old
|
|
|
|
-- special remotes.
|
|
|
|
, \k -> mkurl k (hashDirMixed (HashLevels 2)) P.</> kf k
|
|
|
|
]
|
|
|
|
-- Special remotes that do not need hash directories.
|
|
|
|
, [ \k -> baseurl P.</> kf k ]
|
|
|
|
-- Layouts without a key directory, used by some special remotes.
|
|
|
|
, [ \k -> mkurl k (hashDirLower def)
|
|
|
|
, \k -> mkurl k (hashDirMixed def)
|
|
|
|
]
|
|
|
|
-- Layouts with only 1 level of hash directory,
|
|
|
|
-- rather than the default 2.
|
|
|
|
, [ \k -> mkurl k (hashDirLower (HashLevels 1))
|
|
|
|
, \k -> mkurl k (hashDirMixed (HashLevels 1))
|
|
|
|
]
|
|
|
|
]
|
|
|
|
where
|
|
|
|
mkurl k hasher = baseurl P.</> fromRawFilePath (hasher k) P.</> kf k
|
|
|
|
kf k = fromRawFilePath (keyFile k)
|