external: nice error message for keys with spaces in their name

External special remotes will refuse to operate on keys with spaces in
their names. That has never worked correctly due to the design of the
external special remote protocol. Display an error message suggesting
migration.

Not super happy with this, but it's a pragmatic solution. Better than
complicating the external special remote interface and all external special
remotes.

Note that I only made it use SafeKey in Request, not Response. git-annex
does not construct a Response, so that would not add any safety. And
presumably, if git-annex avoids feeding any such keys to an external
special remote, it will never have a reason to make a Response using such a
key. If it did, it would result in a protocol error anyway.

There's still a Serializeable instance for Key; it's used by P2P.Protocol.
There, the Key is always in the final position, so it's ok if it contains
spaces.

Note that the protocol documentation has been fixed to say that the File
may contain spaces. One way that can happen, even though the Key can't,
is when using direct mode, and the work tree filename contains spaces.
When sending such a file to the external special remote the worktree
filename is used.

This commit was sponsored by Thom May on Patreon.
This commit is contained in:
Joey Hess 2017-08-17 16:08:35 -04:00
parent 5421e8f695
commit dafafad115
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
5 changed files with 62 additions and 11 deletions

View file

@ -18,6 +18,8 @@ module Remote.External.Types (
Proto.Sendable(..),
Proto.Receivable(..),
Request(..),
SafeKey,
mkSafeKey,
needsPREPARE,
Response(..),
RemoteRequest(..),
@ -36,11 +38,13 @@ import Types.Transfer (Direction(..))
import Config.Cost (Cost)
import Types.Remote (RemoteConfig)
import Types.Availability (Availability(..))
import Types.Key
import Utility.Url (URLString)
import qualified Utility.SimpleProtocol as Proto
import Control.Concurrent.STM
import Network.URI
import Data.Char
data External = External
{ externalType :: ExternalType
@ -77,6 +81,29 @@ type PID = Int
data PrepareStatus = Unprepared | Prepared | FailedPrepare ErrorMsg
-- The protocol does not support keys with spaces in their names;
-- SafeKey can only be constructed for keys that are safe to use with the
-- protocol.
newtype SafeKey = SafeKey Key
deriving (Show)
mkSafeKey :: Key -> Either String SafeKey
mkSafeKey k
| any isSpace (keyName k) = Left $ concat
[ "Sorry, this file cannot be stored on an external special remote because its key's name contains a space. "
, "To avoid this problem, you can run: git-annex migrate --backend="
, formatKeyVariety (keyVariety k)
, " and pass it the name of the file"
]
| otherwise = Right (SafeKey k)
fromSafeKey :: SafeKey -> Key
fromSafeKey (SafeKey k) = k
instance Proto.Serializable SafeKey where
serialize = Proto.serialize . fromSafeKey
deserialize = fmap SafeKey . Proto.deserialize
-- Messages that can be sent to the external remote to request it do something.
data Request
= PREPARE
@ -85,10 +112,10 @@ data Request
| GETAVAILABILITY
| CLAIMURL URLString
| CHECKURL URLString
| TRANSFER Direction Key FilePath
| CHECKPRESENT Key
| REMOVE Key
| WHEREIS Key
| TRANSFER Direction SafeKey FilePath
| CHECKPRESENT SafeKey
| REMOVE SafeKey
| WHEREIS SafeKey
deriving (Show)
-- Does PREPARE need to have been sent before this request?