LCOV - code coverage report
Current view: top level - lib/ldb/ldb_tdb - ldb_tdb.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 177 219 80.8 %
Date: 2021-09-23 10:06:22 Functions: 22 24 91.7 %

          Line data    Source code
       1             : /*
       2             :    ldb database library
       3             : 
       4             :    Copyright (C) Andrew Tridgell 2004
       5             :    Copyright (C) Stefan Metzmacher 2004
       6             :    Copyright (C) Simo Sorce 2006-2008
       7             :    Copyright (C) Matthias Dieter Wallnöfer 2009-2010
       8             : 
       9             :      ** NOTE! The following LGPL license applies to the ldb
      10             :      ** library. This does NOT imply that all of Samba is released
      11             :      ** under the LGPL
      12             : 
      13             :    This library is free software; you can redistribute it and/or
      14             :    modify it under the terms of the GNU Lesser General Public
      15             :    License as published by the Free Software Foundation; either
      16             :    version 3 of the License, or (at your option) any later version.
      17             : 
      18             :    This library is distributed in the hope that it will be useful,
      19             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      21             :    Lesser General Public License for more details.
      22             : 
      23             :    You should have received a copy of the GNU Lesser General Public
      24             :    License along with this library; if not, see <http://www.gnu.org/licenses/>.
      25             : */
      26             : 
      27             : /*
      28             :  *  Name: ldb_tdb
      29             :  *
      30             :  *  Component: ldb tdb backend
      31             :  *
      32             :  *  Description: core functions for tdb backend
      33             :  *
      34             :  *  Author: Andrew Tridgell
      35             :  *  Author: Stefan Metzmacher
      36             :  *
      37             :  *  Modifications:
      38             :  *
      39             :  *  - description: make the module use asynchronous calls
      40             :  *    date: Feb 2006
      41             :  *    Author: Simo Sorce
      42             :  *
      43             :  *  - description: make it possible to use event contexts
      44             :  *    date: Jan 2008
      45             :  *    Author: Simo Sorce
      46             :  *
      47             :  *  - description: fix up memory leaks and small bugs
      48             :  *    date: Oct 2009
      49             :  *    Author: Matthias Dieter Wallnöfer
      50             :  */
      51             : 
      52             : #include "ldb_tdb.h"
      53             : #include "ldb_private.h"
      54             : #include "../ldb_key_value/ldb_kv.h"
      55             : #include <tdb.h>
      56             : 
      57             : /*
      58             :   lock the database for read - use by ltdb_search and ltdb_sequence_number
      59             : */
      60   121383900 : static int ltdb_lock_read(struct ldb_module *module)
      61             : {
      62   121383900 :         void *data = ldb_module_get_private(module);
      63   101168722 :         struct ldb_kv_private *ldb_kv =
      64    20215178 :             talloc_get_type(data, struct ldb_kv_private);
      65   121383900 :         int tdb_ret = 0;
      66             :         int ret;
      67   121383900 :         pid_t pid = getpid();
      68             : 
      69   121383900 :         if (ldb_kv->pid != pid) {
      70           2 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
      71             :                                        __location__
      72             :                                        ": Reusing ldb opend by pid %d in "
      73             :                                        "process %d\n",
      74             :                                        ldb_kv->pid,
      75             :                                        pid);
      76           2 :                 return LDB_ERR_PROTOCOL_ERROR;
      77             :         }
      78             : 
      79   180407313 :         if (tdb_transaction_active(ldb_kv->tdb) == false &&
      80    72516260 :             ldb_kv->read_lock_count == 0) {
      81    35403633 :                 tdb_ret = tdb_lockall_read(ldb_kv->tdb);
      82             :         }
      83   117467093 :         if (tdb_ret == 0) {
      84   121383898 :                 ldb_kv->read_lock_count++;
      85   121383898 :                 return LDB_SUCCESS;
      86             :         }
      87           0 :         ret = ltdb_err_map(tdb_error(ldb_kv->tdb));
      88           0 :         if (ret == LDB_SUCCESS) {
      89           0 :                 ret = LDB_ERR_OPERATIONS_ERROR;
      90             :         }
      91           0 :         ldb_debug_set(ldb_module_get_ctx(module),
      92             :                       LDB_DEBUG_FATAL,
      93             :                       "Failure during ltdb_lock_read(): %s -> %s",
      94           0 :                       tdb_errorstr(ldb_kv->tdb),
      95             :                       ldb_strerror(ret));
      96           0 :         return ret;
      97             : }
      98             : 
      99             : /*
     100             :   unlock the database after a ltdb_lock_read()
     101             : */
     102   121383876 : static int ltdb_unlock_read(struct ldb_module *module)
     103             : {
     104   121383876 :         void *data = ldb_module_get_private(module);
     105   101168698 :         struct ldb_kv_private *ldb_kv =
     106    20215178 :             talloc_get_type(data, struct ldb_kv_private);
     107   121383876 :         pid_t pid = getpid();
     108             : 
     109   121383876 :         if (ldb_kv->pid != pid) {
     110           2 :                 ldb_asprintf_errstring(ldb_module_get_ctx(module),
     111             :                                        __location__
     112             :                                        ": Reusing ldb opend by pid %d in "
     113             :                                        "process %d\n",
     114             :                                        ldb_kv->pid,
     115             :                                        pid);
     116           2 :                 return LDB_ERR_PROTOCOL_ERROR;
     117             :         }
     118   180407265 :         if (!tdb_transaction_active(ldb_kv->tdb) &&
     119    72516236 :             ldb_kv->read_lock_count == 1) {
     120    35403619 :                 tdb_unlockall_read(ldb_kv->tdb);
     121    35403619 :                 ldb_kv->read_lock_count--;
     122    35403619 :                 return 0;
     123             :         }
     124    85980255 :         ldb_kv->read_lock_count--;
     125    85980255 :         return 0;
     126             : }
     127             : 
     128    11532751 : static int ltdb_store(struct ldb_kv_private *ldb_kv,
     129             :                       struct ldb_val ldb_key,
     130             :                       struct ldb_val ldb_data,
     131             :                       int flags)
     132             : {
     133    21188681 :         TDB_DATA key = {
     134    11532751 :                 .dptr = ldb_key.data,
     135    11532751 :                 .dsize = ldb_key.length
     136             :         };
     137    21188681 :         TDB_DATA data = {
     138    11532751 :                 .dptr = ldb_data.data,
     139    11532751 :                 .dsize = ldb_data.length
     140             :         };
     141    11532751 :         bool transaction_active = tdb_transaction_active(ldb_kv->tdb);
     142    11532751 :         if (transaction_active == false){
     143           3 :                 return LDB_ERR_PROTOCOL_ERROR;
     144             :         }
     145    11532748 :         return tdb_store(ldb_kv->tdb, key, data, flags);
     146             : }
     147             : 
     148       55048 : static int ltdb_error(struct ldb_kv_private *ldb_kv)
     149             : {
     150       55048 :         return ltdb_err_map(tdb_error(ldb_kv->tdb));
     151             : }
     152             : 
     153           0 : static const char *ltdb_errorstr(struct ldb_kv_private *ldb_kv)
     154             : {
     155           0 :         return tdb_errorstr(ldb_kv->tdb);
     156             : }
     157             : 
     158      563429 : static int ltdb_delete(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key)
     159             : {
     160     1036350 :         TDB_DATA tdb_key = {
     161      563429 :                 .dptr = ldb_key.data,
     162      563429 :                 .dsize = ldb_key.length
     163             :         };
     164      563429 :         bool transaction_active = tdb_transaction_active(ldb_kv->tdb);
     165      563429 :         if (transaction_active == false){
     166           3 :                 return LDB_ERR_PROTOCOL_ERROR;
     167             :         }
     168      563426 :         return tdb_delete(ldb_kv->tdb, tdb_key);
     169             : }
     170             : 
     171     1168496 : static int ltdb_transaction_start(struct ldb_kv_private *ldb_kv)
     172             : {
     173     1168496 :         pid_t pid = getpid();
     174             : 
     175     1168496 :         if (ldb_kv->pid != pid) {
     176           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
     177             :                                        __location__
     178             :                                        ": Reusing ldb opend by pid %d in "
     179             :                                        "process %d\n",
     180             :                                        ldb_kv->pid,
     181             :                                        pid);
     182           0 :                 return LDB_ERR_PROTOCOL_ERROR;
     183             :         }
     184             : 
     185     1168496 :         return tdb_transaction_start(ldb_kv->tdb);
     186             : }
     187             : 
     188      131004 : static int ltdb_transaction_cancel(struct ldb_kv_private *ldb_kv)
     189             : {
     190      131004 :         pid_t pid = getpid();
     191             : 
     192      131004 :         if (ldb_kv->pid != pid) {
     193           2 :                 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
     194             :                                        __location__
     195             :                                        ": Reusing ldb opend by pid %d in "
     196             :                                        "process %d\n",
     197             :                                        ldb_kv->pid,
     198             :                                        pid);
     199           2 :                 return LDB_ERR_PROTOCOL_ERROR;
     200             :         }
     201             : 
     202      131002 :         return tdb_transaction_cancel(ldb_kv->tdb);
     203             : }
     204             : 
     205     1032150 : static int ltdb_transaction_prepare_commit(struct ldb_kv_private *ldb_kv)
     206             : {
     207     1032150 :         pid_t pid = getpid();
     208             : 
     209     1032150 :         if (ldb_kv->pid != pid) {
     210           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
     211             :                                        __location__
     212             :                                        ": Reusing ldb opend by pid %d in "
     213             :                                        "process %d\n",
     214             :                                        ldb_kv->pid,
     215             :                                        pid);
     216           0 :                 return LDB_ERR_PROTOCOL_ERROR;
     217             :         }
     218             : 
     219     1032150 :         return tdb_transaction_prepare_commit(ldb_kv->tdb);
     220             : }
     221             : 
     222     1037467 : static int ltdb_transaction_commit(struct ldb_kv_private *ldb_kv)
     223             : {
     224     1037467 :         pid_t pid = getpid();
     225             : 
     226     1037467 :         if (ldb_kv->pid != pid) {
     227           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
     228             :                                        __location__
     229             :                                        ": Reusing ldb opend by pid %d in "
     230             :                                        "process %d\n",
     231             :                                        ldb_kv->pid,
     232             :                                        pid);
     233           0 :                 return LDB_ERR_PROTOCOL_ERROR;
     234             :         }
     235             : 
     236     1037467 :         return tdb_transaction_commit(ldb_kv->tdb);
     237             : }
     238             : struct kv_ctx {
     239             :         ldb_kv_traverse_fn kv_traverse_fn;
     240             :         void *ctx;
     241             :         struct ldb_kv_private *ldb_kv;
     242             :         int (*parser)(struct ldb_val key,
     243             :                       struct ldb_val data,
     244             :                       void *private_data);
     245             :         int parser_ret;
     246             : };
     247             : 
     248   428913628 : static int ltdb_traverse_fn_wrapper(struct tdb_context *tdb,
     249             :                                     TDB_DATA tdb_key,
     250             :                                     TDB_DATA tdb_data,
     251             :                                     void *ctx)
     252             : {
     253   428913628 :         struct kv_ctx *kv_ctx = ctx;
     254   678304281 :         struct ldb_val key = {
     255   428913628 :                 .length = tdb_key.dsize,
     256   428913628 :                 .data = tdb_key.dptr,
     257             :         };
     258   678304281 :         struct ldb_val data = {
     259   428913628 :                 .length = tdb_data.dsize,
     260   428913628 :                 .data = tdb_data.dptr,
     261             :         };
     262   428913628 :         return kv_ctx->kv_traverse_fn(kv_ctx->ldb_kv, key, data, kv_ctx->ctx);
     263             : }
     264             : 
     265       70417 : static int ltdb_traverse_fn(struct ldb_kv_private *ldb_kv,
     266             :                             ldb_kv_traverse_fn fn,
     267             :                             void *ctx)
     268             : {
     269       70417 :         struct kv_ctx kv_ctx = {
     270             :             .kv_traverse_fn = fn, .ctx = ctx, .ldb_kv = ldb_kv};
     271       70417 :         if (tdb_transaction_active(ldb_kv->tdb)) {
     272       39939 :                 return tdb_traverse(
     273       39939 :                     ldb_kv->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
     274             :         } else {
     275       30478 :                 return tdb_traverse_read(
     276       30478 :                     ldb_kv->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
     277             :         }
     278             : }
     279             : 
     280       71928 : static int ltdb_update_in_iterate(struct ldb_kv_private *ldb_kv,
     281             :                                   struct ldb_val ldb_key,
     282             :                                   struct ldb_val ldb_key2,
     283             :                                   struct ldb_val ldb_data,
     284             :                                   void *state)
     285             : {
     286             :         int tdb_ret;
     287             :         struct ldb_context *ldb;
     288       71928 :         struct ldb_kv_reindex_context *ctx =
     289             :             (struct ldb_kv_reindex_context *)state;
     290       71928 :         struct ldb_module *module = ldb_kv->module;
     291      143856 :         TDB_DATA key = {
     292       71928 :                 .dptr = ldb_key.data,
     293       71928 :                 .dsize = ldb_key.length
     294             :         };
     295      143856 :         TDB_DATA key2 = {
     296       71928 :                 .dptr = ldb_key2.data,
     297       71928 :                 .dsize = ldb_key2.length
     298             :         };
     299      143856 :         TDB_DATA data = {
     300       71928 :                 .dptr = ldb_data.data,
     301       71928 :                 .dsize = ldb_data.length
     302             :         };
     303             : 
     304       71928 :         ldb = ldb_module_get_ctx(module);
     305             : 
     306       71928 :         tdb_ret = tdb_delete(ldb_kv->tdb, key);
     307       71928 :         if (tdb_ret != 0) {
     308           0 :                 ldb_debug(ldb,
     309             :                           LDB_DEBUG_ERROR,
     310             :                           "Failed to delete %*.*s "
     311             :                           "for rekey as %*.*s: %s",
     312           0 :                           (int)key.dsize,
     313           0 :                           (int)key.dsize,
     314           0 :                           (const char *)key.dptr,
     315           0 :                           (int)key2.dsize,
     316           0 :                           (int)key2.dsize,
     317           0 :                           (const char *)key.dptr,
     318           0 :                           tdb_errorstr(ldb_kv->tdb));
     319           0 :                 ctx->error = ltdb_err_map(tdb_error(ldb_kv->tdb));
     320           0 :                 return -1;
     321             :         }
     322       71928 :         tdb_ret = tdb_store(ldb_kv->tdb, key2, data, 0);
     323       71928 :         if (tdb_ret != 0) {
     324           0 :                 ldb_debug(ldb,
     325             :                           LDB_DEBUG_ERROR,
     326             :                           "Failed to rekey %*.*s as %*.*s: %s",
     327           0 :                           (int)key.dsize,
     328           0 :                           (int)key.dsize,
     329           0 :                           (const char *)key.dptr,
     330           0 :                           (int)key2.dsize,
     331           0 :                           (int)key2.dsize,
     332           0 :                           (const char *)key.dptr,
     333           0 :                           tdb_errorstr(ldb_kv->tdb));
     334           0 :                 ctx->error = ltdb_err_map(tdb_error(ldb_kv->tdb));
     335           0 :                 return -1;
     336             :         }
     337       40815 :         return tdb_ret;
     338             : }
     339             : 
     340   147824888 : static int ltdb_parse_record_wrapper(TDB_DATA tdb_key,
     341             :                                      TDB_DATA tdb_data,
     342             :                                      void *ctx)
     343             : {
     344   147824888 :         struct kv_ctx *kv_ctx = ctx;
     345   263459568 :         struct ldb_val key = {
     346   147824888 :                 .length = tdb_key.dsize,
     347   147824888 :                 .data = tdb_key.dptr,
     348             :         };
     349   263459568 :         struct ldb_val data = {
     350   147824888 :                 .length = tdb_data.dsize,
     351   147824888 :                 .data = tdb_data.dptr,
     352             :         };
     353             : 
     354   147824888 :         kv_ctx->parser_ret = kv_ctx->parser(key, data, kv_ctx->ctx);
     355   147824888 :         return kv_ctx->parser_ret;
     356             : }
     357             : 
     358   180641746 : static int ltdb_parse_record(struct ldb_kv_private *ldb_kv,
     359             :                              struct ldb_val ldb_key,
     360             :                              int (*parser)(struct ldb_val key,
     361             :                                            struct ldb_val data,
     362             :                                            void *private_data),
     363             :                              void *ctx)
     364             : {
     365   180641746 :         struct kv_ctx kv_ctx = {.parser = parser, .ctx = ctx, .ldb_kv = ldb_kv};
     366   322853727 :         TDB_DATA key = {
     367   180641746 :                 .dptr = ldb_key.data,
     368   180641746 :                 .dsize = ldb_key.length
     369             :         };
     370             :         int ret;
     371             : 
     372   262923480 :         if (tdb_transaction_active(ldb_kv->tdb) == false &&
     373   111449385 :             ldb_kv->read_lock_count == 0) {
     374           3 :                 return LDB_ERR_PROTOCOL_ERROR;
     375             :         }
     376             : 
     377   180641743 :         ret = tdb_parse_record(
     378   180641743 :             ldb_kv->tdb, key, ltdb_parse_record_wrapper, &kv_ctx);
     379   180641743 :         if (kv_ctx.parser_ret != LDB_SUCCESS) {
     380         147 :                 return kv_ctx.parser_ret;
     381   180641596 :         } else if (ret == 0) {
     382   144130734 :                 return LDB_SUCCESS;
     383             :         }
     384    32816855 :         return ltdb_err_map(tdb_error(ldb_kv->tdb));
     385             : }
     386             : 
     387       42286 : static int ltdb_iterate_range(struct ldb_kv_private *ldb_kv,
     388             :                               struct ldb_val start_key,
     389             :                               struct ldb_val end_key,
     390             :                               ldb_kv_traverse_fn fn,
     391             :                               void *ctx)
     392             : {
     393             :         /*
     394             :          * We do not implement this operation because we do not know how to
     395             :          * iterate from one key to the next (in a sorted fashion).
     396             :          *
     397             :          * We could mimic it potentially, but it would violate boundaries of
     398             :          * knowledge (data type representation).
     399             :          */
     400       42286 :         return LDB_ERR_OPERATIONS_ERROR;
     401             : }
     402             : 
     403           0 : static const char *ltdb_name(struct ldb_kv_private *ldb_kv)
     404             : {
     405           0 :         return tdb_name(ldb_kv->tdb);
     406             : }
     407             : 
     408    58372738 : static bool ltdb_changed(struct ldb_kv_private *ldb_kv)
     409             : {
     410    58372738 :         int seq = tdb_get_seqnum(ldb_kv->tdb);
     411    58372738 :         bool has_changed = (seq != ldb_kv->tdb_seqnum);
     412             : 
     413    58372738 :         ldb_kv->tdb_seqnum = seq;
     414             : 
     415    58372738 :         return has_changed;
     416             : }
     417             : 
     418     3745558 : static bool ltdb_transaction_active(struct ldb_kv_private *ldb_kv)
     419             : {
     420     3745558 :         return tdb_transaction_active(ldb_kv->tdb);
     421             : }
     422             : 
     423             : /*
     424             :  * Get an estimate of the number of records in a tdb database.
     425             :  *
     426             :  * This implementation will overestimate the number of records in a sparsely
     427             :  * populated database. The size estimate is only used for allocating
     428             :  * an in memory tdb to cache index records during a reindex, overestimating
     429             :  * the contents is acceptable, and preferable to underestimating
     430             :  */
     431             : #define RECORD_SIZE 500
     432           2 : static size_t ltdb_get_size(struct ldb_kv_private *ldb_kv)
     433             : {
     434           2 :         size_t map_size = tdb_map_size(ldb_kv->tdb);
     435           2 :         size_t size = map_size / RECORD_SIZE;
     436             : 
     437           2 :         return size;
     438             : }
     439             : 
     440             : /*
     441             :  * Start a sub transaction
     442             :  * As TDB does not currently support nested transactions, we do nothing and
     443             :  * return LDB_SUCCESS
     444             :  */
     445     1106768 : static int ltdb_nested_transaction_start(struct ldb_kv_private *ldb_kv)
     446             : {
     447     1106768 :         return LDB_SUCCESS;
     448             : }
     449             : 
     450             : /*
     451             :  * Commit a sub transaction
     452             :  * As TDB does not currently support nested transactions, we do nothing and
     453             :  * return LDB_SUCCESS
     454             :  */
     455     1098018 : static int ltdb_nested_transaction_commit(struct ldb_kv_private *ldb_kv)
     456             : {
     457     1098018 :         return LDB_SUCCESS;
     458             : }
     459             : 
     460             : /*
     461             :  * Cancel a sub transaction
     462             :  * As TDB does not currently support nested transactions, we do nothing and
     463             :  * return LDB_SUCCESS
     464             :  */
     465        8750 : static int ltdb_nested_transaction_cancel(struct ldb_kv_private *ldb_kv)
     466             : {
     467        8750 :         return LDB_SUCCESS;
     468             : }
     469             : 
     470             : static const struct kv_db_ops key_value_ops = {
     471             :         /* No support for any additional features */
     472             :         .options = 0,
     473             : 
     474             :         .store = ltdb_store,
     475             :         .delete = ltdb_delete,
     476             :         .iterate = ltdb_traverse_fn,
     477             :         .update_in_iterate = ltdb_update_in_iterate,
     478             :         .fetch_and_parse = ltdb_parse_record,
     479             :         .iterate_range = ltdb_iterate_range,
     480             :         .lock_read = ltdb_lock_read,
     481             :         .unlock_read = ltdb_unlock_read,
     482             :         .begin_write = ltdb_transaction_start,
     483             :         .prepare_write = ltdb_transaction_prepare_commit,
     484             :         .finish_write = ltdb_transaction_commit,
     485             :         .abort_write = ltdb_transaction_cancel,
     486             :         .error = ltdb_error,
     487             :         .errorstr = ltdb_errorstr,
     488             :         .name = ltdb_name,
     489             :         .has_changed = ltdb_changed,
     490             :         .transaction_active = ltdb_transaction_active,
     491             :         .get_size = ltdb_get_size,
     492             :         .begin_nested_write = ltdb_nested_transaction_start,
     493             :         .finish_nested_write = ltdb_nested_transaction_commit,
     494             :         .abort_nested_write = ltdb_nested_transaction_cancel,
     495             : };
     496             : 
     497             : /*
     498             :   connect to the database
     499             : */
     500      576188 : int ltdb_connect(struct ldb_context *ldb, const char *url,
     501             :                  unsigned int flags, const char *options[],
     502             :                  struct ldb_module **_module)
     503             : {
     504             :         const char *path;
     505             :         int tdb_flags, open_flags;
     506             :         struct ldb_kv_private *ldb_kv;
     507             : 
     508             :         /*
     509             :          * We hold locks, so we must use a private event context
     510             :          * on each returned handle
     511             :          */
     512      576188 :         ldb_set_require_private_event_context(ldb);
     513             : 
     514             :         /* parse the url */
     515      576188 :         if (strchr(url, ':')) {
     516      343845 :                 if (strncmp(url, "tdb://", 6) != 0) {
     517           0 :                         ldb_debug(ldb, LDB_DEBUG_ERROR,
     518             :                                   "Invalid tdb URL '%s'", url);
     519           0 :                         return LDB_ERR_OPERATIONS_ERROR;
     520             :                 }
     521      343845 :                 path = url+6;
     522             :         } else {
     523      223949 :                 path = url;
     524             :         }
     525             : 
     526      576188 :         tdb_flags = TDB_DEFAULT | TDB_SEQNUM | TDB_DISALLOW_NESTING;
     527             : 
     528             :         /* check for the 'nosync' option */
     529      576188 :         if (flags & LDB_FLG_NOSYNC) {
     530      569324 :                 tdb_flags |= TDB_NOSYNC;
     531             :         }
     532             : 
     533             :         /* and nommap option */
     534      576188 :         if (flags & LDB_FLG_NOMMAP) {
     535           0 :                 tdb_flags |= TDB_NOMMAP;
     536             :         }
     537             : 
     538      576188 :         ldb_kv = talloc_zero(ldb, struct ldb_kv_private);
     539      576188 :         if (!ldb_kv) {
     540           0 :                 ldb_oom(ldb);
     541           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     542             :         }
     543             : 
     544      576188 :         if (flags & LDB_FLG_RDONLY) {
     545             :                 /*
     546             :                  * This is weird, but because we can only have one tdb
     547             :                  * in this process, and the other one could be
     548             :                  * read-write, we can't use the tdb readonly.  Plus a
     549             :                  * read only tdb prohibits the all-record lock.
     550             :                  */
     551          36 :                 open_flags = O_RDWR;
     552             : 
     553          36 :                 ldb_kv->read_only = true;
     554             : 
     555      576152 :         } else if (flags & LDB_FLG_DONT_CREATE_DB) {
     556             :                 /*
     557             :                  * This is used by ldbsearch to prevent creation of the database
     558             :                  * if the name is wrong
     559             :                  */
     560      513298 :                 open_flags = O_RDWR;
     561             :         } else {
     562             :                 /*
     563             :                  * This is the normal case
     564             :                  */
     565       55529 :                 open_flags = O_CREAT | O_RDWR;
     566             :         }
     567             : 
     568      576188 :         ldb_kv->kv_ops = &key_value_ops;
     569             : 
     570      576188 :         errno = 0;
     571             :         /* note that we use quite a large default hash size */
     572      576188 :         ldb_kv->tdb = ltdb_wrap_open(ldb_kv,
     573             :                                      path,
     574             :                                      10000,
     575             :                                      tdb_flags,
     576             :                                      open_flags,
     577             :                                      ldb_get_create_perms(ldb),
     578             :                                      ldb);
     579      576188 :         if (!ldb_kv->tdb) {
     580         683 :                 ldb_asprintf_errstring(ldb,
     581         681 :                                        "Unable to open tdb '%s': %s", path, strerror(errno));
     582         683 :                 ldb_debug(ldb, LDB_DEBUG_ERROR,
     583         681 :                           "Unable to open tdb '%s': %s", path, strerror(errno));
     584         683 :                 talloc_free(ldb_kv);
     585         683 :                 if (errno == EACCES || errno == EPERM) {
     586           0 :                         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
     587             :                 }
     588         683 :                 return LDB_ERR_OPERATIONS_ERROR;
     589             :         }
     590             : 
     591      575505 :         return ldb_kv_init_store(
     592             :             ldb_kv, "ldb_tdb backend", ldb, options, _module);
     593             : }

Generated by: LCOV version 1.13