Skip to content

Commit

Permalink
scp: Fix pushing large files
Browse files Browse the repository at this point in the history
Fixes: #654
Signed-off-by: Jakub Jelen <[email protected]>
  • Loading branch information
Jakuje committed Nov 15, 2024
1 parent cc2ceff commit 2a191d6
Showing 1 changed file with 14 additions and 4 deletions.
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

0 comments on commit 2a191d6

Please sign in to comment.