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",
74 "pz:termlist_term_count",
87 struct setting_dictionary
94 // This establishes the precedence of wildcard expressions
95 #define SETTING_WILDCARD_NO 0 // No wildcard
96 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
97 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
99 // Returns size of settings directory
100 int settings_num(struct conf_service *service)
102 return service->dictionary->num;
105 /* Find and possible create a new dictionary entry. Pass valid NMEM pointer if creation is allowed, otherwise null */
106 static int settings_index_lookup(struct setting_dictionary *dictionary, const char *name, NMEM nmem)
114 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
115 maxlen = (p - name) + 1;
117 maxlen = strlen(name) + 1;
118 for (i = 0; i < dictionary->num; i++)
119 if (!strncmp(name, dictionary->dict[i], maxlen))
123 if (!strncmp("pz:", name, 3))
124 yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name);
125 if (dictionary->num + 1 > dictionary->size)
128 nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
129 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
130 dictionary->dict = tmp;
131 dictionary->size *= 2;
133 dictionary->dict[dictionary->num] = nmem_strdup(nmem, name);
134 dictionary->dict[dictionary->num][maxlen-1] = '\0';
135 return dictionary->num++;
138 int settings_create_offset(struct conf_service *service, const char *name)
140 return settings_index_lookup(service->dictionary, name, service->nmem);
143 int settings_lookup_offset(struct conf_service *service, const char *name)
145 return settings_index_lookup(service->dictionary, name, 0);
148 char *settings_name(struct conf_service *service, int offset)
150 assert(offset < service->dictionary->num);
151 return service->dictionary->dict[offset];
155 // Apply a session override to a database
156 void service_apply_setting(struct conf_service *service, char *setting, char *value)
158 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
159 int offset = settings_create_offset(service, setting);
160 expand_settings_array(&service->settings->settings, &service->settings->num_settings, offset, service->nmem);
165 new->next = service->settings->settings[offset];
166 service->settings->settings[offset] = new;
170 static int isdir(const char *path)
174 if (stat(path, &st) < 0)
176 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
179 return st.st_mode & S_IFDIR;
182 // Read settings from an XML file, calling handler function for each setting
183 int settings_read_node_x(xmlNode *n,
185 void (*fun)(void *client_data,
186 struct setting *set))
188 int ret_val = 0; /* success */
189 char *namea = (char *) xmlGetProp(n, (xmlChar *) "name");
190 char *targeta = (char *) xmlGetProp(n, (xmlChar *) "target");
191 char *valuea = (char *) xmlGetProp(n, (xmlChar *) "value");
192 char *usera = (char *) xmlGetProp(n, (xmlChar *) "user");
193 char *precedencea = (char *) xmlGetProp(n, (xmlChar *) "precedence");
195 for (n = n->children; n; n = n->next)
197 if (n->type != XML_ELEMENT_NODE)
199 if (!strcmp((const char *) n->name, "set"))
202 char *name = (char *) xmlGetProp(n, (xmlChar *) "name");
203 char *target = (char *) xmlGetProp(n, (xmlChar *) "target");
204 char *value = (char *) xmlGetProp(n, (xmlChar *) "value");
205 char *user = (char *) xmlGetProp(n, (xmlChar *) "user");
206 char *precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
209 set.precedence = atoi((char *) precedence);
210 else if (precedencea)
211 set.precedence = atoi((char *) precedencea);
215 set.target = target ? target : targeta;
216 set.name = name ? name : namea;
217 set.value = value ? value : valuea;
220 if (set.name && set.value && set.target)
221 (*fun)(client_data, &set);
225 yaz_log(YLOG_WARN, "missing value and/or target for "
226 "setting name=%s", set.name);
228 yaz_log(YLOG_WARN, "missing name/value/target for setting");
239 yaz_log(YLOG_WARN, "Unknown element %s in settings file",
245 xmlFree(precedencea);
252 static int read_settings_file(const char *path,
254 void (*fun)(void *client_data,
255 struct setting *set))
257 xmlDoc *doc = xmlParseFile(path);
263 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
266 n = xmlDocGetRootElement(doc);
267 ret = settings_read_node_x(n, client_data, fun);
274 // Recursively read files or directories, invoking a
275 // callback for each one
276 static int read_settings(const char *path,
278 void (*fun)(void *client_data,
279 struct setting *set))
288 if (!(d = opendir(path)))
290 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
293 while ((de = readdir(d)))
296 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
298 sprintf(tmp, "%s/%s", path, de->d_name);
299 if (read_settings(tmp, client_data, fun))
304 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
305 ret = read_settings_file(path, client_data, fun);
309 // Determines if a ZURL is a wildcard, and what kind
310 static int zurl_wildcard(const char *zurl)
313 return SETTING_WILDCARD_NO;
315 return SETTING_WILDCARD_YES;
316 else if (*(zurl + strlen(zurl) - 1) == '*')
317 return SETTING_WILDCARD_DB;
319 return SETTING_WILDCARD_NO;
322 struct update_database_context {
324 struct conf_service *service;
327 void expand_settings_array(struct setting ***set_ar, int *num, int offset,
334 int i, n_num = offset + 10;
335 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
336 for (i = 0; i < *num; i++)
337 n_ar[i] = (*set_ar)[i];
338 for (; i < n_num; i++)
345 void expand_settings_array2(struct settings *settings, int offset, NMEM nmem)
349 if (offset >= settings->num_settings)
351 int i, n_num = offset + 10;
352 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
353 for (i = 0; i < settings->num_settings; i++)
354 n_ar[i] = settings->settings[i];
355 for (; i < n_num; i++)
357 settings->num_settings = n_num;
358 settings->settings = n_ar;
362 static void update_settings(struct setting *set, struct settings *settings, int offset, NMEM nmem)
365 yaz_log(YLOG_LOG, "update service settings offset %d with %s=%s", offset, set->name, set->value);
366 expand_settings_array2(settings, offset, nmem);
368 // First we determine if this setting is overriding any existing settings
369 // with the same name.
370 assert(offset < settings->num_settings);
371 for (sp = &settings->settings[offset]; *sp; )
372 if (!strcmp((*sp)->name, set->name))
374 if ((*sp)->precedence < set->precedence)
376 // We discard the value (nmem keeps track of the space)
377 *sp = (*sp)->next; // unlink value from existing setting
379 else if ((*sp)->precedence > set->precedence)
381 // Db contains a higher-priority setting. Abort search
384 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
386 // target-specific value trumps wildcard. Delete.
387 *sp = (*sp)->next; // unlink.....
389 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
390 // Db already contains higher-priority setting. Abort search
397 if (!*sp) // is null when there are no higher-priority settings, so we add one
399 struct setting *new = nmem_malloc(nmem, sizeof(*new));
400 memset(new, 0, sizeof(*new));
401 new->precedence = set->precedence;
402 new->target = nmem_strdup_null(nmem, set->target);
403 new->name = nmem_strdup_null(nmem, set->name);
404 new->value = nmem_strdup_null(nmem, set->value);
405 new->next = settings->settings[offset];
406 settings->settings[offset] = new;
411 // This is called from grep_databases -- adds/overrides setting for a target
412 // This is also where the rules for precedence of settings are implemented
413 static void update_database_fun(void *context, struct database *db)
415 struct setting *set = ((struct update_database_context *)
417 struct conf_service *service = ((struct update_database_context *)
422 // Is this the right database?
423 if (!match_zurl(db->id, set->target))
426 offset = settings_create_offset(service, set->name);
427 expand_settings_array(&db->settings, &db->num_settings, offset, service->nmem);
429 // First we determine if this setting is overriding any existing settings
430 // with the same name.
431 assert(offset < db->num_settings);
432 for (sp = &db->settings[offset]; *sp; )
433 if (!strcmp((*sp)->name, set->name))
435 if ((*sp)->precedence < set->precedence)
437 // We discard the value (nmem keeps track of the space)
438 *sp = (*sp)->next; // unlink value from existing setting
440 else if ((*sp)->precedence > set->precedence)
442 // Db contains a higher-priority setting. Abort search
445 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
447 // target-specific value trumps wildcard. Delete.
448 *sp = (*sp)->next; // unlink.....
450 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
451 // Db already contains higher-priority setting. Abort search
458 if (!*sp) // is null when there are no higher-priority settings, so we add one
460 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
462 memset(new, 0, sizeof(*new));
463 new->precedence = set->precedence;
464 new->target = nmem_strdup(service->nmem, set->target);
465 new->name = nmem_strdup(service->nmem, set->name);
466 new->value = nmem_strdup(service->nmem, set->value);
467 new->next = db->settings[offset];
468 db->settings[offset] = new;
472 // Callback -- updates database records with dictionary entries as appropriate
473 // This is used in pass 2 to assign name/value pairs to databases
474 static void update_databases(void *client_data, struct setting *set)
476 struct conf_service *service = (struct conf_service *) client_data;
477 struct update_database_context context;
479 context.service = service;
480 predef_grep_databases(&context, service, update_database_fun);
483 // This simply copies the 'hard' (application-specific) settings
484 // to the settings dictionary.
485 static void initialize_hard_settings(struct conf_service *service)
487 struct setting_dictionary *dict = service->dictionary;
488 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
489 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
490 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
491 dict->num = dict->size;
494 // Read any settings names introduced in service definition (config) and add to dictionary
495 // This is done now to avoid errors if user settings are declared in session overrides
496 void initialize_soft_settings(struct conf_service *service)
499 for (i = 0; i < service->num_metadata; i++)
501 struct conf_metadata *md = &service->metadata[i];
503 if (md->setting != Metadata_setting_no)
504 settings_create_offset(service, md->name);
506 // Also create setting for some metadata attributes.
509 WRBUF wrbuf = wrbuf_alloc();
510 yaz_log(YLOG_DEBUG, "Metadata %s has limitmap: %s ",md->name, md->limitmap);
511 wrbuf_printf(wrbuf, "pz:limitmap:%s", md->name);
512 index = settings_create_offset(service, wrbuf_cstr(wrbuf));
516 yaz_log(YLOG_DEBUG, "Service %s default %s=%s",
517 (service->id ? service->id: "unknown"), wrbuf_cstr(wrbuf), md->limitmap);
518 new.name = (char *) wrbuf_cstr(wrbuf);
519 new.value = md->limitmap;
523 offset = settings_create_offset(service, new.name);
524 update_settings(&new, service->settings, offset, service->nmem);
526 wrbuf_destroy(wrbuf);
527 // TODO same for facetmap
532 static void prepare_target_dictionary(void *client_data, struct setting *set)
534 struct conf_service *service = (struct conf_service *) client_data;
536 // If target address is not wildcard, add the database
537 if (*set->target && !zurl_wildcard(set->target))
538 create_database_for_service(set->target, service);
541 void init_settings(struct conf_service *service)
543 struct setting_dictionary *new;
545 assert(service->nmem);
547 new = nmem_malloc(service->nmem, sizeof(*new));
548 memset(new, 0, sizeof(*new));
549 service->dictionary = new;
550 initialize_hard_settings(service);
551 initialize_soft_settings(service);
554 int settings_read_file(struct conf_service *service, const char *path,
558 return read_settings(path, service, prepare_target_dictionary);
560 return read_settings(path, service, update_databases);
563 int settings_read_node(struct conf_service *service, xmlNode *n,
567 return settings_read_node_x(n, service, prepare_target_dictionary);
569 return settings_read_node_x(n, service, update_databases);
575 * c-file-style: "Stroustrup"
576 * indent-tabs-mode: nil
578 * vim: shiftwidth=4 tabstop=8 expandtab