def listdir(self, path): """ return directory contents, excluding . and .. """ entries = [] dirp = ctypes.c_void_p() ret = self.conn.libcephfs.ceph_opendir(self.conn.cluster, str(path), ctypes.byref(dirp)) if ret < 0: if ret == -errno.ENOENT: raise NotFound raise cephfs.make_ex(ret, "error in opendir: %s" % path) buf = ctypes.create_string_buffer(1024) ret = self.conn.libcephfs.ceph_getdnames(self.conn.cluster, dirp, buf, ctypes.c_int(1024)) if ret < 0: raise cephfs.make_ex(ret, "error in getdnames: %s" % path) start = 0 offset = 0 while offset < ret: if buf[offset] == '\0' or offset == ret: fname = buf[start:offset] if fname != '.' and fname != '..': entries.append(fname) start = offset + 1 offset += 1 ret = self.conn.libcephfs.ceph_closedir(self.conn.cluster, dirp) if ret < 0: raise cephfs.make_ex(ret, "error in closedir: %s" % path) return entries
def read(self, limit=-1): """ Read at most 'limit' bytes. If zero or negative, read until EOF. """ if self.fdesc is None: return '' retbuf = '' if limit and limit > 0: readbuf = ctypes.create_string_buffer(limit) else: readbuf = ctypes.create_string_buffer(128*1024) while True: readlen = 128*1024 if limit and limit > 0: if len(retbuf) >= limit: return retbuf else: if limit - len(readbuf) < readlen: readlen = limit - len(retbuf) ret = self.conn.libcephfs.ceph_read(self.conn.cluster, self.fdesc, readbuf, ctypes.c_longlong(readlen), ctypes.c_longlong(-1)) if ret == 0: return retbuf elif ret > 0: retbuf += readbuf[0:ret] else: raise cephfs.make_ex(ret, "error in read") return retbuf
def rmdir(self, path): """ delete directory """ ret = self.conn.libcephfs.ceph_rmdir(self.conn.cluster, str(path)) if ret < 0: if ret == -errno.ENOENT: raise NotFound raise cephfs.make_ex(ret, "error in rmdir: %s" % path)
def seek(self, offset, whence=0): """ seek """ if self.fdesc is None: return ret = self.conn.libcephfs.ceph_lseek(self.conn.cluster, self.fdesc, ctypes.c_longlong(offset), ctypes.c_int(whence)) if ret < 0: raise cephfs.make_ex(ret, "error in seek")
def write(self, data): """ write data """ if self.fdesc is None: return offset = 0 total = len(data) while offset < total: datalen = len(data) - offset if datalen > 128*1024: datalen = 128*1024 ret = self.conn.libcephfs.ceph_write(self.conn.cluster, self.fdesc, ctypes.c_char_p(data[offset:offset+datalen]), ctypes.c_longlong(datalen), ctypes.c_longlong(-1)) if ret >= 0: offset += ret else: raise cephfs.make_ex(ret, "error in write")