From 604a4fd438200d36c65aec089bdf43dba0b87e6f Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Thu, 20 Oct 2016 16:04:04 +1300 Subject: [PATCH] ldb: Add helper function ldb_schema_attribute_remove_flagged() This helps us avoid keeping a list of attributes to later remove on @ATTRIBUTES reload Signed-off-by: Andrew Bartlett Reviewed-by: Garming Sam --- lib/ldb/common/ldb_attributes.c | 33 +++++++++++++++++++++++++++++++++ lib/ldb/include/ldb.h | 5 +++++ lib/ldb/include/ldb_private.h | 6 ++++++ 3 files changed, 44 insertions(+) diff --git a/lib/ldb/common/ldb_attributes.c b/lib/ldb/common/ldb_attributes.c index 767f69cdbbc..ca6707edf65 100644 --- a/lib/ldb/common/ldb_attributes.c +++ b/lib/ldb/common/ldb_attributes.c @@ -215,6 +215,39 @@ void ldb_schema_attribute_remove(struct ldb_context *ldb, const char *name) ldb->schema.num_attributes--; } +/* + remove attributes with a specified flag (eg LDB_ATTR_FLAG_FROM_DB) for this ldb context + + This is to permit correct reloads +*/ +void ldb_schema_attribute_remove_flagged(struct ldb_context *ldb, unsigned int flag) +{ + ptrdiff_t i; + + for (i = 0; i < ldb->schema.num_attributes;) { + const struct ldb_schema_attribute *a + = &ldb->schema.attributes[i]; + /* FIXED attributes are never removed */ + if (a->flags & LDB_ATTR_FLAG_FIXED) { + i++; + continue; + } + if ((a->flags & flag) == 0) { + i++; + continue; + } + if (a->flags & LDB_ATTR_FLAG_ALLOCATED) { + talloc_free(discard_const_p(char, a->name)); + } + if (i < ldb->schema.num_attributes - 1) { + memmove(&ldb->schema.attributes[i], + a+1, sizeof(*a) * (ldb->schema.num_attributes-(i+1))); + } + + ldb->schema.num_attributes--; + } +} + /* setup a attribute handler using a standard syntax */ diff --git a/lib/ldb/include/ldb.h b/lib/ldb/include/ldb.h index ef292302d39..397f9946710 100644 --- a/lib/ldb/include/ldb.h +++ b/lib/ldb/include/ldb.h @@ -427,6 +427,11 @@ const struct ldb_dn_extended_syntax *ldb_dn_extended_syntax_by_name(struct ldb_c */ #define LDB_ATTR_FLAG_FORCE_BASE64_LDIF (1<<5) +/* + * The attribute was loaded from a DB, rather than via the C API + */ +#define LDB_ATTR_FLAG_FROM_DB (1<<6) + /** LDAP attribute syntax for a DN diff --git a/lib/ldb/include/ldb_private.h b/lib/ldb/include/ldb_private.h index 26a9d426afb..ef0b6971581 100644 --- a/lib/ldb/include/ldb_private.h +++ b/lib/ldb/include/ldb_private.h @@ -163,6 +163,12 @@ extern const struct ldb_backend_ops ldb_ldapi_backend_ops; extern const struct ldb_backend_ops ldb_ldaps_backend_ops; int ldb_setup_wellknown_attributes(struct ldb_context *ldb); +/* + remove attributes with a specified flag (eg LDB_ATTR_FLAG_FROM_DB) for this ldb context + + This is to permit correct reloads +*/ +void ldb_schema_attribute_remove_flagged(struct ldb_context *ldb, unsigned int flag); const char **ldb_subclass_list(struct ldb_context *ldb, const char *classname); void ldb_subclass_remove(struct ldb_context *ldb, const char *classname); -- 2.34.1