]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/nextid.c
Happy New Year
[openldap] / servers / slapd / back-bdb / nextid.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2018 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 int bdb_next_id( BackendDB *be, ID *out )
25 {
26         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
27
28         ldap_pvt_thread_mutex_lock( &bdb->bi_lastid_mutex );
29         *out = ++bdb->bi_lastid;
30         ldap_pvt_thread_mutex_unlock( &bdb->bi_lastid_mutex );
31
32         return 0;
33 }
34
35 int bdb_last_id( BackendDB *be, DB_TXN *tid )
36 {
37         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
38         int rc;
39         ID id = 0;
40         unsigned char idbuf[sizeof(ID)];
41         DBT key, data;
42         DBC *cursor;
43
44         DBTzero( &key );
45         key.flags = DB_DBT_USERMEM;
46         key.data = (char *) idbuf;
47         key.ulen = sizeof( idbuf );
48
49         DBTzero( &data );
50         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
51
52         /* Get a read cursor */
53         rc = bdb->bi_id2entry->bdi_db->cursor( bdb->bi_id2entry->bdi_db,
54                 tid, &cursor, 0 );
55
56         if (rc == 0) {
57                 rc = cursor->c_get(cursor, &key, &data, DB_LAST);
58                 cursor->c_close(cursor);
59         }
60
61         switch(rc) {
62         case DB_NOTFOUND:
63                 rc = 0;
64                 break;
65         case 0:
66                 BDB_DISK2ID( idbuf, &id );
67                 break;
68
69         default:
70                 Debug( LDAP_DEBUG_ANY,
71                         "=> bdb_last_id: get failed: %s (%d)\n",
72                         db_strerror(rc), rc, 0 );
73                 goto done;
74         }
75
76         bdb->bi_lastid = id;
77
78 done:
79         return rc;
80 }