]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2children.c
UNDO LAST COMMIT.
[openldap] / servers / slapd / back-ldbm / id2children.c
1 /* id2children.c - routines to deal with the id2children index */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #include <ac/string.h>
7
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12
13 int
14 id2children_add(
15     Backend     *be,
16     Entry       *p,
17     Entry       *e
18 )
19 {
20         struct dbcache  *db;
21         Datum           key;
22         char            buf[20];
23
24         ldbm_datum_init( key );
25
26         Debug( LDAP_DEBUG_TRACE, "=> id2children_add( %ld, %ld )\n",
27                p ? p->e_id : 0, e->e_id, 0 );
28
29         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
30             LDBM_WRCREAT )) == NULL ) {
31                 Debug( LDAP_DEBUG_ANY,
32                     "<= id2children_add -1 could not open \"id2children%s\"\n",
33                     LDBM_SUFFIX, 0, 0 );
34                 return( -1 );
35         }
36
37         sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
38         key.dptr = buf;
39         key.dsize = strlen( buf ) + 1;
40
41         if ( idl_insert_key( be, db, key, e->e_id ) != 0 ) {
42                 Debug( LDAP_DEBUG_TRACE, "<= id2children_add -1 (idl_insert)\n",
43                     0, 0, 0 );
44                 ldbm_cache_close( be, db );
45                 return( -1 );
46         }
47
48         ldbm_cache_close( be, db );
49
50         Debug( LDAP_DEBUG_TRACE, "<= id2children_add 0\n", 0, 0, 0 );
51         return( 0 );
52 }
53
54
55 int
56 id2children_remove(
57     Backend     *be,
58     Entry       *p,
59     Entry       *e
60 )
61 {
62         struct dbcache  *db;
63         Datum           key;
64         char            buf[20];
65
66         Debug( LDAP_DEBUG_TRACE, "=> id2children_remove( %ld, %ld )\n", p ? p->e_id
67             : 0, e->e_id, 0 );
68
69         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
70             LDBM_WRCREAT )) == NULL ) {
71                 Debug( LDAP_DEBUG_ANY,
72                     "<= id2children_remove -1 could not open \"id2children%s\"\n",
73                     LDBM_SUFFIX, 0, 0 );
74                 return( -1 );
75         }
76
77         ldbm_datum_init( key );
78         sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
79         key.dptr = buf;
80         key.dsize = strlen( buf ) + 1;
81
82         if ( idl_delete_key( be, db, key, e->e_id ) != 0 ) {
83                 Debug( LDAP_DEBUG_TRACE, "<= id2children_remove -1 (idl_delete)\n",
84                     0, 0, 0 );
85                 ldbm_cache_close( be, db );
86                 return( -1 );
87         }
88
89         ldbm_cache_close( be, db );
90
91         Debug( LDAP_DEBUG_TRACE, "<= id2children_remove 0\n", 0, 0, 0 );
92         return( 0 );
93 }
94
95 int
96 has_children(
97     Backend     *be,
98     Entry       *p
99 )
100 {
101         struct dbcache  *db;
102         Datum           key;
103         int             rc = 0;
104         ID_BLOCK                *idl;
105         char            buf[20];
106
107         ldbm_datum_init( key );
108
109         Debug( LDAP_DEBUG_TRACE, "=> has_children( %ld )\n", p->e_id , 0, 0 );
110
111         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
112             LDBM_WRCREAT )) == NULL ) {
113                 Debug( LDAP_DEBUG_ANY,
114                     "<= has_children -1 could not open \"id2children%s\"\n",
115                     LDBM_SUFFIX, 0, 0 );
116                 return( 0 );
117         }
118
119         sprintf( buf, "%c%ld", EQ_PREFIX, p->e_id );
120         key.dptr = buf;
121         key.dsize = strlen( buf ) + 1;
122
123         idl = idl_fetch( be, db, key );
124
125         ldbm_cache_close( be, db );
126
127         if( idl != NULL ) {
128                 idl_free( idl );
129                 rc = 1;
130         }
131
132         Debug( LDAP_DEBUG_TRACE, "<= has_children( %ld ): %s\n",
133                 p->e_id, rc ? "yes" : "no", 0 );
134         return( rc );
135 }