Skip to content

Commit d3678e7

Browse files
committed
sftp: Fix overwriting existing files while downloading
Thanks @kucharskim for the report and testing! Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent 09df17e commit d3678e7

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/pylibsshext/sftp.pyx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,16 @@ cdef class SFTP:
9191
if rf is NULL:
9292
raise LibsshSFTPException("Opening remote file [%s] for read failed with error [%s]" % (remote_file, self._get_sftp_error_str()))
9393

94-
while True:
95-
file_data = sftp.sftp_read(rf, <void *>read_buffer, sizeof(char) * 1024)
96-
if file_data == 0:
97-
break
98-
elif file_data < 0:
99-
sftp.sftp_close(rf)
100-
raise LibsshSFTPException("Reading data from remote file [%s] failed with error [%s]"
101-
% (remote_file, self._get_sftp_error_str()))
102-
103-
with open(local_file, 'ab') as f:
94+
with open(local_file, 'wb') as f:
95+
while True:
96+
file_data = sftp.sftp_read(rf, <void *>read_buffer, sizeof(char) * 1024)
97+
if file_data == 0:
98+
break
99+
elif file_data < 0:
100+
sftp.sftp_close(rf)
101+
raise LibsshSFTPException("Reading data from remote file [%s] failed with error [%s]"
102+
% (remote_file, self._get_sftp_error_str()))
103+
104104
bytes_written = f.write(read_buffer[:file_data])
105105
if bytes_written and file_data != bytes_written:
106106
sftp.sftp_close(rf)

0 commit comments

Comments
 (0)