diff options
author | Andrew Tridgell <tridge@samba.org> | 2005-10-06 06:57:09 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:39:27 -0500 |
commit | 78d0e79c9f9263e7f3798aa2e174a347ea1a3df1 (patch) | |
tree | b6407a778000a9fb9ea039d8f70a3396777874df /source4/lib/ldb/common | |
parent | 01e6c562086b42a59fc1ac6aa1a3359747b96fe6 (diff) | |
download | samba-78d0e79c9f9263e7f3798aa2e174a347ea1a3df1.tar.gz samba-78d0e79c9f9263e7f3798aa2e174a347ea1a3df1.tar.bz2 samba-78d0e79c9f9263e7f3798aa2e174a347ea1a3df1.zip |
r10759: make modules easier to write by allowing modules to only implement the
functions they care about, instead of all functions. This also makes
it more likely that future changes to ldb will not break existing
modules
(This used to be commit 45f0c967b58e7c1b2e900a4d74cfde2a2c527dfa)
Diffstat (limited to 'source4/lib/ldb/common')
-rw-r--r-- | source4/lib/ldb/common/ldb.c | 54 | ||||
-rw-r--r-- | source4/lib/ldb/common/ldb_modules.c | 66 |
2 files changed, 72 insertions, 48 deletions
diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c index 29ee323ad4..725044d3f4 100644 --- a/source4/lib/ldb/common/ldb.c +++ b/source4/lib/ldb/common/ldb.c @@ -114,16 +114,25 @@ static void ldb_reset_err_string(struct ldb_context *ldb) } } +#define FIRST_OP(ldb, op) do { \ + module = ldb->modules; \ + while (module && module->ops->op == NULL) module = module->next; \ + if (module == NULL) return -1; \ +} while (0) + /* start a transaction */ int ldb_transaction_start(struct ldb_context *ldb) { + struct ldb_module *module; + FIRST_OP(ldb, start_transaction); + ldb->transaction_active++; ldb_reset_err_string(ldb); - return ldb->modules->ops->start_transaction(ldb->modules); + return module->ops->start_transaction(module); } /* @@ -131,6 +140,9 @@ int ldb_transaction_start(struct ldb_context *ldb) */ int ldb_transaction_commit(struct ldb_context *ldb) { + struct ldb_module *module; + FIRST_OP(ldb, end_transaction); + if (ldb->transaction_active > 0) { ldb->transaction_active--; } else { @@ -139,7 +151,7 @@ int ldb_transaction_commit(struct ldb_context *ldb) ldb_reset_err_string(ldb); - return ldb->modules->ops->end_transaction(ldb->modules); + return module->ops->end_transaction(module); } /* @@ -147,6 +159,9 @@ int ldb_transaction_commit(struct ldb_context *ldb) */ int ldb_transaction_cancel(struct ldb_context *ldb) { + struct ldb_module *module; + FIRST_OP(ldb, del_transaction); + if (ldb->transaction_active > 0) { ldb->transaction_active--; } else { @@ -155,7 +170,7 @@ int ldb_transaction_cancel(struct ldb_context *ldb) ldb_reset_err_string(ldb); - return ldb->modules->ops->del_transaction(ldb->modules); + return module->ops->del_transaction(module); } /* @@ -201,9 +216,12 @@ int ldb_search_bytree(struct ldb_context *ldb, struct ldb_parse_tree *tree, const char * const *attrs, struct ldb_message ***res) { + struct ldb_module *module; + FIRST_OP(ldb, search_bytree); + ldb_reset_err_string(ldb); - return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res); + return module->ops->search_bytree(module, base, scope, tree, attrs, res); } /* @@ -213,8 +231,11 @@ int ldb_search_bytree(struct ldb_context *ldb, int ldb_add(struct ldb_context *ldb, const struct ldb_message *message) { + struct ldb_module *module; int status; + FIRST_OP(ldb, add_record); + ldb_reset_err_string(ldb); status = ldb_msg_sanity_check(message); @@ -224,12 +245,12 @@ int ldb_add(struct ldb_context *ldb, status = ldb_transaction_start(ldb); if (status != LDB_SUCCESS) return status; - status = ldb->modules->ops->add_record(ldb->modules, message); + status = module->ops->add_record(module, message); if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb); return ldb_transaction_commit(ldb); } - return ldb->modules->ops->add_record(ldb->modules, message); + return module->ops->add_record(module, message); } /* @@ -238,8 +259,11 @@ int ldb_add(struct ldb_context *ldb, int ldb_modify(struct ldb_context *ldb, const struct ldb_message *message) { + struct ldb_module *module; int status; + FIRST_OP(ldb, modify_record); + ldb_reset_err_string(ldb); status = ldb_msg_sanity_check(message); @@ -249,12 +273,12 @@ int ldb_modify(struct ldb_context *ldb, status = ldb_transaction_start(ldb); if (status != LDB_SUCCESS) return status; - status = ldb->modules->ops->modify_record(ldb->modules, message); + status = module->ops->modify_record(module, message); if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb); return ldb_transaction_commit(ldb); } - return ldb->modules->ops->modify_record(ldb->modules, message); + return module->ops->modify_record(module, message); } @@ -263,20 +287,23 @@ int ldb_modify(struct ldb_context *ldb, */ int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn) { + struct ldb_module *module; int status; + FIRST_OP(ldb, delete_record); + ldb_reset_err_string(ldb); if (! ldb->transaction_active) { status = ldb_transaction_start(ldb); if (status != LDB_SUCCESS) return status; - status = ldb->modules->ops->delete_record(ldb->modules, dn); + status = module->ops->delete_record(module, dn); if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb); return ldb_transaction_commit(ldb); } - return ldb->modules->ops->delete_record(ldb->modules, dn); + return module->ops->delete_record(module, dn); } /* @@ -284,20 +311,23 @@ int ldb_delete(struct ldb_context *ldb, const struct ldb_dn *dn) */ int ldb_rename(struct ldb_context *ldb, const struct ldb_dn *olddn, const struct ldb_dn *newdn) { + struct ldb_module *module; int status; + FIRST_OP(ldb, rename_record); + ldb_reset_err_string(ldb); if (! ldb->transaction_active) { status = ldb_transaction_start(ldb); if (status != LDB_SUCCESS) return status; - status = ldb->modules->ops->rename_record(ldb->modules, olddn, newdn); + status = module->ops->rename_record(module, olddn, newdn); if (status != LDB_SUCCESS) return ldb_transaction_cancel(ldb); return ldb_transaction_commit(ldb); } - return ldb->modules->ops->rename_record(ldb->modules, olddn, newdn); + return module->ops->rename_record(module, olddn, newdn); } diff --git a/source4/lib/ldb/common/ldb_modules.c b/source4/lib/ldb/common/ldb_modules.c index 955e46b91a..b0ede9893b 100644 --- a/source4/lib/ldb/common/ldb_modules.c +++ b/source4/lib/ldb/common/ldb_modules.c @@ -213,6 +213,18 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[]) } /* + by using this we allow ldb modules to only implement the functions they care about, + which makes writing a module simpler, and makes it more likely to keep working + when ldb is extended +*/ +#define FIND_OP(module, op) do { \ + module = module->next; \ + while (module && module->ops->op == NULL) module = module->next; \ + if (module == NULL) return -1; \ +} while (0) + + +/* helper functions to call the next module in chain */ int ldb_next_search_bytree(struct ldb_module *module, @@ -221,10 +233,8 @@ int ldb_next_search_bytree(struct ldb_module *module, struct ldb_parse_tree *tree, const char * const *attrs, struct ldb_message ***res) { - if (!module->next) { - return -1; - } - return module->next->ops->search_bytree(module->next, base, scope, tree, attrs, res); + FIND_OP(module, search_bytree); + return module->ops->search_bytree(module, base, scope, tree, attrs, res); } int ldb_next_search(struct ldb_module *module, @@ -235,15 +245,13 @@ int ldb_next_search(struct ldb_module *module, { struct ldb_parse_tree *tree; int ret; - if (!module->next) { - return -1; - } + FIND_OP(module, search_bytree); tree = ldb_parse_tree(module, expression); if (tree == NULL) { ldb_set_errstring(module, talloc_strdup(module, "Unable to parse search expression")); return -1; } - ret = module->next->ops->search_bytree(module->next, base, scope, tree, attrs, res); + ret = module->ops->search_bytree(module, base, scope, tree, attrs, res); talloc_free(tree); return ret; } @@ -251,58 +259,44 @@ int ldb_next_search(struct ldb_module *module, int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message) { - if (!module->next) { - return -1; - } - return module->next->ops->add_record(module->next, message); + FIND_OP(module, add_record); + return module->ops->add_record(module, message); } int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message) { - if (!module->next) { - return -1; - } - return module->next->ops->modify_record(module->next, message); + FIND_OP(module, modify_record); + return module->ops->modify_record(module, message); } int ldb_next_delete_record(struct ldb_module *module, const struct ldb_dn *dn) { - if (!module->next) { - return -1; - } - return module->next->ops->delete_record(module->next, dn); + FIND_OP(module, delete_record); + return module->ops->delete_record(module, dn); } int ldb_next_rename_record(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn) { - if (!module->next) { - return -1; - } - return module->next->ops->rename_record(module->next, olddn, newdn); + FIND_OP(module, rename_record); + return module->ops->rename_record(module, olddn, newdn); } int ldb_next_start_trans(struct ldb_module *module) { - if (!module->next) { - return -1; - } - return module->next->ops->start_transaction(module->next); + FIND_OP(module, start_transaction); + return module->ops->start_transaction(module); } int ldb_next_end_trans(struct ldb_module *module) { - if (!module->next) { - return -1; - } - return module->next->ops->end_transaction(module->next); + FIND_OP(module, end_transaction); + return module->ops->end_transaction(module); } int ldb_next_del_trans(struct ldb_module *module) { - if (!module->next) { - return -1; - } - return module->next->ops->del_transaction(module->next); + FIND_OP(module, del_transaction); + return module->ops->del_transaction(module); } void ldb_set_errstring(struct ldb_module *module, char *err_string) |