Debian Codegraph @

src/modules/db/rfc822db/rfc822db.c

raw
1 #include "common.h"
2 #include "configuration.h"
3 #include "database.h"
4 #include "rfc822.h"
5 #include "rfc822db.h"
6 #include "template.h"
7 #include "question.h"
8 #include "strutl.h"
9
10 #include <dirent.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <ctype.h>
18 #include <time.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <search.h>
22
23 static FILE *outf = NULL;
24
25 static struct template *rfc822db_template_get(struct template_db *db,
26 const char *ltag);
27
28 static int nodetemplatecomp(const void *pa, const void *pb) {
29 return strcmp(((struct template *)pa)->tag,
30 ((struct template *)pb)->tag);
31 }
32
33 static int nodequestioncomp(const void *pa, const void *pb) {
34 return strcmp(((struct question *)pa)->tag,
35 ((struct question *)pb)->tag);
36 }
37
38
39 static void parse_variables(struct question *q, char *string)
40 {
41 char *wc, *owc;
42 if (!string)
43 return;
44 owc = wc = strdup(string);
45
46 while (wc != NULL && *wc != '\0')
47 {
48 char *delim = wc;
49 char *striptmp_var, *striptmp_val;
50 int finished = 0;
51 while (*delim != '=' && *delim != '\0')
52 delim++;
53 if (*delim == '\0')
54 finished = 1;
55 *delim = '\0';
56
57 striptmp_var = strdup(strstrip(wc));
58
59 delim++;
60 wc = delim;
61 while (*delim != '\n' && *delim != '\0')
62 delim++;
63 if (*delim == '\0')
64 finished = 1;
65 *delim = '\0';
66
67 striptmp_val = strdup(strstrip(wc));
68 question_variable_add(q,striptmp_var, striptmp_val);
69
70 free(striptmp_val);
71 free(striptmp_var);
72
73 wc = delim;
74 wc++;
75 if (finished != 0)
76 break;
77
78 while (*wc == ' ' || *wc == '\t')
79 {
80 wc++;
81 }
82
83 }
84 free(owc);
85 }
86
87 static void parse_owners(struct question *q, char *string)
88 {
89 char *wc, *owc;
90
91 if (!string)
92 return;
93
94 owc = wc = strdup(string);
95
96 while (wc != NULL)
97 {
98 char *delim = wc;
99 int finished = 0;
100 while (*delim != ',' && *delim != '\0')
101 delim++;
102 if (*delim == '\0')
103 finished = 1;
104 *delim = '\0';
105 question_owner_add(q,wc);
106 if (finished != 0)
107 break;
108 wc = delim + 1;
109 while (*wc == ' ' || *wc == '\t')
110 {
111 wc++;
112 }
113
114 }
115
116 free(owc);
117 }
118
119 static void parse_flags(struct question *q, char *string)
120 {
121 char *wc, *owc;
122
123 if (!string)
124 return;
125
126 owc = wc = strdup(string);
127
128 while (wc != NULL)
129 {
130 char *delim = wc;
131 int finished = 0;
132 while (*delim != ',' && *delim != '\0')
133 delim++;
134 if (*delim == '\0')
135 finished = 1;
136 *delim = '\0';
137 question_set_flag(q, wc);
138 if (finished != 0)
139 break;
140 wc = delim + 1;
141 while (*wc == ' ' || *wc == '\t')
142 {
143 wc++;
144 }
145 }
146
147 free(owc);
148 }
149
150 static FILE *rfc822db_file_open(struct configuration *config, const char *configpath, int *retval)
151 {
152 const char *path;
153 FILE *inf = NULL;
154 char tmp[1024];
155
156 *retval = DC_OK;
157
158 snprintf(tmp, sizeof(tmp), "%s::path", configpath);
159 path = config->get(config, tmp, 0);
160 if (path == NULL)
161 {
162 INFO(INFO_VERBOSE, "Cannot open database <empty>");
163 *retval = DC_NOTOK;
164 }
165 else if ((inf = fopen(path, "r")) == NULL)
166 {
167 if (errno == ENOENT)
168 {
169 const char *modestr;
170 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
171
172 INFO(INFO_DEBUG, "Database file doesn't exist. Trying to create it");
173
174 snprintf(tmp, sizeof(tmp), "%s::mode", configpath);
175 modestr = config->get(config, tmp, 0);
176 if (modestr)
177 mode = strtol(modestr, NULL, 8);
178
179 outf = fopen(path, "w");
180 if ((outf == NULL) || (fclose(outf) != 0) || (chmod(path, mode) != 0))
181 {
182 INFO(INFO_VERBOSE, "Cannot create database file %s: %s",
183 path, strerror(errno));
184 *retval = DC_NOTOK;
185 }
186 outf = NULL;
187 }
188 else
189 *retval = DC_NOTOK;
190 }
191
192 return inf;
193 }
194
195 static void rfc822db_template_destroyitem(void *data)
196 {
197 template_deref((struct template *) data);
198 }
199
200 /* templates */
201 static int rfc822db_template_initialize(struct template_db *db, struct configuration *cfg)
202 {
203 struct template_db_cache *dbdata;
204 /* fprintf(stderr,"rfc822db_initialize(db,cfg)\n");*/
205 dbdata = malloc(sizeof(struct template_db_cache));
206 if (dbdata == NULL)
207 return DC_NOTOK;
208
209 dbdata->root = NULL;
210 dbdata->iterator = NULL;
211 dbdata->dirty = false;
212 db->data = dbdata;
213
214 return DC_OK;
215 }
216
217 static int rfc822db_template_shutdown(struct template_db *db)
218 {
219 struct template_db_cache *dbdata = db->data;
220 if (dbdata == NULL)
221 return DC_OK;
222 if (dbdata->root)
223 tdestroy(dbdata->root, rfc822db_template_destroyitem);
224 if (dbdata->iterator)
225 di_slist_destroy(dbdata->iterator, rfc822db_template_destroyitem);
226 free(dbdata);
227 db->data = NULL;
228 return DC_OK;
229 }
230
231 /*
232 * Function: rfc822db_template_load
233 * Input: template database
234 * Output: DC_OK/DC_NOTOK
235 * Description: parse a template db file and put it into the cache
236 * Assumptions: the file is in valid rfc822 format
237 */
238 static int rfc822db_template_load(struct template_db *db)
239 {
240 struct template_db_cache *dbdata = db->data;
241 FILE *inf;
242 struct rfc822_header *header = NULL;
243 int openerror;
244
245 INFO(INFO_VERBOSE, "rfc822db_template_load(db)");
246 inf = rfc822db_file_open(db->config, db->configpath, &openerror);
247 if (!inf)
248 {
249 return openerror;
250 }
251
252 while ((header = rfc822_parse_stanza(inf)) != NULL)
253 {
254 struct template *tmp;
255 const char *name;
256 struct rfc822_header *h;
257
258 name = rfc822_header_lookup(header, "name");
259 if (name == NULL)
260 {
261 INFO(INFO_ERROR, "Read a stanza without a name");
262 rfc822_header_destroy(header);
263 continue;
264 }
265
266 tmp = template_new(name);
267 for (h = header; h != NULL; h = h->next)
268 if (strcmp(h->header, "Name") != 0)
269 template_lset(tmp, NULL, h->header, h->value);
270
271 tmp->next = NULL;
272 tsearch(tmp, &dbdata->root, nodetemplatecomp);
273 rfc822_header_destroy(header);
274 }
275
276 fclose(inf);
277
278 return DC_OK;
279 }
280
281 /*
282 * Function: rfc822db_template_reload
283 * Input: template database
284 * Output: DC_OK/DC_NOTOK
285 * Description: reparse a template db file and update the cache; used when
286 * the language is changed
287 * Assumptions: the file is in valid rfc822 format
288 */
289 static int rfc822db_template_reload(struct template_db *db)
290 {
291 struct template_db_cache *dbdata = db->data;
292 char tmp[1024];
293 const char *path;
294 FILE *inf;
295 struct rfc822_header *header = NULL;
296
297 INFO(INFO_VERBOSE, "rfc822db_template_reload(db)");
298 snprintf(tmp, sizeof(tmp), "%s::path", db->configpath);
299 path = db->config->get(db->config, tmp, 0);
300 if (path == NULL ||
301 (inf = fopen(path, "r")) == NULL)
302 {
303 INFO(INFO_VERBOSE, "Cannot open template file %s",
304 path ? path : "<empty>");
305 return DC_NOTOK;
306 }
307
308 while ((header = rfc822_parse_stanza(inf)) != NULL)
309 {
310 struct template *tmp;
311 bool is_new = false;
312 const char *name;
313 struct rfc822_header *h;
314
315 name = rfc822_header_lookup(header, "name");
316 if (name == NULL)
317 {
318 INFO(INFO_ERROR, "Read a stanza without a name");
319 rfc822_header_destroy(header);
320 continue;
321 }
322
323 INFO(INFO_VERBOSE, "Template %s:", name);
324 tmp = rfc822db_template_get(db, name);
325 if (tmp)
326 template_l10nclear(tmp);
327 else
328 {
329 tmp = template_new(name);
330 is_new = true;
331 }
332 for (h = header; h != NULL; h = h->next)
333 if (strcmp(h->header, "Name") != 0) {
334 INFO(INFO_VERBOSE, " %s=%s", h->header, h->value);
335 template_lset(tmp, NULL, h->header, h->value);
336 }
337
338 tmp->next = NULL;
339 if (is_new)
340 tsearch(tmp, &dbdata->root, nodetemplatecomp);
341 else
342 template_deref(tmp);
343 rfc822_header_destroy(header);
344 }
345
346 fclose(inf);
347
348 return DC_OK;
349 }
350
351 static void rfc822db_template_dump(const void *node, const VISIT which, const int depth)
352 {
353 const char *p, *lang;
354 const char **field;
355 const struct template *t = (*(struct template **) node);
356
357 switch (which) {
358 case preorder:
359 break;
360 case endorder:
361 break;
362 case postorder:
363 case leaf:
364 p = template_lget(t, NULL, "tag");
365 INFO(INFO_VERBOSE, "dumping template %s", p);
366
367 for (field = template_fields_list; *field != NULL; field++)
368 {
369 p = template_lget(t, NULL, *field);
370 if (p != NULL)
371 {
372 if (strcmp(*field, "tag") == 0)
373 fprintf(outf, "Name: %s\n", escapestr(p));
374 else
375 fprintf(outf, "%c%s: %s\n",
376 toupper((*field)[0]), (*field)+1, escapestr(p));
377 }
378 }
379
380 lang = template_next_lang(t, NULL);
381 while (lang)
382 {
383 for (field = template_fields_list; *field != NULL; field++)
384 {
385 p = template_lget(t, lang, *field);
386 if (p != NULL && p != template_lget(t, NULL, *field))
387 {
388 if (strcmp(lang, "C") == 0)
389 fprintf(outf, "%c%s-C: %s\n",
390 toupper((*field)[0]), (*field)+1, escapestr(p));
391 else
392 fprintf(outf, "%c%s-%s.UTF-8: %s\n",
393 toupper((*field)[0]), (*field)+1, lang, escapestr(p));
394 }
395 }
396 lang = template_next_lang(t, lang);
397 }
398 fprintf(outf, "\n");
399 }
400 }
401
402 static int rfc822db_save(const void *root, const char *path,
403 void (*action)(const void *nodep, const VISIT which,
404 const int depth))
405 {
406 char *newpath;
407 newpath = malloc(strlen(path) + strlen("-new") + 1);
408 sprintf(newpath, "%s-new", path);
409
410 if ((outf = fopen(newpath, "w")) == NULL)
411 {
412 INFO(INFO_ERROR, "Cannot open file %s: %s",
413 newpath, strerror(errno));
414 free(newpath);
415 return DC_NOTOK;
416 }
417
418 twalk(root, action);
419
420 if (fclose(outf) == EOF)
421 perror("fclose");
422 else
423 {
424 char *oldpath = malloc(strlen(path) + strlen("-old") + 1);
425 sprintf(oldpath, "%s-old", path);
426
427 rename(path, oldpath);
428 free(oldpath);
429
430 rename(newpath, path);
431 }
432 outf = NULL;
433 free(newpath);
434
435 return DC_OK;
436 }
437
438 static int rfc822db_template_save(struct template_db *db)
439 {
440 struct template_db_cache *dbdata = db->data;
441 char tmp[1024];
442 const char *path;
443 struct stat st;
444
445 if (outf != NULL)
446 {
447 INFO(INFO_ERROR, "Internal inconsistency error, outf is not NULL");
448 return DC_NOTOK;
449 }
450
451 snprintf(tmp, sizeof(tmp), "%s::path", db->configpath);
452 path = db->config->get(db->config, tmp, 0);
453 if (path == NULL)
454 {
455 INFO(INFO_ERROR, "Cannot open template file <empty>");
456 return DC_NOTOK;
457 }
458
459 if (!dbdata->dirty && stat(path, &st) == 0)
460 {
461 INFO(INFO_DEBUG, "Template database %s clean; not saving", path);
462 return DC_OK;
463 }
464
465 return rfc822db_save(dbdata->root, path, rfc822db_template_dump);
466 }
467
468 static struct template *rfc822db_template_get(struct template_db *db,
469 const char *ltag)
470 {
471 struct template_db_cache *dbdata = db->data;
472 struct template *t, t2;
473 t2.tag = (char*) ltag; /* Get rid of warning from gcc --
474 this should be safe */
475 t = tfind(&t2, &dbdata->root, nodetemplatecomp);
476 if (t)
477 {
478 t = (*(struct template **) t);
479 template_ref(t);
480 }
481
482 return t;
483 }
484
485 static int rfc822db_template_set(struct template_db *db, struct template *template)
486 {
487 struct template_db_cache *dbdata = db->data;
488 INFO(INFO_VERBOSE, "rfc822db_template_set(db,t=%s)", template->tag);
489
490 tdelete(template, &dbdata->root, nodetemplatecomp);
491 tsearch(template, &dbdata->root, nodetemplatecomp);
492 dbdata->dirty = true;
493
494 template_ref(template);
495
496 return DC_OK;
497 }
498
499 static int rfc822db_template_remove(struct template_db *db, const char *tag)
500 {
501 struct template_db_cache *dbdata = db->data;
502 struct template *t, t2;
503
504 INFO(INFO_VERBOSE, "rfc822db_template_remove(db,tag=%s)",tag);
505
506 memset(&t2, 0, sizeof (struct template));
507 t2.tag = (char*) tag;
508 t = tfind(&t2, &dbdata->root, nodetemplatecomp);
509
510 if (t)
511 {
512 t = *(struct template **) t;
513 tdelete(t, &dbdata->root, nodetemplatecomp);
514 dbdata->dirty = true;
515 template_deref(t);
516 return DC_OK;
517 }
518
519 return DC_NOTOK;
520 }
521
522 /* TODO: This is an ugly hack because there's no better way to do this
523 * within the constraints of twalk() (since there's no user-data argument).
524 * If we ever switch to some other tree API, this should go away
525 * immediately. If we ever need iterate() to be thread-safe, this *needs* to
526 * go away.
527 */
528 static di_slist *template_iterator;
529 static void rfc822db_template_makeiterator(const void *nodep, const VISIT which,
530 const int depth)
531 {
532 if (which == postorder || which == leaf)
533 di_slist_append(template_iterator,
534 template_dup(*(struct template **) nodep));
535 }
536
537 static struct template *rfc822db_template_iterate(struct template_db *db,
538 void **iter)
539 {
540 struct template_db_cache *dbdata = db->data;
541 di_slist_node *node;
542 struct template *t;
543
544 INFO(INFO_VERBOSE, "rfc822db_template_iterate(db,*iter=%p)", *iter);
545
546 node = *(di_slist_node **) iter;
547 if (node == NULL) {
548 if (dbdata->iterator)
549 di_slist_destroy(dbdata->iterator, rfc822db_template_destroyitem);
550 dbdata->iterator = di_slist_alloc();
551 template_iterator = dbdata->iterator; /* non-thread-safe */
552 twalk(dbdata->root, rfc822db_template_makeiterator);
553 template_iterator = NULL;
554 *iter = node = dbdata->iterator->head;
555 } else
556 *iter = node = node->next;
557
558 if (node == NULL) {
559 di_slist_destroy(dbdata->iterator, rfc822db_template_destroyitem);
560 dbdata->iterator = NULL;
561 return NULL;
562 }
563
564 t = (struct template *) node->data;
565 template_ref(t);
566 return t;
567 }
568
569 static void rfc822db_question_destroyitem(void *data)
570 {
571 question_deref((struct question *) data);
572 }
573
574 /* config database */
575 static int rfc822db_question_initialize(struct question_db *db, struct configuration *cfg)
576 {
577 struct question_db_cache *dbdata;
578 /* fprintf(stderr,"rfc822db_initialize(db,cfg)\n");*/
579 dbdata = malloc(sizeof(struct question_db_cache));
580
581 if (dbdata == NULL)
582 return DC_NOTOK;
583
584 dbdata->root = NULL;
585 dbdata->iterator = NULL;
586 dbdata->dirty = false;
587 db->data = dbdata;
588
589 return DC_OK;
590 }
591
592 static int rfc822db_question_shutdown(struct question_db *db)
593 {
594 struct question_db_cache *dbdata = db->data;
595 if (dbdata == NULL)
596 return DC_OK;
597 if (dbdata->root)
598 tdestroy(dbdata->root, rfc822db_question_destroyitem);
599 if (dbdata->iterator)
600 di_slist_destroy(dbdata->iterator, rfc822db_question_destroyitem);
601 free(dbdata);
602 db->data = NULL;
603 return DC_OK;
604 }
605
606 /*
607 * Function: rfc822db_question_load
608 * Input: question database
609 * Output: DC_OK/DC_NOTOK
610 * Description: parse a question db file and put it into the cache
611 * Assumptions: the file is in valid rfc822 format
612 */
613 static int rfc822db_question_load(struct question_db *db)
614 {
615 struct question_db_cache *dbdata = db->data;
616 FILE *inf;
617 struct rfc822_header *header = NULL;
618 int openerror;
619
620 INFO(INFO_VERBOSE, "rfc822db_question_load(db)");
621 inf = rfc822db_file_open(db->config, db->configpath, &openerror);
622 if (!inf)
623 {
624 return openerror;
625 }
626
627 while ((header = rfc822_parse_stanza(inf)) != NULL)
628 {
629 struct question *tmp;
630 const char *name;
631
632 name = rfc822_header_lookup(header, "name");
633
634 if (name == NULL || *name == 0)
635 {
636 INFO(INFO_ERROR, "Read a stanza without a name");
637 rfc822_header_destroy(header);
638 continue;
639 }
640
641 tmp = question_new(name);
642
643 question_setvalue(tmp, rfc822_header_lookup(header, "value"));
644 parse_flags(tmp, rfc822_header_lookup(header,"flags"));
645 parse_owners(tmp, rfc822_header_lookup(header, "owners"));
646 parse_variables(tmp, rfc822_header_lookup(header, "variables"));
647 tmp->template = db->tdb->methods.get(db->tdb, rfc822_header_lookup(header, "template"));
648 if (tmp->template == NULL) {
649 tmp->template = template_new(name);
650 db->tdb->methods.set(db->tdb, tmp->template);
651 }
652 tsearch(tmp, &dbdata->root, nodequestioncomp);
653 rfc822_header_destroy(header);
654 }
655
656 fclose(inf);
657
658 return DC_OK;
659 }
660
661 static void rfc822db_question_dump(const void *node, const VISIT which, const int depth)
662 {
663 struct questionowner *owner;
664 struct questionvariable *var;
665
666 const struct question *q = (*(struct question **) node);
667 switch (which) {
668 case preorder:
669 break;
670 case endorder:
671 break;
672 case postorder:
673 case leaf:
674
675 INFO(INFO_VERBOSE, "dumping question %s", (q)->tag);
676 fprintf(outf, "Name: %s\n", escapestr((q)->tag));
677 fprintf(outf, "Template: %s\n", escapestr((q)->template->tag));
678 if ((q)->value)
679 fprintf(outf, "Value: %s\n", escapestr((q)->value));
680
681 if ((owner = (q)->owners))
682 {
683 fprintf(outf, "Owners: ");
684 for (; owner != NULL; owner = owner->next)
685 {
686 fprintf(outf, "%s", escapestr(owner->owner));
687 if (owner->next != NULL)
688 fprintf(outf, ", ");
689 }
690 fprintf(outf, "\n");
691 }
692
693 if (question_get_flag(q, DC_QFLAG_SEEN))
694 {
695 fprintf(outf, "Flags: seen\n");
696 }
697
698 if ((var = (q)->variables))
699 {
700 fprintf(outf, "Variables:\n");
701 for (; var != NULL; var = var->next)
702 {
703 /* escapestr uses a static buf, so we do this in
704 * two steps */
705 fprintf(outf, " %s = ", (var->variable ? escapestr(var->variable) : ""));
706 fprintf(outf, "%s\n", (var->value ? escapestr(var->value) : ""));
707 }
708 }
709
710 fprintf(outf, "\n");
711 }
712 }
713
714
715 static int rfc822db_question_save(struct question_db *db)
716 {
717 struct question_db_cache *dbdata = db->data;
718 const char *path;
719 char tmp[1024];
720 struct stat st;
721
722 snprintf(tmp, sizeof(tmp), "%s::path", db->configpath);
723 path = db->config->get(db->config, tmp, 0);
724 if (path == NULL)
725 {
726 INFO(INFO_ERROR, "Cannot open question file <empty>");
727 return DC_NOTOK;
728 }
729
730 if (!dbdata->dirty && stat(path, &st) == 0)
731 {
732 INFO(INFO_DEBUG, "Question database %s clean; not saving", path);
733 return DC_OK;
734 }
735
736 return rfc822db_save(dbdata->root, path, rfc822db_question_dump);
737 }
738
739 static struct question *rfc822db_question_get(struct question_db *db,
740 const char *ltag)
741 {
742 struct question_db_cache *dbdata = db->data;
743 struct question *q, q2;
744
745 memset(&q2, 0, sizeof (struct question));
746 q2.tag = strdup(ltag);
747 q = tfind(&q2, &dbdata->root, nodequestioncomp);
748 free(q2.tag);
749 if (q != NULL)
750 {
751 q = *(struct question **)q;
752 question_ref(q);
753 }
754
755 return q;
756 }
757
758 static int rfc822db_question_set(struct question_db *db, struct question *question)
759 {
760 struct question_db_cache *dbdata = db->data;
761 struct question *q;
762
763 INFO(INFO_VERBOSE, "rfc822db_question_set(db,q=%s,q=%p)", question->tag, question);
764
765 /* merge old and new owner lists */
766 q = tfind(question, &dbdata->root, nodequestioncomp);
767 if (q != NULL) {
768 q = *(struct question **)q;
769 const struct questionowner *owner = q->owners;
770 while (owner) {
771 question_owner_add(question, owner->owner);
772 owner = owner->next;
773 }
774 }
775
776 tdelete(question, &dbdata->root, nodequestioncomp);
777 tsearch(question, &dbdata->root, nodequestioncomp);
778 dbdata->dirty = true;
779 question_ref(question);
780
781 return DC_OK;
782 }
783
784 static int rfc822db_question_remove(struct question_db *db, const char *tag)
785 {
786 struct question_db_cache *dbdata = db->data;
787 struct question *q, q2;
788
789 INFO(INFO_VERBOSE, "rfc822db_question_remove(db,tag=%s)", tag);
790
791 memset(&q2, 0, sizeof (struct question));
792 q2.tag = (char*) tag;
793 q = tfind(&q2, &dbdata->root, nodequestioncomp);
794 if (q != NULL) {
795 q = *(struct question **) q;
796 tdelete(q, &dbdata->root, nodequestioncomp);
797 dbdata->dirty = true;
798 question_deref(q);
799 return DC_OK;
800 }
801 return DC_NOTOK;
802 }
803
804 static int rfc822db_question_disown(struct question_db *db, const char *tag,
805 const char *owner)
806 {
807 struct question *q = rfc822db_question_get(db, tag);
808 /* fprintf(stderr,"rfc822db_question_disown(db, tag=%s, owner=%s)\n",
809 tag,owner);*/
810 if (q == NULL)
811 return DC_NOTOK;
812 question_owner_delete(q, owner);
813 if (q->owners == NULL)
814 rfc822db_question_remove(db, q->tag);
815 else
816 rfc822db_question_set(db, q);
817 question_deref(q);
818
819 return DC_OK;
820 }
821
822 /* TODO: This is an ugly hack because there's no better way to do this
823 * within the constraints of twalk() (since there's no user-data argument).
824 * If we ever switch to some other tree API, this should go away
825 * immediately. If we ever need iterate() to be thread-safe, this *needs* to
826 * go away.
827 */
828 static di_slist *question_iterator;
829 static void rfc822db_question_makeiterator(const void *nodep, const VISIT which,
830 const int depth)
831 {
832 if (which == postorder || which == leaf)
833 di_slist_append(question_iterator,
834 question_dup(*(struct question **) nodep));
835 }
836
837 static struct question *rfc822db_question_iterate(struct question_db *db,
838 void **iter)
839 {
840 struct question_db_cache *dbdata = db->data;
841 di_slist_node *node;
842 struct question *q;
843
844 INFO(INFO_VERBOSE, "rfc822db_question_iterate(db,*iter=%p)", *iter);
845
846 node = *(di_slist_node **) iter;
847 if (node == NULL) {
848 if (dbdata->iterator)
849 di_slist_destroy(dbdata->iterator, rfc822db_question_destroyitem);
850 dbdata->iterator = di_slist_alloc();
851 question_iterator = dbdata->iterator; /* non-thread-safe */
852 twalk(dbdata->root, rfc822db_question_makeiterator);
853 question_iterator = NULL;
854 *iter = node = dbdata->iterator->head;
855 } else
856 *iter = node = node->next;
857
858 if (node == NULL) {
859 di_slist_destroy(dbdata->iterator, rfc822db_question_destroyitem);
860 dbdata->iterator = NULL;
861 return NULL;
862 }
863
864 q = (struct question *) node->data;
865 question_ref(q);
866 return q;
867 }
868
869 struct template_db_module debconf_template_db_module = {
870 .initialize = rfc822db_template_initialize,
871 .shutdown = rfc822db_template_shutdown,
872 .load = rfc822db_template_load,
873 .reload = rfc822db_template_reload,
874 .save = rfc822db_template_save,
875 .set = rfc822db_template_set,
876 .get = rfc822db_template_get,
877 .remove = rfc822db_template_remove,
878 .iterate = rfc822db_template_iterate,
879 };
880
881 struct question_db_module debconf_question_db_module = {
882 .initialize = rfc822db_question_initialize,
883 .shutdown = rfc822db_question_shutdown,
884 .load = rfc822db_question_load,
885 .save = rfc822db_question_save,
886 .set = rfc822db_question_set,
887 .get = rfc822db_question_get,
888 .disown = rfc822db_question_disown,
889 .remove = rfc822db_question_remove,
890 .iterate = rfc822db_question_iterate,
891 };
892
893 static void dump_question(struct question *q) {
894 fprintf(stderr,"\nDUMPING QUESTION\n");
895 fprintf(stderr,"Question: %s\n", q->tag);
896 fprintf(stderr,"Value: %s\n", q->value);
897 fprintf(stderr,"Ref count: %d\n", q->ref);
898 fprintf(stderr,"Flags: %d\n", q->flags);
899 fprintf(stderr,"Template: %p", q->template);
900 fprintf(stderr,"(%s)\n", q->template->tag);
901 }
902
903 #if 0
904 int main(int argc, char** argv)
905 {
906 FILE *in;
907 struct question *t = NULL;
908 /* struct rfc822_header *head; */
909 in = fopen("testdata", "r");
910 t = rfc822db_parse_questions(t,in);
911
912 while (t) {
913 struct questionowner *q = t->owners;
914 struct questionvariable *v = t->variables;
915 printf("\nname: %s\nvalue: %s\n", t->tag, t->value);
916 while(q) {
917 printf("owner: %s\n",q->owner);
918 q = q->next;
919 }
920
921 while(v) {
922 printf("vars: %s = %s\n",v->variable, v->value);
923 v = v->next;
924 }
925
926 t = t->next;
927 }
928
929 /* head = rfc822db_parse_stanza(in);
930 while (head != NULL) {
931 printf("key: %s\nvalue:%s\n", head->header, head->value);
932 head = head->next;
933 }*/
934 return 0;
935 }
936 #endif