]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/cr.c
Don't bother setting up threads if there are no indexed attrs
[openldap] / servers / slapd / cr.c
index 6e33b5a7280515ff1f9089a79cc764c6ea245676..366246e1231b2c7d0787e455a1fc5c9e75d95ece 100644 (file)
@@ -1,8 +1,17 @@
 /* cr.c - content rule routines */
 /* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2006 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
  */
 
 #include "portable.h"
@@ -14,7 +23,6 @@
 #include <ac/socket.h>
 
 #include "slap.h"
-#include "ldap_pvt.h"
 
 struct cindexrec {
        struct berval   cir_name;
@@ -22,8 +30,8 @@ struct cindexrec {
 };
 
 static Avlnode *cr_index = NULL;
-static LDAP_SLIST_HEAD(CRList, slap_content_rule) cr_list
-       = LDAP_SLIST_HEAD_INITIALIZER(&cr_list);
+static LDAP_STAILQ_HEAD(CRList, slap_content_rule) cr_list
+       = LDAP_STAILQ_HEAD_INITIALIZER(cr_list);
 
 static int
 cr_index_cmp(
@@ -95,9 +103,9 @@ cr_destroy( void )
 
        avl_free(cr_index, ldap_memfree);
 
-       while( !LDAP_SLIST_EMPTY(&cr_list) ) {
-               c = LDAP_SLIST_FIRST(&cr_list);
-               LDAP_SLIST_REMOVE_HEAD(&cr_list, scr_next);
+       while( !LDAP_STAILQ_EMPTY(&cr_list) ) {
+               c = LDAP_STAILQ_FIRST(&cr_list);
+               LDAP_STAILQ_REMOVE_HEAD(&cr_list, scr_next);
 
                cr_destroy_one( c );
        }
@@ -112,9 +120,6 @@ cr_insert(
        struct cindexrec        *cir;
        char                    **names;
 
-       LDAP_SLIST_NEXT( scr, scr_next ) = NULL;
-       LDAP_SLIST_INSERT_HEAD(&cr_list, scr, scr_next);
-
        if ( scr->scr_oid ) {
                cir = (struct cindexrec *)
                        ch_calloc( 1, sizeof(struct cindexrec) );
@@ -122,8 +127,8 @@ cr_insert(
                cir->cir_name.bv_len = strlen( scr->scr_oid );
                cir->cir_cr = scr;
 
-               assert( cir->cir_name.bv_val );
-               assert( cir->cir_cr );
+               assert( cir->cir_name.bv_val != NULL );
+               assert( cir->cir_cr != NULL );
 
                if ( avl_insert( &cr_index, (caddr_t) cir,
                                 cr_index_cmp, avl_dup_error ) )
@@ -145,8 +150,8 @@ cr_insert(
                        cir->cir_name.bv_len = strlen( *names );
                        cir->cir_cr = scr;
 
-                       assert( cir->cir_name.bv_val );
-                       assert( cir->cir_cr );
+                       assert( cir->cir_name.bv_val != NULL );
+                       assert( cir->cir_cr != NULL );
 
                        if ( avl_insert( &cr_index, (caddr_t) cir,
                                         cr_index_cmp, avl_dup_error ) )
@@ -163,6 +168,8 @@ cr_insert(
                }
        }
 
+       LDAP_STAILQ_INSERT_TAIL(&cr_list, scr, scr_next);
+
        return 0;
 }
 
@@ -326,12 +333,14 @@ int
 cr_add(
     LDAPContentRule    *cr,
        int user,
+       ContentRule **rscr,
     const char         **err
 )
 {
        ContentRule     *scr;
        int             code;
        int             op = 0;
+       char    *oidm = NULL;
 
        if ( cr->cr_names != NULL ) {
                int i;
@@ -351,7 +360,7 @@ cr_add(
                        return SLAP_SCHERR_OIDM;
                }
                if ( oid != cr->cr_oid ) {
-                       ldap_memfree( cr->cr_oid );
+                       oidm = cr->cr_oid;
                        cr->cr_oid = oid;
                }
        }
@@ -359,6 +368,7 @@ cr_add(
        scr = (ContentRule *) ch_calloc( 1, sizeof(ContentRule) );
        AC_MEMCPY( &scr->scr_crule, cr, sizeof(LDAPContentRule) );
 
+       scr->scr_oidmacro = oidm;
        scr->scr_sclass = oc_find(cr->cr_oid);
        if ( !scr->scr_sclass ) {
                *err = cr->cr_oid;
@@ -391,9 +401,68 @@ cr_add(
        }
 
        code = cr_insert(scr,err);
+       if ( code == 0 && rscr )
+               *rscr = scr;
        return code;
 }
 
+void
+cr_unparse( BerVarray *res, ContentRule *start, ContentRule *end, int sys )
+{
+       ContentRule *cr;
+       int i, num;
+       struct berval bv, *bva = NULL, idx;
+       char ibuf[32];
+
+       if ( !start )
+               start = LDAP_STAILQ_FIRST( &cr_list );
+
+       /* count the result size */
+       i = 0;
+       for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
+               if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
+               i++;
+               if ( cr == end ) break;
+       }
+       if (!i) return;
+
+       num = i;
+       bva = ch_malloc( (num+1) * sizeof(struct berval) );
+       BER_BVZERO( bva );
+       idx.bv_val = ibuf;
+       if ( sys ) {
+               idx.bv_len = 0;
+               ibuf[0] = '\0';
+       }
+       i = 0;
+       for ( cr=start; cr; cr=LDAP_STAILQ_NEXT(cr, scr_next)) {
+               LDAPContentRule lcr, *lcrp;
+               if ( sys && !(cr->scr_flags & SLAP_CR_HARDCODE)) continue;
+               if ( cr->scr_oidmacro ) {
+                       lcr = cr->scr_crule;
+                       lcr.cr_oid = cr->scr_oidmacro;
+                       lcrp = &lcr;
+               } else {
+                       lcrp = &cr->scr_crule;
+               }
+               if ( ldap_contentrule2bv( lcrp, &bv ) == NULL ) {
+                       ber_bvarray_free( bva );
+               }
+               if ( !sys ) {
+                       idx.bv_len = sprintf(idx.bv_val, "{%d}", i);
+               }
+               bva[i].bv_len = idx.bv_len + bv.bv_len;
+               bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
+               strcpy( bva[i].bv_val, ibuf );
+               strcpy( bva[i].bv_val + idx.bv_len, bv.bv_val );
+               i++;
+               bva[i].bv_val = NULL;
+               ldap_memfree( bv.bv_val );
+               if ( cr == end ) break;
+       }
+       *res = bva;
+}
+
 int
 cr_schema_info( Entry *e )
 {
@@ -404,7 +473,7 @@ cr_schema_info( Entry *e )
        struct berval   val;
        struct berval   nval;
 
-       LDAP_SLIST_FOREACH(cr, &cr_list, scr_next) {
+       LDAP_STAILQ_FOREACH(cr, &cr_list, scr_next) {
                if ( ldap_contentrule2bv( &cr->scr_crule, &val ) == NULL ) {
                        return -1;
                }