The branch, master has been updated
       via  425386ff6141bba2e7b1d8f3c27e96aaf1c5cb95 (commit)
       via  3ed33813bb6aa1ca932372c2a2ce36152b6af50b (commit)
      from  74218726e89c297eb957b9df989dd42fd1601742 (commit)

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 425386ff6141bba2e7b1d8f3c27e96aaf1c5cb95
Author: Andrew Bartlett <[email protected]>
Date:   Wed Aug 26 15:59:00 2009 +1000

    s4:ldb Add ldb_ldif_write_string() and python wrappers
    
    This allows us to turn a python LdbMessage back into a string.
    
    Andrew Bartlett

commit 3ed33813bb6aa1ca932372c2a2ce36152b6af50b
Author: Andrew Bartlett <[email protected]>
Date:   Wed Aug 26 15:01:12 2009 +1000

    s4:ldb Add hooks to get/set the flags on a ldb_message_element
    
    Also add tests to prove that we got this correct, and correct the
    existing tests which used the wrong constants.
    
    Andrew Bartlett

-----------------------------------------------------------------------

Summary of changes:
 source4/lib/ldb/common/ldb_ldif.c   |   40 ++++++++++++++++++++++
 source4/lib/ldb/include/ldb.h       |   14 ++++++++
 source4/lib/ldb/pyldb.c             |   64 +++++++++++++++++++++++++++++++++++
 source4/lib/ldb/tests/python/api.py |   48 +++++++++++++++++++++++---
 4 files changed, 160 insertions(+), 6 deletions(-)


Changeset truncated at 500 lines:

diff --git a/source4/lib/ldb/common/ldb_ldif.c 
b/source4/lib/ldb/common/ldb_ldif.c
index d890ff8..30370e6 100644
--- a/source4/lib/ldb/common/ldb_ldif.c
+++ b/source4/lib/ldb/common/ldb_ldif.c
@@ -759,3 +759,43 @@ int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, 
const struct ldb_ldif
        state.f = f;
        return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
 }
+
+/*
+  wrapper around ldif_write() for a string
+*/
+struct ldif_write_string_state {
+       char *string;
+};
+
+static int ldif_printf_string(void *private_data, const char *fmt, ...) 
PRINTF_ATTRIBUTE(2, 3);
+
+static int ldif_printf_string(void *private_data, const char *fmt, ...)
+{
+       struct ldif_write_string_state *state =
+               (struct ldif_write_string_state *)private_data;
+       va_list ap;
+       size_t oldlen = strlen(state->string);
+       va_start(ap, fmt);
+       
+       state->string = talloc_vasprintf_append(state->string, fmt, ap);
+       va_end(ap);
+       if (!state->string) {
+               return -1;
+       }
+               
+       return strlen(state->string) - oldlen;
+}
+
+char *ldb_ldif_write_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
+                           const struct ldb_ldif *ldif)
+{
+       struct ldif_write_string_state state;
+       state.string = talloc_strdup(mem_ctx, "");
+       if (!state.string) {
+               return NULL;
+       }
+       if (ldb_ldif_write(ldb, ldif_printf_string, &state, ldif) == -1) {
+               return NULL;
+       }
+       return state.string;
+}
diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h
index 20f0f9c..8972fc8 100644
--- a/source4/lib/ldb/include/ldb.h
+++ b/source4/lib/ldb/include/ldb.h
@@ -1450,6 +1450,20 @@ struct ldb_ldif *ldb_ldif_read_string(struct ldb_context 
*ldb, const char **s);
 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct 
