]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/attr.c
Use #ifdef, not #if
[openldap] / servers / slapd / back-bdb2 / attr.c
1 /* attr.c - backend routines for dealing with attributes */
2 /* $OpenLDAP$ */
3
4 #include "portable.h"
5
6 #include <stdio.h>
7
8 #include <ac/socket.h>
9 #include <ac/string.h>
10
11 #include "slap.h"
12 #include "back-bdb2.h"
13
14 static int
15 ainfo_type_cmp(
16     char                *type,
17     struct attrinfo     *a
18 )
19 {
20         return( strcasecmp( type, a->ai_type ) );
21 }
22
23 static int
24 ainfo_cmp(
25     struct attrinfo     *a,
26     struct attrinfo     *b
27 )
28 {
29         return( strcasecmp( a->ai_type, b->ai_type ) );
30 }
31
32 /*
33  * Called when a duplicate "index" line is encountered.
34  *
35  * returns 1 => original from init code, indexmask updated
36  *         2 => original not from init code, warn the user
37  */
38
39 static int
40 ainfo_dup(
41     struct attrinfo     *a,
42     struct attrinfo     *b
43 )
44 {
45         /*
46          * if the duplicate definition is because we initialized the attr,
47          * just add what came from the config file. otherwise, complain.
48          */
49         if ( a->ai_indexmask & INDEX_FROMINIT ) {
50                 a->ai_indexmask |= b->ai_indexmask;
51
52                 return( 1 );
53         }
54
55         return( 2 );
56 }
57
58 void
59 bdb2i_attr_masks(
60     struct ldbminfo     *li,
61     char                *type,
62     int                 *indexmask,
63     int                 *syntaxmask
64 )
65 {
66         struct attrinfo *a;
67
68         *indexmask = 0;
69         *syntaxmask = 0;
70         if ( (a = (struct attrinfo *) avl_find( li->li_attrs, type,
71             (AVL_CMP) ainfo_type_cmp )) == NULL ) {
72                 if ( (a = (struct attrinfo *) avl_find( li->li_attrs, "default",
73                     (AVL_CMP) ainfo_type_cmp )) == NULL ) {
74                         return;
75                 }
76         }
77         *indexmask = a->ai_indexmask;
78         if ( strcasecmp( a->ai_type, "default" ) == 0 ) {
79                 *syntaxmask = attr_syntax( type );
80         } else {
81                 *syntaxmask = a->ai_syntaxmask;
82         }
83 }
84
85
86 /*  BDB2 changed  */
87 void
88 bdb2i_attr_index_config(
89     struct ldbminfo     *li,
90     const char          *fname,
91     int                 lineno,
92     int                 argc,
93     char                **argv,
94     int                 init
95 )
96 {
97         int             i, j;
98         char            **attrs, **indexes;
99         struct attrinfo *a;
100
101         attrs = str2charray( argv[0], "," );
102         if ( argc > 1 ) {
103                 indexes = str2charray( argv[1], "," );
104         }
105         for ( i = 0; attrs[i] != NULL; i++ ) {
106                 a = (struct attrinfo *) ch_malloc( sizeof(struct attrinfo) );
107                 a->ai_type = ch_strdup( attrs[i] );
108                 a->ai_syntaxmask = attr_syntax( a->ai_type );
109                 if ( argc == 1 ) {
110                         a->ai_indexmask = (INDEX_PRESENCE | INDEX_EQUALITY |
111                             INDEX_APPROX | INDEX_SUB);
112                 } else {
113                         a->ai_indexmask = 0;
114                         for ( j = 0; indexes[j] != NULL; j++ ) {
115                                 if ( strncasecmp( indexes[j], "pres", 4 )
116                                     == 0 ) {
117                                         a->ai_indexmask |= INDEX_PRESENCE;
118                                 } else if ( strncasecmp( indexes[j], "eq", 2 )
119                                     == 0 ) {
120                                         a->ai_indexmask |= INDEX_EQUALITY;
121                                 } else if ( strncasecmp( indexes[j], "approx",
122                                     6 ) == 0 ) {
123                                         a->ai_indexmask |= INDEX_APPROX;
124                                 } else if ( strncasecmp( indexes[j], "sub", 3 )
125                                     == 0 ) {
126                                         a->ai_indexmask |= INDEX_SUB;
127                                 } else if ( strncasecmp( indexes[j], "none", 4 )
128                                     == 0 ) {
129                                         if ( a->ai_indexmask != 0 ) {
130                                                 fprintf( stderr,
131 "%s: line %d: index type \"none\" cannot be combined with other types\n",
132                                                     fname, lineno );
133                                         }
134                                         a->ai_indexmask = 0;
135                                 } else {
136                                         fprintf( stderr,
137                         "%s: line %d: unknown index type \"%s\" (ignored)\n",
138                                             fname, lineno, indexes[j] );
139                                         fprintf( stderr,
140         "valid index types are \"pres\", \"eq\", \"approx\", or \"sub\"\n" );
141                                 }
142                         }
143                 }
144                 if ( init ) {
145                         a->ai_indexmask |= INDEX_FROMINIT;
146                 } else {
147                         if ( a->ai_indexmask )
148                                 bdb2i_txn_attr_config( li, a->ai_type, 0 );
149                 }
150
151                 switch (avl_insert( &li->li_attrs, (caddr_t) a,
152                         (AVL_CMP) ainfo_cmp, (AVL_DUP) ainfo_dup ))
153                 {
154                 case 1:         /* duplicate - updating init version */
155                         free( a->ai_type );
156                         free( (char *) a );
157                         break;
158
159                 case 2:         /* user duplicate - ignore and warn */
160                         fprintf( stderr,
161     "%s: line %d: duplicate index definition for attr \"%s\" (ignored)\n",
162                             fname, lineno, a->ai_type );
163                         free( a->ai_type );
164                         free( (char *) a );
165                         break;
166
167                 default:;       /* inserted ok */
168                         /* FALL */
169                 }
170         }
171         charray_free( attrs );
172         if ( argc > 1 )
173                 charray_free( indexes );
174 }