1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-01-03 05:35:58 +00:00

new sftp: URLs, see #8372

sftp://user@host:port/rel/path
sftp://user@host:port//abs/path
This commit is contained in:
Thomas Waldmann 2024-10-14 14:36:21 +02:00
parent 83414b037d
commit d60303eb8c
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
2 changed files with 26 additions and 5 deletions

View file

@ -431,6 +431,12 @@ class Location:
(?P<path>(/([^:]|(:(?!:)))+)) # start with /, then any chars, but no "::"
"""
# path must not contain :: (it ends at :: or string end), but may contain single colons.
# it may or may not start with a /.
path_re = r"""
(?P<path>(([^:]|(:(?!:)))+)) # any chars, but no "::"
"""
# host NAME, or host IP ADDRESS (v4 or v6, v6 must be in square brackets)
host_re = r"""
(?P<host>(
@ -461,9 +467,9 @@ class Location:
+ optional_user_re
+ host_re
+ r""" # user@ (optional), host name or address
(?::(?P<port>\d+))? # :port (optional)
(?::(?P<port>\d+))?/ # :port (optional) + "/" as separator
"""
+ abs_path_re,
+ path_re,
re.VERBOSE,
) # path
@ -618,6 +624,14 @@ def canonical_path(self):
path = self.path
if self.proto == "rclone":
return f"{self.proto}:{self.path}"
elif self.proto == "sftp":
return (
f"{self.proto}://"
f"{(self.user + '@') if self.user else ''}"
f"{self._host if self._host else ''}"
f"{self.port if self.port else ''}/"
f"{path}"
)
else:
return "{}://{}{}{}{}".format(
self.proto if self.proto else "???",

View file

@ -197,11 +197,18 @@ def test_rclone(self, monkeypatch, keys_dir):
def test_sftp(self, monkeypatch, keys_dir):
monkeypatch.delenv("BORG_REPO", raising=False)
# relative path
assert (
repr(Location("sftp://user@host:1234/some/path"))
== "Location(proto='sftp', user='user', host='host', port=1234, path='/some/path')"
repr(Location("sftp://user@host:1234/rel/path"))
== "Location(proto='sftp', user='user', host='host', port=1234, path='rel/path')"
)
assert Location("sftp://user@host:1234/some/path").to_key_filename() == keys_dir + "host__some_path"
assert Location("sftp://user@host:1234/rel/path").to_key_filename() == keys_dir + "host__rel_path"
# absolute path
assert (
repr(Location("sftp://user@host:1234//abs/path"))
== "Location(proto='sftp', user='user', host='host', port=1234, path='/abs/path')"
)
assert Location("sftp://user@host:1234//abs/path").to_key_filename() == keys_dir + "host__abs_path"
def test_socket(self, monkeypatch, keys_dir):
monkeypatch.delenv("BORG_REPO", raising=False)