]> git.sur5r.net Git - openldap/blob - libraries/librewrite/params.c
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / libraries / librewrite / params.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2000-2013 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENT:
16  * This work was initially developed by Pierangelo Masarati for
17  * inclusion in OpenLDAP Software.
18  */
19
20 #include <portable.h>
21
22 #include "rewrite-int.h"
23
24 /*
25  * Defines and inits a variable with global scope
26  */
27 int
28 rewrite_param_set(
29                 struct rewrite_info *info,
30                 const char *name,
31                 const char *value
32 )
33 {
34         struct rewrite_var *var;
35         int rc = REWRITE_SUCCESS;
36
37         assert( info != NULL );
38         assert( name != NULL );
39         assert( value != NULL );
40
41 #ifdef USE_REWRITE_LDAP_PVT_THREADS
42         ldap_pvt_thread_rdwr_wlock( &info->li_params_mutex );
43 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
44
45         var = rewrite_var_find( info->li_params, name );
46         if ( var != NULL ) {
47                 assert( var->lv_value.bv_val != NULL );
48                 free( var->lv_value.bv_val );
49                 var->lv_value.bv_val = strdup( value );
50                 var->lv_value.bv_len = strlen( value );
51
52         } else {
53                 var = rewrite_var_insert( &info->li_params, name, value );
54         }
55
56         if ( var == NULL || var->lv_value.bv_val == NULL ) {
57                 rc = REWRITE_ERR;
58         }
59         
60 #ifdef USE_REWRITE_LDAP_PVT_THREADS
61         ldap_pvt_thread_rdwr_wunlock( &info->li_params_mutex );
62 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
63
64         return rc;
65 }
66
67 /*
68  * Gets a var with global scope
69  */
70 int
71 rewrite_param_get(
72                 struct rewrite_info *info,
73                 const char *name,
74                 struct berval *value
75 )
76 {
77         struct rewrite_var *var;
78         int rc = REWRITE_SUCCESS;
79
80         assert( info != NULL );
81         assert( name != NULL );
82         assert( value != NULL );
83
84         value->bv_val = NULL;
85         value->bv_len = 0;
86         
87 #ifdef USE_REWRITE_LDAP_PVT_THREADS
88         ldap_pvt_thread_rdwr_rlock( &info->li_params_mutex );
89 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
90         
91         var = rewrite_var_find( info->li_params, name );
92         if ( var != NULL ) {
93                 value->bv_val = strdup( var->lv_value.bv_val );
94                 value->bv_len = var->lv_value.bv_len;
95         }
96
97         if ( var == NULL || value->bv_val == NULL ) {
98                 rc = REWRITE_ERR;
99         }
100         
101 #ifdef USE_REWRITE_LDAP_PVT_THREADS
102         ldap_pvt_thread_rdwr_runlock( &info->li_params_mutex );
103 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
104
105         return REWRITE_SUCCESS;
106 }
107
108 static void
109 rewrite_param_free(
110                 void *tmp
111 )
112 {
113         struct rewrite_var *var = ( struct rewrite_var * )tmp;
114         assert( var != NULL );
115
116         assert( var->lv_name != NULL );
117         assert( var->lv_value.bv_val != NULL );
118
119         free( var->lv_name );
120         free( var->lv_value.bv_val );
121         free( var );
122 }
123
124 /*
125  * Destroys the parameter tree
126  */
127 int
128 rewrite_param_destroy(
129                 struct rewrite_info *info
130 )
131 {
132         int count;
133
134         assert( info != NULL );
135         
136 #ifdef USE_REWRITE_LDAP_PVT_THREADS
137         ldap_pvt_thread_rdwr_wlock( &info->li_params_mutex );
138 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
139         
140         count = avl_free( info->li_params, rewrite_param_free );
141         info->li_params = NULL;
142
143 #ifdef USE_REWRITE_LDAP_PVT_THREADS
144         ldap_pvt_thread_rdwr_wunlock( &info->li_params_mutex );
145 #endif /* USE_REWRITE_LDAP_PVT_THREADS */
146
147         return REWRITE_SUCCESS;
148 }
149