summaryrefslogtreecommitdiffstats
path: root/src/db
diff options
context:
space:
mode:
authorSumit Bose <sbose@redhat.com>2014-10-08 16:12:52 +0200
committerJakub Hrozek <jhrozek@redhat.com>2014-10-16 17:56:07 +0200
commit8a2a503fa5c01ea037d28b7c902b8821a11084bd (patch)
tree05b87b9b9c26b72fae1c1f983007894868401081 /src/db
parentca49ae1eee321751681e99f3ebe2547211db3bf6 (diff)
downloadsssd-8a2a503fa5c01ea037d28b7c902b8821a11084bd.tar.gz
sssd-8a2a503fa5c01ea037d28b7c902b8821a11084bd.tar.xz
sssd-8a2a503fa5c01ea037d28b7c902b8821a11084bd.zip
sysdb: add sysdb_attrs_add_val_safe() and sysdb_attrs_add_string_safe()
sysdb_attrs_add_val_safe() works like sysdb_attrs_add_val() but checks if the attribute value to add already exists. In this case the value list is not changed. This is useful if values are added from different sources at different times to avoid LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS errors from ldb_modify() later on. sysdb_attrs_add_string_safe() does the same for string arguments Reviewed-by: Pavel Březina <pbrezina@redhat.com>
Diffstat (limited to 'src/db')
-rw-r--r--src/db/sysdb.c40
-rw-r--r--src/db/sysdb.h4
2 files changed, 42 insertions, 2 deletions
diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index 88cff241f..8d6f00b52 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -508,18 +508,31 @@ int sysdb_attrs_get_string_array(struct sysdb_attrs *attrs, const char *name,
return EOK;
}
-int sysdb_attrs_add_val(struct sysdb_attrs *attrs,
- const char *name, const struct ldb_val *val)
+
+static int sysdb_attrs_add_val_int(struct sysdb_attrs *attrs,
+ const char *name, bool check_values,
+ const struct ldb_val *val)
{
struct ldb_message_element *el = NULL;
struct ldb_val *vals;
int ret;
+ size_t c;
ret = sysdb_attrs_get_el(attrs, name, &el);
if (ret != EOK) {
return ret;
}
+ if (check_values) {
+ for (c = 0; c < el->num_values; c++) {
+ if (val->length == el->values[c].length
+ && memcmp(val->data, el->values[c].data,
+ val->length) == 0) {
+ return EOK;
+ }
+ }
+ }
+
vals = talloc_realloc(attrs->a, el->values,
struct ldb_val, el->num_values+1);
if (!vals) return ENOMEM;
@@ -535,6 +548,29 @@ int sysdb_attrs_add_val(struct sysdb_attrs *attrs,
return EOK;
}
+int sysdb_attrs_add_val(struct sysdb_attrs *attrs,
+ const char *name, const struct ldb_val *val)
+{
+ return sysdb_attrs_add_val_int(attrs, name, false, val);
+}
+
+/* Check if the same value already exists. */
+int sysdb_attrs_add_val_safe(struct sysdb_attrs *attrs,
+ const char *name, const struct ldb_val *val)
+{
+ return sysdb_attrs_add_val_int(attrs, name, true, val);
+}
+
+int sysdb_attrs_add_string_safe(struct sysdb_attrs *attrs,
+ const char *name, const char *str)
+{
+ struct ldb_val v;
+
+ v.data = (uint8_t *)discard_const(str);
+ v.length = strlen(str);
+
+ return sysdb_attrs_add_val_safe(attrs, name, &v);
+}
int sysdb_attrs_add_string(struct sysdb_attrs *attrs,
const char *name, const char *str)
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index b039a673a..602db2c04 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -287,6 +287,10 @@ struct range_info {
/* values are copied in the structure, allocated on "attrs" */
int sysdb_attrs_add_val(struct sysdb_attrs *attrs,
const char *name, const struct ldb_val *val);
+int sysdb_attrs_add_val_safe(struct sysdb_attrs *attrs,
+ const char *name, const struct ldb_val *val);
+int sysdb_attrs_add_string_safe(struct sysdb_attrs *attrs,
+ const char *name, const char *str);
int sysdb_attrs_add_string(struct sysdb_attrs *attrs,
const char *name, const char *str);
int sysdb_attrs_add_lower_case_string(struct sysdb_attrs *attrs,