summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2021-06-18 06:05:16 +0200
committerGitHub <noreply@github.com>2021-06-18 06:05:16 +0200
commitb17bd0ef2ec47e7fcbf8fa901fd8752dcc5cb44b (patch)
tree2cb9e791016248246673ca4cae90a515ce2e857b /python
parent5f4ed4033195b1c92af0899c549c2bc20a5f5ea7 (diff)
parentb3fbc7bd6bf6de9ab09e8e344e77f53e9213d392 (diff)
downloadvyos-1x-b17bd0ef2ec47e7fcbf8fa901fd8752dcc5cb44b.tar.gz
vyos-1x-b17bd0ef2ec47e7fcbf8fa901fd8752dcc5cb44b.zip
Merge pull request #882 from erkin/current
T3356: remote: Use the local filename if the destination is a directory in SFTP transfers
Diffstat (limited to 'python')
-rw-r--r--python/vyos/remote.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/python/vyos/remote.py b/python/vyos/remote.py
index c36b77630..05f739dc8 100644
--- a/python/vyos/remote.py
+++ b/python/vyos/remote.py
@@ -17,6 +17,7 @@ from ftplib import FTP
import os
import shutil
import socket
+import stat
import sys
import tempfile
import urllib.parse
@@ -152,7 +153,18 @@ def transfer_sftp(mode, local_path, hostname, remote_path,\
ssh.connect(hostname, port, username, password, sock=sock)
with ssh.open_sftp() as sftp:
if mode == 'upload':
- sftp.put(local_path, remote_path, callback=callback)
+ try:
+ # If the remote path is a directory, use the original filename.
+ if stat.S_ISDIR(sftp.stat(remote_path).st_mode):
+ path = os.path.join(remote_path, os.path.basename(local_path))
+ # A file exists at this destination. We're simply going to clobber it.
+ else:
+ path = remote_path
+ # This path doesn't point at any existing file. We can freely use this filename.
+ except IOError:
+ path = remote_path
+ finally:
+ sftp.put(local_path, path, callback=callback)
elif mode == 'download':
sftp.get(remote_path, local_path, callback=callback)
elif mode == 'size':