ldb_ldif *msg);
 
 /**
+   Write an LDIF message to a string
+
+   \param ldb the ldb context (from ldb_init())
+   \param mem_ctx the talloc context on which to attach the string)
+   \param msg the message to write out
+
+   \return the string containing the LDIF, or NULL on error
+
+   \sa ldb_ldif_read_string for the reader equivalent to this function.
+*/
+char * ldb_ldif_write_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
+                         const struct ldb_ldif *msg);
+
+/**
    Base64 encode a buffer
 
    \param mem_ctx the memory context that the result is allocated
diff --git a/source4/lib/ldb/pyldb.c b/source4/lib/ldb/pyldb.c
index 67e1d5c..3f7fa2f 100644
--- a/source4/lib/ldb/pyldb.c
+++ b/source4/lib/ldb/pyldb.c
@@ -813,6 +813,41 @@ static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif 
*ldif)
 }
 
 
+static PyObject *py_ldb_write_ldif(PyLdbMessageObject *self, PyObject *args)
+{
+       int changetype;
+       PyObject *py_msg;
+       struct ldb_ldif ldif;
+       PyObject *ret;
+       char *string;
+       TALLOC_CTX *mem_ctx;
+
+       if (!PyArg_ParseTuple(args, "Oi", &py_msg, &changetype))
+               return NULL;
+
+       if (!PyLdbMessage_Check(py_msg)) {
+               PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for 
msg");
+               return NULL;
+       }
+
+       ldif.msg = PyLdbMessage_AsMessage(py_msg);
+       ldif.changetype = changetype;
+
+       mem_ctx = talloc_new(NULL);
+
+       string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, 
&ldif);
+       if (!string) {
+               PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF");
+               return NULL;
+       }
+
+       ret = PyString_FromString(string);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
+
 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
 {
        PyObject *list;
@@ -1116,6 +1151,9 @@ static PyMethodDef py_ldb_methods[] = {
        { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
                "S.parse_ldif(ldif) -> iter(messages)\n"
                "Parse a string formatted using LDIF." },
+       { "write_ldif", (PyCFunction)py_ldb_write_ldif, METH_VARARGS,
+               "S.write_ldif(message, changetype) -> ldif\n"
+               "Print the message as a string formatted using LDIF." },
        { "msg_diff", (PyCFunction)py_ldb_msg_diff, METH_VARARGS,
                "S.msg_diff(Message) -> Message\n"
                "Return an LDB Message of the difference between two Message 
objects." },
@@ -1493,8 +1531,30 @@ static PyObject 
*py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObjec
                                                                 
&(PyLdbMessageElement_AsMessageElement(self)->values[i]));
 }
 
+static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, 
PyObject *args)
+{
+       struct ldb_message_element *el;
+
+       el = PyLdbMessageElement_AsMessageElement(self);
+       return PyInt_FromLong(el->flags);
+}
+
+static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, 
PyObject *args)
+{
+       int flags;
+       struct ldb_message_element *el;
+       if (!PyArg_ParseTuple(args, "i", &flags))
+               return NULL;
+
+       el = PyLdbMessageElement_AsMessageElement(self);
+       el->flags = flags;
+       Py_RETURN_NONE;
+}
+
 static PyMethodDef py_ldb_msg_element_methods[] = {
        { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
+       { "set_flags", (PyCFunction)py_ldb_msg_element_set_flags, METH_VARARGS, 
NULL },
+       { "flags", (PyCFunction)py_ldb_msg_element_flags, METH_NOARGS, NULL },
        { NULL },
 };
 
@@ -2326,6 +2386,10 @@ void initldb(void)
        PyModule_AddObject(m, "CHANGETYPE_DELETE", 
PyInt_FromLong(LDB_CHANGETYPE_DELETE));
        PyModule_AddObject(m, "CHANGETYPE_MODIFY", 
PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
 
+       PyModule_AddObject(m, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD));
+       PyModule_AddObject(m, "FLAG_MOD_REPLACE", 
PyInt_FromLong(LDB_FLAG_MOD_REPLACE));
+       PyModule_AddObject(m, "FLAG_MOD_DELETE", 
PyInt_FromLong(LDB_FLAG_MOD_DELETE));
+
        PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
        PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", 
PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
        PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", 
PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
diff --git a/source4/lib/ldb/tests/python/api.py 
b/source4/lib/ldb/tests/python/api.py
index d946bf0..88983ac 100755
--- a/source4/lib/ldb/tests/python/api.py
+++ b/source4/lib/ldb/tests/python/api.py
@@ -195,10 +195,13 @@ class SimpleLdb(unittest.TestCase):
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=modifydelete")
-            m["bla"] = ldb.MessageElement([], ldb.CHANGETYPE_DELETE, "bla")
+            m["bla"] = ldb.MessageElement([], ldb.FLAG_MOD_DELETE, "bla")
+            self.assertEquals(ldb.FLAG_MOD_DELETE, m["bla"].flags())
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(1, len(rm))
+            rm = l.search(m.dn, attrs=["bla"])[0]
+            self.assertEquals(0, len(rm))
         finally:
             l.delete(ldb.Dn(l, "dc=modifydelete"))
 
@@ -211,7 +214,8 @@ class SimpleLdb(unittest.TestCase):
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=add")
-            m["bla"] = ldb.MessageElement(["456"], ldb.CHANGETYPE_ADD, "bla")
+            m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
+            self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(2, len(rm))
@@ -219,7 +223,7 @@ class SimpleLdb(unittest.TestCase):
         finally:
             l.delete(ldb.Dn(l, "dc=add"))
 
-    def test_modify_modify(self):
+    def test_modify_replace(self):
         l = ldb.Ldb(filename())
         m = ldb.Message()
         m.dn = ldb.Dn(l, "dc=modify2")
@@ -228,14 +232,44 @@ class SimpleLdb(unittest.TestCase):
         try:
             m = ldb.Message()
             m.dn = ldb.Dn(l, "dc=modify2")
-            m["bla"] = ldb.MessageElement(["456"], ldb.CHANGETYPE_MODIFY, 
"bla")
+            m["bla"] = ldb.MessageElement(["789"], ldb.FLAG_MOD_REPLACE, "bla")
+            self.assertEquals(ldb.FLAG_MOD_REPLACE, m["bla"].flags())
             l.modify(m)
             rm = l.search(m.dn)[0]
             self.assertEquals(2, len(rm))
-            self.assertEquals(["1234"], list(rm["bla"]))
+            self.assertEquals(["789"], list(rm["bla"]))
+            rm = l.search(m.dn, attrs=["bla"])[0]
+            self.assertEquals(1, len(rm))
         finally:
             l.delete(ldb.Dn(l, "dc=modify2"))
 
+    def test_modify_flags_change(self):
+        l = ldb.Ldb(filename())
+        m = ldb.Message()
+        m.dn = ldb.Dn(l, "dc=add")
+        m["bla"] = ["1234"]
+        l.add(m)
+        try:
+            m = ldb.Message()
+            m.dn = ldb.Dn(l, "dc=add")
+            m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
+            self.assertEquals(ldb.FLAG_MOD_ADD, m["bla"].flags())
+            l.modify(m)
+            rm = l.search(m.dn)[0]
+            self.assertEquals(2, len(rm))
+            self.assertEquals(["1234", "456"], list(rm["bla"]))
+            
+            #Now create another modify, but switch the flags before we do it
+            m["bla"] = ldb.MessageElement(["456"], ldb.FLAG_MOD_ADD, "bla")
+            m["bla"].set_flags(ldb.FLAG_MOD_DELETE)
+            l.modify(m)
+            rm = l.search(m.dn, attrs=["bla"])[0]
+            self.assertEquals(1, len(rm))
+            self.assertEquals(["1234"], list(rm["bla"]))
+            
+        finally:
+            l.delete(ldb.Dn(l, "dc=add"))
+
     def test_transaction_commit(self):
         l = ldb.Ldb(filename())
         l.transaction_start()
@@ -368,7 +402,9 @@ class DnTests(unittest.TestCase):
         msg = msgs.next()
         self.assertEquals("foo=bar", str(msg[1].dn))
         self.assertTrue(isinstance(msg[1], ldb.Message))
-
+        ldif = self.ldb.write_ldif(msg[1], ldb.CHANGETYPE_NONE)
+        self.assertEquals("dn: foo=bar\n\n", ldif)
+        
     def test_parse_ldif_more(self):
         msgs = self.ldb.parse_ldif("dn: foo=bar\n\n\ndn: bar=bar")
         msg = msgs.next()


-- 
Samba Shared Repository

Reply via email to