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 *name, char *value)
164 int offset = settings_create_offset(service, name);
165 expand_settings_array(&service->settings->settings, &service->settings->num_settings, offset, service->nmem);
166 for (s = service->settings->settings[offset]; s; s = s->next)
167 if (!strcmp(s->name, name))
172 s = nmem_malloc(service->nmem, sizeof(*s));
177 s->next = service->settings->settings[offset];
178 service->settings->settings[offset] = s;
182 static int isdir(const char *path)
186 if (stat(path, &st) < 0)
188 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
191 return st.st_mode & S_IFDIR;
194 // Read settings from an XML file, calling handler function for each setting
195 int settings_read_node_x(xmlNode *n,
197 void (*fun)(void *client_data,
198 struct setting *set))
200 int ret_val = 0; /* success */
201 char *namea = (char *) xmlGetProp(n, (xmlChar *) "name");
202 char *targeta = (char *) xmlGetProp(n, (xmlChar *) "target");
203 char *valuea = (char *) xmlGetProp(n, (xmlChar *) "value");
204 char *usera = (char *) xmlGetProp(n, (xmlChar *) "user");
205 char *precedencea = (char *) xmlGetProp(n, (xmlChar *) "precedence");
207 for (n = n->children; n; n = n->next)
209 if (n->type != XML_ELEMENT_NODE)
211 if (!strcmp((const char *) n->name, "set"))
213 xmlNode *root = n->children;
215 char *name = (char *) xmlGetProp(n, (xmlChar *) "name");
216 char *target = (char *) xmlGetProp(n, (xmlChar *) "target");
217 char *value = (char *) xmlGetProp(n, (xmlChar *) "value");
218 char *user = (char *) xmlGetProp(n, (xmlChar *) "user");
219 char *precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
220 xmlChar *buf_out = 0;
225 set.precedence = atoi((char *) precedence);
226 else if (precedencea)
227 set.precedence = atoi((char *) precedencea);
231 set.target = target ? target : targeta;
232 set.name = name ? name : namea;
234 while (root && root->type != XML_ELEMENT_NODE)
237 set.value = value ? value : valuea;
239 { /* xml document content for this setting */
240 xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0");
244 yaz_log(YLOG_WARN, "bad XML content for setting "
245 "name=%s", set.name);
247 yaz_log(YLOG_WARN, "bad XML content for setting");
253 xmlDocSetRootElement(doc, xmlCopyNode(root, 1));
254 xmlDocDumpMemory(doc, &buf_out, &len_out);
255 /* xmlDocDumpMemory 0-terminates */
256 set.value = (char *) buf_out;
261 if (set.name && set.value && set.target)
262 (*fun)(client_data, &set);
266 yaz_log(YLOG_WARN, "missing value and/or target for "
267 "setting name=%s", set.name);
269 yaz_log(YLOG_WARN, "missing name/value/target for setting");
281 yaz_log(YLOG_WARN, "Unknown element %s in settings file",
287 xmlFree(precedencea);
294 static int read_settings_file(const char *path,
296 void (*fun)(void *client_data,
297 struct setting *set))
299 xmlDoc *doc = xmlParseFile(path);
305 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
308 n = xmlDocGetRootElement(doc);
309 ret = settings_read_node_x(n, client_data, fun);
316 // Recursively read files or directories, invoking a
317 // callback for each one
318 static int read_settings(const char *path,
320 void (*fun)(void *client_data,
321 struct setting *set))
330 if (!(d = opendir(path)))
332 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
335 while ((de = readdir(d)))
338 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
340 sprintf(tmp, "%s/%s", path, de->d_name);
341 if (read_settings(tmp, client_data, fun))
346 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
347 ret = read_settings_file(path, client_data, fun);
351 // Determines if a ZURL is a wildcard, and what kind
352 static int zurl_wildcard(const char *zurl)
355 return SETTING_WILDCARD_NO;
357 return SETTING_WILDCARD_YES;
358 else if (*(zurl + strlen(zurl) - 1) == '*')
359 return SETTING_WILDCARD_DB;
361 return SETTING_WILDCARD_NO;
364 struct update_database_context {
366 struct conf_service *service;
369 void expand_settings_array(struct setting ***set_ar, int *num, int offset,
376 int i, n_num = offset + 10;
377 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
378 for (i = 0; i < *num; i++)
379 n_ar[i] = (*set_ar)[i];
380 for (; i < n_num; i++)
387 void expand_settings_array2(struct settings_array *settings, int offset, NMEM nmem)
391 if (offset >= settings->num_settings)
393 int i, n_num = offset + 10;
394 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
395 for (i = 0; i < settings->num_settings; i++)
396 n_ar[i] = settings->settings[i];
397 for (; i < n_num; i++)
399 settings->num_settings = n_num;
400 settings->settings = n_ar;
404 static void update_settings(struct setting *set, struct settings_array *settings, int offset, NMEM nmem)
407 yaz_log(YLOG_DEBUG, "update service settings offset %d with %s=%s", offset, set->name, set->value);
408 expand_settings_array2(settings, offset, nmem);
410 // First we determine if this setting is overriding any existing settings
411 // with the same name.
412 assert(offset < settings->num_settings);
413 for (sp = &settings->settings[offset]; *sp; )
414 if (!strcmp((*sp)->name, set->name))
416 if ((*sp)->precedence < set->precedence)
418 // We discard the value (nmem keeps track of the space)
419 *sp = (*sp)->next; // unlink value from existing setting
421 else if ((*sp)->precedence > set->precedence)
423 // Db contains a higher-priority setting. Abort search
426 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
428 // target-specific value trumps wildcard. Delete.
429 *sp = (*sp)->next; // unlink.....
431 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
432 // Db already contains higher-priority setting. Abort search
439 if (!*sp) // is null when there are no higher-priority settings, so we add one
441 struct setting *new = nmem_malloc(nmem, sizeof(*new));
442 memset(new, 0, sizeof(*new));
443 new->precedence = set->precedence;
444 new->target = nmem_strdup_null(nmem, set->target);
445 new->name = nmem_strdup_null(nmem, set->name);
446 new->value = nmem_strdup_null(nmem, set->value);
447 new->next = settings->settings[offset];
448 settings->settings[offset] = new;
453 // This is called from grep_databases -- adds/overrides setting for a target
454 // This is also where the rules for precedence of settings are implemented
455 static void update_database_fun(void *context, struct database *db)
457 struct setting *set = ((struct update_database_context *)
459 struct conf_service *service = ((struct update_database_context *)
464 // Is this the right database?
465 if (!match_zurl(db->id, set->target))
468 offset = settings_create_offset(service, set->name);
469 expand_settings_array(&db->settings, &db->num_settings, offset, service->nmem);
471 // First we determine if this setting is overriding any existing settings
472 // with the same name.
473 assert(offset < db->num_settings);
474 for (sp = &db->settings[offset]; *sp; )
475 if (!strcmp((*sp)->name, set->name))
477 if ((*sp)->precedence < set->precedence)
479 // We discard the value (nmem keeps track of the space)
480 *sp = (*sp)->next; // unlink value from existing setting
482 else if ((*sp)->precedence > set->precedence)
484 // Db contains a higher-priority setting. Abort search
487 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
489 // target-specific value trumps wildcard. Delete.
490 *sp = (*sp)->next; // unlink.....
492 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
493 // Db already contains higher-priority setting. Abort search
500 if (!*sp) // is null when there are no higher-priority settings, so we add one
502 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
504 memset(new, 0, sizeof(*new));
505 new->precedence = set->precedence;
506 new->target = nmem_strdup(service->nmem, set->target);
507 new->name = nmem_strdup(service->nmem, set->name);
508 new->value = nmem_strdup(service->nmem, set->value);
509 new->next = db->settings[offset];
510 db->settings[offset] = new;
514 // Callback -- updates database records with dictionary entries as appropriate
515 // This is used in pass 2 to assign name/value pairs to databases
516 static void update_databases(void *client_data, struct setting *set)
518 struct conf_service *service = (struct conf_service *) client_data;
519 struct update_database_context context;
521 context.service = service;
522 predef_grep_databases(&context, service, update_database_fun);
525 // This simply copies the 'hard' (application-specific) settings
526 // to the settings dictionary.
527 static void initialize_hard_settings(struct conf_service *service)
529 struct setting_dictionary *dict = service->dictionary;
530 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
531 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
532 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
533 dict->num = dict->size;
536 // Read any settings names introduced in service definition (config) and add to dictionary
537 // This is done now to avoid errors if user settings are declared in session overrides
538 void initialize_soft_settings(struct conf_service *service)
541 for (i = 0; i < service->num_metadata; i++)
543 struct conf_metadata *md = &service->metadata[i];
545 if (md->setting != Metadata_setting_no)
546 settings_create_offset(service, md->name);
548 // Also create setting for some metadata attributes.
551 WRBUF wrbuf = wrbuf_alloc();
552 yaz_log(YLOG_DEBUG, "Metadata %s has limitmap: %s ",md->name, md->limitmap);
553 wrbuf_printf(wrbuf, "pz:limitmap:%s", md->name);
554 index = settings_create_offset(service, wrbuf_cstr(wrbuf));
558 yaz_log(YLOG_DEBUG, "Service %s default %s=%s",
559 (service->id ? service->id: "unknown"), wrbuf_cstr(wrbuf), md->limitmap);
560 new.name = (char *) wrbuf_cstr(wrbuf);
561 new.value = md->limitmap;
565 offset = settings_create_offset(service, new.name);
566 update_settings(&new, service->settings, offset, service->nmem);
568 wrbuf_destroy(wrbuf);
569 // TODO same for facetmap
574 static void prepare_target_dictionary(void *client_data, struct setting *set)
576 struct conf_service *service = (struct conf_service *) client_data;
578 // If target address is not wildcard, add the database
579 if (*set->target && !zurl_wildcard(set->target))
580 create_database_for_service(set->target, service);
583 void init_settings(struct conf_service *service)
585 struct setting_dictionary *new;
587 assert(service->nmem);
589 new = nmem_malloc(service->nmem, sizeof(*new));
590 memset(new, 0, sizeof(*new));
591 service->dictionary = new;
592 initialize_hard_settings(service);
593 initialize_soft_settings(service);
596 int settings_read_file(struct conf_service *service, const char *path,
600 return read_settings(path, service, prepare_target_dictionary);
602 return read_settings(path, service, update_databases);
605 int settings_read_node(struct conf_service *service, xmlNode *n,
609 return settings_read_node_x(n, service, prepare_target_dictionary);
611 return settings_read_node_x(n, service, update_databases);
617 * c-file-style: "Stroustrup"
618 * indent-tabs-mode: nil
620 * vim: shiftwidth=4 tabstop=8 expandtab