id2children.c 2.7 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
/* id2children.c - routines to deal with the id2children index */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
#include <stdio.h>
6
#include <ac/string.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
8
9

#include <ac/socket.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
10
11
12
13
14
15
16
17
18
19
20
#include "slap.h"
#include "back-ldbm.h"

int
id2children_add(
    Backend	*be,
    Entry	*p,
    Entry	*e
)
{
	struct dbcache	*db;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
21
	Datum		key;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
22
23
24
25
	int		len, rc;
	IDList		*idl;
	char		buf[20];

26
	ldbm_datum_init( key );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
27

28
29
	Debug( LDAP_DEBUG_TRACE, "=> id2children_add( %lu, %lu )\n",
	       p ? p->e_id : 0, e->e_id, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
31
32
33
34
35
36
37
38

	if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
	    LDBM_WRCREAT )) == NULL ) {
		Debug( LDAP_DEBUG_ANY,
		    "<= id2children_add -1 could not open \"id2children%s\"\n",
		    LDBM_SUFFIX, 0, 0 );
		return( -1 );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
	sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
41
42
43
	key.dptr = buf;
	key.dsize = strlen( buf ) + 1;

	if ( idl_insert_key( be, db, key, e->e_id ) != 0 ) {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
		Debug( LDAP_DEBUG_TRACE, "<= id2children_add -1 (idl_insert)\n",
		    0, 0, 0 );
		ldbm_cache_close( be, db );
		return( -1 );
	}

	ldbm_cache_close( be, db );

	Debug( LDAP_DEBUG_TRACE, "<= id2children_add 0\n", 0, 0, 0 );
	return( 0 );
}


int
id2children_remove(
    Backend	*be,
    Entry	*p,
    Entry	*e
)
{
	struct dbcache	*db;
	Datum		key;
	int		len, rc;
	IDList		*idl;
	char		buf[20];

70
	Debug( LDAP_DEBUG_TRACE, "=> id2children_remove( %lu, %lu )\n", p ? p->e_id
71
72
73
74
75
	    : 0, e->e_id, 0 );

	if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
	    LDBM_WRCREAT )) == NULL ) {
		Debug( LDAP_DEBUG_ANY,
76
		    "<= id2children_remove -1 could not open \"id2children%s\"\n",
77
78
79
80
		    LDBM_SUFFIX, 0, 0 );
		return( -1 );
	}

81
	ldbm_datum_init( key );
82
	sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
83
84
85
86
	key.dptr = buf;
	key.dsize = strlen( buf ) + 1;

	if ( idl_delete_key( be, db, key, e->e_id ) != 0 ) {
87
		Debug( LDAP_DEBUG_TRACE, "<= id2children_remove -1 (idl_delete)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
88
89
90
91
92
93
94
		    0, 0, 0 );
		ldbm_cache_close( be, db );
		return( -1 );
	}

	ldbm_cache_close( be, db );

95
	Debug( LDAP_DEBUG_TRACE, "<= id2children_remove 0\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
	return( 0 );
}

int
has_children(
    Backend	*be,
    Entry	*p
)
{
	struct dbcache	*db;
	Datum		key;
	int		rc;
	IDList		*idl;
	char		buf[20];

111
	ldbm_datum_init( key );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
112

113
	Debug( LDAP_DEBUG_TRACE, "=> has_children( %lu )\n", p->e_id , 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
114
115
116
117
118
119
120
121
122

	if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
	    LDBM_WRCREAT )) == NULL ) {
		Debug( LDAP_DEBUG_ANY,
		    "<= has_children -1 could not open \"id2children%s\"\n",
		    LDBM_SUFFIX, 0, 0 );
		return( 0 );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
123
	sprintf( buf, "%c%ld", EQ_PREFIX, p->e_id );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
124
125
126
127
128
129
130
131
132
133
134
135
	key.dptr = buf;
	key.dsize = strlen( buf ) + 1;

	idl = idl_fetch( be, db, key );

	ldbm_cache_close( be, db );
	rc = idl ? 1 : 0;
	idl_free( idl );

	Debug( LDAP_DEBUG_TRACE, "<= has_children %d\n", rc, 0, 0 );
	return( rc );
}