From ab1bea7bee0dc427dde5b2f8cac37df5302f5227 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= <fidencio@redhat.com>
Date: Wed, 25 May 2016 23:00:59 +0200
Subject: [PATCH v2 4/4] sysdb: add sysdb_ldb_msg_{add,replace,delete}_ulong()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

As the add_ulong() convenience can add, replace or remove a unsigned
long according to the operation received as its argument, some confusion
can easily happen due to its misleading name.

In order to improve the explicitness of our code, let's introduce
sysdb_ldb_msg_add_ulong(), sysdb_ldb_msg_replace_ulong() and
sysdb_ldb_msg_delete_ulong().

These new functions are basically wrappers of add_ulong() (now
sysdb_ldb_msg_ulong_helper()), calling it using the proper flag
according to each function.

Any code previously using add_ulong() is now adapted to use these brand
new functions.

Related: https://fedorahosted.org/sssd/ticket/1656

Signed-off-by: Fabiano Fidêncio <fabiano@fidencio.org>
---
 src/db/sysdb.c          | 22 ++++++++++++++++++++--
 src/db/sysdb_ops.c      | 21 ++++++++++-----------
 src/db/sysdb_private.h  |  8 ++++++--
 src/db/sysdb_ranges.c   | 20 ++++++++++----------
 src/db/sysdb_services.c | 10 ++++------
 5 files changed, 50 insertions(+), 31 deletions(-)

diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index b67b6875c5f4811802c314cb2378a24eaab4803e..8f1f8e56b596269ac4ddcfa359fe98c0d387c987 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -2143,8 +2143,8 @@ int sysdb_ldb_msg_delete_string(struct ldb_message *msg,
     return sysdb_ldb_msg_string_helper(msg, LDB_FLAG_MOD_DELETE, attr, value);
 }
 
-int add_ulong(struct ldb_message *msg, int flags,
-              const char *attr, unsigned long value)
+static int sysdb_ldb_msg_ulong_helper(struct ldb_message *msg, int flags,
+                                      const char *attr, unsigned long value)
 {
     int ret;
 
@@ -2155,3 +2155,21 @@ int add_ulong(struct ldb_message *msg, int flags,
     }
     return ENOMEM;
 }
+
+int sysdb_ldb_msg_add_ulong(struct ldb_message *msg,
+                            const char *attr, unsigned long value)
+{
+    return sysdb_ldb_msg_ulong_helper(msg, LDB_FLAG_MOD_ADD, attr, value);
+}
+
+int sysdb_ldb_msg_replace_ulong(struct ldb_message *msg,
+                                const char *attr, unsigned long value)
+{
+    return sysdb_ldb_msg_ulong_helper(msg, LDB_FLAG_MOD_REPLACE, attr, value);
+}
+
+int sysdb_ldb_delete_ulong(struct ldb_message *msg,
+                           const char *attr, unsigned long value)
+{
+    return sysdb_ldb_msg_ulong_helper(msg, LDB_FLAG_MOD_DELETE, attr, value);
+}
diff --git a/src/db/sysdb_ops.c b/src/db/sysdb_ops.c
index 0bd577a513aa70410cca231bab872047b359910d..09448dac12ef11b1ca2abc9d745d9188e99d12a6 100644
--- a/src/db/sysdb_ops.c
+++ b/src/db/sysdb_ops.c
@@ -963,8 +963,7 @@ int sysdb_get_new_id(struct sss_domain_info *domain,
     }
     msg->dn = base_dn;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_REPLACE,
-                    SYSDB_NEXTID, new_id + 1);
+    ret = sysdb_ldb_msg_replace_ulong(msg, SYSDB_NEXTID, new_id + 1);
     if (ret) {
         goto done;
     }
@@ -1030,10 +1029,10 @@ int sysdb_add_basic_user(struct sss_domain_info *domain,
     ret = sysdb_ldb_msg_add_string(msg, SYSDB_NAME, name);
     if (ret) goto done;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_UIDNUM, (unsigned long)uid);
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_UIDNUM, (unsigned long)uid);
     if (ret) goto done;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_GIDNUM, (unsigned long)gid);
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_GIDNUM, (unsigned long)gid);
     if (ret) goto done;
 
     /* We set gecos to be the same as fullname on user creation,
@@ -1058,8 +1057,8 @@ int sysdb_add_basic_user(struct sss_domain_info *domain,
     }
 
     /* creation time */
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
-                    (unsigned long)time(NULL));
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_CREATE_TIME,
+                                  (unsigned long)time(NULL));
     if (ret) goto done;
 
     ret = ldb_add(domain->sysdb->ldb, msg);
@@ -1445,12 +1444,12 @@ int sysdb_add_basic_group(struct sss_domain_info *domain,
     ret = sysdb_ldb_msg_add_string(msg, SYSDB_NAME, name);
     if (ret) goto done;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_GIDNUM, (unsigned long)gid);
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_GIDNUM, (unsigned long)gid);
     if (ret) goto done;
 
     /* creation time */
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
-                    (unsigned long)time(NULL));
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_CREATE_TIME,
+                                  (unsigned long)time(NULL));
     if (ret) goto done;
 
     ret = ldb_add(domain->sysdb->ldb, msg);
