xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/back-mdb/nextid.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: nextid.c,v 1.1.1.1 2014/05/28 09:58:50 tron 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-2014 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 "portable.h"
20 
21 #include <stdio.h>
22 #include <ac/string.h>
23 
24 #include "back-mdb.h"
25 
26 int mdb_next_id( BackendDB *be, MDB_cursor *mc, ID *out )
27 {
28 	struct mdb_info *mdb = (struct mdb_info *) be->be_private;
29 	int rc;
30 	ID id = 0;
31 	MDB_val key;
32 
33 	rc = mdb_cursor_get(mc, &key, NULL, MDB_LAST);
34 
35 	switch(rc) {
36 	case MDB_NOTFOUND:
37 		rc = 0;
38 		*out = 1;
39 		break;
40 	case 0:
41 		memcpy( &id, key.mv_data, sizeof( id ));
42 		*out = ++id;
43 		break;
44 
45 	default:
46 		Debug( LDAP_DEBUG_ANY,
47 			"=> mdb_next_id: get failed: %s (%d)\n",
48 			mdb_strerror(rc), rc, 0 );
49 		goto done;
50 	}
51 	mdb->mi_nextid = *out;
52 
53 done:
54 	return rc;
55 }
56