@@ -21,7 +21,7 @@ from sftp_handles cimport SFTPFile, SFTPDir
21
21
from sftp_attributes cimport SFTPAttributes
22
22
from sftp_statvfs cimport SFTPStatVFS
23
23
from utils cimport handle_ssh_error_codes, to_bytes
24
- from .exceptions import SFTPError
24
+ from .exceptions import SFTPError, SFTPHandleError
25
25
26
26
cimport c_sftp
27
27
from c_ssh cimport ssh_get_error, ssh_get_error_code, timeval
@@ -98,7 +98,7 @@ cdef class SFTP:
98
98
with nogil:
99
99
c_dir = c_sftp.sftp_opendir(self ._sftp, c_path)
100
100
if c_dir is NULL :
101
- raise SFTPError (ssh_get_error(self .session._session))
101
+ raise SFTPHandleError (ssh_get_error(self .session._session))
102
102
_dir = SFTPDir.from_ptr(c_dir, self )
103
103
return _dir
104
104
@@ -134,7 +134,7 @@ cdef class SFTP:
134
134
with nogil:
135
135
c_file = c_sftp.sftp_open(self ._sftp, c_path, accesstype, mode)
136
136
if c_file is NULL :
137
- raise SFTPError (ssh_get_error(self .session._session))
137
+ raise SFTPHandleError (ssh_get_error(self .session._session))
138
138
_file = SFTPFile.from_ptr(c_file, self )
139
139
return _file
140
140
@@ -144,15 +144,19 @@ cdef class SFTP:
144
144
cdef int rc
145
145
with nogil:
146
146
rc = c_sftp.sftp_unlink(self ._sftp, c_path)
147
- return handle_ssh_error_codes(rc, self .session._session)
147
+ if rc < 0 :
148
+ raise SFTPError(ssh_get_error(self .session._session))
149
+ return rc
148
150
149
151
def rmdir (self , path not None ):
150
152
cdef bytes b_path = to_bytes(path)
151
153
cdef const char * c_path = b_path
152
154
cdef int rc
153
155
with nogil:
154
156
rc = c_sftp.sftp_rmdir(self ._sftp, c_path)
155
- return handle_ssh_error_codes(rc, self .session._session)
157
+ if rc < 0 :
158
+ raise SFTPError(ssh_get_error(self .session._session))
159
+ return rc
156
160
157
161
def mkdir (self , path not None , c_sftp.mode_t mode ):
158
162
cdef bytes b_path = to_bytes(path)
@@ -170,7 +174,9 @@ cdef class SFTP:
170
174
cdef int rc
171
175
with nogil:
172
176
rc = c_sftp.sftp_rename(self ._sftp, c_orig, c_newname)
173
- return handle_ssh_error_codes(rc, self .session._session)
177
+ if rc < 0 :
178
+ raise SFTPError(ssh_get_error(self .session._session))
179
+ return rc
174
180
175
181
def setstat (self , path not None , SFTPAttributes attr ):
176
182
cdef bytes b_path = to_bytes(path)
@@ -187,15 +193,19 @@ cdef class SFTP:
187
193
cdef int rc
188
194
with nogil:
189
195
rc = c_sftp.sftp_chown(self ._sftp, c_path, owner, group)
190
- return handle_ssh_error_codes(rc, self .session._session)
196
+ if rc < 0 :
197
+ raise SFTPError(ssh_get_error(self .session._session))
198
+ return rc
191
199
192
200
def chmod (self , path not None , c_sftp.mode_t mode ):
193
201
cdef bytes b_path = to_bytes(path)
194
202
cdef const char * c_path = b_path
195
203
cdef int rc
196
204
with nogil:
197
205
rc = c_sftp.sftp_chmod(self ._sftp, c_path, mode)
198
- return handle_ssh_error_codes(rc, self .session._session)
206
+ if rc < 0 :
207
+ raise SFTPError(ssh_get_error(self .session._session))
208
+ return rc
199
209
200
210
def utimes (self , path not None , long seconds , long microseconds ):
201
211
cdef bytes b_path = to_bytes(path)
@@ -211,7 +221,9 @@ cdef class SFTP:
211
221
_val.tv_usec = microseconds
212
222
rc = c_sftp.sftp_utimes(self ._sftp, c_path, _val)
213
223
free(_val)
214
- return handle_ssh_error_codes(rc, self .session._session)
224
+ if rc < 0 :
225
+ raise SFTPError(ssh_get_error(self .session._session))
226
+ return rc
215
227
216
228
def symlink (self , source not None , dest not None ):
217
229
cdef bytes b_source = to_bytes(source)
@@ -221,7 +233,9 @@ cdef class SFTP:
221
233
cdef int rc
222
234
with nogil:
223
235
rc = c_sftp.sftp_symlink(self ._sftp, c_source, c_dest)
224
- return handle_ssh_error_codes(rc, self .session._session)
236
+ if rc < 0 :
237
+ raise SFTPError(ssh_get_error(self .session._session))
238
+ return rc
225
239
226
240
def readlink (self , path not None ):
227
241
cdef bytes b_path = to_bytes(path)
@@ -231,9 +245,7 @@ cdef class SFTP:
231
245
with nogil:
232
246
_link = c_sftp.sftp_readlink(self ._sftp, c_path)
233
247
if _link is NULL :
234
- return handle_ssh_error_codes(
235
- ssh_get_error_code(self .session._session),
236
- self .session._session)
248
+ raise SFTPError(ssh_get_error(self .session._session))
237
249
b_link = _link
238
250
return b_link
239
251
@@ -245,9 +257,7 @@ cdef class SFTP:
245
257
with nogil:
246
258
c_vfs = c_sftp.sftp_statvfs(self ._sftp, c_path)
247
259
if c_vfs is NULL :
248
- return handle_ssh_error_codes(
249
- ssh_get_error_code(self .session._session),
250
- self .session._session)
260
+ raise SFTPError(ssh_get_error(self .session._session))
251
261
vfs = SFTPStatVFS.from_ptr(c_vfs, self )
252
262
return vfs
253
263
@@ -259,14 +269,12 @@ cdef class SFTP:
259
269
with nogil:
260
270
_rpath = c_sftp.sftp_canonicalize_path(self ._sftp, c_path)
261
271
if _rpath is NULL :
262
- return handle_ssh_error_codes(
263
- ssh_get_error_code(self .session._session),
264
- self .session._session)
272
+ raise SFTPError(ssh_get_error(self .session._session))
265
273
b_rpath = _rpath
266
274
return b_rpath
267
275
268
276
def server_version (self ):
269
277
cdef int rc
270
278
with nogil:
271
279
rc = c_sftp.sftp_server_version(self ._sftp)
272
- return rc
280
+ return handle_ssh_error_codes(rc, self .session._session)
0 commit comments