@@ -1765,8 +1764,8 @@ int sysdb_add_basic_netgroup(struct sss_domain_info *domain,
     }
 
     /* creation time */
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
-                    (unsigned long) time(NULL));
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_CREATE_TIME,
+                                  (unsigned long) time(NULL));
     if (ret) goto done;
 
     ret = ldb_add(domain->sysdb->ldb, msg);
diff --git a/src/db/sysdb_private.h b/src/db/sysdb_private.h
index 197bac6ee1701f30e436a019be270c2b3b390243..704bb64439a1e95380de18ea22964ab8130bb0b8 100644
--- a/src/db/sysdb_private.h
+++ b/src/db/sysdb_private.h
@@ -130,8 +130,12 @@ int sysdb_ldb_msg_replace_string(struct ldb_message *msg,
                                  const char *attr, const char *value);
 int sysdb_ldb_msg_delete_string(struct ldb_message *msg,
                                 const char *attr, const char *value);
-int add_ulong(struct ldb_message *msg, int flags,
-              const char *attr, unsigned long value);
+int sysdb_ldb_msg_add_ulong(struct ldb_message *msg,
+                            const char *attr, unsigned long value);
+int sysdb_ldb_msg_replace_ulong(struct ldb_message *msg,
+                                const char *attr, unsigned long value);
+int sysdb_ldb_msg_delete_ulong(struct ldb_message *msg,
+                               const char *attr, unsigned long value);
 
 /* The utility function to create a subdomain sss_domain_info object is handy
  * for unit tests, so it should be available in a header, but not a public util
diff --git a/src/db/sysdb_ranges.c b/src/db/sysdb_ranges.c
index 6528d2b1cd4c8c2d3e676212f3763243a8c4aafa..f675853b51e021f97d3d62047631ad9f931fd06e 100644
--- a/src/db/sysdb_ranges.c
+++ b/src/db/sysdb_ranges.c
@@ -207,8 +207,8 @@ errno_t sysdb_range_create(struct sysdb_ctx *sysdb, struct range_info *range)
                                        SYSDB_DOMAIN_ID_RANGE_CLASS);
         if (ret) goto done;
 
-        ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_SECONDARY_BASE_RID,
-                        (unsigned long) range->secondary_base_rid);
+        ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_SECONDARY_BASE_RID,
+                                      (unsigned long)range->secondary_base_rid);
         if (ret) goto done;
     } else if (range->trusted_dom_sid != NULL &&
                range->secondary_base_rid == 0) {
@@ -224,20 +224,20 @@ errno_t sysdb_range_create(struct sysdb_ctx *sysdb, struct range_info *range)
     ret = sysdb_ldb_msg_add_string(msg, SYSDB_NAME, range->name);
     if (ret) goto done;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_BASE_ID,
-                    (unsigned long) range->base_id);
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_BASE_ID,
+                                  (unsigned long)range->base_id);
     if (ret) goto done;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_ID_RANGE_SIZE,
-                    (unsigned long) range->id_range_size);
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_ID_RANGE_SIZE,
+                                  (unsigned long)range->id_range_size);
     if (ret) goto done;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_BASE_RID,
-                    (unsigned long) range->base_rid);
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_BASE_RID,
+                                  (unsigned long)range->base_rid);
     if (ret) goto done;
 
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
-                    (unsigned long)time(NULL));
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_CREATE_TIME,
+                                  (unsigned long)time(NULL));
     if (ret) goto done;
 
     ret = sysdb_ldb_msg_add_string(msg, SYSDB_ID_RANGE_TYPE, range->range_type);
diff --git a/src/db/sysdb_services.c b/src/db/sysdb_services.c
index 0010160f8ce22214aa0d6c48ef7ad9300836a45b..a6f12a123474eb99480b334fdac766fb2aa390f0 100644
--- a/src/db/sysdb_services.c
+++ b/src/db/sysdb_services.c
@@ -468,8 +468,7 @@ sysdb_svc_add(TALLOC_CTX *mem_ctx,
     if (ret != EOK) goto done;
 
     /* Set the port number */
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD,
-                    SYSDB_SVC_PORT, port);
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_SVC_PORT, port);
     if (ret != EOK) goto done;
 
     /* If this service has any aliases, include them */
@@ -506,8 +505,8 @@ sysdb_svc_add(TALLOC_CTX *mem_ctx,
     }
 
     /* creation time */
-    ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
-                    (unsigned long)time(NULL));
+    ret = sysdb_ldb_msg_add_ulong(msg, SYSDB_CREATE_TIME,
+                                  (unsigned long)time(NULL));
     if (ret) goto done;
 
     lret = ldb_add(domain->sysdb->ldb, msg);
@@ -551,8 +550,7 @@ sysdb_svc_update(struct sysdb_ctx *sysdb,
     msg->dn = dn;
 
     /* Update the port */
-    ret = add_ulong(msg, SYSDB_MOD_REP,
-                    SYSDB_SVC_PORT, port);
+    ret = sysdb_ldb_msg_replace_ulong(msg, SYSDB_SVC_PORT, port);
     if (ret != EOK) goto done;
 
     if (aliases && aliases[0]) {
-- 
2.7.4

