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",
93 struct setting_dictionary
100 // This establishes the precedence of wildcard expressions
101 #define SETTING_WILDCARD_NO 0 // No wildcard
102 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
103 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
105 // Returns size of settings directory
106 int settings_num(struct conf_service *service)
108 return service->dictionary->num;
111 /* Find and possible create a new dictionary entry. Pass valid NMEM pointer if creation is allowed, otherwise null */
112 static int settings_index_lookup(struct setting_dictionary *dictionary, const char *name, NMEM nmem)
120 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
121 maxlen = (p - name) + 1;
123 maxlen = strlen(name) + 1;
124 for (i = 0; i < dictionary->num; i++)
125 if (!strncmp(name, dictionary->dict[i], maxlen))
129 if (!strncmp("pz:", name, 3))
130 yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name);
131 if (dictionary->num + 1 > dictionary->size)
134 nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
135 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
136 dictionary->dict = tmp;
137 dictionary->size *= 2;
139 dictionary->dict[dictionary->num] = nmem_strdup(nmem, name);
140 dictionary->dict[dictionary->num][maxlen-1] = '\0';
141 return dictionary->num++;
144 int settings_create_offset(struct conf_service *service, const char *name)
146 return settings_index_lookup(service->dictionary, name, service->nmem);
149 int settings_lookup_offset(struct conf_service *service, const char *name)
151 return settings_index_lookup(service->dictionary, name, 0);
154 char *settings_name(struct conf_service *service, int offset)
156 assert(offset < service->dictionary->num);
157 return service->dictionary->dict[offset];
160 static int isdir(const char *path)
164 if (stat(path, &st) < 0)
166 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
169 return st.st_mode & S_IFDIR;
172 // Read settings from an XML file, calling handler function for each setting
173 int settings_read_node_x(xmlNode *n,
175 void (*fun)(void *client_data,
176 struct setting *set))
178 int ret_val = 0; /* success */
179 char *namea = (char *) xmlGetProp(n, (xmlChar *) "name");
180 char *targeta = (char *) xmlGetProp(n, (xmlChar *) "target");
181 char *valuea = (char *) xmlGetProp(n, (xmlChar *) "value");
182 char *usera = (char *) xmlGetProp(n, (xmlChar *) "user");
183 char *precedencea = (char *) xmlGetProp(n, (xmlChar *) "precedence");
185 for (n = n->children; n; n = n->next)
187 if (n->type != XML_ELEMENT_NODE)
189 if (!strcmp((const char *) n->name, "set"))
191 xmlNode *root = n->children;
193 char *name = (char *) xmlGetProp(n, (xmlChar *) "name");
194 char *target = (char *) xmlGetProp(n, (xmlChar *) "target");
195 char *value = (char *) xmlGetProp(n, (xmlChar *) "value");
196 char *user = (char *) xmlGetProp(n, (xmlChar *) "user");
197 char *precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
198 xmlChar *buf_out = 0;
203 set.precedence = atoi((char *) precedence);
204 else if (precedencea)
205 set.precedence = atoi((char *) precedencea);
209 set.target = target ? target : targeta;
210 set.name = name ? name : namea;
212 while (root && root->type != XML_ELEMENT_NODE)
215 set.value = value ? value : valuea;
217 { /* xml document content for this setting */
218 xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0");
222 yaz_log(YLOG_WARN, "bad XML content for setting "
223 "name=%s", set.name);
225 yaz_log(YLOG_WARN, "bad XML content for setting");
231 xmlDocSetRootElement(doc, xmlCopyNode(root, 1));
232 xmlDocDumpMemory(doc, &buf_out, &len_out);
233 /* xmlDocDumpMemory 0-terminates */
234 set.value = (char *) buf_out;
239 if (set.name && set.value && set.target)
240 (*fun)(client_data, &set);
244 yaz_log(YLOG_WARN, "missing value and/or target for "
245 "setting name=%s", set.name);
247 yaz_log(YLOG_WARN, "missing name/value/target for setting");
259 yaz_log(YLOG_WARN, "Unknown element %s in settings file",
265 xmlFree(precedencea);
272 static int read_settings_file(const char *path,
274 void (*fun)(void *client_data,
275 struct setting *set))
277 xmlDoc *doc = xmlParseFile(path);
283 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
286 n = xmlDocGetRootElement(doc);
287 ret = settings_read_node_x(n, client_data, fun);
294 // Recursively read files or directories, invoking a
295 // callback for each one
296 static int read_settings(const char *path,
298 void (*fun)(void *client_data,
299 struct setting *set))
308 if (!(d = opendir(path)))
310 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
313 while ((de = readdir(d)))
316 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
318 sprintf(tmp, "%s/%s", path, de->d_name);
319 if (read_settings(tmp, client_data, fun))
324 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
325 ret = read_settings_file(path, client_data, fun);
329 // Determines if a ZURL is a wildcard, and what kind
330 static int zurl_wildcard(const char *zurl)
333 return SETTING_WILDCARD_NO;
335 return SETTING_WILDCARD_YES;
336 else if (*(zurl + strlen(zurl) - 1) == '*')
337 return SETTING_WILDCARD_DB;
339 return SETTING_WILDCARD_NO;
342 struct update_database_context {
344 struct conf_service *service;
347 void expand_settings_array(struct setting ***set_ar, int *num, int offset,
354 int i, n_num = offset + 10;
355 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
356 for (i = 0; i < *num; i++)
357 n_ar[i] = (*set_ar)[i];
358 for (; i < n_num; i++)
365 void expand_settings_array2(struct settings_array *settings, int offset, NMEM nmem)
369 if (offset >= settings->num_settings)
371 int i, n_num = offset + 10;
372 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
373 for (i = 0; i < settings->num_settings; i++)
374 n_ar[i] = settings->settings[i];
375 for (; i < n_num; i++)
377 settings->num_settings = n_num;
378 settings->settings = n_ar;
382 static void update_settings(struct setting *set, struct settings_array *settings, int offset, NMEM nmem)
385 yaz_log(YLOG_DEBUG, "update service settings offset %d with %s=%s", offset, set->name, set->value);
386 expand_settings_array2(settings, offset, nmem);
388 // First we determine if this setting is overriding any existing settings
389 // with the same name.
390 assert(offset < settings->num_settings);
391 for (sp = &settings->settings[offset]; *sp; )
392 if (!strcmp((*sp)->name, set->name))
394 if ((*sp)->precedence < set->precedence)
396 // We discard the value (nmem keeps track of the space)
397 *sp = (*sp)->next; // unlink value from existing setting
399 else if ((*sp)->precedence > set->precedence)
401 // Db contains a higher-priority setting. Abort search
404 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
406 // target-specific value trumps wildcard. Delete.
407 *sp = (*sp)->next; // unlink.....
409 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
410 // Db already contains higher-priority setting. Abort search
417 if (!*sp) // is null when there are no higher-priority settings, so we add one
419 struct setting *new = nmem_malloc(nmem, sizeof(*new));
420 memset(new, 0, sizeof(*new));
421 new->precedence = set->precedence;
422 new->target = nmem_strdup_null(nmem, set->target);
423 new->name = nmem_strdup_null(nmem, set->name);
424 new->value = nmem_strdup_null(nmem, set->value);
425 new->next = settings->settings[offset];
426 settings->settings[offset] = new;
431 // This is called from grep_databases -- adds/overrides setting for a target
432 // This is also where the rules for precedence of settings are implemented
433 static void update_database_fun(void *context, struct database *db)
435 struct setting *set = ((struct update_database_context *)
437 struct conf_service *service = ((struct update_database_context *)
442 // Is this the right database?
443 if (!match_zurl(db->id, set->target))
446 offset = settings_create_offset(service, set->name);
447 expand_settings_array(&db->settings, &db->num_settings, offset, service->nmem);
449 // First we determine if this setting is overriding any existing settings
450 // with the same name.
451 assert(offset < db->num_settings);
452 for (sp = &db->settings[offset]; *sp; )
453 if (!strcmp((*sp)->name, set->name))
455 if ((*sp)->precedence < set->precedence)
457 // We discard the value (nmem keeps track of the space)
458 *sp = (*sp)->next; // unlink value from existing setting
460 else if ((*sp)->precedence > set->precedence)
462 // Db contains a higher-priority setting. Abort search
465 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
467 // target-specific value trumps wildcard. Delete.
468 *sp = (*sp)->next; // unlink.....
470 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
471 // Db already contains higher-priority setting. Abort search
478 if (!*sp) // is null when there are no higher-priority settings, so we add one
480 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
482 memset(new, 0, sizeof(*new));
483 new->precedence = set->precedence;
484 new->target = nmem_strdup(service->nmem, set->target);
485 new->name = nmem_strdup(service->nmem, set->name);
486 new->value = nmem_strdup(service->nmem, set->value);
487 new->next = db->settings[offset];
488 db->settings[offset] = new;
492 // Callback -- updates database records with dictionary entries as appropriate
493 // This is used in pass 2 to assign name/value pairs to databases
494 static void update_databases(void *client_data, struct setting *set)
496 struct conf_service *service = (struct conf_service *) client_data;
497 struct update_database_context context;
499 context.service = service;
500 predef_grep_databases(&context, service, update_database_fun);
503 // This simply copies the 'hard' (application-specific) settings
504 // to the settings dictionary.
505 static void initialize_hard_settings(struct conf_service *service)
507 struct setting_dictionary *dict = service->dictionary;
508 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
509 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
510 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
511 dict->num = dict->size;
514 // Read any settings names introduced in service definition (config) and add to dictionary
515 // This is done now to avoid errors if user settings are declared in session overrides
516 void initialize_soft_settings(struct conf_service *service)
519 for (i = 0; i < service->num_metadata; i++)
521 struct conf_metadata *md = &service->metadata[i];
523 if (md->setting != Metadata_setting_no)
524 settings_create_offset(service, md->name);
526 // Also create setting for some metadata attributes.
529 WRBUF wrbuf = wrbuf_alloc();
530 yaz_log(YLOG_DEBUG, "Metadata %s has limitmap: %s ",md->name, md->limitmap);
531 wrbuf_printf(wrbuf, "pz:limitmap:%s", md->name);
532 index = settings_create_offset(service, wrbuf_cstr(wrbuf));
536 yaz_log(YLOG_DEBUG, "Service %s default %s=%s",
537 (service->id ? service->id: "unknown"), wrbuf_cstr(wrbuf), md->limitmap);
538 new.name = (char *) wrbuf_cstr(wrbuf);
539 new.value = md->limitmap;
543 offset = settings_create_offset(service, new.name);
544 update_settings(&new, service->settings, offset, service->nmem);
546 wrbuf_destroy(wrbuf);
547 // TODO same for facetmap
552 static void prepare_target_dictionary(void *client_data, struct setting *set)
554 struct conf_service *service = (struct conf_service *) client_data;
556 // If target address is not wildcard, add the database
557 if (*set->target && !zurl_wildcard(set->target))
558 create_database_for_service(set->target, service);
561 void init_settings(struct conf_service *service)
563 struct setting_dictionary *new;
565 assert(service->nmem);
567 new = nmem_malloc(service->nmem, sizeof(*new));
568 memset(new, 0, sizeof(*new));
569 service->dictionary = new;
570 initialize_hard_settings(service);
571 initialize_soft_settings(service);
574 int settings_read_file(struct conf_service *service, const char *path,
578 return read_settings(path, service, prepare_target_dictionary);
580 return read_settings(path, service, update_databases);
583 int settings_read_node(struct conf_service *service, xmlNode *n,
587 return settings_read_node_x(n, service, prepare_target_dictionary);
589 return settings_read_node_x(n, service, update_databases);
595 * c-file-style: "Stroustrup"
596 * indent-tabs-mode: nil
598 * vim: shiftwidth=4 tabstop=8 expandtab