1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2012 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 // This module implements a generic system of settings
21 // (attribute-value) that can be associated with search targets. The
22 // system supports both default values, per-target overrides, and
33 #include <sys/types.h>
34 #include <yaz/dirent.h>
38 #include <libxml/parser.h>
39 #include <libxml/tree.h>
48 // Used for initializing setting_dictionary with pazpar2-specific settings
49 static char *hard_settings[] = {
70 "pz:negotiation_charset",
72 "pz:reuse_connections",
73 "pz:termlist_term_factor",
84 struct setting_dictionary
91 // This establishes the precedence of wildcard expressions
92 #define SETTING_WILDCARD_NO 0 // No wildcard
93 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
94 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
96 // Returns size of settings directory
97 int settings_num(struct conf_service *service)
99 return service->dictionary->num;
102 static int settings_lookup(struct conf_service *service, const char *name,
108 struct setting_dictionary *dictionary = service->dictionary;
112 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
113 maxlen = (p - name) + 1;
115 maxlen = strlen(name) + 1;
116 for (i = 0; i < dictionary->num; i++)
117 if (!strncmp(name, dictionary->dict[i], maxlen))
121 if (!strncmp("pz:", name, 3))
122 yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name);
123 if (dictionary->num + 1 > dictionary->size)
126 nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*));
127 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
128 dictionary->dict = tmp;
129 dictionary->size *= 2;
131 dictionary->dict[dictionary->num] = nmem_strdup(service->nmem, name);
132 dictionary->dict[dictionary->num][maxlen-1] = '\0';
133 return dictionary->num++;
136 int settings_create_offset(struct conf_service *service, const char *name)
138 return settings_lookup(service, name, 1);
141 int settings_lookup_offset(struct conf_service *service, const char *name)
143 return settings_lookup(service, name, 0);
146 char *settings_name(struct conf_service *service, int offset)
148 assert(offset < service->dictionary->num);
149 return service->dictionary->dict[offset];
152 static int isdir(const char *path)
156 if (stat(path, &st) < 0)
158 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
161 return st.st_mode & S_IFDIR;
164 // Read settings from an XML file, calling handler function for each setting
165 int settings_read_node_x(xmlNode *n,
167 void (*fun)(void *client_data,
168 struct setting *set))
170 int ret_val = 0; /* success */
171 char *namea = (char *) xmlGetProp(n, (xmlChar *) "name");
172 char *targeta = (char *) xmlGetProp(n, (xmlChar *) "target");
173 char *valuea = (char *) xmlGetProp(n, (xmlChar *) "value");
174 char *usera = (char *) xmlGetProp(n, (xmlChar *) "user");
175 char *precedencea = (char *) xmlGetProp(n, (xmlChar *) "precedence");
177 for (n = n->children; n; n = n->next)
179 if (n->type != XML_ELEMENT_NODE)
181 if (!strcmp((const char *) n->name, "set"))
184 char *name = (char *) xmlGetProp(n, (xmlChar *) "name");
185 char *target = (char *) xmlGetProp(n, (xmlChar *) "target");
186 char *value = (char *) xmlGetProp(n, (xmlChar *) "value");
187 char *user = (char *) xmlGetProp(n, (xmlChar *) "user");
188 char *precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
191 set.precedence = atoi((char *) precedence);
192 else if (precedencea)
193 set.precedence = atoi((char *) precedencea);
197 set.target = target ? target : targeta;
198 set.name = name ? name : namea;
199 set.value = value ? value : valuea;
202 if (set.name && set.value && set.target)
203 (*fun)(client_data, &set);
207 yaz_log(YLOG_WARN, "missing value and/or target for "
208 "setting name=%s", set.name);
210 yaz_log(YLOG_WARN, "missing name/value/target for setting");
221 yaz_log(YLOG_WARN, "Unknown element %s in settings file",
227 xmlFree(precedencea);
234 static int read_settings_file(const char *path,
236 void (*fun)(void *client_data,
237 struct setting *set))
239 xmlDoc *doc = xmlParseFile(path);
245 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
248 n = xmlDocGetRootElement(doc);
249 ret = settings_read_node_x(n, client_data, fun);
256 // Recursively read files or directories, invoking a
257 // callback for each one
258 static int read_settings(const char *path,
260 void (*fun)(void *client_data,
261 struct setting *set))
270 if (!(d = opendir(path)))
272 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
275 while ((de = readdir(d)))
278 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
280 sprintf(tmp, "%s/%s", path, de->d_name);
281 if (read_settings(tmp, client_data, fun))
286 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
287 ret = read_settings_file(path, client_data, fun);
291 // Determines if a ZURL is a wildcard, and what kind
292 static int zurl_wildcard(const char *zurl)
295 return SETTING_WILDCARD_NO;
297 return SETTING_WILDCARD_YES;
298 else if (*(zurl + strlen(zurl) - 1) == '*')
299 return SETTING_WILDCARD_DB;
301 return SETTING_WILDCARD_NO;
304 struct update_database_context {
306 struct conf_service *service;
309 void expand_settings_array(struct setting ***set_ar, int *num, int offset,
316 int i, n_num = offset + 10;
317 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
318 for (i = 0; i < *num; i++)
319 n_ar[i] = (*set_ar)[i];
320 for (; i < n_num; i++)
327 // This is called from grep_databases -- adds/overrides setting for a target
328 // This is also where the rules for precedence of settings are implemented
329 static void update_database_fun(void *context, struct database *db)
331 struct setting *set = ((struct update_database_context *)
333 struct conf_service *service = ((struct update_database_context *)
338 // Is this the right database?
339 if (!match_zurl(db->id, set->target))
342 offset = settings_create_offset(service, set->name);
343 expand_settings_array(&db->settings, &db->num_settings, offset,
346 // First we determine if this setting is overriding any existing settings
347 // with the same name.
348 assert(offset < db->num_settings);
349 for (sp = &db->settings[offset]; *sp; )
350 if (!strcmp((*sp)->name, set->name))
352 if ((*sp)->precedence < set->precedence)
354 // We discard the value (nmem keeps track of the space)
355 *sp = (*sp)->next; // unlink value from existing setting
357 else if ((*sp)->precedence > set->precedence)
359 // Db contains a higher-priority setting. Abort search
362 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
364 // target-specific value trumps wildcard. Delete.
365 *sp = (*sp)->next; // unlink.....
367 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
368 // Db already contains higher-priority setting. Abort search
375 if (!*sp) // is null when there are no higher-priority settings, so we add one
377 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
379 memset(new, 0, sizeof(*new));
380 new->precedence = set->precedence;
381 new->target = nmem_strdup(service->nmem, set->target);
382 new->name = nmem_strdup(service->nmem, set->name);
383 new->value = nmem_strdup(service->nmem, set->value);
384 new->next = db->settings[offset];
385 db->settings[offset] = new;
389 // Callback -- updates database records with dictionary entries as appropriate
390 // This is used in pass 2 to assign name/value pairs to databases
391 static void update_databases(void *client_data, struct setting *set)
393 struct conf_service *service = (struct conf_service *) client_data;
394 struct update_database_context context;
396 context.service = service;
397 predef_grep_databases(&context, service, update_database_fun);
400 // This simply copies the 'hard' (application-specific) settings
401 // to the settings dictionary.
402 static void initialize_hard_settings(struct conf_service *service)
404 struct setting_dictionary *dict = service->dictionary;
405 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
406 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
407 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
408 dict->num = dict->size;
411 // Read any settings names introduced in service definition (config) and add to dictionary
412 // This is done now to avoid errors if user settings are declared in session overrides
413 static void initialize_soft_settings(struct conf_service *service)
417 for (i = 0; i < service->num_metadata; i++)
419 struct conf_metadata *md = &service->metadata[i];
421 if (md->setting == Metadata_setting_no)
424 settings_create_offset(service, md->name);
428 static void prepare_target_dictionary(void *client_data, struct setting *set)
430 struct conf_service *service = (struct conf_service *) client_data;
432 // If target address is not wildcard, add the database
433 if (*set->target && !zurl_wildcard(set->target))
434 create_database_for_service(set->target, service);
437 void init_settings(struct conf_service *service)
439 struct setting_dictionary *new;
441 assert(service->nmem);
443 new = nmem_malloc(service->nmem, sizeof(*new));
444 memset(new, 0, sizeof(*new));
445 service->dictionary = new;
446 initialize_hard_settings(service);
447 initialize_soft_settings(service);
450 int settings_read_file(struct conf_service *service, const char *path,
454 return read_settings(path, service, prepare_target_dictionary);
456 return read_settings(path, service, update_databases);
459 int settings_read_node(struct conf_service *service, xmlNode *n,
463 return settings_read_node_x(n, service, prepare_target_dictionary);
465 return settings_read_node_x(n, service, update_databases);
471 * c-file-style: "Stroustrup"
472 * indent-tabs-mode: nil
474 * vim: shiftwidth=4 tabstop=8 expandtab