1 /* idl.c - ldap id list handling routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
15 #define IDL_CMP(x,y) ( x < y ? -1 : ( x > y ? 1 : 0 ) )
17 static int idl_search( ID *ids, ID id )
20 * binary search of id in ids
21 * if found, returns position of id
22 * if not found, returns first postion greater than id
31 cursor = base + pivot;
32 val = IDL_CMP( id, ids[cursor + 1] );
37 } else if ( val > 0 ) {
53 static int idl_insert( ID *ids, ID id )
55 int x = idl_search( ids, id );
67 } else if ( ++ids[0] >= BDB_IDL_MAX ) {
72 AC_MEMCPY( &ids[x+1], &ids[x], (ids[0]-x) * sizeof(ID) );
80 static int idl_delete( ID *ids, ID id )
82 int x = idl_search( ids, id );
84 if( x == 0 || ids[x] != id ) {
88 } else if ( --ids[0] == 0 ) {
89 if( x != 1 ) return -1;
92 AC_MEMCPY( &ids[x], &ids[x+1], (1+ids[0]-x) * sizeof(ID) );
107 ID ids[BDB_IDL_SIZE];
110 assert( id != NOID );
113 data.ulen = sizeof( ids );
114 data.flags = DB_DBT_USERMEM;
116 /* fetch the key and grab a write lock */
117 rc = db->get( db, tid, key, &data, DB_RMW );
119 if( rc == DB_NOTFOUND ) {
122 data.size = 2 * sizeof( ID );
124 } else if ( rc != 0 ) {
127 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
128 /* size not multiple of ID size */
131 } else if ( BDB_IS_ALLIDS(ids) ) {
134 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
139 rc = idl_insert( ids, id );
141 if( rc != 0 ) return rc;
143 data.size = (ids[0]+1) * sizeof( ID );
147 rc = db->put( db, tid, key, &data, 0 );
161 ID ids[BDB_IDL_SIZE];
164 assert( id != NOID );
167 data.ulen = sizeof( ids );
168 data.flags = DB_DBT_USERMEM;
170 /* fetch the key and grab a write lock */
171 rc = db->get( db, tid, key, &data, DB_RMW );
176 } else if ( data.size == 0 || data.size % sizeof( ID ) ) {
177 /* size not multiple of ID size */
180 } else if ( BDB_IS_ALLIDS(ids) ) {
183 } else if ( data.size != (1 + ids[0]) * sizeof( ID ) ) {
188 rc = idl_delete( ids, id );
190 if( rc != 0 ) return rc;
192 if( BDB_IS_ALLIDS(ids) ) {
194 rc = db->del( db, tid, key, 0 );
198 data.size = (ids[0]+1) * sizeof( ID );
202 rc = db->put( db, tid, key, &data, 0 );