1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 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",
88 struct setting_dictionary
95 // This establishes the precedence of wildcard expressions
96 #define SETTING_WILDCARD_NO 0 // No wildcard
97 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
98 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
100 // Returns size of settings directory
101 int settings_num(struct conf_service *service)
103 return service->dictionary->num;
106 /* Find and possible create a new dictionary entry. Pass valid NMEM pointer if creation is allowed, otherwise null */
107 static int settings_index_lookup(struct setting_dictionary *dictionary, const char *name, NMEM nmem)
115 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
116 maxlen = (p - name) + 1;
118 maxlen = strlen(name) + 1;
119 for (i = 0; i < dictionary->num; i++)
120 if (!strncmp(name, dictionary->dict[i], maxlen))
124 if (!strncmp("pz:", name, 3))
125 yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name);
126 if (dictionary->num + 1 > dictionary->size)
129 nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
130 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
131 dictionary->dict = tmp;
132 dictionary->size *= 2;
134 dictionary->dict[dictionary->num] = nmem_strdup(nmem, name);
135 dictionary->dict[dictionary->num][maxlen-1] = '\0';
136 return dictionary->num++;
139 int settings_create_offset(struct conf_service *service, const char *name)
141 return settings_index_lookup(service->dictionary, name, service->nmem);
144 int settings_lookup_offset(struct conf_service *service, const char *name)
146 return settings_index_lookup(service->dictionary, name, 0);
149 char *settings_name(struct conf_service *service, int offset)
151 assert(offset < service->dictionary->num);
152 return service->dictionary->dict[offset];
156 // Apply a session override to a database
157 void service_apply_setting(struct conf_service *service, char *setting, char *value)
159 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
160 int offset = settings_create_offset(service, setting);
161 expand_settings_array(&service->settings->settings, &service->settings->num_settings, offset, service->nmem);
166 new->next = service->settings->settings[offset];
167 service->settings->settings[offset] = new;
171 static int isdir(const char *path)
175 if (stat(path, &st) < 0)
177 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
180 return st.st_mode & S_IFDIR;
183 // Read settings from an XML file, calling handler function for each setting
184 int settings_read_node_x(xmlNode *n,
186 void (*fun)(void *client_data,
187 struct setting *set))
189 int ret_val = 0; /* success */
190 char *namea = (char *) xmlGetProp(n, (xmlChar *) "name");
191 char *targeta = (char *) xmlGetProp(n, (xmlChar *) "target");
192 char *valuea = (char *) xmlGetProp(n, (xmlChar *) "value");
193 char *usera = (char *) xmlGetProp(n, (xmlChar *) "user");
194 char *precedencea = (char *) xmlGetProp(n, (xmlChar *) "precedence");
196 for (n = n->children; n; n = n->next)
198 if (n->type != XML_ELEMENT_NODE)
200 if (!strcmp((const char *) n->name, "set"))
202 xmlNode *root = n->children;
204 char *name = (char *) xmlGetProp(n, (xmlChar *) "name");
205 char *target = (char *) xmlGetProp(n, (xmlChar *) "target");
206 char *value = (char *) xmlGetProp(n, (xmlChar *) "value");
207 char *user = (char *) xmlGetProp(n, (xmlChar *) "user");
208 char *precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
209 xmlChar *buf_out = 0;
214 set.precedence = atoi((char *) precedence);
215 else if (precedencea)
216 set.precedence = atoi((char *) precedencea);
220 set.target = target ? target : targeta;
221 set.name = name ? name : namea;
223 while (root && root->type != XML_ELEMENT_NODE)
226 set.value = value ? value : valuea;
228 { /* xml document content for this setting */
229 xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0");
233 yaz_log(YLOG_WARN, "bad XML content for setting "
234 "name=%s", set.name);
236 yaz_log(YLOG_WARN, "bad XML content for setting");
242 xmlDocSetRootElement(doc, xmlCopyNode(root, 1));
243 xmlDocDumpMemory(doc, &buf_out, &len_out);
244 /* xmlDocDumpMemory 0-terminates */
245 set.value = (char *) buf_out;
250 if (set.name && set.value && set.target)
251 (*fun)(client_data, &set);
255 yaz_log(YLOG_WARN, "missing value and/or target for "
256 "setting name=%s", set.name);
258 yaz_log(YLOG_WARN, "missing name/value/target for setting");
270 yaz_log(YLOG_WARN, "Unknown element %s in settings file",
276 xmlFree(precedencea);
283 static int read_settings_file(const char *path,
285 void (*fun)(void *client_data,
286 struct setting *set))
288 xmlDoc *doc = xmlParseFile(path);
294 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
297 n = xmlDocGetRootElement(doc);
298 ret = settings_read_node_x(n, client_data, fun);
305 // Recursively read files or directories, invoking a
306 // callback for each one
307 static int read_settings(const char *path,
309 void (*fun)(void *client_data,
310 struct setting *set))
319 if (!(d = opendir(path)))
321 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
324 while ((de = readdir(d)))
327 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
329 sprintf(tmp, "%s/%s", path, de->d_name);
330 if (read_settings(tmp, client_data, fun))
335 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
336 ret = read_settings_file(path, client_data, fun);
340 // Determines if a ZURL is a wildcard, and what kind
341 static int zurl_wildcard(const char *zurl)
344 return SETTING_WILDCARD_NO;
346 return SETTING_WILDCARD_YES;
347 else if (*(zurl + strlen(zurl) - 1) == '*')
348 return SETTING_WILDCARD_DB;
350 return SETTING_WILDCARD_NO;
353 struct update_database_context {
355 struct conf_service *service;
358 void expand_settings_array(struct setting ***set_ar, int *num, int offset,
365 int i, n_num = offset + 10;
366 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
367 for (i = 0; i < *num; i++)
368 n_ar[i] = (*set_ar)[i];
369 for (; i < n_num; i++)
376 void expand_settings_array2(struct settings_array *settings, int offset, NMEM nmem)
380 if (offset >= settings->num_settings)
382 int i, n_num = offset + 10;
383 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
384 for (i = 0; i < settings->num_settings; i++)
385 n_ar[i] = settings->settings[i];
386 for (; i < n_num; i++)
388 settings->num_settings = n_num;
389 settings->settings = n_ar;
393 static void update_settings(struct setting *set, struct settings_array *settings, int offset, NMEM nmem)
396 yaz_log(YLOG_DEBUG, "update service settings offset %d with %s=%s", offset, set->name, set->value);
397 expand_settings_array2(settings, offset, nmem);
399 // First we determine if this setting is overriding any existing settings
400 // with the same name.
401 assert(offset < settings->num_settings);
402 for (sp = &settings->settings[offset]; *sp; )
403 if (!strcmp((*sp)->name, set->name))
405 if ((*sp)->precedence < set->precedence)
407 // We discard the value (nmem keeps track of the space)
408 *sp = (*sp)->next; // unlink value from existing setting
410 else if ((*sp)->precedence > set->precedence)
412 // Db contains a higher-priority setting. Abort search
415 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
417 // target-specific value trumps wildcard. Delete.
418 *sp = (*sp)->next; // unlink.....
420 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
421 // Db already contains higher-priority setting. Abort search
428 if (!*sp) // is null when there are no higher-priority settings, so we add one
430 struct setting *new = nmem_malloc(nmem, sizeof(*new));
431 memset(new, 0, sizeof(*new));
432 new->precedence = set->precedence;
433 new->target = nmem_strdup_null(nmem, set->target);
434 new->name = nmem_strdup_null(nmem, set->name);
435 new->value = nmem_strdup_null(nmem, set->value);
436 new->next = settings->settings[offset];
437 settings->settings[offset] = new;
442 // This is called from grep_databases -- adds/overrides setting for a target
443 // This is also where the rules for precedence of settings are implemented
444 static void update_database_fun(void *context, struct database *db)
446 struct setting *set = ((struct update_database_context *)
448 struct conf_service *service = ((struct update_database_context *)
453 // Is this the right database?
454 if (!match_zurl(db->id, set->target))
457 offset = settings_create_offset(service, set->name);
458 expand_settings_array(&db->settings, &db->num_settings, offset, service->nmem);
460 // First we determine if this setting is overriding any existing settings
461 // with the same name.
462 assert(offset < db->num_settings);
463 for (sp = &db->settings[offset]; *sp; )
464 if (!strcmp((*sp)->name, set->name))
466 if ((*sp)->precedence < set->precedence)
468 // We discard the value (nmem keeps track of the space)
469 *sp = (*sp)->next; // unlink value from existing setting
471 else if ((*sp)->precedence > set->precedence)
473 // Db contains a higher-priority setting. Abort search
476 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
478 // target-specific value trumps wildcard. Delete.
479 *sp = (*sp)->next; // unlink.....
481 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
482 // Db already contains higher-priority setting. Abort search
489 if (!*sp) // is null when there are no higher-priority settings, so we add one
491 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
493 memset(new, 0, sizeof(*new));
494 new->precedence = set->precedence;
495 new->target = nmem_strdup(service->nmem, set->target);
496 new->name = nmem_strdup(service->nmem, set->name);
497 new->value = nmem_strdup(service->nmem, set->value);
498 new->next = db->settings[offset];
499 db->settings[offset] = new;
503 // Callback -- updates database records with dictionary entries as appropriate
504 // This is used in pass 2 to assign name/value pairs to databases
505 static void update_databases(void *client_data, struct setting *set)
507 struct conf_service *service = (struct conf_service *) client_data;
508 struct update_database_context context;
510 context.service = service;
511 predef_grep_databases(&context, service, update_database_fun);
514 // This simply copies the 'hard' (application-specific) settings
515 // to the settings dictionary.
516 static void initialize_hard_settings(struct conf_service *service)
518 struct setting_dictionary *dict = service->dictionary;
519 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
520 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
521 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
522 dict->num = dict->size;
525 // Read any settings names introduced in service definition (config) and add to dictionary
526 // This is done now to avoid errors if user settings are declared in session overrides
527 void initialize_soft_settings(struct conf_service *service)
530 for (i = 0; i < service->num_metadata; i++)
532 struct conf_metadata *md = &service->metadata[i];
534 if (md->setting != Metadata_setting_no)
535 settings_create_offset(service, md->name);
537 // Also create setting for some metadata attributes.
540 WRBUF wrbuf = wrbuf_alloc();
541 yaz_log(YLOG_DEBUG, "Metadata %s has limitmap: %s ",md->name, md->limitmap);
542 wrbuf_printf(wrbuf, "pz:limitmap:%s", md->name);
543 index = settings_create_offset(service, wrbuf_cstr(wrbuf));
547 yaz_log(YLOG_DEBUG, "Service %s default %s=%s",
548 (service->id ? service->id: "unknown"), wrbuf_cstr(wrbuf), md->limitmap);
549 new.name = (char *) wrbuf_cstr(wrbuf);
550 new.value = md->limitmap;
554 offset = settings_create_offset(service, new.name);
555 update_settings(&new, service->settings, offset, service->nmem);
557 wrbuf_destroy(wrbuf);
558 // TODO same for facetmap
563 static void prepare_target_dictionary(void *client_data, struct setting *set)
565 struct conf_service *service = (struct conf_service *) client_data;
567 // If target address is not wildcard, add the database
568 if (*set->target && !zurl_wildcard(set->target))
569 create_database_for_service(set->target, service);
572 void init_settings(struct conf_service *service)
574 struct setting_dictionary *new;
576 assert(service->nmem);
578 new = nmem_malloc(service->nmem, sizeof(*new));
579 memset(new, 0, sizeof(*new));
580 service->dictionary = new;
581 initialize_hard_settings(service);
582 initialize_soft_settings(service);
585 int settings_read_file(struct conf_service *service, const char *path,
589 return read_settings(path, service, prepare_target_dictionary);
591 return read_settings(path, service, update_databases);
594 int settings_read_node(struct conf_service *service, xmlNode *n,
598 return settings_read_node_x(n, service, prepare_target_dictionary);
600 return settings_read_node_x(n, service, update_databases);
606 * c-file-style: "Stroustrup"
607 * indent-tabs-mode: nil
609 * vim: shiftwidth=4 tabstop=8 expandtab