Skip to content

Commit 688b1b3

Browse files
author
Pan
committed
Updated sftp error handling. Added sftp tests.
1 parent afb490d commit 688b1b3

File tree

5 files changed

+296
-24
lines changed

5 files changed

+296
-24
lines changed

ssh/error_codes.pyx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file is part of ssh-python.
2+
# Copyright (C) 2018 Panos Kittenis
3+
#
4+
# This library is free software; you can redistribute it and/or
5+
# modify it under the terms of the GNU Lesser General Public
6+
# License as published by the Free Software Foundation, version 2.1.
7+
#
8+
# This library is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
# Lesser General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU Lesser General Public
14+
# License along with this library; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-130
16+
17+
cimport c_ssh
18+
19+
20+
SSH_OK = c_ssh.SSH_OK
21+
SSH_EOF = c_ssh.SSH_EOF
22+
SSH_ERROR = c_ssh.SSH_ERROR
23+
SSH_AGAIN = c_ssh.SSH_AGAIN

ssh/session.pyx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ from sftp cimport SFTP
2929
from exceptions import OptionError, InvalidAPIUse
3030

3131
cimport c_ssh
32-
from c_sftp cimport sftp_session, sftp_new
32+
from c_sftp cimport sftp_session, sftp_new, sftp_init
3333

3434

3535
cdef bint _check_connected(c_ssh.ssh_session session) nogil except -1:
@@ -95,6 +95,26 @@ cdef class Session:
9595
c_ssh.ssh_get_error_code(self._session), self._session)
9696
return SFTP.from_ptr(_sftp, self)
9797

98+
def sftp_init(self):
99+
"""Convenience function for creating and initialising new SFTP
100+
session.
101+
102+
Not part of libssh API."""
103+
cdef sftp_session _sftp
104+
cdef SFTP sftp
105+
cdef int rc
106+
with nogil:
107+
_check_connected(self._session)
108+
_sftp = sftp_new(self._session)
109+
if _sftp is NULL:
110+
with gil:
111+
return handle_ssh_error_codes(
112+
c_ssh.ssh_get_error_code(self._session), self._session)
113+
rc = sftp_init(_sftp)
114+
sftp = SFTP.from_ptr(_sftp, self)
115+
handle_ssh_error_codes(rc, self._session)
116+
return sftp
117+
98118
def connect(self):
99119
cdef int rc
100120
with nogil:

ssh/sftp.pyx

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ from sftp_handles cimport SFTPFile, SFTPDir
2121
from sftp_attributes cimport SFTPAttributes
2222
from sftp_statvfs cimport SFTPStatVFS
2323
from utils cimport handle_ssh_error_codes, to_bytes
24-
from .exceptions import SFTPHandleError
24+
from .exceptions import SFTPError
2525

2626
cimport c_sftp
27-
from c_ssh cimport ssh_get_error_code, timeval
27+
from c_ssh cimport ssh_get_error, ssh_get_error_code, timeval
2828

2929

3030
cdef class SFTP:
@@ -98,12 +98,7 @@ cdef class SFTP:
9898
with nogil:
9999
c_dir = c_sftp.sftp_opendir(self._sftp, c_path)
100100
if c_dir is NULL:
101-
# May or may not be an 'ssh error' which only handles
102-
# three error types
103-
handle_ssh_error_codes(
104-
ssh_get_error_code(self.session._session),
105-
self.session._session)
106-
raise SFTPHandleError
101+
raise SFTPError(ssh_get_error(self.session._session))
107102
_dir = SFTPDir.from_ptr(c_dir, self)
108103
return _dir
109104

@@ -115,10 +110,7 @@ cdef class SFTP:
115110
with nogil:
116111
c_attrs = c_sftp.sftp_stat(self._sftp, c_path)
117112
if c_attrs is NULL:
118-
handle_ssh_error_codes(
119-
ssh_get_error_code(self.session._session),
120-
self.session._session)
121-
raise SFTPHandleError
113+
raise SFTPError(ssh_get_error(self.session._session))
122114
_attrs = SFTPAttributes.from_ptr(c_attrs, self)
123115
return _attrs
124116

@@ -130,10 +122,7 @@ cdef class SFTP:
130122
with nogil:
131123
c_attrs = c_sftp.sftp_lstat(self._sftp, c_path)
132124
if c_attrs is NULL:
133-
handle_ssh_error_codes(
134-
ssh_get_error_code(self.session._session),
135-
self.session._session)
136-
raise SFTPHandleError
125+
raise SFTPError(ssh_get_error(self.session._session))
137126
_attrs = SFTPAttributes.from_ptr(c_attrs, self)
138127
return _attrs
139128

@@ -145,10 +134,7 @@ cdef class SFTP:
145134
with nogil:
146135
c_file = c_sftp.sftp_open(self._sftp, c_path, accesstype, mode)
147136
if c_file is NULL:
148-
handle_ssh_error_codes(
149-
ssh_get_error_code(self.session._session),
150-
self.session._session)
151-
raise SFTPHandleError
137+
raise SFTPError(ssh_get_error(self.session._session))
152138
_file = SFTPFile.from_ptr(c_file, self)
153139
return _file
154140

@@ -158,7 +144,7 @@ cdef class SFTP:
158144
cdef int rc
159145
with nogil:
160146
rc = c_sftp.sftp_unlink(self._sftp, c_path)
161-
return handle_ssh_error_codes(rc, self.session._session)
147+
return handle_ssh_error_codes(rc, self.session._session)
162148

163149
def rmdir(self, path not None):
164150
cdef bytes b_path = to_bytes(path)

ssh/sftp_handles.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ cdef class SFTPFile:
130130
if size == SSH_ERROR:
131131
raise SFTPError(ssh_get_error(self.sftp.session._session))
132132
elif size == SSH_AGAIN:
133-
return SSH_AGAIN
133+
return SSH_AGAIN, buf
134134
finally:
135135
free(c_buf)
136136
return size, buf

0 commit comments

Comments
 (0)