1 /* This file is part of Pazpar2.
2 Copyright (C) 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",
85 "pz:authentication_mode",
92 struct setting_dictionary
99 // This establishes the precedence of wildcard expressions
100 #define SETTING_WILDCARD_NO 0 // No wildcard
101 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
102 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
104 // Returns size of settings directory
105 int settings_num(struct conf_service *service)
107 return service->dictionary->num;
110 /* Find and possible create a new dictionary entry. Pass valid NMEM pointer if creation is allowed, otherwise null */
111 static int settings_index_lookup(struct setting_dictionary *dictionary, const char *name, NMEM nmem)
119 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
120 maxlen = (p - name) + 1;
122 maxlen = strlen(name) + 1;
123 for (i = 0; i < dictionary->num; i++)
124 if (!strncmp(name, dictionary->dict[i], maxlen))
128 if (!strncmp("pz:", name, 3))
129 yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name);
130 if (dictionary->num + 1 > dictionary->size)
133 nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
134 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
135 dictionary->dict = tmp;
136 dictionary->size *= 2;
138 dictionary->dict[dictionary->num] = nmem_strdup(nmem, name);
139 dictionary->dict[dictionary->num][maxlen-1] = '\0';
140 return dictionary->num++;
143 int settings_create_offset(struct conf_service *service, const char *name)
145 return settings_index_lookup(service->dictionary, name, service->nmem);
148 int settings_lookup_offset(struct conf_service *service, const char *name)
150 return settings_index_lookup(service->dictionary, name, 0);
153 char *settings_name(struct conf_service *service, int offset)
155 assert(offset < service->dictionary->num);
156 return service->dictionary->dict[offset];
160 // Apply a session override to a database
161 void service_apply_setting(struct conf_service *service, char *setting, char *value)
163 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
164 int offset = settings_create_offset(service, setting);
165 expand_settings_array(&service->settings->settings, &service->settings->num_settings, offset, service->nmem);
170 new->next = service->settings->settings[offset];
171 service->settings->settings[offset] = new;
175 static int isdir(const char *path)
179 if (stat(path, &st) < 0)
181 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
184 return st.st_mode & S_IFDIR;
187 // Read settings from an XML file, calling handler function for each setting
188 int settings_read_node_x(xmlNode *n,
190 void (*fun)(void *client_data,
191 struct setting *set))
193 int ret_val = 0; /* success */
194 char *namea = (char *) xmlGetProp(n, (xmlChar *) "name");
195 char *targeta = (char *) xmlGetProp(n, (xmlChar *) "target");
196 char *valuea = (char *) xmlGetProp(n, (xmlChar *) "value");
197 char *usera = (char *) xmlGetProp(n, (xmlChar *) "user");
198 char *precedencea = (char *) xmlGetProp(n, (xmlChar *) "precedence");
200 for (n = n->children; n; n = n->next)
202 if (n->type != XML_ELEMENT_NODE)
204 if (!strcmp((const char *) n->name, "set"))
206 xmlNode *root = n->children;
208 char *name = (char *) xmlGetProp(n, (xmlChar *) "name");
209 char *target = (char *) xmlGetProp(n, (xmlChar *) "target");
210 char *value = (char *) xmlGetProp(n, (xmlChar *) "value");
211 char *user = (char *) xmlGetProp(n, (xmlChar *) "user");
212 char *precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
213 xmlChar *buf_out = 0;
218 set.precedence = atoi((char *) precedence);
219 else if (precedencea)
220 set.precedence = atoi((char *) precedencea);
224 set.target = target ? target : targeta;
225 set.name = name ? name : namea;
227 while (root && root->type != XML_ELEMENT_NODE)
230 set.value = value ? value : valuea;
232 { /* xml document content for this setting */
233 xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0");
237 yaz_log(YLOG_WARN, "bad XML content for setting "
238 "name=%s", set.name);
240 yaz_log(YLOG_WARN, "bad XML content for setting");
246 xmlDocSetRootElement(doc, xmlCopyNode(root, 1));
247 xmlDocDumpMemory(doc, &buf_out, &len_out);
248 /* xmlDocDumpMemory 0-terminates */
249 set.value = (char *) buf_out;
254 if (set.name && set.value && set.target)
255 (*fun)(client_data, &set);
259 yaz_log(YLOG_WARN, "missing value and/or target for "
260 "setting name=%s", set.name);
262 yaz_log(YLOG_WARN, "missing name/value/target for setting");
274 yaz_log(YLOG_WARN, "Unknown element %s in settings file",
280 xmlFree(precedencea);
287 static int read_settings_file(const char *path,
289 void (*fun)(void *client_data,
290 struct setting *set))
292 xmlDoc *doc = xmlParseFile(path);
298 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
301 n = xmlDocGetRootElement(doc);
302 ret = settings_read_node_x(n, client_data, fun);
309 // Recursively read files or directories, invoking a
310 // callback for each one
311 static int read_settings(const char *path,
313 void (*fun)(void *client_data,
314 struct setting *set))
323 if (!(d = opendir(path)))
325 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
328 while ((de = readdir(d)))
331 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
333 sprintf(tmp, "%s/%s", path, de->d_name);
334 if (read_settings(tmp, client_data, fun))
339 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
340 ret = read_settings_file(path, client_data, fun);
344 // Determines if a ZURL is a wildcard, and what kind
345 static int zurl_wildcard(const char *zurl)
348 return SETTING_WILDCARD_NO;
350 return SETTING_WILDCARD_YES;
351 else if (*(zurl + strlen(zurl) - 1) == '*')
352 return SETTING_WILDCARD_DB;
354 return SETTING_WILDCARD_NO;
357 struct update_database_context {
359 struct conf_service *service;
362 void expand_settings_array(struct setting ***set_ar, int *num, int offset,
369 int i, n_num = offset + 10;
370 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
371 for (i = 0; i < *num; i++)
372 n_ar[i] = (*set_ar)[i];
373 for (; i < n_num; i++)
380 void expand_settings_array2(struct settings_array *settings, int offset, NMEM nmem)
384 if (offset >= settings->num_settings)
386 int i, n_num = offset + 10;
387 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
388 for (i = 0; i < settings->num_settings; i++)
389 n_ar[i] = settings->settings[i];
390 for (; i < n_num; i++)
392 settings->num_settings = n_num;
393 settings->settings = n_ar;
397 static void update_settings(struct setting *set, struct settings_array *settings, int offset, NMEM nmem)
400 yaz_log(YLOG_DEBUG, "update service settings offset %d with %s=%s", offset, set->name, set->value);
401 expand_settings_array2(settings, offset, nmem);
403 // First we determine if this setting is overriding any existing settings
404 // with the same name.
405 assert(offset < settings->num_settings);
406 for (sp = &settings->settings[offset]; *sp; )
407 if (!strcmp((*sp)->name, set->name))
409 if ((*sp)->precedence < set->precedence)
411 // We discard the value (nmem keeps track of the space)
412 *sp = (*sp)->next; // unlink value from existing setting
414 else if ((*sp)->precedence > set->precedence)
416 // Db contains a higher-priority setting. Abort search
419 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
421 // target-specific value trumps wildcard. Delete.
422 *sp = (*sp)->next; // unlink.....
424 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
425 // Db already contains higher-priority setting. Abort search
432 if (!*sp) // is null when there are no higher-priority settings, so we add one
434 struct setting *new = nmem_malloc(nmem, sizeof(*new));
435 memset(new, 0, sizeof(*new));
436 new->precedence = set->precedence;
437 new->target = nmem_strdup_null(nmem, set->target);
438 new->name = nmem_strdup_null(nmem, set->name);
439 new->value = nmem_strdup_null(nmem, set->value);
440 new->next = settings->settings[offset];
441 settings->settings[offset] = new;
446 // This is called from grep_databases -- adds/overrides setting for a target
447 // This is also where the rules for precedence of settings are implemented
448 static void update_database_fun(void *context, struct database *db)
450 struct setting *set = ((struct update_database_context *)
452 struct conf_service *service = ((struct update_database_context *)
457 // Is this the right database?
458 if (!match_zurl(db->id, set->target))
461 offset = settings_create_offset(service, set->name);
462 expand_settings_array(&db->settings, &db->num_settings, offset, service->nmem);
464 // First we determine if this setting is overriding any existing settings
465 // with the same name.
466 assert(offset < db->num_settings);
467 for (sp = &db->settings[offset]; *sp; )
468 if (!strcmp((*sp)->name, set->name))
470 if ((*sp)->precedence < set->precedence)
472 // We discard the value (nmem keeps track of the space)
473 *sp = (*sp)->next; // unlink value from existing setting
475 else if ((*sp)->precedence > set->precedence)
477 // Db contains a higher-priority setting. Abort search
480 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
482 // target-specific value trumps wildcard. Delete.
483 *sp = (*sp)->next; // unlink.....
485 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
486 // Db already contains higher-priority setting. Abort search
493 if (!*sp) // is null when there are no higher-priority settings, so we add one
495 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
497 memset(new, 0, sizeof(*new));
498 new->precedence = set->precedence;
499 new->target = nmem_strdup(service->nmem, set->target);
500 new->name = nmem_strdup(service->nmem, set->name);
501 new->value = nmem_strdup(service->nmem, set->value);
502 new->next = db->settings[offset];
503 db->settings[offset] = new;
507 // Callback -- updates database records with dictionary entries as appropriate
508 // This is used in pass 2 to assign name/value pairs to databases
509 static void update_databases(void *client_data, struct setting *set)
511 struct conf_service *service = (struct conf_service *) client_data;
512 struct update_database_context context;
514 context.service = service;
515 predef_grep_databases(&context, service, update_database_fun);
518 // This simply copies the 'hard' (application-specific) settings
519 // to the settings dictionary.
520 static void initialize_hard_settings(struct conf_service *service)
522 struct setting_dictionary *dict = service->dictionary;
523 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
524 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
525 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
526 dict->num = dict->size;
529 // Read any settings names introduced in service definition (config) and add to dictionary
530 // This is done now to avoid errors if user settings are declared in session overrides
531 void initialize_soft_settings(struct conf_service *service)
534 for (i = 0; i < service->num_metadata; i++)
536 struct conf_metadata *md = &service->metadata[i];
538 if (md->setting != Metadata_setting_no)
539 settings_create_offset(service, md->name);
541 // Also create setting for some metadata attributes.
544 WRBUF wrbuf = wrbuf_alloc();
545 yaz_log(YLOG_DEBUG, "Metadata %s has limitmap: %s ",md->name, md->limitmap);
546 wrbuf_printf(wrbuf, "pz:limitmap:%s", md->name);
547 index = settings_create_offset(service, wrbuf_cstr(wrbuf));
551 yaz_log(YLOG_DEBUG, "Service %s default %s=%s",
552 (service->id ? service->id: "unknown"), wrbuf_cstr(wrbuf), md->limitmap);
553 new.name = (char *) wrbuf_cstr(wrbuf);
554 new.value = md->limitmap;
558 offset = settings_create_offset(service, new.name);
559 update_settings(&new, service->settings, offset, service->nmem);
561 wrbuf_destroy(wrbuf);
562 // TODO same for facetmap
567 static void prepare_target_dictionary(void *client_data, struct setting *set)
569 struct conf_service *service = (struct conf_service *) client_data;
571 // If target address is not wildcard, add the database
572 if (*set->target && !zurl_wildcard(set->target))
573 create_database_for_service(set->target, service);
576 void init_settings(struct conf_service *service)
578 struct setting_dictionary *new;
580 assert(service->nmem);
582 new = nmem_malloc(service->nmem, sizeof(*new));
583 memset(new, 0, sizeof(*new));
584 service->dictionary = new;
585 initialize_hard_settings(service);
586 initialize_soft_settings(service);
589 int settings_read_file(struct conf_service *service, const char *path,
593 return read_settings(path, service, prepare_target_dictionary);
595 return read_settings(path, service, update_databases);
598 int settings_read_node(struct conf_service *service, xmlNode *n,
602 return settings_read_node_x(n, service, prepare_target_dictionary);
604 return settings_read_node_x(n, service, update_databases);
610 * c-file-style: "Stroustrup"
611 * indent-tabs-mode: nil
613 * vim: shiftwidth=4 tabstop=8 expandtab