]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librados: add python bindings for getxattrs
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 19 May 2011 21:27:19 +0000 (14:27 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 19 May 2011 21:49:50 +0000 (14:49 -0700)
Add python bindings for getxattrs. Test getxattr, getxattrs, and
setxattr.

Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/pybind/rados.py
src/test/ceph-pybind-test.py

index f28d39517bc33005f3ef724ff2d3dda203d7f580..29bfd8e891307dfb4775596f85cc0c68c9edbae4 100755 (executable)
@@ -1,7 +1,7 @@
 """librados Python ctypes wrapper
 Copyright 2011, Hannu Valtonen <hannu.valtonen@ormod.com>
 """
-from ctypes import CDLL, c_char_p, c_size_t, c_void_p,\
+from ctypes import CDLL, c_char_p, c_size_t, c_void_p, c_int, \
     create_string_buffer, byref, Structure, c_uint64, c_ubyte, c_byte, pointer
 import ctypes
 import datetime
@@ -108,9 +108,9 @@ Rados object in state %s." % (self.state))
         self.shutdown()
 
     def version(self):
-        major = ctypes.c_int()
-        minor = ctypes.c_int()
-        extra = ctypes.c_int()
+        major = c_int(0)
+        minor = c_int(0)
+        extra = c_int(0)
         self.librados.rados_version(byref(major), byref(minor), byref(extra))
         return Version(major.value, minor.value, extra.value)
 
@@ -219,6 +219,31 @@ class ObjectIterator(object):
     def __del__(self):
         self.ioctx.librados.rados_objects_list_close(self.ctx)
 
+class XattrIterator(object):
+    """Extended attribute iterator"""
+    def __init__(self, ioctx, it, oid):
+        self.ioctx = ioctx
+        self.it = it
+        self.oid = oid
+    def __iter__(self):
+        return self
+    def next(self):
+        name_ = c_char_p(0)
+        val_ = c_char_p(0)
+        len_ = c_int(0)
+        ret = self.ioctx.librados.\
+            rados_getxattrs_next(self.it, byref(name_), byref(val_), byref(len_))
+        if (ret != 0):
+          raise make_ex(ret, "error iterating over the extended attributes \
+in '%s'" % self.oid)
+        if name_.value == None:
+            raise StopIteration()
+        name = ctypes.string_at(name_)
+        val = ctypes.string_at(val_, len_)
+        return (name, val)
+    def __del__(self):
+        self.ioctx.librados.rados_getxattrs_end(self.it)
+
 class SnapIterator(object):
     """Snapshot iterator"""
     def __init__(self, ioctx):
@@ -403,6 +428,14 @@ written." % (self.name, ret, length))
             raise make_ex(ret, "Failed to get xattr %r" % xattr_name)
         return ret_buf.value
 
+    def get_xattrs(self, oid):
+        self.require_ioctx_open()
+        it = c_void_p(0)
+        ret = self.librados.rados_getxattrs(self.io, oid, byref(it))
+        if ret != 0:
+            raise make_ex(ret, "Failed to get rados xattrs for object %r" % oids)
+        return XattrIterator(self, it, oid)
+
     def set_xattr(self, key, xattr_name, xattr_value):
         self.require_ioctx_open()
         ret = self.librados.rados_setxattr(self.io, c_char_p(key),
@@ -499,6 +532,10 @@ class Object(object):
         self.require_object_exists()
         return self.ioctx.get_xattr(self.key, xattr_name)
 
+    def get_xattrs(self, xattr_name):
+        self.require_object_exists()
+        return self.ioctx.get_xattrs(self.key, xattr_name)
+
     def set_xattr(self, xattr_name, xattr_value):
         self.require_object_exists()
         return self.ioctx.set_xattr(self.key, xattr_name, xattr_value)
index 62a8b30c0ffc44bf4ac182bb8449d5a4df3baa44..4deeca626901f361ce0df647335761f606d83653 100755 (executable)
@@ -57,10 +57,32 @@ def_str = foo3_ioctx.read("def")
 if (def_str != "d"):
     raise RuntimeError("error reading object def: expected value d, \
 got %s" % def_str)
-
 for obj in foo3_ioctx.list_objects():
     print str(obj)
 
+# do some things with extended attributes
+foo3_ioctx.set_xattr("abc", "a", "1")
+foo3_ioctx.set_xattr("def", "b", "2")
+foo3_ioctx.set_xattr("abc", "c", "3")
+ret = foo3_ioctx.get_xattr("abc", "a")
+if (ret != "1"):
+  raise RuntimeError("error: expected object abc to have a=1")
+ret = foo3_ioctx.get_xattr("def", "b")
+if (ret != "2"):
+  raise RuntimeError("error: expected object def to have b=2")
+ret = foo3_ioctx.get_xattr("abc", "c")
+if (ret != "3"):
+  raise RuntimeError("error: expected object abc to have c=3")
+found = {}
+for k,v in foo3_ioctx.get_xattrs("abc"):
+  found[k] = v
+if (len(found) != 2):
+  raise RuntimeError("error: expected two extended attributes on abc")
+if (found["a"] != "1"):
+  raise RuntimeError("error: expected object abc to have a=1")
+if (found["c"] != "3"):
+  raise RuntimeError("error: expected object abc to have c=3")
+
 # create some snapshots and do stuff with them
 print "creating snap bjork"
 foo3_ioctx.create_snap("bjork")