mirror of
https://github.com/ceph/ceph
synced 2024-12-18 17:37:38 +00:00
pycephfs: Add directory operations for pybind
Signed-off-by: Haomai Wang <haomaiwang@gmail.com>
This commit is contained in:
parent
94a7d11e4a
commit
08a60bbf0c
@ -2,7 +2,8 @@
|
||||
This module is a thin wrapper around libcephfs.
|
||||
"""
|
||||
from ctypes import CDLL, c_char_p, c_size_t, c_void_p, c_int, c_long, c_uint, c_ulong, \
|
||||
create_string_buffer, byref, Structure, pointer, c_char
|
||||
c_ushort, create_string_buffer, byref, Structure, pointer, c_char, POINTER, \
|
||||
c_uint8
|
||||
from ctypes.util import find_library
|
||||
import errno
|
||||
|
||||
@ -82,6 +83,12 @@ class cephfs_statvfs(Structure):
|
||||
("f_flag", c_uint),
|
||||
("f_namemax", c_uint)]
|
||||
|
||||
class cephfs_dirent(Structure):
|
||||
_fields_ = [("d_ino", c_long),
|
||||
("d_off", c_ulong),
|
||||
("d_reclen", c_ushort),
|
||||
("d_type", c_uint8),
|
||||
("d_name", c_char*256)]
|
||||
|
||||
# struct timespec {
|
||||
# long int tv_sec;
|
||||
@ -299,6 +306,41 @@ class LibCephFS(object):
|
||||
if ret < 0:
|
||||
raise make_ex(ret, "chdir failed")
|
||||
|
||||
def isdir(self, dirent):
|
||||
return dirent['d_type'] == 0x4
|
||||
|
||||
def opendir(self, path):
|
||||
self.require_state("mounted")
|
||||
if not isinstance(path, str):
|
||||
raise TypeError('path must be a string')
|
||||
dir_handler = c_void_p()
|
||||
ret = self.libcephfs.ceph_opendir(self.cluster, c_char_p(path),
|
||||
pointer(dir_handler));
|
||||
if ret < 0:
|
||||
raise make_ex(ret, "opendir failed")
|
||||
return dir_handler
|
||||
|
||||
def readdir(self, dir_handler):
|
||||
self.require_state("mounted")
|
||||
self.libcephfs.ceph_readdir.restype = POINTER(cephfs_dirent)
|
||||
while True:
|
||||
dirent = self.libcephfs.ceph_readdir(self.cluster, dir_handler)
|
||||
if not dirent:
|
||||
return None
|
||||
|
||||
if dirent.contents.d_name != '.' and dirent.contents.d_name != '..':
|
||||
return {'d_ino': dirent.contents.d_ino,
|
||||
'd_off': dirent.contents.d_off,
|
||||
'd_reclen': dirent.contents.d_reclen,
|
||||
'd_type': dirent.contents.d_type,
|
||||
'd_name': dirent.contents.d_name}
|
||||
|
||||
def closedir(self, dir_handler):
|
||||
self.require_state("mounted")
|
||||
ret = self.libcephfs.ceph_closedir(self.cluster, dir_handler)
|
||||
if ret < 0:
|
||||
raise make_ex(ret, "closedir failed")
|
||||
|
||||
def mkdir(self, path, mode):
|
||||
self.require_state("mounted")
|
||||
if not isinstance(path, str):
|
||||
|
Loading…
Reference in New Issue
Block a user