2014-08-07 19:45:56 +00:00
|
|
|
{- WebDAV locations.
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-08-07 19:45:56 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-08-07 19:45:56 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
|
|
|
|
module Remote.WebDAV.DavLocation where
|
|
|
|
|
|
|
|
import Types
|
2017-09-15 20:34:45 +00:00
|
|
|
import Types.Export
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Locations
|
2014-08-07 19:45:56 +00:00
|
|
|
import Utility.Url (URLString)
|
2017-05-16 03:32:17 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
import Utility.Split
|
|
|
|
#endif
|
2019-12-02 16:26:33 +00:00
|
|
|
import Utility.FileSystemEncoding
|
2014-08-07 19:45:56 +00:00
|
|
|
|
|
|
|
import System.FilePath.Posix -- for manipulating url paths
|
|
|
|
import Network.Protocol.HTTP.DAV (inDAVLocation, DAVT)
|
|
|
|
import Control.Monad.IO.Class (MonadIO)
|
2017-09-12 19:45:03 +00:00
|
|
|
import Network.URI
|
2015-01-28 19:55:17 +00:00
|
|
|
import Data.Default
|
2014-08-07 19:45:56 +00:00
|
|
|
|
|
|
|
-- Relative to the top of the DAV url.
|
|
|
|
type DavLocation = String
|
|
|
|
|
2017-09-12 19:45:03 +00:00
|
|
|
{- Runs action with a new location relative to the current location. -}
|
2014-08-07 19:45:56 +00:00
|
|
|
inLocation :: (MonadIO m) => DavLocation -> DAVT m a -> DAVT m a
|
2017-09-12 19:45:03 +00:00
|
|
|
inLocation d = inDAVLocation (</> d')
|
|
|
|
where
|
|
|
|
d' = escapeURIString isUnescapedInURI d
|
2014-08-07 19:45:56 +00:00
|
|
|
|
|
|
|
{- The directory where files(s) for a key are stored. -}
|
2014-08-08 17:40:55 +00:00
|
|
|
keyDir :: Key -> DavLocation
|
2019-12-18 20:45:03 +00:00
|
|
|
keyDir k = addTrailingPathSeparator $ hashdir </> fromRawFilePath (keyFile k)
|
2014-08-07 19:45:56 +00:00
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
2019-12-11 18:12:22 +00:00
|
|
|
hashdir = fromRawFilePath $ hashDirLower def k
|
2014-08-07 19:45:56 +00:00
|
|
|
#else
|
2019-12-11 18:12:22 +00:00
|
|
|
hashdir = replace "\\" "/" (fromRawFilePath $ hashDirLower def k)
|
2014-08-07 19:45:56 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-08 17:40:55 +00:00
|
|
|
keyLocation :: Key -> DavLocation
|
2019-12-18 20:45:03 +00:00
|
|
|
keyLocation k = keyDir k ++ fromRawFilePath (keyFile k)
|
2014-08-08 17:40:55 +00:00
|
|
|
|
2019-02-07 17:47:57 +00:00
|
|
|
{- Paths containing # or ? cannot be represented in an url, so fails on
|
|
|
|
- those. -}
|
|
|
|
exportLocation :: ExportLocation -> Either String DavLocation
|
|
|
|
exportLocation l =
|
2019-12-02 16:26:33 +00:00
|
|
|
let p = fromRawFilePath $ fromExportLocation l
|
|
|
|
in if any (`elem` p) illegalinurl
|
2019-02-07 17:47:57 +00:00
|
|
|
then Left ("Cannot store file containing '#' or '?' on webdav: " ++ p)
|
|
|
|
else Right p
|
2019-12-02 16:26:33 +00:00
|
|
|
where
|
|
|
|
illegalinurl = ['#', '?'] :: [Char]
|
2017-09-15 19:52:31 +00:00
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
{- Where we store temporary data for a key as it's being uploaded. -}
|
|
|
|
keyTmpLocation :: Key -> DavLocation
|
2019-12-18 20:45:03 +00:00
|
|
|
keyTmpLocation = tmpLocation . fromRawFilePath . keyFile
|
2014-08-07 19:45:56 +00:00
|
|
|
|
2021-03-16 18:17:29 +00:00
|
|
|
{- Where we store temporary data for a file as it's being exported.
|
|
|
|
-
|
|
|
|
- This could be just the keyTmpLocation, but when the file is in a
|
|
|
|
- subdirectory, the temp file is put in there. Partly this is to keep
|
2023-03-14 02:39:16 +00:00
|
|
|
- it close to the final destination; also certain webdav servers
|
2021-03-16 18:17:29 +00:00
|
|
|
- seem to be buggy when renaming files from the root into a subdir,
|
|
|
|
- and so writing to the subdir avoids such problems.
|
|
|
|
-}
|
2021-03-12 19:16:23 +00:00
|
|
|
exportTmpLocation :: ExportLocation -> Key -> DavLocation
|
2021-03-16 18:17:29 +00:00
|
|
|
exportTmpLocation l k
|
|
|
|
| length (splitDirectories p) > 1 = takeDirectory p </> keyTmpLocation k
|
|
|
|
| otherwise = keyTmpLocation k
|
2021-03-12 19:16:23 +00:00
|
|
|
where
|
2021-03-16 18:17:29 +00:00
|
|
|
p = fromRawFilePath (fromExportLocation l)
|
2021-03-12 19:16:23 +00:00
|
|
|
|
2014-08-07 19:45:56 +00:00
|
|
|
tmpLocation :: FilePath -> DavLocation
|
2017-09-15 19:52:31 +00:00
|
|
|
tmpLocation f = "git-annex-webdav-tmp-" ++ f
|
2014-08-07 19:45:56 +00:00
|
|
|
|
|
|
|
locationParent :: String -> Maybe String
|
|
|
|
locationParent loc
|
2017-10-11 15:09:27 +00:00
|
|
|
| loc `elem` tops || parent `elem` tops = Nothing
|
|
|
|
| otherwise = Just parent
|
2014-08-07 19:45:56 +00:00
|
|
|
where
|
|
|
|
tops = ["/", "", "."]
|
2017-10-11 15:09:27 +00:00
|
|
|
parent = takeDirectory loc
|
2014-08-07 19:45:56 +00:00
|
|
|
|
|
|
|
locationUrl :: URLString -> DavLocation -> URLString
|
|
|
|
locationUrl baseurl loc = baseurl </> loc
|