Skip to content

Commit a5292bd

Browse files
committed
scp: Fix pushing large files
Fixes: #654 Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 4250532 commit a5292bd

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/pylibsshext/scp.pyx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,25 @@ cdef class SCP:
7474
)
7575

7676
try:
77+
# Read buffer
78+
read_buffer_size = min(file_size, SCP_MAX_CHUNK)
79+
7780
# Begin to send to the file
7881
rc = libssh.ssh_scp_push_file(scp, filename_b, file_size, file_mode)
7982
if rc != libssh.SSH_OK:
8083
raise LibsshSCPException("Can't open remote file: %s" % self._get_ssh_error_str())
8184

82-
# Write to the open file
83-
rc = libssh.ssh_scp_write(scp, PyBytes_AS_STRING(f.read()), file_size)
84-
if rc != libssh.SSH_OK:
85-
raise LibsshSCPException("Can't write to remote file: %s" % self._get_ssh_error_str())
85+
remaining_bytes_to_read = file_size
86+
while remaining_bytes_to_read > 0:
87+
# Read the chunk from local file
88+
read_bytes = min(remaining_bytes_to_read, read_buffer_size)
89+
read_buffer = f.read(read_bytes)
90+
91+
# Write to the open file
92+
rc = libssh.ssh_scp_write(scp, PyBytes_AS_STRING(read_buffer), read_bytes)
93+
if rc != libssh.SSH_OK:
94+
raise LibsshSCPException("Can't write to remote file: %s" % self._get_ssh_error_str())
95+
remaining_bytes_to_read -= read_bytes
8696
finally:
8797
libssh.ssh_scp_close(scp)
8898
libssh.ssh_scp_free(scp)

0 commit comments

Comments
 (0)