1818"""
1919SFTP channel class and related SFTP flags.
2020
21+ File types
22+ ------------
23+ :var LIBSSH2_SFTP_S_IFMT: Type of file mask
24+ :var LIBSSH2_SFTP_S_IFIFO: Named pipe (fifo)
25+ :var LIBSSH2_SFTP_S_IFCHR: Character special (character device)
26+ :var LIBSSH2_SFTP_S_IFDIR: Directory
27+ :var LIBSSH2_SFTP_S_IFBLK: Block special (block device)
28+ :var LIBSSH2_SFTP_S_IFREG: Regular file
29+ :var LIBSSH2_SFTP_S_IFLNK: Symbolic link
30+ :var LIBSSH2_SFTP_S_IFSOCK: Socket
31+
2132File transfer flags
2233--------------------
2334:var LIBSSH2_FXF_READ: File read flag
@@ -53,14 +64,20 @@ ____________
5364:var LIBSSH2_SFTP_S_IROTH: Read
5465:var LIBSSH2_SFTP_S_IWOTH: Write
5566:var LIBSSH2_SFTP_S_IXOTH: Execute
67+
68+ Generic mode masks
69+ ___________________
70+
71+ :var LIBSSH2_SFTP_ST_RDONLY: Read only
72+ :var LIBSSH2_SFTP_ST_NOSUID: No suid
5673"""
5774
5875from libc.stdlib cimport malloc, free
5976
6077from session cimport Session
6178from error_codes cimport _LIBSSH2_ERROR_BUFFER_TOO_SMALL
6279from channel cimport Channel, PyChannel
63- from utils cimport to_bytes, to_str_len
80+ from utils cimport to_bytes, to_str_len, handle_error_codes
6481from sftp_handle cimport SFTPHandle, PySFTPHandle, SFTPAttributes, SFTPStatVFS
6582
6683from exceptions import SFTPHandleError, SFTPBufferTooSmall, SFTPIOError
@@ -70,7 +87,23 @@ cimport c_sftp
7087
7188
7289# File types
73- # TODO
90+
91+ # Type of file mask
92+ LIBSSH2_SFTP_S_IFMT = c_sftp.LIBSSH2_SFTP_S_IFMT
93+ # named pipe (fifo)
94+ LIBSSH2_SFTP_S_IFIFO = c_sftp.LIBSSH2_SFTP_S_IFIFO
95+ # character special
96+ LIBSSH2_SFTP_S_IFCHR = c_sftp.LIBSSH2_SFTP_S_IFCHR
97+ # directory
98+ LIBSSH2_SFTP_S_IFDIR = c_sftp.LIBSSH2_SFTP_S_IFDIR
99+ # block special (block device)
100+ LIBSSH2_SFTP_S_IFBLK = c_sftp.LIBSSH2_SFTP_S_IFBLK
101+ # regular
102+ LIBSSH2_SFTP_S_IFREG = c_sftp.LIBSSH2_SFTP_S_IFREG
103+ # symbolic link
104+ LIBSSH2_SFTP_S_IFLNK = c_sftp.LIBSSH2_SFTP_S_IFLNK
105+ # socket
106+ LIBSSH2_SFTP_S_IFSOCK = c_sftp.LIBSSH2_SFTP_S_IFSOCK
74107
75108
76109# File Transfer Flags
@@ -84,6 +117,7 @@ LIBSSH2_FXF_EXCL = c_sftp.LIBSSH2_FXF_EXCL
84117
85118
86119# File mode masks
120+
87121# Read, write, execute/search by owner
88122LIBSSH2_SFTP_S_IRWXU = c_sftp.LIBSSH2_SFTP_S_IRWXU
89123LIBSSH2_SFTP_S_IRUSR = c_sftp.LIBSSH2_SFTP_S_IRUSR
@@ -100,7 +134,9 @@ LIBSSH2_SFTP_S_IROTH = c_sftp.LIBSSH2_SFTP_S_IROTH
100134LIBSSH2_SFTP_S_IWOTH = c_sftp.LIBSSH2_SFTP_S_IWOTH
101135LIBSSH2_SFTP_S_IXOTH = c_sftp.LIBSSH2_SFTP_S_IXOTH
102136
137+ # Read only
103138LIBSSH2_SFTP_ST_RDONLY = c_sftp.LIBSSH2_SFTP_ST_RDONLY
139+ # No suid
104140LIBSSH2_SFTP_ST_NOSUID = c_sftp.LIBSSH2_SFTP_ST_NOSUID
105141
106142
@@ -124,13 +160,26 @@ cdef class SFTP:
124160 with nogil:
125161 c_sftp.libssh2_sftp_shutdown(self ._sftp)
126162
163+ @property
164+ def session (self ):
165+ """ Originating session."""
166+ return self ._session
167+
168+ cdef int _handle_error(self , int errcode, path) except - 1 :
169+ if errcode == c_ssh2.LIBSSH2_ERROR_EAGAIN:
170+ return errcode
171+ raise SFTPHandleError(
172+ " Could not open file or directory %s - error code %s - %s " , path,
173+ errcode, self ._session.last_error())
174+
127175 def get_channel (self ):
128176 """ Get new channel from the SFTP session"""
129177 cdef c_ssh2.LIBSSH2_CHANNEL * _channel
130178 with nogil:
131179 _channel = c_sftp.libssh2_sftp_get_channel(self ._sftp)
132180 if _channel is NULL :
133- return
181+ return handle_error_codes(c_ssh2.libssh2_session_last_errno(
182+ self ._session._session))
134183 return PyChannel(_channel, self ._session)
135184
136185 def open_ex (self , const char *filename ,
@@ -144,9 +193,8 @@ cdef class SFTP:
144193 self ._sftp, filename, filename_len, flags,
145194 mode, open_type)
146195 if _handle is NULL :
147- raise SFTPHandleError(
148- " Could not open file %s - error code %s - %s " , filename,
149- self ._session.last_errno(), self ._session.last_error())
196+ return self ._handle_error(c_ssh2.libssh2_session_last_errno(
197+ self ._session._session), filename)
150198 handle = PySFTPHandle(_handle, self )
151199 return handle
152200
@@ -191,9 +239,8 @@ cdef class SFTP:
191239 _handle = c_sftp.libssh2_sftp_open(
192240 self ._sftp, _filename, flags, mode)
193241 if _handle is NULL :
194- raise SFTPHandleError(
195- " Could not open file %s - error code %s - %s " , filename,
196- self ._session.last_errno(), self ._session.last_error())
242+ return self ._handle_error(c_ssh2.libssh2_session_last_errno(
243+ self ._session._session), filename)
197244 return PySFTPHandle(_handle, self )
198245
199246 def opendir (self , path not None ):
@@ -213,9 +260,8 @@ cdef class SFTP:
213260 with nogil:
214261 _handle = c_sftp.libssh2_sftp_opendir(self ._sftp, _path)
215262 if _handle is NULL :
216- raise SFTPHandleError(
217- " Could not open directory %s - error code %s - %s " , path,
218- self ._session.last_errno(), self ._session.last_error())
263+ return self ._handle_error(c_ssh2.libssh2_session_last_errno(
264+ self ._session._session), path)
219265 return PySFTPHandle(_handle, self )
220266
221267 def rename_ex (self , const char *source_filename ,
0 commit comments