Blob Blame History Raw
From d93b4fe14b0f72bd8311497d18204f153c104007 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Po=C5=82awski?= <ppolawsk@redhat.com>
Date: Fri, 13 Mar 2020 00:57:55 +0000
Subject: [PATCH 14/14] SYSDB: Cache selector as enum
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Sysdb has two sources of cache: timestamp based and persistent.
This change changes implementation of that selector from
binary flag to enum.

Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>

Reviewed-by: Pavel Březina <pbrezina@redhat.com>
---
 src/db/sysdb.h                         |  9 +++++---
 src/db/sysdb_search.c                  | 32 ++++++++++++++++++++++----
 src/providers/be_refresh.c             | 10 ++++----
 src/tests/cmocka/test_sysdb_ts_cache.c | 10 ++++----
 4 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index 0e6e9266b..beee710af 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -1217,14 +1217,17 @@ int sysdb_search_users(TALLOC_CTX *mem_ctx,
                        size_t *msgs_count,
                        struct ldb_message ***msgs);
 
-#define SYSDB_SEARCH_WITH_TS_ONLY_TS_FILTER     0x0001
-#define SYSDB_SEARCH_WITH_TS_ONLY_SYSDB_FILTER  0x0002
+enum sysdb_cache_type {
+    SYSDB_CACHE_TYPE_NONE,
+    SYSDB_CACHE_TYPE_TIMESTAMP,
+    SYSDB_CACHE_TYPE_PERSISTENT
+};
 
 errno_t sysdb_search_with_ts_attr(TALLOC_CTX *mem_ctx,
                                   struct sss_domain_info *domain,
                                   struct ldb_dn *base_dn,
                                   enum ldb_scope scope,
-                                  int optflags,
+                                  enum sysdb_cache_type search_cache,
                                   const char *filter,
                                   const char *attrs[],
                                   struct ldb_result **_result);
diff --git a/src/db/sysdb_search.c b/src/db/sysdb_search.c
index a71c43112..6c89b4c98 100644
--- a/src/db/sysdb_search.c
+++ b/src/db/sysdb_search.c
@@ -639,7 +639,7 @@ errno_t sysdb_search_with_ts_attr(TALLOC_CTX *mem_ctx,
                                   struct sss_domain_info *domain,
                                   struct ldb_dn *base_dn,
                                   enum ldb_scope scope,
-                                  int optflags,
+                                  enum sysdb_cache_type search_cache,
                                   const char *filter,
                                   const char *attrs[],
                                   struct ldb_result **_res)
