]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/id2children.c
171e6c2618dd27b13adb3117297994de3c0dcb13
[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         int             len, rc;
23         IDList          *idl;
24         char            buf[20];
25
26 #ifdef HAVE_BERKELEY_DB2
27         Datum           data;
28
29         ldbm_datum_init( key );
30         ldbm_datum_init( data );
31 #endif
32
33         Debug( LDAP_DEBUG_TRACE, "=> id2children_add( %lu, %lu )\n",
34                p ? p->e_id : 0, e->e_id, 0 );
35
36         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
37             LDBM_WRCREAT )) == NULL ) {
38                 Debug( LDAP_DEBUG_ANY,
39                     "<= id2children_add -1 could not open \"id2children%s\"\n",
40                     LDBM_SUFFIX, 0, 0 );
41                 return( -1 );
42         }
43
44         sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
45         key.dptr = buf;
46         key.dsize = strlen( buf ) + 1;
47
48         if ( idl_insert_key( be, db, key, e->e_id ) != 0 ) {
49                 Debug( LDAP_DEBUG_TRACE, "<= id2children_add -1 (idl_insert)\n",
50                     0, 0, 0 );
51                 ldbm_cache_close( be, db );
52                 return( -1 );
53         }
54
55         ldbm_cache_close( be, db );
56
57         Debug( LDAP_DEBUG_TRACE, "<= id2children_add 0\n", 0, 0, 0 );
58         return( 0 );
59 }
60
61
62 int
63 id2children_remove(
64     Backend     *be,
65     Entry       *p,
66     Entry       *e
67 )
68 {
69         struct dbcache  *db;
70         Datum           key;
71         int             len, rc;
72         IDList          *idl;
73         char            buf[20];
74
75         Debug( LDAP_DEBUG_TRACE, "=> id2children_remove( %lu, %lu )\n", p ? p->e_id
76             : 0, e->e_id, 0 );
77
78         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
79             LDBM_WRCREAT )) == NULL ) {
80                 Debug( LDAP_DEBUG_ANY,
81                     "<= id2children_remove -1 could not open \"id2children%s\"\n",
82                     LDBM_SUFFIX, 0, 0 );
83                 return( -1 );
84         }
85
86         ldbm_datum_init( key );
87         sprintf( buf, "%c%ld", EQ_PREFIX, p ? p->e_id : 0 );
88         key.dptr = buf;
89         key.dsize = strlen( buf ) + 1;
90
91         if ( idl_delete_key( be, db, key, e->e_id ) != 0 ) {
92                 Debug( LDAP_DEBUG_TRACE, "<= id2children_remove -1 (idl_insert)\n",
93                     0, 0, 0 );
94                 ldbm_cache_close( be, db );
95                 return( -1 );
96         }
97
98         ldbm_cache_close( be, db );
99
100         Debug( LDAP_DEBUG_TRACE, "<= id2children_remove 0\n", 0, 0, 0 );
101         return( 0 );
102 }
103
104 int
105 has_children(
106     Backend     *be,
107     Entry       *p
108 )
109 {
110         struct dbcache  *db;
111         Datum           key;
112         int             rc;
113         IDList          *idl;
114         char            buf[20];
115
116         ldbm_datum_init( key );
117
118         Debug( LDAP_DEBUG_TRACE, "=> has_children( %lu )\n", p->e_id , 0, 0 );
119
120         if ( (db = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
121             LDBM_WRCREAT )) == NULL ) {
122                 Debug( LDAP_DEBUG_ANY,
123                     "<= has_children -1 could not open \"id2children%s\"\n",
124                     LDBM_SUFFIX, 0, 0 );
125                 return( 0 );
126         }
127
128         sprintf( buf, "%c%ld", EQ_PREFIX, p->e_id );
129         key.dptr = buf;
130         key.dsize = strlen( buf ) + 1;
131
132         idl = idl_fetch( be, db, key );
133
134         ldbm_cache_close( be, db );
135         rc = idl ? 1 : 0;
136         idl_free( idl );
137
138         Debug( LDAP_DEBUG_TRACE, "<= has_children %d\n", rc, 0, 0 );
139         return( rc );
140 }