svn commit: samba r6833 - in branches/SAMBA_4_0/source/lib/ldb:
common include tools
tridge at samba.org
tridge at samba.org
Mon May 16 22:31:46 GMT 2005
Author: tridge
Date: 2005-05-16 22:31:45 +0000 (Mon, 16 May 2005)
New Revision: 6833
WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=6833
Log:
split out the routine that calculates the diff between two ldb messages from ldbedit,
so other progs can use it.
Modified:
branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c
branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
branches/SAMBA_4_0/source/lib/ldb/tools/ldbedit.c
Changeset:
Modified: branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c 2005-05-16 21:57:26 UTC (rev 6832)
+++ branches/SAMBA_4_0/source/lib/ldb/common/ldb_msg.c 2005-05-16 22:31:45 UTC (rev 6833)
@@ -456,3 +456,58 @@
return msg2;
}
+
+
+/*
+ return a ldb_message representing the differences between msg1 and msg2. If you
+ then use this in a ldb_modify() call it can be used to save edits to a message
+*/
+struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
+ struct ldb_message *msg1,
+ struct ldb_message *msg2)
+{
+ struct ldb_message *mod;
+ struct ldb_message_element *el;
+ unsigned int i;
+
+ mod = ldb_msg_new(ldb);
+
+ mod->dn = msg1->dn;
+ mod->num_elements = 0;
+ mod->elements = NULL;
+
+ msg2 = ldb_msg_canonicalize(ldb, msg2);
+ if (msg2 == NULL) {
+ return NULL;
+ }
+
+ /* look in msg2 to find elements that need to be added
+ or modified */
+ for (i=0;i<msg2->num_elements;i++) {
+ el = ldb_msg_find_element(msg1, msg2->elements[i].name);
+
+ if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
+ continue;
+ }
+
+ if (ldb_msg_add(ldb, mod,
+ &msg2->elements[i],
+ el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
+ return NULL;
+ }
+ }
+
+ /* look in msg1 to find elements that need to be deleted */
+ for (i=0;i<msg1->num_elements;i++) {
+ el = ldb_msg_find_element(msg2, msg1->elements[i].name);
+ if (!el) {
+ if (ldb_msg_add_empty(ldb, mod,
+ msg1->elements[i].name,
+ LDB_FLAG_MOD_DELETE) != 0) {
+ return NULL;
+ }
+ }
+ }
+
+ return mod;
+}
Modified: branches/SAMBA_4_0/source/lib/ldb/include/ldb.h
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/include/ldb.h 2005-05-16 21:57:26 UTC (rev 6832)
+++ branches/SAMBA_4_0/source/lib/ldb/include/ldb.h 2005-05-16 22:31:45 UTC (rev 6833)
@@ -295,6 +295,10 @@
const struct ldb_message *msg);
+struct ldb_message *ldb_msg_diff(struct ldb_context *ldb,
+ struct ldb_message *msg1,
+ struct ldb_message *msg2);
+
struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v);
/*
Modified: branches/SAMBA_4_0/source/lib/ldb/tools/ldbedit.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/tools/ldbedit.c 2005-05-16 21:57:26 UTC (rev 6832)
+++ branches/SAMBA_4_0/source/lib/ldb/tools/ldbedit.c 2005-05-16 22:31:45 UTC (rev 6833)
@@ -65,52 +65,13 @@
struct ldb_message *msg2)
{
struct ldb_message *mod;
- struct ldb_message_element *el;
- unsigned int i;
- int count = 0;
- mod = ldb_msg_new(ldb);
-
- mod->dn = msg1->dn;
- mod->num_elements = 0;
- mod->elements = NULL;
-
- msg2 = ldb_msg_canonicalize(ldb, msg2);
- if (msg2 == NULL) {
- fprintf(stderr, "Failed to canonicalise msg2\n");
+ mod = ldb_msg_diff(ldb, msg1, msg2);
+ if (mod == NULL) {
+ fprintf(stderr, "Failed to calculate message differences\n");
return -1;
}
-
- /* look in msg2 to find elements that need to be added
- or modified */
- for (i=0;i<msg2->num_elements;i++) {
- el = ldb_msg_find_element(msg1, msg2->elements[i].name);
- if (el && ldb_msg_element_compare(el, &msg2->elements[i]) == 0) {
- continue;
- }
-
- if (ldb_msg_add(ldb, mod,
- &msg2->elements[i],
- el?LDB_FLAG_MOD_REPLACE:LDB_FLAG_MOD_ADD) != 0) {
- return -1;
- }
- count++;
- }
-
- /* look in msg1 to find elements that need to be deleted */
- for (i=0;i<msg1->num_elements;i++) {
- el = ldb_msg_find_element(msg2, msg1->elements[i].name);
- if (!el) {
- if (ldb_msg_add_empty(ldb, mod,
- msg1->elements[i].name,
- LDB_FLAG_MOD_DELETE) != 0) {
- return -1;
- }
- count++;
- }
- }
-
if (mod->num_elements == 0) {
return 0;
}
@@ -125,7 +86,7 @@
ldif_write_msg(ldb, stdout, LDB_CHANGETYPE_MODIFY, mod);
}
- return count;
+ return mod->num_elements;
}
/*
More information about the samba-cvs
mailing list