Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pushing large files over SCP #661

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog-fragments/661.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Uploading large files over SCP no longer fails -- by :user:`Jakuje`.
18 changes: 14 additions & 4 deletions src/pylibsshext/scp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,25 @@ cdef class SCP:
)

try:
# Read buffer
read_buffer_size = min(file_size, SCP_MAX_CHUNK)

# Begin to send to the file
rc = libssh.ssh_scp_push_file(scp, filename_b, file_size, file_mode)
if rc != libssh.SSH_OK:
raise LibsshSCPException("Can't open remote file: %s" % self._get_ssh_error_str())

# Write to the open file
rc = libssh.ssh_scp_write(scp, PyBytes_AS_STRING(f.read()), file_size)
if rc != libssh.SSH_OK:
raise LibsshSCPException("Can't write to remote file: %s" % self._get_ssh_error_str())
remaining_bytes_to_read = file_size
while remaining_bytes_to_read > 0:
# Read the chunk from local file
read_bytes = min(remaining_bytes_to_read, read_buffer_size)
read_buffer = f.read(read_bytes)

# Write to the open file
rc = libssh.ssh_scp_write(scp, PyBytes_AS_STRING(read_buffer), read_bytes)
if rc != libssh.SSH_OK:
raise LibsshSCPException("Can't write to remote file: %s" % self._get_ssh_error_str())
remaining_bytes_to_read -= read_bytes
finally:
libssh.ssh_scp_close(scp)
libssh.ssh_scp_free(scp)
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/scp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,9 @@ def test_get_large(dst_path, src_path_large, ssh_scp, large_payload):
"""Check that SCP file download gets over 64kB of data."""
ssh_scp.get(str(src_path_large), str(dst_path))
assert dst_path.read_bytes() == large_payload


def test_put_large(dst_path, src_path_large, ssh_scp, large_payload):
"""Check that SCP file download gets over 64kB of data."""
ssh_scp.put(str(src_path_large), str(dst_path))
assert dst_path.read_bytes() == large_payload