s4:dsdb Move dsdb_save_partition_usn() to be a module helper function
authorAndrew Bartlett <abartlet@samba.org>
Mon, 22 Mar 2010 05:00:39 +0000 (16:00 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 22 Mar 2010 09:24:41 +0000 (20:24 +1100)
This function should not traverse the module stack again, but instead
run from this point.  Also add a matching
dsdb_module_load_partition_usn() and change repl_meta_data to match.

Andrew Bartlett

source4/dsdb/common/util.c
source4/dsdb/samdb/ldb_modules/repl_meta_data.c
source4/dsdb/samdb/ldb_modules/util.c

index 9c2950994cbed42c3933a7ac116ea4b9d0252157..b469b062326e02b076d3b57aba169fe8dc2fbd31 100644 (file)
@@ -2585,94 +2585,6 @@ int dsdb_load_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
        return LDB_SUCCESS;     
 }
 
-/*
-  save uSNHighest and uSNUrgent attributes in the @REPLCHANGED object for a
-  partition
- */
-int dsdb_save_partition_usn(struct ldb_context *ldb, struct ldb_dn *dn,
-                               uint64_t uSN, uint64_t urgent_uSN)
-{
-       struct ldb_request *req;
-       struct ldb_message *msg;
-       struct dsdb_control_current_partition *p_ctrl;
-       int ret;
-
-       msg = ldb_msg_new(ldb);
-       if (msg == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       msg->dn = ldb_dn_new(msg, ldb, "@REPLCHANGED");
-       if (msg->dn == NULL) {
-               talloc_free(msg);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       
-       ret = ldb_msg_add_fmt(msg, "uSNHighest", "%llu", (unsigned long long)uSN);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(msg);
-               return ret;
-       }
-       msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
-       
-       /* urgent_uSN is optional so may not be stored */
-       if (urgent_uSN) {
-               ret = ldb_msg_add_fmt(msg, "uSNUrgent", "%llu", (unsigned long long)urgent_uSN);
-               if (ret != LDB_SUCCESS) {
-                       talloc_free(msg);
-                       return ret;
-               }
-               msg->elements[1].flags = LDB_FLAG_MOD_REPLACE;
-       }
-
-
-       p_ctrl = talloc(msg, struct dsdb_control_current_partition);
-       if (p_ctrl == NULL) {
-               talloc_free(msg);
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-       p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
-       p_ctrl->dn = dn;
-
-       ret = ldb_build_mod_req(&req, ldb, msg,
-                               msg,
-                               NULL,
-                               NULL, ldb_op_default_callback,
-                               NULL);
-again:
-       if (ret != LDB_SUCCESS) {
-               talloc_free(msg);
-               return ret;
-       }
-       
-       ret = ldb_request_add_control(req,
-                                     DSDB_CONTROL_CURRENT_PARTITION_OID,
-                                     false, p_ctrl);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(msg);
-               return ret;
-       }
-       
-       /* Run the new request */
-       ret = ldb_request(ldb, req);
-       
-       if (ret == LDB_SUCCESS) {
-               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
-       }
-       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-               ret = ldb_build_add_req(&req, ldb, msg,
-                                       msg,
-                                       NULL,
-                                       NULL, ldb_op_default_callback,
-                                       NULL);
-               goto again;
-       }
-       
-       talloc_free(msg);
-       
-       return ret;
-}
-
 int drsuapi_DsReplicaCursor2_compare(const struct drsuapi_DsReplicaCursor2 *c1,
                                                   const struct drsuapi_DsReplicaCursor2 *c2)
 {
index 91cd825a8467896e3a77be2c3dee354dcf5b046b..8b4e012ba9af8cd5be02d6a56fae62102bc3ec6e 100644 (file)
@@ -454,15 +454,14 @@ static int replmd_notify_store(struct ldb_module *module)
 {
        struct replmd_private *replmd_private =
                talloc_get_type(ldb_module_get_private(module), struct replmd_private);
-       struct ldb_context *ldb = ldb_module_get_ctx(module);
 
        while (replmd_private->ncs) {
                int ret;
                struct nc_entry *modified_partition = replmd_private->ncs;
 
-               ret = dsdb_save_partition_usn(ldb, modified_partition->dn,
-                                               modified_partition->mod_usn,
-                                               modified_partition->mod_usn_urgent);
+               ret = dsdb_module_save_partition_usn(module, modified_partition->dn,
+                                                    modified_partition->mod_usn,
+                                                    modified_partition->mod_usn_urgent);
                if (ret != LDB_SUCCESS) {
                        DEBUG(0,(__location__ ": Failed to save partition uSN for %s\n",
                                 ldb_dn_get_linearized(modified_partition->dn)));
index d9c05a6c3049ccd17cfccca9825de281e0d0af8c..d04a5f7b47cac6fabd9c33280df03e122efb253a 100644 (file)
@@ -620,6 +620,181 @@ int dsdb_module_set_integer(struct ldb_module *module, struct ldb_dn *dn,
        return ret;
 }
 
+/*
+  load the uSNHighest and the uSNUrgent attributes from the @REPLCHANGED
+  object for a partition
+ */
+int dsdb_module_load_partition_usn(struct ldb_module *module, struct ldb_dn *dn,
+                                 uint64_t *uSN, uint64_t *urgent_uSN)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct ldb_request *req;
+       int ret;
+       TALLOC_CTX *tmp_ctx = talloc_new(module);
+       struct dsdb_control_current_partition *p_ctrl;
+       struct ldb_result *res;
+
+       res = talloc_zero(tmp_ctx, struct ldb_result);
+       if (!res) {
+               talloc_free(tmp_ctx);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       ret = ldb_build_search_req(&req, ldb, tmp_ctx,
+                                  ldb_dn_new(tmp_ctx, ldb, "@REPLCHANGED"),
+                                  LDB_SCOPE_BASE,
+                                  NULL, NULL,
+                                  NULL,
+                                  res, ldb_search_default_callback,
+                                  NULL);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
+       p_ctrl = talloc(req, struct dsdb_control_current_partition);
+       if (p_ctrl == NULL) {
+               talloc_free(res);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
+       p_ctrl->dn = dn;
+
+
+       ret = ldb_request_add_control(req,
+                                     DSDB_CONTROL_CURRENT_PARTITION_OID,
+                                     false, p_ctrl);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
+       /* Run the new request */
+       ret = ldb_next_request(module, req);
+
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+       }
+
+       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+               /* it hasn't been created yet, which means
+                  an implicit value of zero */
+               *uSN = 0;
+               talloc_free(tmp_ctx);
+               return LDB_SUCCESS;
+       }
+
+       if (ret != LDB_SUCCESS) {
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+
+       if (res->count < 1) {
+               *uSN = 0;
+               if (urgent_uSN) {
+                       *urgent_uSN = 0;
+               }
+       } else {
+               *uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNHighest", 0);
+               if (urgent_uSN) {
+                       *urgent_uSN = ldb_msg_find_attr_as_uint64(res->msgs[0], "uSNUrgent", 0);
+               }
+       }
+
+       talloc_free(tmp_ctx);
+
+       return LDB_SUCCESS;
+}
+
+/*
+  save uSNHighest and uSNUrgent attributes in the @REPLCHANGED object for a
+  partition
+ */
+int dsdb_module_save_partition_usn(struct ldb_module *module, struct ldb_dn *dn,
+                                  uint64_t uSN, uint64_t urgent_uSN)
+{
+       struct ldb_context *ldb = ldb_module_get_ctx(module);
+       struct ldb_request *req;
+       struct ldb_message *msg;
+       struct dsdb_control_current_partition *p_ctrl;
+       int ret;
+
+       msg = ldb_msg_new(module);
+       if (msg == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       msg->dn = ldb_dn_new(msg, ldb, "@REPLCHANGED");
+       if (msg->dn == NULL) {
+               talloc_free(msg);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       ret = ldb_msg_add_fmt(msg, "uSNHighest", "%llu", (unsigned long long)uSN);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(msg);
+               return ret;
+       }
+       msg->elements[0].flags = LDB_FLAG_MOD_REPLACE;
+
+       /* urgent_uSN is optional so may not be stored */
+       if (urgent_uSN) {
+               ret = ldb_msg_add_fmt(msg, "uSNUrgent", "%llu", (unsigned long long)urgent_uSN);
+               if (ret != LDB_SUCCESS) {
+                       talloc_free(msg);
+                       return ret;
+               }
+               msg->elements[1].flags = LDB_FLAG_MOD_REPLACE;
+       }
+
+
+       p_ctrl = talloc(msg, struct dsdb_control_current_partition);
+       if (p_ctrl == NULL) {
+               talloc_free(msg);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       p_ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
+       p_ctrl->dn = dn;
+
+       ret = ldb_build_mod_req(&req, ldb, msg,
+                               msg,
+                               NULL,
+                               NULL, ldb_op_default_callback,
+                               NULL);
+again:
+       if (ret != LDB_SUCCESS) {
+               talloc_free(msg);
+               return ret;
+       }
+
+       ret = ldb_request_add_control(req,
+                                     DSDB_CONTROL_CURRENT_PARTITION_OID,
+                                     false, p_ctrl);
+       if (ret != LDB_SUCCESS) {
+               talloc_free(msg);
+               return ret;
+       }
+
+       /* Run the new request */
+       ret = ldb_next_request(module, req);
+
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+       }
+       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+               ret = ldb_build_add_req(&req, ldb, msg,
+                                       msg,
+                                       NULL,
+                                       NULL, ldb_op_default_callback,
+                                       NULL);
+               goto again;
+       }
+
+       talloc_free(msg);
+
+       return ret;
+}
+
 bool dsdb_module_am_system(struct ldb_module *module)
 {
        struct ldb_context *ldb = ldb_module_get_ctx(module);