1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 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>
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[] = {
71 struct setting_dictionary
78 // This establishes the precedence of wildcard expressions
79 #define SETTING_WILDCARD_NO 0 // No wildcard
80 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
81 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
83 // Returns size of settings directory
84 int settings_num(struct conf_service *service)
86 return service->dictionary->num;
89 int settings_offset(struct conf_service *service, const char *name)
95 for (i = 0; i < service->dictionary->num; i++)
96 if (!strcmp(name, service->dictionary->dict[i]))
101 // Ignores everything after second colon, if present
102 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
103 int settings_offset_cprefix(struct conf_service *service, const char *name)
109 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
110 maxlen = (p - name) + 1;
111 for (i = 0; i < service->dictionary->num; i++)
112 if (!strncmp(name, service->dictionary->dict[i], maxlen))
117 char *settings_name(struct conf_service *service, int offset)
119 return service->dictionary->dict[offset];
122 static int isdir(const char *path)
126 if (stat(path, &st) < 0)
128 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
131 return st.st_mode & S_IFDIR;
134 // Read settings from an XML file, calling handler function for each setting
135 static void read_settings_file(const char *path,
136 struct conf_service *service,
137 void (*fun)(struct conf_service *service,
138 struct setting *set))
140 xmlDoc *doc = xmlParseFile(path);
142 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
146 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
149 n = xmlDocGetRootElement(doc);
150 namea = xmlGetProp(n, (xmlChar *) "name");
151 targeta = xmlGetProp(n, (xmlChar *) "target");
152 valuea = xmlGetProp(n, (xmlChar *) "value");
153 usera = xmlGetProp(n, (xmlChar *) "user");
154 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
155 for (n = n->children; n; n = n->next)
157 if (n->type != XML_ELEMENT_NODE)
159 if (!strcmp((const char *) n->name, "set"))
161 char *name, *target, *value, *user, *precedence;
163 name = (char *) xmlGetProp(n, (xmlChar *) "name");
164 target = (char *) xmlGetProp(n, (xmlChar *) "target");
165 value = (char *) xmlGetProp(n, (xmlChar *) "value");
166 user = (char *) xmlGetProp(n, (xmlChar *) "user");
167 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
169 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
171 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
181 // Copy everything into a temporary buffer -- we decide
182 // later if we are keeping it.
184 set.precedence = atoi((char *) precedence);
185 else if (precedencea)
186 set.precedence = atoi((char *) precedencea);
190 strcpy(targetb, target);
192 strcpy(targetb, (const char *) targeta);
193 set.target = targetb;
197 strcpy(nameb, (const char *) namea);
200 strcpy(valueb, value);
202 strcpy(valueb, (const char *) valuea);
205 (*fun)(service, &set);
215 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
220 xmlFree(precedencea);
228 // Recursively read files or directories, invoking a
229 // callback for each one
230 static void read_settings(const char *path,
231 struct conf_service *service,
232 void (*fun)(struct conf_service *service,
233 struct setting *set))
241 if (!(d = opendir(path)))
243 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
246 while ((de = readdir(d)))
249 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
251 sprintf(tmp, "%s/%s", path, de->d_name);
252 read_settings(tmp, service, fun);
256 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
257 read_settings_file(path, service, fun);
260 // Determines if a ZURL is a wildcard, and what kind
261 static int zurl_wildcard(const char *zurl)
264 return SETTING_WILDCARD_NO;
266 return SETTING_WILDCARD_YES;
267 else if (*(zurl + strlen(zurl) - 1) == '*')
268 return SETTING_WILDCARD_DB;
270 return SETTING_WILDCARD_NO;
273 // Callback. Adds a new entry to the dictionary if necessary
274 // This is used in pass 1 to determine layout of dictionary
275 // and to load any databases mentioned
276 static void prepare_dictionary(struct conf_service *service,
279 struct setting_dictionary *dictionary = service->dictionary;
284 // If target address is not wildcard, add the database
285 if (*set->target && !zurl_wildcard(set->target))
286 find_database(set->target, 0, service);
288 // Determine if we already have a dictionary entry
289 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
291 for (i = 0; i < dictionary->num; i++)
292 if (!strcmp(dictionary->dict[i], set->name))
295 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
297 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
301 // Create a new dictionary entry
302 // Grow dictionary if necessary
303 if (!dictionary->size)
305 nmem_malloc(service->nmem, (dictionary->size = 50) * sizeof(char*));
306 else if (dictionary->num + 1 > dictionary->size)
309 nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*));
310 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
311 dictionary->dict = tmp;
312 dictionary->size *= 2;
314 dictionary->dict[dictionary->num++] = nmem_strdup(service->nmem, set->name);
318 struct update_database_context {
320 struct conf_service *service;
323 // This is called from grep_databases -- adds/overrides setting for a target
324 // This is also where the rules for precedence of settings are implemented
325 static void update_database(void *context, struct database *db)
327 struct setting *set = ((struct update_database_context *)
329 struct conf_service *service = ((struct update_database_context *)
331 struct setting *s, **sp;
334 // Is this the right database?
335 if (!match_zurl(db->url, set->target))
338 if ((offset = settings_offset_cprefix(service, set->name)) < 0)
341 // First we determine if this setting is overriding any existing settings
342 // with the same name.
343 for (s = db->settings[offset], sp = &db->settings[offset]; s;
344 sp = &s->next, s = s->next)
345 if (!strcmp(s->name, set->name))
347 if (s->precedence < set->precedence)
348 // We discard the value (nmem keeps track of the space)
349 *sp = (*sp)->next; // unlink value from existing setting
350 else if (s->precedence > set->precedence)
351 // Db contains a higher-priority setting. Abort search
353 if (zurl_wildcard(s->target) > zurl_wildcard(set->target))
354 // target-specific value trumps wildcard. Delete.
355 *sp = (*sp)->next; // unlink.....
356 else if (!zurl_wildcard(s->target))
357 // Db already contains higher-priority setting. Abort search
360 if (!s) // s will be null when there are no higher-priority settings -- we add one
362 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
364 memset(new, 0, sizeof(*new));
365 new->precedence = set->precedence;
366 new->target = nmem_strdup(service->nmem, set->target);
367 new->name = nmem_strdup(service->nmem, set->name);
368 new->value = nmem_strdup(service->nmem, set->value);
369 new->next = db->settings[offset];
370 db->settings[offset] = new;
374 // Callback -- updates database records with dictionary entries as appropriate
375 // This is used in pass 2 to assign name/value pairs to databases
376 static void update_databases(struct conf_service *service,
379 struct update_database_context context;
381 context.service = service;
382 predef_grep_databases(&context, service, 0, update_database);
385 // This simply copies the 'hard' (application-specific) settings
386 // to the settings dictionary.
387 static void initialize_hard_settings(struct conf_service *service)
389 struct setting_dictionary *dict = service->dictionary;
390 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
391 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
392 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
393 dict->num = dict->size;
396 // Read any settings names introduced in service definition (config) and add to dictionary
397 // This is done now to avoid errors if user settings are declared in session overrides
398 static void initialize_soft_settings(struct conf_service *service)
402 for (i = 0; i < service->num_metadata; i++)
405 struct conf_metadata *md = &service->metadata[i];
407 if (md->setting == Metadata_setting_no)
415 prepare_dictionary(service, &set);
419 static void prepare_target_dictionary(struct conf_service *service,
422 struct setting_dictionary *dictionary = service->dictionary;
427 // If target address is not wildcard, add the database
428 if (*set->target && !zurl_wildcard(set->target))
429 find_database(set->target, 0, service);
431 // Determine if we already have a dictionary entry
432 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
434 for (i = 0; i < dictionary->num; i++)
435 if (!strcmp(dictionary->dict[i], set->name))
437 yaz_log(YLOG_WARN, "setting %s not configured as metadata", set->name);
440 // If we ever decide we need to be able to specify multiple settings directories,
441 // the two calls to read_settings must be split -- so the dictionary is prepared
442 // for the contents of every directory before the databases are updated.
443 void settings_read(struct conf_service *service, const char *path)
445 read_settings(path, service, prepare_target_dictionary);
446 read_settings(path, service, update_databases);
449 void init_settings(struct conf_service *service)
451 struct setting_dictionary *new;
453 assert(service->nmem);
455 new = nmem_malloc(service->nmem, sizeof(*new));
456 memset(new, 0, sizeof(*new));
457 service->dictionary = new;
458 initialize_hard_settings(service);
459 initialize_soft_settings(service);
465 * c-file-style: "Stroustrup"
466 * indent-tabs-mode: nil
468 * vim: shiftwidth=4 tabstop=8 expandtab