]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pycephfs: add read/write support
authorHaomai Wang <haomaiwang@gmail.com>
Thu, 30 Apr 2015 16:16:30 +0000 (00:16 +0800)
committerHaomai Wang <haomaiwang@gmail.com>
Mon, 4 May 2015 16:05:51 +0000 (00:05 +0800)
Signed-off-by: Haomai Wang <haomaiwang@gmail.com>
src/pybind/cephfs.py
src/test/pybind/test_cephfs.py

index 52a94acd80200d5c20f38d9bd18c12df1e154ba6..ed2cbd78445a7b5f663370a14a72ed3310c727b9 100644 (file)
@@ -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))
index 8c98bf737e50029c0e982faa5006eb989b7c6b14..3626cf54b392fe5383def97a56ec1921106b8875 100644 (file)
@@ -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')