Skip to content

Commit

Permalink
Change _get_sftp_error_str to actually return strs
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Aug 16, 2021
1 parent 1ec16a9 commit a14cb2b
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/pylibsshext/sftp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ cdef class SFTP:

rf = sftp.sftp_open(self._libssh_sftp_session, remote_file_b, O_WRONLY | O_CREAT | O_TRUNC, sftp.S_IRWXU)
if rf is NULL:
raise LibsshSFTPException("Opening remote file [%s] for write failed with error [%s]" % (remote_file, MSG_MAP.get(self._get_sftp_error_str())))
raise LibsshSFTPException("Opening remote file [%s] for write failed with error [%s]" % (remote_file, self._get_sftp_error_str()))
buffer = f.read(1024)

while buffer != b"":
Expand All @@ -71,10 +71,9 @@ cdef class SFTP:
if written != length:
sftp.sftp_close(rf)
raise LibsshSFTPException(
"Writing to remote file [%s] failed with error [%s: %s]" % (
"Writing to remote file [%s] failed with error [%s]" % (
remote_file,
MSG_MAP.get(self._get_sftp_error_str()),
self.session._get_session_error_str(),
self._get_sftp_error_str(),
)
)
buffer = f.read(1024)
Expand All @@ -90,7 +89,7 @@ cdef class SFTP:

rf = sftp.sftp_open(self._libssh_sftp_session, remote_file_b, O_RDONLY, sftp.S_IRWXU)
if rf is NULL:
raise LibsshSFTPException("Opening remote file [%s] for read failed with error [%s]" % (remote_file, MSG_MAP.get(self._get_sftp_error_str())))
raise LibsshSFTPException("Opening remote file [%s] for read failed with error [%s]" % (remote_file, self._get_sftp_error_str()))

while True:
file_data = sftp.sftp_read(rf, <void *>read_buffer, sizeof(char) * 1024)
Expand All @@ -99,7 +98,7 @@ cdef class SFTP:
elif file_data < 0:
sftp.sftp_close(rf)
raise LibsshSFTPException("Reading data from remote file [%s] failed with error [%s]"
% (remote_file, MSG_MAP.get(self._get_sftp_error_str())))
% (remote_file, self._get_sftp_error_str()))

with open(local_file, 'wb+') as f:
bytes_written = f.write(read_buffer[:file_data])
Expand All @@ -108,7 +107,7 @@ cdef class SFTP:
raise LibsshSFTPException("Number of bytes [%s] read from remote file [%s]"
" does not match number of bytes [%s] written to local file [%s]"
" due to error [%s]"
% (file_data, remote_file, bytes_written, local_file, MSG_MAP.get(self._get_sftp_error_str())))
% (file_data, remote_file, bytes_written, local_file, self._get_sftp_error_str()))
sftp.sftp_close(rf)

def close(self):
Expand All @@ -117,4 +116,7 @@ cdef class SFTP:
self._libssh_sftp_session = NULL

def _get_sftp_error_str(self):
return sftp.sftp_get_error(self._libssh_sftp_session)
error = sftp.sftp_get_error(self._libssh_sftp_session)
if error in MSG_MAP and error != sftp.SSH_FX_FAILURE:
return MSG_MAP[error]
return "Generic failure: %s" % self.session._get_session_error_str()

0 comments on commit a14cb2b

Please sign in to comment.