From: Haomai Wang Date: Thu, 30 Apr 2015 16:16:30 +0000 (+0800) Subject: pycephfs: add read/write support X-Git-Tag: v9.0.2~227^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=56b82225466eef628faa6562aa91634535d133db;p=ceph.git pycephfs: add read/write support Signed-off-by: Haomai Wang --- diff --git a/src/pybind/cephfs.py b/src/pybind/cephfs.py index 52a94acd8020..ed2cbd78445a 100644 --- a/src/pybind/cephfs.py +++ b/src/pybind/cephfs.py @@ -3,7 +3,7 @@ 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, \ c_ushort, create_string_buffer, byref, Structure, pointer, c_char, POINTER, \ - c_uint8 + c_uint8, c_int64 from ctypes.util import find_library from collections import namedtuple import errno @@ -299,6 +299,8 @@ class LibCephFS(object): self.state = "mounted" def statfs(self, path): + if not isinstance(path, basestring): + raise TypeError('path must be a string') self.require_state("mounted") statbuf = cephfs_statvfs() ret = self.libcephfs.ceph_statfs(self.cluster, c_char_p(path), byref(statbuf)) @@ -427,12 +429,41 @@ class LibCephFS(object): if ret < 0: raise make_ex(ret, "error in close") + def read(self, fd, offset, l): + self.require_state("mounted") + if not isinstance(offset, int): + raise TypeError('path must be an int') + if not isinstance(l, int): + raise TypeError('path must be an int') + + buf = create_string_buffer(l) + ret = self.libcephfs.ceph_read(self.cluster, c_int(fd), + buf, c_int64(l), c_int64(offset)) + if ret < 0: + raise make_ex(ret, "error in close") + return buf.value + + def write(self, fd, buf, offset): + self.require_state("mounted") + if not isinstance(buf, basestring): + raise TypeError('buf must be a string') + if not isinstance(offset, int): + raise TypeError('offset must be an int') + + ret = self.libcephfs.ceph_write(self.cluster, c_int(fd), + c_char_p(buf), c_int64(len(buf)), + c_int64(offset)) + if ret < 0: + raise make_ex(ret, "error in close") + return ret + def getxattr(self, path, name): if not isinstance(path, basestring): raise TypeError('path must be a string') if not isinstance(name, basestring): raise TypeError('name must be a string') + self.require_state("mounted") l = 255 buf = create_string_buffer(l) actual_l = self.libcephfs.ceph_getxattr(self.cluster, path, name, buf, c_int(l)) diff --git a/src/test/pybind/test_cephfs.py b/src/test/pybind/test_cephfs.py index 8c98bf737e50..3626cf54b392 100644 --- a/src/test/pybind/test_cephfs.py +++ b/src/test/pybind/test_cephfs.py @@ -6,7 +6,7 @@ cephfs = None def setup_module(): global cephfs - cephfs = cephfs.LibCephfs(conffile='') + cephfs = cephfs.LibCephFS(conffile='') cephfs.mount() def teardown_module(): @@ -14,9 +14,9 @@ def teardown_module(): cephfs.shutdown() def test_mount(): - cephfs.mount() cephfs.shutdown() assert_raise(cephfs.LibCephFSStateError, cephfs.statfs) + cephfs.mount() def test_version(): cephfs.version() @@ -50,7 +50,7 @@ def test_walk_dir(): cephfs.closedir(handler) def test_xattr(): - assert_raise(InvalidValue, cephfs.setxattr, "/", "key", "value", 0) + assert_raise(cephfs.InvalidValue, cephfs.setxattr, "/", "key", "value", 0) cephfs.setxattr("/", "user.key", "value", 0) assert_equal("value", cephfs.getxattr("/", "user.key")) @@ -61,7 +61,8 @@ def test_rename(): cephfs.stat("/a/b") def test_open(): - assert_raise(ObjectExists, cephfs.open, 'file-1', 'r') + assert_raise(cephfs.ObjectNotFound, cephfs.open, 'file-1', 'r') + assert_raise(cephfs.ObjectNotFound, cephfs.open, 'file-1', 'r+') fd = cephfs.open('file-1', 'w') cephfs.close(fd) fd = cephfs.open('file-1', 'r')