Skip to content

Commit 4845da9

Browse files
author
Pan
committed
Added sftp attributes and stats properties.
Added helper function to convert ssh string to bytes.
1 parent b3c60fc commit 4845da9

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

ssh/sftp_attributes.pyx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
from libc.stdlib cimport malloc, free
1818

1919
from sftp cimport SFTP
20+
from utils cimport ssh_string_to_bytes
2021

2122
cimport c_sftp
23+
from c_ssh cimport ssh_string
2224

2325

2426
cdef class SFTPAttributes:
@@ -134,18 +136,24 @@ cdef class SFTPAttributes:
134136
def mtime_nseconds(self):
135137
return self._attrs.mtime_nseconds if self._attrs is not NULL else None
136138

137-
# @property
138-
# def acl(self):
139-
# return self._attrs.acl if self._attrs is not NULL else None
139+
@property
140+
def acl(self):
141+
if self._attrs is NULL:
142+
return
143+
return ssh_string_to_bytes(self._attrs.acl)
140144

141145
@property
142146
def extended_count(self):
143147
return self._attrs.extended_count if self._attrs is not NULL else None
144148

145-
# @property
146-
# def extended_type(self):
147-
# return self._attrs.extended_type if self._attrs is not NULL else None
149+
@property
150+
def extended_type(self):
151+
if self._attrs is NULL:
152+
return
153+
return ssh_string_to_bytes(self._attrs.extended_type)
148154

149-
# @property
150-
# def extended_data(self):
151-
# return self._attrs.extended_data if self._attrs is not NULL else None
155+
@property
156+
def extended_data(self):
157+
if self._attrs is NULL:
158+
return
159+
return ssh_string_to_bytes(self._attrs.extended_data)

ssh/sftp_statvfs.pyx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,47 @@ cdef class SFTPStatVFS:
3434
cdef SFTPStatVFS _vfs = SFTPStatVFS.__new__(SFTPStatVFS, sftp)
3535
_vfs._stats = stats
3636
return _vfs
37+
38+
@property
39+
def f_bsize(self):
40+
return self._stats.f_bsize if self._stats is not NULL else None
41+
42+
@property
43+
def f_frsize(self):
44+
return self._stats.f_frsize if self._stats is not NULL else None
45+
46+
@property
47+
def f_blocks(self):
48+
return self._stats.f_blocks if self._stats is not NULL else None
49+
50+
@property
51+
def f_bfree(self):
52+
return self._stats.f_bfree if self._stats is not NULL else None
53+
54+
@property
55+
def f_bavail(self):
56+
return self._stats.f_bavail if self._stats is not NULL else None
57+
58+
@property
59+
def f_files(self):
60+
return self._stats.f_files if self._stats is not NULL else None
61+
62+
@property
63+
def f_ffree(self):
64+
return self._stats.f_ffree if self._stats is not NULL else None
65+
66+
@property
67+
def f_favail(self):
68+
return self._stats.f_favail if self._stats is not NULL else None
69+
70+
@property
71+
def f_fsid(self):
72+
return self._stats.f_fsid if self._stats is not NULL else None
73+
74+
@property
75+
def f_flag(self):
76+
return self._stats.f_flag if self._stats is not NULL else None
77+
78+
@property
79+
def f_namemax(self):
80+
return self._stats.f_namemax if self._stats is not NULL else None

ssh/utils.pxd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
# License along with this library; if not, write to the Free Software
1515
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1616

17-
from c_ssh cimport ssh_session
17+
from c_ssh cimport ssh_session, ssh_string
1818

1919
cdef bytes to_bytes(_str)
2020
cdef object to_str(char *c_str)
2121
cdef object to_str_len(char *c_str, int length)
2222
cdef int handle_ok_error_codes(int errcode) except -1
2323
cdef int handle_ssh_error_codes(int errcode, ssh_session) except -1
2424
cdef int handle_auth_error_codes(int errcode, ssh_session) except -1
25+
cdef bytes ssh_string_to_bytes(ssh_string _str)

ssh/utils.pyx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from cpython.version cimport PY_MAJOR_VERSION
1818

1919
from c_ssh cimport ssh_error_types_e, ssh_get_error, ssh_auth_e, \
20-
SSH_OK, SSH_ERROR, SSH_AGAIN, SSH_EOF, ssh_session
20+
SSH_OK, SSH_ERROR, SSH_AGAIN, SSH_EOF, ssh_session, ssh_string, \
21+
ssh_string_get_char, ssh_string_free, ssh_string_len
2122

2223
from exceptions import RequestDenied, FatalError, OtherError, \
2324
AuthenticationPartial, AuthenticationDenied, AuthenticationError, \
@@ -48,6 +49,24 @@ cdef object to_str_len(char *c_str, int length):
4849
return c_str[:length].decode(ENCODING)
4950

5051

52+
cdef bytes ssh_string_to_bytes(ssh_string _str):
53+
if _str is NULL:
54+
return
55+
cdef const char *c_str
56+
cdef size_t str_len
57+
cdef bytes b_str = None
58+
with nogil:
59+
str_len = ssh_string_len(_str)
60+
c_str = ssh_string_get_char(_str)
61+
if c_str is NULL:
62+
raise MemoryError
63+
try:
64+
b_str = c_str[:str_len]
65+
return b_str
66+
finally:
67+
ssh_string_free(_str)
68+
69+
5170
cdef int handle_ok_error_codes(int errcode) except -1:
5271
if errcode == SSH_OK:
5372
return SSH_OK

0 commit comments

Comments
 (0)