@@ -666,7 +666,8 @@ errno_t sysdb_search_with_ts_attr(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
-    if (optflags & SYSDB_SEARCH_WITH_TS_ONLY_SYSDB_FILTER) {
+    switch (search_cache) {
+    case SYSDB_CACHE_TYPE_PERSISTENT: {
         /* We only care about searching the persistent db */
         ts_cache_res = talloc_zero(tmp_ctx, struct ldb_result);
         if (ts_cache_res == NULL) {
@@ -675,7 +676,14 @@ errno_t sysdb_search_with_ts_attr(TALLOC_CTX *mem_ctx,
         }
         ts_cache_res->count = 0;
         ts_cache_res->msgs = NULL;
-    } else {
+
+        break;
+    }
+
+    case SYSDB_CACHE_TYPE_TIMESTAMP:
+        /* FALLTHOUGH*/
+        SSS_ATTRIBUTE_FALLTHROUGH;
+    default: {
         /* Because the timestamp database does not contain all the
          * attributes, we need to search the persistent db for each
          * of the entries found and merge the results
@@ -708,9 +716,13 @@ errno_t sysdb_search_with_ts_attr(TALLOC_CTX *mem_ctx,
         if (ret != EOK) {
             goto done;
         }
+
+        break;
+    }
     }
 
-    if (optflags & SYSDB_SEARCH_WITH_TS_ONLY_TS_FILTER) {
+    switch (search_cache) {
+    case SYSDB_CACHE_TYPE_TIMESTAMP: {
         /* The filter only contains timestamp attrs, no need to search the
          * persistent db
          */
@@ -718,7 +730,14 @@ errno_t sysdb_search_with_ts_attr(TALLOC_CTX *mem_ctx,
             res->count = ts_cache_res->count;
             res->msgs = talloc_steal(res, ts_cache_res->msgs);
         }
-    } else {
+
+        break;
+    }
+
+    case SYSDB_CACHE_TYPE_PERSISTENT:
+        /* FALLTHOUGH*/
+        SSS_ATTRIBUTE_FALLTHROUGH;
+    default: {
         /* Because some of the attributes being searched might exist in the persistent
          * database only, we also search the persistent db
          */
@@ -738,6 +757,9 @@ errno_t sysdb_search_with_ts_attr(TALLOC_CTX *mem_ctx,
             ret = ENOMEM;
             goto done;
         }
+
+        break;
+    }
     }
 
     *_res = talloc_steal(mem_ctx, res);
diff --git a/src/providers/be_refresh.c b/src/providers/be_refresh.c
index d503556a3..8fcfb86b4 100644
--- a/src/providers/be_refresh.c
+++ b/src/providers/be_refresh.c
@@ -35,7 +35,7 @@ static errno_t be_refresh_get_values_ex(TALLOC_CTX *mem_ctx,
                                         struct ldb_dn *base_dn,
                                         const char *key_attr,
                                         const char *value_attr,
-                                        int optflags,
+                                        enum sysdb_cache_type search_cache,
                                         char ***_values)
 {
     TALLOC_CTX *tmp_ctx = NULL;
@@ -65,7 +65,7 @@ static errno_t be_refresh_get_values_ex(TALLOC_CTX *mem_ctx,
 
     ret = sysdb_search_with_ts_attr(tmp_ctx, domain, base_dn,
                                     LDB_SCOPE_SUBTREE,
-                                    optflags,
+                                    search_cache,
                                     filter, attrs,
                                     &res);
     if (ret != EOK) {
@@ -103,7 +103,7 @@ static errno_t be_refresh_get_values(TALLOC_CTX *mem_ctx,
     struct ldb_dn *base_dn = NULL;
     errno_t ret;
     const char *key_attr;
-    int optflags = SYSDB_SEARCH_WITH_TS_ONLY_TS_FILTER;
+    enum sysdb_cache_type search_cache = SYSDB_CACHE_TYPE_TIMESTAMP;
 
     switch (type) {
     case BE_REFRESH_TYPE_INITGROUPS:
@@ -121,7 +121,7 @@ static errno_t be_refresh_get_values(TALLOC_CTX *mem_ctx,
     case BE_REFRESH_TYPE_NETGROUPS:
         key_attr = SYSDB_CACHE_EXPIRE;
         // Netgroup will reside in persistent cache rather than timestamp one
-        optflags = SYSDB_SEARCH_WITH_TS_ONLY_SYSDB_FILTER;
+        search_cache = SYSDB_CACHE_TYPE_PERSISTENT;
         base_dn = sysdb_netgroup_base_dn(mem_ctx, domain);
         break;
     default:
@@ -136,7 +136,7 @@ static errno_t be_refresh_get_values(TALLOC_CTX *mem_ctx,
 
     ret = be_refresh_get_values_ex(mem_ctx, domain, period,
                                    base_dn, key_attr,
-                                   attr_name, optflags, _values);
+                                   attr_name, search_cache, _values);
 
     talloc_free(base_dn);
     return ret;
diff --git a/src/tests/cmocka/test_sysdb_ts_cache.c b/src/tests/cmocka/test_sysdb_ts_cache.c
index d2296d1b8..ae8b1b16c 100644
--- a/src/tests/cmocka/test_sysdb_ts_cache.c
+++ b/src/tests/cmocka/test_sysdb_ts_cache.c
@@ -1438,7 +1438,7 @@ static void test_sysdb_search_with_ts(void **state)
                                     test_ctx->tctx->dom,
                                     base_dn,
                                     LDB_SCOPE_SUBTREE,
-                                    0,
+                                    SYSDB_CACHE_TYPE_NONE,
                                     SYSDB_NAME"=*",
                                     attrs,
                                     &res);
@@ -1523,7 +1523,7 @@ static void test_sysdb_search_with_ts(void **state)
                                     test_ctx->tctx->dom,
                                     base_dn,
                                     LDB_SCOPE_SUBTREE,
-                                    0,
+                                    SYSDB_CACHE_TYPE_NONE,
                                     filter,
                                     attrs,
                                     &res);
@@ -1552,7 +1552,7 @@ static void test_sysdb_search_with_ts(void **state)
                                     test_ctx->tctx->dom,
                                     base_dn,
                                     LDB_SCOPE_SUBTREE,
-                                    SYSDB_SEARCH_WITH_TS_ONLY_TS_FILTER,
+                                    SYSDB_CACHE_TYPE_TIMESTAMP,
                                     filter,
                                     attrs,
                                     &res);
@@ -1571,7 +1571,7 @@ static void test_sysdb_search_with_ts(void **state)
                                     test_ctx->tctx->dom,
                                     base_dn,
                                     LDB_SCOPE_SUBTREE,
-                                    SYSDB_SEARCH_WITH_TS_ONLY_SYSDB_FILTER,
+                                    SYSDB_CACHE_TYPE_PERSISTENT,
                                     filter,
                                     attrs,
                                     &res);
@@ -1596,7 +1596,7 @@ static void test_sysdb_search_with_ts(void **state)
                                     test_ctx->tctx->dom,
                                     base_dn,
                                     LDB_SCOPE_SUBTREE,
-                                    0,
+                                    SYSDB_CACHE_TYPE_NONE,
                                     filter,
                                     attrs,
                                     &res);
-- 
2.21.1