xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-mdb/nextid.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: nextid.c,v 1.3 2021/08/14 16:15:00 christos Exp $	*/
2 
3 /* init.c - initialize mdb backend */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6  *
7  * Copyright 2000-2021 The OpenLDAP Foundation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 
19 #include <sys/cdefs.h>
20 __RCSID("$NetBSD: nextid.c,v 1.3 2021/08/14 16:15:00 christos Exp $");
21 
22 #include "portable.h"
23 
24 #include <stdio.h>
25 #include <ac/string.h>
26 
27 #include "back-mdb.h"
28 
mdb_next_id(BackendDB * be,MDB_cursor * mc,ID * out)29 int mdb_next_id( BackendDB *be, MDB_cursor *mc, ID *out )
30 {
31 	struct mdb_info *mdb = (struct mdb_info *) be->be_private;
32 	int rc;
33 	ID id = 0;
34 	MDB_val key;
35 
36 	rc = mdb_cursor_get(mc, &key, NULL, MDB_LAST);
37 
38 	switch(rc) {
39 	case MDB_NOTFOUND:
40 		rc = 0;
41 		*out = 1;
42 		break;
43 	case 0:
44 		memcpy( &id, key.mv_data, sizeof( id ));
45 		*out = ++id;
46 		break;
47 
48 	default:
49 		Debug( LDAP_DEBUG_ANY,
50 			"=> mdb_next_id: get failed: %s (%d)\n",
51 			mdb_strerror(rc), rc );
52 		goto done;
53 	}
54 	mdb->mi_nextid = *out;
55 
56 done:
57 	return rc;
58 }
59