2012-11-15 00:25:00 +00:00
|
|
|
{- WebDAV remotes.
|
|
|
|
-
|
2017-09-12 18:08:00 +00:00
|
|
|
- Copyright 2012-2017 Joey Hess <id@joeyh.name>
|
2012-11-15 00:25:00 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-11-15 00:25:00 +00:00
|
|
|
-}
|
|
|
|
|
2016-12-10 12:24:27 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2014-07-30 15:19:05 +00:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
2013-02-27 04:07:28 +00:00
|
|
|
|
2014-02-11 18:06:50 +00:00
|
|
|
module Remote.WebDAV (remote, davCreds, configUrl) where
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
import Network.Protocol.HTTP.DAV
|
|
|
|
import qualified Data.Map as M
|
2014-08-07 19:45:56 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2012-11-15 17:46:16 +00:00
|
|
|
import qualified Data.ByteString.UTF8 as B8
|
2012-11-16 21:58:58 +00:00
|
|
|
import qualified Data.ByteString.Lazy.UTF8 as L8
|
2017-09-12 18:08:00 +00:00
|
|
|
import Network.HTTP.Client (HttpException(..), RequestBody)
|
2017-11-21 16:45:49 +00:00
|
|
|
#if MIN_VERSION_http_client(0,5,0)
|
2017-09-28 16:01:58 +00:00
|
|
|
import qualified Network.HTTP.Client as HTTP
|
2017-11-21 16:45:49 +00:00
|
|
|
#endif
|
2012-11-16 04:09:22 +00:00
|
|
|
import Network.HTTP.Types
|
2012-11-17 03:16:18 +00:00
|
|
|
import System.IO.Error
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
import Control.Monad.Catch
|
2017-10-07 18:11:32 +00:00
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
|
|
|
import System.Log.Logger (debugM)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2012-11-15 00:25:00 +00:00
|
|
|
import Types.Remote
|
2017-09-15 20:34:45 +00:00
|
|
|
import Types.Export
|
2012-11-15 00:25:00 +00:00
|
|
|
import qualified Git
|
|
|
|
import Config
|
2013-03-13 20:16:01 +00:00
|
|
|
import Config.Cost
|
2012-11-15 00:25:00 +00:00
|
|
|
import Remote.Helper.Special
|
2015-08-17 14:42:14 +00:00
|
|
|
import Remote.Helper.Messages
|
2014-08-07 23:32:23 +00:00
|
|
|
import Remote.Helper.Http
|
2019-02-20 19:55:01 +00:00
|
|
|
import Remote.Helper.ExportImport
|
2014-07-24 18:49:22 +00:00
|
|
|
import qualified Remote.Helper.Chunked.Legacy as Legacy
|
2012-11-15 00:25:00 +00:00
|
|
|
import Creds
|
2013-03-28 21:03:04 +00:00
|
|
|
import Utility.Metered
|
2017-09-12 19:13:42 +00:00
|
|
|
import Utility.Url (URLString, matchStatusCodeException, matchHttpExceptionContent)
|
2013-09-07 22:38:00 +00:00
|
|
|
import Annex.UUID
|
2014-08-07 19:45:56 +00:00
|
|
|
import Remote.WebDAV.DavLocation
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2016-12-10 12:24:27 +00:00
|
|
|
#if MIN_VERSION_http_client(0,5,0)
|
|
|
|
import Network.HTTP.Client (HttpExceptionContent(..), responseStatus)
|
|
|
|
#endif
|
|
|
|
|
2012-11-15 00:25:00 +00:00
|
|
|
remote :: RemoteType
|
2017-09-07 17:45:31 +00:00
|
|
|
remote = RemoteType
|
|
|
|
{ typename = "webdav"
|
|
|
|
, enumerate = const (findSpecialRemotes "webdav")
|
|
|
|
, generate = gen
|
|
|
|
, setup = webdavSetup
|
2017-09-12 18:08:00 +00:00
|
|
|
, exportSupported = exportIsSupported
|
2019-02-20 19:55:01 +00:00
|
|
|
, importSupported = importUnsupported
|
2017-09-07 17:45:31 +00:00
|
|
|
}
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
2013-01-01 17:52:47 +00:00
|
|
|
gen r u c gc = new <$> remoteCost gc expensiveRemoteCost
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
convert WebDAV to new special remote interface, adding new-style chunking support
Reusing http connection when operating on chunks is not done yet,
I had to submit some patches to DAV to support that. However, this is no
slower than old-style chunking was.
Note that it's a fileRetriever and a fileStorer, despite DAV using
bytestrings that would allow streaming. As a result, upload/download of
encrypted files is made a bit more expensive, since it spools them to temp
files. This was needed to get the progress meters to work.
There are probably ways to avoid that.. But it turns out that the current
DAV interface buffers the whole file content in memory, and I have
sent in a patch to DAV to improve its interfaces. Using the new interfaces,
it's certainly going to need to be a fileStorer, in order to read the file
size from the file (getting the size of a bytestring would destroy
laziness). It should be possible to use the new interface to make it be a
byteRetriever, so I'll change that when I get to it.
This commit was sponsored by Andreas Olsson.
2014-08-06 20:55:32 +00:00
|
|
|
new cst = Just $ specialRemote c
|
2014-08-07 22:32:07 +00:00
|
|
|
(prepareDAV this $ store chunkconfig)
|
|
|
|
(prepareDAV this $ retrieve chunkconfig)
|
|
|
|
(prepareDAV this $ remove)
|
|
|
|
(prepareDAV this $ checkKey this chunkconfig)
|
2012-11-15 00:25:00 +00:00
|
|
|
this
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
2014-12-16 19:26:13 +00:00
|
|
|
this = Remote
|
|
|
|
{ uuid = u
|
|
|
|
, cost = cst
|
|
|
|
, name = Git.repoDescribe r
|
|
|
|
, storeKey = storeKeyDummy
|
|
|
|
, retrieveKeyFile = retreiveKeyFileDummy
|
|
|
|
, retrieveKeyFileCheap = retrieveCheap
|
2018-06-21 15:35:27 +00:00
|
|
|
-- HttpManagerRestricted is used here, so this is
|
|
|
|
-- secure.
|
|
|
|
, retrievalSecurityPolicy = RetrievalAllKeysSecure
|
2014-12-16 19:26:13 +00:00
|
|
|
, removeKey = removeKeyDummy
|
2015-10-08 19:01:38 +00:00
|
|
|
, lockContent = Nothing
|
2014-12-16 19:26:13 +00:00
|
|
|
, checkPresent = checkPresentDummy
|
|
|
|
, checkPresentCheap = False
|
2019-01-30 18:55:28 +00:00
|
|
|
, exportActions = ExportActions
|
|
|
|
{ storeExport = storeExportDav this
|
|
|
|
, retrieveExport = retrieveExportDav this
|
|
|
|
, checkPresentExport = checkPresentExportDav this
|
|
|
|
, removeExport = removeExportDav this
|
2017-09-15 17:15:47 +00:00
|
|
|
, removeExportDirectory = Just $
|
2019-01-30 18:55:28 +00:00
|
|
|
removeExportDirectoryDav this
|
|
|
|
, renameExport = renameExportDav this
|
2017-09-12 18:08:00 +00:00
|
|
|
}
|
2019-02-20 19:55:01 +00:00
|
|
|
, importActions = importUnsupported
|
2014-12-16 19:26:13 +00:00
|
|
|
, whereisKey = Nothing
|
|
|
|
, remoteFsck = Nothing
|
|
|
|
, repairRepo = Nothing
|
|
|
|
, config = c
|
2018-06-04 18:31:55 +00:00
|
|
|
, getRepo = return r
|
2014-12-16 19:26:13 +00:00
|
|
|
, gitconfig = gc
|
|
|
|
, localpath = Nothing
|
|
|
|
, readonly = False
|
2018-08-30 15:12:18 +00:00
|
|
|
, appendonly = False
|
2014-12-16 19:26:13 +00:00
|
|
|
, availability = GloballyAvailable
|
|
|
|
, remotetype = remote
|
|
|
|
, mkUnavailable = gen r u (M.insert "url" "http://!dne!/" c) gc
|
|
|
|
, getInfo = includeCredsInfo c (davCreds u) $
|
|
|
|
[("url", fromMaybe "unknown" (M.lookup "url" c))]
|
|
|
|
, claimUrl = Nothing
|
|
|
|
, checkUrl = Nothing
|
|
|
|
}
|
convert WebDAV to new special remote interface, adding new-style chunking support
Reusing http connection when operating on chunks is not done yet,
I had to submit some patches to DAV to support that. However, this is no
slower than old-style chunking was.
Note that it's a fileRetriever and a fileStorer, despite DAV using
bytestrings that would allow streaming. As a result, upload/download of
encrypted files is made a bit more expensive, since it spools them to temp
files. This was needed to get the progress meters to work.
There are probably ways to avoid that.. But it turns out that the current
DAV interface buffers the whole file content in memory, and I have
sent in a patch to DAV to improve its interfaces. Using the new interfaces,
it's certainly going to need to be a fileStorer, in order to read the file
size from the file (getting the size of a bytestring would destroy
laziness). It should be possible to use the new interface to make it be a
byteRetriever, so I'll change that when I get to it.
This commit was sponsored by Andreas Olsson.
2014-08-06 20:55:32 +00:00
|
|
|
chunkconfig = getChunkConfig c
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2017-02-07 18:35:58 +00:00
|
|
|
webdavSetup :: SetupStage -> Maybe UUID -> Maybe CredPair -> RemoteConfig -> RemoteGitConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
webdavSetup _ mu mcreds c gc = do
|
2013-09-07 22:38:00 +00:00
|
|
|
u <- maybe (liftIO genUUID) return mu
|
2014-08-08 18:19:08 +00:00
|
|
|
url <- case M.lookup "url" c of
|
2016-11-16 01:29:54 +00:00
|
|
|
Nothing -> giveup "Specify url="
|
2014-08-08 18:19:08 +00:00
|
|
|
Just url -> return url
|
2016-05-23 21:27:15 +00:00
|
|
|
(c', encsetup) <- encryptionSetup c gc
|
2016-05-23 21:03:20 +00:00
|
|
|
creds <- maybe (getCreds c' gc u) (return . Just) mcreds
|
2012-11-15 00:25:00 +00:00
|
|
|
testDav url creds
|
2018-03-27 16:41:57 +00:00
|
|
|
gitConfigSpecialRemote u c' [("webdav", "true")]
|
2016-05-23 21:08:43 +00:00
|
|
|
c'' <- setRemoteCredPair encsetup c' gc (davCreds u) creds
|
2013-09-07 22:38:00 +00:00
|
|
|
return (c'', u)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2014-08-07 22:32:07 +00:00
|
|
|
prepareDAV :: Remote -> (Maybe DavHandle -> helper) -> Preparer helper
|
|
|
|
prepareDAV = resourcePrepare . const . withDAVHandle
|
|
|
|
|
|
|
|
store :: ChunkConfig -> Maybe DavHandle -> Storer
|
|
|
|
store _ Nothing = byteStorer $ \_k _b _p -> return False
|
2014-08-07 23:32:23 +00:00
|
|
|
store (LegacyChunks chunksize) (Just dav) = fileStorer $ \k f p -> liftIO $
|
|
|
|
withMeteredFile f p $ storeLegacyChunked chunksize k dav
|
|
|
|
store _ (Just dav) = httpStorer $ \k reqbody -> liftIO $ goDAV dav $ do
|
|
|
|
let tmp = keyTmpLocation k
|
2014-08-08 17:40:55 +00:00
|
|
|
let dest = keyLocation k
|
2017-09-12 18:08:00 +00:00
|
|
|
storeHelper dav tmp dest reqbody
|
|
|
|
return True
|
|
|
|
|
|
|
|
storeHelper :: DavHandle -> DavLocation -> DavLocation -> RequestBody -> DAVT IO ()
|
|
|
|
storeHelper dav tmp dest reqbody = do
|
2017-09-15 19:52:31 +00:00
|
|
|
maybe noop (void . mkColRecursive) (locationParent tmp)
|
2017-10-07 18:11:32 +00:00
|
|
|
debugDav $ "putContent " ++ tmp
|
2014-08-07 23:32:23 +00:00
|
|
|
inLocation tmp $
|
|
|
|
putContentM' (contentType, reqbody)
|
2017-09-12 18:08:00 +00:00
|
|
|
finalizeStore dav tmp dest
|
convert WebDAV to new special remote interface, adding new-style chunking support
Reusing http connection when operating on chunks is not done yet,
I had to submit some patches to DAV to support that. However, this is no
slower than old-style chunking was.
Note that it's a fileRetriever and a fileStorer, despite DAV using
bytestrings that would allow streaming. As a result, upload/download of
encrypted files is made a bit more expensive, since it spools them to temp
files. This was needed to get the progress meters to work.
There are probably ways to avoid that.. But it turns out that the current
DAV interface buffers the whole file content in memory, and I have
sent in a patch to DAV to improve its interfaces. Using the new interfaces,
it's certainly going to need to be a fileStorer, in order to read the file
size from the file (getting the size of a bytestring would destroy
laziness). It should be possible to use the new interface to make it be a
byteRetriever, so I'll change that when I get to it.
This commit was sponsored by Andreas Olsson.
2014-08-06 20:55:32 +00:00
|
|
|
|
2017-09-12 18:08:00 +00:00
|
|
|
finalizeStore :: DavHandle -> DavLocation -> DavLocation -> DAVT IO ()
|
|
|
|
finalizeStore dav tmp dest = do
|
2017-10-07 18:11:32 +00:00
|
|
|
debugDav $ "delContent " ++ dest
|
2014-08-07 19:45:56 +00:00
|
|
|
inLocation dest $ void $ safely $ delContentM
|
|
|
|
maybe noop (void . mkColRecursive) (locationParent dest)
|
2017-09-12 18:08:00 +00:00
|
|
|
moveDAV (baseURL dav) tmp dest
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2015-04-14 20:35:10 +00:00
|
|
|
retrieveCheap :: Key -> AssociatedFile -> FilePath -> Annex Bool
|
|
|
|
retrieveCheap _ _ _ = return False
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2014-08-07 22:32:07 +00:00
|
|
|
retrieve :: ChunkConfig -> Maybe DavHandle -> Retriever
|
2016-11-16 01:29:54 +00:00
|
|
|
retrieve _ Nothing = giveup "unable to connect"
|
2014-08-08 17:17:24 +00:00
|
|
|
retrieve (LegacyChunks _) (Just dav) = retrieveLegacyChunked dav
|
|
|
|
retrieve _ (Just dav) = fileRetriever $ \d k p -> liftIO $
|
2017-09-12 18:08:00 +00:00
|
|
|
goDAV dav $ retrieveHelper (keyLocation k) d p
|
|
|
|
|
|
|
|
retrieveHelper :: DavLocation -> FilePath -> MeterUpdate -> DAVT IO ()
|
2017-10-07 18:11:32 +00:00
|
|
|
retrieveHelper loc d p = do
|
|
|
|
debugDav $ "retrieve " ++ loc
|
|
|
|
inLocation loc $
|
|
|
|
withContentM $ httpBodyRetriever d p
|
convert WebDAV to new special remote interface, adding new-style chunking support
Reusing http connection when operating on chunks is not done yet,
I had to submit some patches to DAV to support that. However, this is no
slower than old-style chunking was.
Note that it's a fileRetriever and a fileStorer, despite DAV using
bytestrings that would allow streaming. As a result, upload/download of
encrypted files is made a bit more expensive, since it spools them to temp
files. This was needed to get the progress meters to work.
There are probably ways to avoid that.. But it turns out that the current
DAV interface buffers the whole file content in memory, and I have
sent in a patch to DAV to improve its interfaces. Using the new interfaces,
it's certainly going to need to be a fileStorer, in order to read the file
size from the file (getting the size of a bytestring would destroy
laziness). It should be possible to use the new interface to make it be a
byteRetriever, so I'll change that when I get to it.
This commit was sponsored by Andreas Olsson.
2014-08-06 20:55:32 +00:00
|
|
|
|
2014-08-07 22:32:07 +00:00
|
|
|
remove :: Maybe DavHandle -> Remover
|
|
|
|
remove Nothing _ = return False
|
2017-09-12 18:08:00 +00:00
|
|
|
remove (Just dav) k = liftIO $ goDAV dav $
|
2014-08-07 22:32:07 +00:00
|
|
|
-- Delete the key's whole directory, including any
|
|
|
|
-- legacy chunked files, etc, in a single action.
|
2017-09-12 18:08:00 +00:00
|
|
|
removeHelper (keyDir k)
|
|
|
|
|
|
|
|
removeHelper :: DavLocation -> DAVT IO Bool
|
|
|
|
removeHelper d = do
|
2017-10-07 18:11:32 +00:00
|
|
|
debugDav $ "delContent " ++ d
|
2017-09-12 18:08:00 +00:00
|
|
|
v <- safely $ inLocation d delContentM
|
|
|
|
case v of
|
|
|
|
Just _ -> return True
|
|
|
|
Nothing -> do
|
|
|
|
v' <- existsDAV d
|
|
|
|
case v' of
|
|
|
|
Right False -> return True
|
|
|
|
_ -> return False
|
2014-08-07 22:32:07 +00:00
|
|
|
|
|
|
|
checkKey :: Remote -> ChunkConfig -> Maybe DavHandle -> CheckPresent
|
2016-11-16 01:29:54 +00:00
|
|
|
checkKey r _ Nothing _ = giveup $ name r ++ " not configured"
|
2014-08-08 17:17:24 +00:00
|
|
|
checkKey r chunkconfig (Just dav) k = do
|
2015-08-17 14:42:14 +00:00
|
|
|
showChecking r
|
2014-08-08 17:17:24 +00:00
|
|
|
case chunkconfig of
|
|
|
|
LegacyChunks _ -> checkKeyLegacyChunked dav k
|
|
|
|
_ -> do
|
|
|
|
v <- liftIO $ goDAV dav $
|
2014-08-08 17:40:55 +00:00
|
|
|
existsDAV (keyLocation k)
|
2016-11-16 01:29:54 +00:00
|
|
|
either giveup return v
|
2012-11-17 03:16:18 +00:00
|
|
|
|
2019-01-30 18:55:28 +00:00
|
|
|
storeExportDav :: Remote -> FilePath -> Key -> ExportLocation -> MeterUpdate -> Annex Bool
|
2019-02-07 17:47:57 +00:00
|
|
|
storeExportDav r f k loc p = case exportLocation loc of
|
|
|
|
Right dest -> withDAVHandle r $ \mh -> runExport mh $ \dav -> do
|
|
|
|
reqbody <- liftIO $ httpBodyStorer f p
|
|
|
|
storeHelper dav (keyTmpLocation k) dest reqbody
|
|
|
|
return True
|
|
|
|
Left err -> do
|
|
|
|
warning err
|
|
|
|
return False
|
2017-09-12 18:08:00 +00:00
|
|
|
|
2019-01-30 18:55:28 +00:00
|
|
|
retrieveExportDav :: Remote -> Key -> ExportLocation -> FilePath -> MeterUpdate -> Annex Bool
|
2019-02-07 17:47:57 +00:00
|
|
|
retrieveExportDav r _k loc d p = case exportLocation loc of
|
|
|
|
Right src -> withDAVHandle r $ \mh -> runExport mh $ \_dav -> do
|
|
|
|
retrieveHelper src d p
|
|
|
|
return True
|
|
|
|
Left _err -> return False
|
2017-09-12 18:08:00 +00:00
|
|
|
|
2019-01-30 18:55:28 +00:00
|
|
|
checkPresentExportDav :: Remote -> Key -> ExportLocation -> Annex Bool
|
2019-02-07 17:47:57 +00:00
|
|
|
checkPresentExportDav r _k loc = case exportLocation loc of
|
|
|
|
Right p -> withDAVHandle r $ \case
|
|
|
|
Nothing -> giveup $ name r ++ " not configured"
|
|
|
|
Just h -> liftIO $ do
|
|
|
|
v <- goDAV h $ existsDAV p
|
|
|
|
either giveup return v
|
|
|
|
Left err -> giveup err
|
2017-09-12 18:08:00 +00:00
|
|
|
|
2019-01-30 18:55:28 +00:00
|
|
|
removeExportDav :: Remote -> Key -> ExportLocation -> Annex Bool
|
2019-02-07 17:47:57 +00:00
|
|
|
removeExportDav r _k loc = case exportLocation loc of
|
|
|
|
Right p -> withDAVHandle r $ \mh -> runExport mh $ \_dav ->
|
|
|
|
removeHelper p
|
|
|
|
-- When the exportLocation is not legal for webdav,
|
|
|
|
-- the content is certianly not stored there, so it's ok for
|
|
|
|
-- removal to succeed. This allows recovery after failure to store
|
|
|
|
-- content there, as the user can rename the problem file and
|
|
|
|
-- this will be called to make sure it's gone.
|
|
|
|
Left _err -> return True
|
2017-09-15 17:15:47 +00:00
|
|
|
|
2019-01-30 18:55:28 +00:00
|
|
|
removeExportDirectoryDav :: Remote -> ExportDirectory -> Annex Bool
|
|
|
|
removeExportDirectoryDav r dir = withDAVHandle r $ \mh -> runExport mh $ \_dav -> do
|
2017-10-07 18:11:32 +00:00
|
|
|
let d = fromExportDirectory dir
|
|
|
|
debugDav $ "delContent " ++ d
|
|
|
|
safely (inLocation d delContentM)
|
2017-09-15 17:15:47 +00:00
|
|
|
>>= maybe (return False) (const $ return True)
|
|
|
|
|
2019-03-11 16:44:12 +00:00
|
|
|
renameExportDav :: Remote -> Key -> ExportLocation -> ExportLocation -> Annex (Maybe Bool)
|
2019-02-07 17:47:57 +00:00
|
|
|
renameExportDav r _k src dest = case (exportLocation src, exportLocation dest) of
|
|
|
|
(Right srcl, Right destl) -> withDAVHandle r $ \case
|
|
|
|
Just h
|
|
|
|
-- box.com's DAV endpoint has buggy handling of renames,
|
|
|
|
-- so avoid renaming when using it.
|
2019-03-11 16:44:12 +00:00
|
|
|
| boxComUrl `isPrefixOf` baseURL h -> return Nothing
|
|
|
|
| otherwise -> do
|
|
|
|
v <- runExport (Just h) $ \dav -> do
|
|
|
|
maybe noop (void . mkColRecursive) (locationParent destl)
|
|
|
|
moveDAV (baseURL dav) srcl destl
|
|
|
|
return True
|
|
|
|
return (Just v)
|
|
|
|
Nothing -> return (Just False)
|
|
|
|
_ -> return (Just False)
|
2017-09-12 18:08:00 +00:00
|
|
|
|
2017-09-12 20:59:04 +00:00
|
|
|
runExport :: Maybe DavHandle -> (DavHandle -> DAVT IO Bool) -> Annex Bool
|
|
|
|
runExport Nothing _ = return False
|
|
|
|
runExport (Just h) a = fromMaybe False <$> liftIO (goDAV h $ safely (a h))
|
2017-09-12 18:08:00 +00:00
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
configUrl :: Remote -> Maybe URLString
|
2013-12-02 20:01:20 +00:00
|
|
|
configUrl r = fixup <$> M.lookup "url" (config r)
|
|
|
|
where
|
|
|
|
-- box.com DAV url changed
|
2017-09-13 19:09:52 +00:00
|
|
|
fixup = replace "https://www.box.com/dav/" boxComUrl
|
|
|
|
|
|
|
|
boxComUrl :: URLString
|
|
|
|
boxComUrl = "https://dav.box.com/dav/"
|
2013-04-27 19:16:06 +00:00
|
|
|
|
2014-08-07 22:32:07 +00:00
|
|
|
type DavUser = B8.ByteString
|
|
|
|
type DavPass = B8.ByteString
|
|
|
|
|
|
|
|
baseURL :: DavHandle -> URLString
|
|
|
|
baseURL (DavHandle _ _ _ u) = u
|
|
|
|
|
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
toDavUser :: String -> DavUser
|
|
|
|
toDavUser = B8.fromString
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2012-11-15 17:46:16 +00:00
|
|
|
toDavPass :: String -> DavPass
|
|
|
|
toDavPass = B8.fromString
|
|
|
|
|
2012-11-15 00:25:00 +00:00
|
|
|
{- Test if a WebDAV store is usable, by writing to a test file, and then
|
2014-08-07 19:45:56 +00:00
|
|
|
- deleting the file.
|
|
|
|
-
|
|
|
|
- Also ensures that the path of the url exists, trying to create it if not.
|
|
|
|
-
|
|
|
|
- Throws an error if store is not usable.
|
|
|
|
-}
|
|
|
|
testDav :: URLString -> Maybe CredPair -> Annex ()
|
|
|
|
testDav url (Just (u, p)) = do
|
2015-10-11 17:29:44 +00:00
|
|
|
showAction "testing WebDAV server"
|
2014-08-07 22:32:07 +00:00
|
|
|
test $ liftIO $ evalDAVT url $ do
|
|
|
|
prepDAV user pass
|
2014-08-07 19:45:56 +00:00
|
|
|
makeParentDirs
|
2017-09-15 19:52:31 +00:00
|
|
|
inLocation (tmpLocation "test") $ do
|
2015-12-11 16:13:20 +00:00
|
|
|
putContentM (Nothing, L8.fromString "test")
|
2014-08-07 19:45:56 +00:00
|
|
|
delContentM
|
2012-11-17 19:30:11 +00:00
|
|
|
where
|
2014-08-07 19:45:56 +00:00
|
|
|
test a = liftIO $
|
|
|
|
either (\e -> throwIO $ "WebDAV test failed: " ++ show e)
|
2012-12-01 18:32:50 +00:00
|
|
|
(const noop)
|
2014-02-24 22:21:51 +00:00
|
|
|
=<< tryNonAsync a
|
2012-11-17 19:30:11 +00:00
|
|
|
|
2012-11-16 04:09:22 +00:00
|
|
|
user = toDavUser u
|
|
|
|
pass = toDavPass p
|
2012-11-16 17:32:18 +00:00
|
|
|
testDav _ Nothing = error "Need to configure webdav username and password."
|
2012-11-15 00:25:00 +00:00
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
{- Tries to make all the parent directories in the WebDAV urls's path,
|
|
|
|
- right down to the root.
|
|
|
|
-
|
|
|
|
- Ignores any failures, which can occur for reasons including the WebDAV
|
|
|
|
- server only serving up WebDAV in a subdirectory. -}
|
|
|
|
makeParentDirs :: DAVT IO ()
|
|
|
|
makeParentDirs = go
|
|
|
|
where
|
|
|
|
go = do
|
|
|
|
l <- getDAVLocation
|
|
|
|
case locationParent l of
|
|
|
|
Nothing -> noop
|
|
|
|
Just p -> void $ safely $ inDAVLocation (const p) go
|
|
|
|
void $ safely mkCol
|
|
|
|
|
|
|
|
{- Checks if the directory exists. If not, tries to create its
|
|
|
|
- parent directories, all the way down to the root, and finally creates
|
|
|
|
- it. -}
|
|
|
|
mkColRecursive :: DavLocation -> DAVT IO Bool
|
|
|
|
mkColRecursive d = go =<< existsDAV d
|
|
|
|
where
|
|
|
|
go (Right True) = return True
|
2017-10-07 18:11:32 +00:00
|
|
|
go _ = do
|
|
|
|
debugDav $ "mkCol " ++ d
|
|
|
|
ifM (inLocation d mkCol)
|
|
|
|
( return True
|
|
|
|
, do
|
|
|
|
case locationParent d of
|
|
|
|
Nothing -> makeParentDirs
|
|
|
|
Just parent -> void (mkColRecursive parent)
|
|
|
|
inLocation d mkCol
|
|
|
|
)
|
2014-08-07 19:45:56 +00:00
|
|
|
|
2016-05-23 21:03:20 +00:00
|
|
|
getCreds :: RemoteConfig -> RemoteGitConfig -> UUID -> Annex (Maybe CredPair)
|
|
|
|
getCreds c gc u = getRemoteCredPairFor "webdav" c gc (davCreds u)
|
2012-11-15 00:25:00 +00:00
|
|
|
|
|
|
|
davCreds :: UUID -> CredPairStorage
|
|
|
|
davCreds u = CredPairStorage
|
2012-12-13 04:45:27 +00:00
|
|
|
{ credPairFile = fromUUID u
|
|
|
|
, credPairEnvironment = ("WEBDAV_USERNAME", "WEBDAV_PASSWORD")
|
2018-12-04 17:37:43 +00:00
|
|
|
, credPairRemoteKey = "davcreds"
|
2012-12-13 04:45:27 +00:00
|
|
|
}
|
2014-02-24 22:21:51 +00:00
|
|
|
|
|
|
|
{- Content-Type to use for files uploaded to WebDAV. -}
|
|
|
|
contentType :: Maybe B8.ByteString
|
|
|
|
contentType = Just $ B8.fromString "application/octet-stream"
|
|
|
|
|
|
|
|
throwIO :: String -> IO a
|
|
|
|
throwIO msg = ioError $ mkIOError userErrorType msg Nothing Nothing
|
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
moveDAV :: URLString -> DavLocation -> DavLocation -> DAVT IO ()
|
2017-10-07 18:11:32 +00:00
|
|
|
moveDAV baseurl src dest = do
|
|
|
|
debugDav $ "moveContent " ++ src ++ " " ++ newurl
|
|
|
|
inLocation src $ moveContentM (B8.fromString newurl)
|
2014-02-24 22:21:51 +00:00
|
|
|
where
|
2017-10-07 18:11:32 +00:00
|
|
|
newurl = locationUrl baseurl dest
|
2014-02-24 22:21:51 +00:00
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
existsDAV :: DavLocation -> DAVT IO (Either String Bool)
|
2017-10-07 18:11:32 +00:00
|
|
|
existsDAV l = do
|
|
|
|
debugDav $ "getProps " ++ l
|
|
|
|
inLocation l check `catchNonAsync` (\e -> return (Left $ show e))
|
2014-02-24 22:21:51 +00:00
|
|
|
where
|
2014-08-07 19:45:56 +00:00
|
|
|
check = do
|
2016-02-09 15:47:35 +00:00
|
|
|
-- Some DAV services only support depth of 1, and
|
|
|
|
-- more depth is certainly not needed to check if a
|
|
|
|
-- location exists.
|
|
|
|
setDepth (Just Depth1)
|
2017-09-12 19:13:42 +00:00
|
|
|
catchJust missinghttpstatus
|
2014-02-24 22:21:51 +00:00
|
|
|
(getPropsM >> ispresent True)
|
|
|
|
(const $ ispresent False)
|
2014-08-07 19:45:56 +00:00
|
|
|
ispresent = return . Right
|
2017-09-12 19:13:42 +00:00
|
|
|
missinghttpstatus e =
|
|
|
|
matchStatusCodeException (== notFound404) e
|
|
|
|
<|> matchHttpExceptionContent toomanyredirects e
|
|
|
|
toomanyredirects (TooManyRedirects _) = True
|
|
|
|
toomanyredirects _ = False
|
2014-02-24 22:21:51 +00:00
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
safely :: DAVT IO a -> DAVT IO (Maybe a)
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
safely = eitherToMaybe <$$> tryNonAsync
|
2014-08-07 19:45:56 +00:00
|
|
|
|
2014-08-07 22:32:07 +00:00
|
|
|
choke :: IO (Either String a) -> IO a
|
|
|
|
choke f = do
|
|
|
|
x <- f
|
|
|
|
case x of
|
|
|
|
Left e -> error e
|
|
|
|
Right r -> return r
|
|
|
|
|
|
|
|
data DavHandle = DavHandle DAVContext DavUser DavPass URLString
|
|
|
|
|
|
|
|
withDAVHandle :: Remote -> (Maybe DavHandle -> Annex a) -> Annex a
|
|
|
|
withDAVHandle r a = do
|
2016-05-23 21:03:20 +00:00
|
|
|
mcreds <- getCreds (config r) (gitconfig r) (uuid r)
|
2014-08-07 22:32:07 +00:00
|
|
|
case (mcreds, configUrl r) of
|
|
|
|
(Just (user, pass), Just baseurl) ->
|
unify exception handling into Utility.Exception
Removed old extensible-exceptions, only needed for very old ghc.
Made webdav use Utility.Exception, to work after some changes in DAV's
exception handling.
Removed Annex.Exception. Mostly this was trivial, but note that
tryAnnex is replaced with tryNonAsync and catchAnnex replaced with
catchNonAsync. In theory that could be a behavior change, since the former
caught all exceptions, and the latter don't catch async exceptions.
However, in practice, nothing in the Annex monad uses async exceptions.
Grepping for throwTo and killThread only find stuff in the assistant,
which does not seem related.
Command.Add.undo is changed to accept a SomeException, and things
that use it for rollback now catch non-async exceptions, rather than
only IOExceptions.
2014-08-08 01:55:44 +00:00
|
|
|
withDAVContext baseurl $ \ctx ->
|
2014-08-07 22:32:07 +00:00
|
|
|
a (Just (DavHandle ctx (toDavUser user) (toDavPass pass) baseurl))
|
|
|
|
_ -> a Nothing
|
|
|
|
|
|
|
|
goDAV :: DavHandle -> DAVT IO a -> IO a
|
2014-08-09 05:38:13 +00:00
|
|
|
goDAV (DavHandle ctx user pass _) a = choke $ run $ prettifyExceptions $ do
|
2014-08-07 22:32:07 +00:00
|
|
|
prepDAV user pass
|
2014-02-24 22:21:51 +00:00
|
|
|
a
|
|
|
|
where
|
2014-08-07 22:32:07 +00:00
|
|
|
run = fst <$$> runDAVContext ctx
|
|
|
|
|
2014-08-09 05:38:13 +00:00
|
|
|
{- Catch StatusCodeException and trim it to only the statusMessage part,
|
|
|
|
- eliminating a lot of noise, which can include the whole request that
|
|
|
|
- failed. The rethrown exception is no longer a StatusCodeException. -}
|
2016-12-10 12:24:27 +00:00
|
|
|
#if MIN_VERSION_http_client(0,5,0)
|
|
|
|
prettifyExceptions :: DAVT IO a -> DAVT IO a
|
|
|
|
prettifyExceptions a = catchJust (matchStatusCodeException (const True)) a go
|
|
|
|
where
|
2017-09-28 16:01:58 +00:00
|
|
|
go (HttpExceptionRequest req (StatusCodeException response message)) = giveup $ unwords
|
2016-12-10 12:24:27 +00:00
|
|
|
[ "DAV failure:"
|
|
|
|
, show (responseStatus response)
|
|
|
|
, show (message)
|
2017-09-28 16:01:58 +00:00
|
|
|
, "HTTP request:"
|
|
|
|
, show (HTTP.method req)
|
|
|
|
, show (HTTP.path req)
|
2016-12-10 12:24:27 +00:00
|
|
|
]
|
|
|
|
go e = throwM e
|
|
|
|
#else
|
2014-08-09 05:38:13 +00:00
|
|
|
prettifyExceptions :: DAVT IO a -> DAVT IO a
|
|
|
|
prettifyExceptions a = catchJust (matchStatusCodeException (const True)) a go
|
|
|
|
where
|
2017-09-28 16:01:58 +00:00
|
|
|
go (StatusCodeException status _ _) = giveup $ unwords
|
2014-08-09 05:38:13 +00:00
|
|
|
[ "DAV failure:"
|
|
|
|
, show (statusCode status)
|
|
|
|
, show (statusMessage status)
|
|
|
|
]
|
|
|
|
go e = throwM e
|
2016-12-10 12:24:27 +00:00
|
|
|
#endif
|
2014-08-09 05:38:13 +00:00
|
|
|
|
2014-08-07 22:32:07 +00:00
|
|
|
prepDAV :: DavUser -> DavPass -> DAVT IO ()
|
|
|
|
prepDAV user pass = do
|
|
|
|
setResponseTimeout Nothing -- disable default (5 second!) timeout
|
|
|
|
setCreds user pass
|
2014-08-08 17:17:24 +00:00
|
|
|
|
|
|
|
--
|
|
|
|
-- Legacy chunking code, to be removed eventually.
|
|
|
|
--
|
|
|
|
|
|
|
|
storeLegacyChunked :: ChunkSize -> Key -> DavHandle -> L.ByteString -> IO Bool
|
|
|
|
storeLegacyChunked chunksize k dav b =
|
|
|
|
Legacy.storeChunks k tmp dest storer recorder finalizer
|
|
|
|
where
|
|
|
|
storehttp l b' = void $ goDAV dav $ do
|
|
|
|
maybe noop (void . mkColRecursive) (locationParent l)
|
2017-10-07 18:11:32 +00:00
|
|
|
debugDav $ "putContent " ++ l
|
2014-08-08 17:17:24 +00:00
|
|
|
inLocation l $ putContentM (contentType, b')
|
|
|
|
storer locs = Legacy.storeChunked chunksize locs storehttp b
|
|
|
|
recorder l s = storehttp l (L8.fromString s)
|
|
|
|
finalizer tmp' dest' = goDAV dav $
|
2017-09-12 18:08:00 +00:00
|
|
|
finalizeStore dav tmp' (fromJust $ locationParent dest')
|
2014-08-08 17:17:24 +00:00
|
|
|
|
2016-02-09 15:50:40 +00:00
|
|
|
tmp = addTrailingPathSeparator $ keyTmpLocation k
|
2014-08-08 17:40:55 +00:00
|
|
|
dest = keyLocation k
|
2014-08-08 17:17:24 +00:00
|
|
|
|
|
|
|
retrieveLegacyChunked :: DavHandle -> Retriever
|
|
|
|
retrieveLegacyChunked dav = fileRetriever $ \d k p -> liftIO $
|
|
|
|
withStoredFilesLegacyChunked k dav onerr $ \locs ->
|
2014-08-08 17:40:55 +00:00
|
|
|
Legacy.meteredWriteFileChunks p d locs $ \l ->
|
2017-10-07 18:11:32 +00:00
|
|
|
goDAV dav $ do
|
|
|
|
debugDav $ "getContent " ++ l
|
2014-08-08 17:40:55 +00:00
|
|
|
inLocation l $
|
|
|
|
snd <$> getContentM
|
2014-08-08 17:17:24 +00:00
|
|
|
where
|
|
|
|
onerr = error "download failed"
|
|
|
|
|
|
|
|
checkKeyLegacyChunked :: DavHandle -> CheckPresent
|
|
|
|
checkKeyLegacyChunked dav k = liftIO $
|
|
|
|
either error id <$> withStoredFilesLegacyChunked k dav onerr check
|
|
|
|
where
|
|
|
|
check [] = return $ Right True
|
|
|
|
check (l:ls) = do
|
|
|
|
v <- goDAV dav $ existsDAV l
|
|
|
|
if v == Right True
|
|
|
|
then check ls
|
|
|
|
else return v
|
|
|
|
|
|
|
|
{- Failed to read the chunkcount file; see if it's missing,
|
|
|
|
- or if there's a problem accessing it,
|
|
|
|
- or perhaps this was an intermittent error. -}
|
|
|
|
onerr f = do
|
|
|
|
v <- goDAV dav $ existsDAV f
|
|
|
|
return $ if v == Right True
|
|
|
|
then Left $ "failed to read " ++ f
|
|
|
|
else v
|
|
|
|
|
|
|
|
withStoredFilesLegacyChunked
|
|
|
|
:: Key
|
|
|
|
-> DavHandle
|
|
|
|
-> (DavLocation -> IO a)
|
|
|
|
-> ([DavLocation] -> IO a)
|
|
|
|
-> IO a
|
|
|
|
withStoredFilesLegacyChunked k dav onerr a = do
|
|
|
|
let chunkcount = keyloc ++ Legacy.chunkCount
|
2017-10-07 18:11:32 +00:00
|
|
|
v <- goDAV dav $ safely $ do
|
|
|
|
debugDav $ "getContent " ++ chunkcount
|
2014-08-08 17:17:24 +00:00
|
|
|
inLocation chunkcount $
|
|
|
|
snd <$> getContentM
|
|
|
|
case v of
|
|
|
|
Just s -> a $ Legacy.listChunks keyloc $ L8.toString s
|
|
|
|
Nothing -> do
|
|
|
|
chunks <- Legacy.probeChunks keyloc $ \f ->
|
|
|
|
(== Right True) <$> goDAV dav (existsDAV f)
|
|
|
|
if null chunks
|
|
|
|
then onerr chunkcount
|
|
|
|
else a chunks
|
|
|
|
where
|
2014-08-08 17:40:55 +00:00
|
|
|
keyloc = keyLocation k
|
2017-10-07 18:11:32 +00:00
|
|
|
|
|
|
|
debugDav :: MonadIO m => String -> DAVT m ()
|
|
|
|
debugDav msg = liftIO $ debugM "WebDAV" msg
|