| 1 |
#include "common.h" |
| 2 |
#include "configuration.h" |
| 3 |
#include "database.h" |
| 4 |
#include "textdb.h" |
| 5 |
#include "template.h" |
| 6 |
#include "question.h" |
| 7 |
#include "strutl.h" |
| 8 |
|
| 9 |
#include <dirent.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <string.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <sys/types.h> |
| 14 |
#include <sys/stat.h> |
| 15 |
|
| 16 |
static void translate_tag_name(char *buf) |
| 17 |
{ |
| 18 |
/* remove / from the tag name so that we can use it as a filename */ |
| 19 |
char *t = buf; |
| 20 |
for (; *t != 0; t++) |
| 21 |
if (*t == '/') *t = ':'; /* : is illegal in templates etc */ |
| 22 |
} |
| 23 |
|
| 24 |
static char *template_filename(struct template_db *db, const char *tag) |
| 25 |
{ |
| 26 |
static char filename[1024]; |
| 27 |
static char tmp[1024]; |
| 28 |
char tagname[1024]; |
| 29 |
filename[0] = 0; |
| 30 |
|
| 31 |
strncpy(tagname, tag, sizeof(tagname)); |
| 32 |
translate_tag_name(tagname); |
| 33 |
|
| 34 |
snprintf(tmp, sizeof(tmp), "%s::path", db->configpath); |
| 35 |
|
| 36 |
snprintf(filename, sizeof(filename), "%s/%s", |
| 37 |
db->config->get(db->config, tmp, ""), tagname); |
| 38 |
|
| 39 |
return filename; |
| 40 |
} |
| 41 |
|
| 42 |
static char *question_filename(struct question_db *db, const char *tag) |
| 43 |
{ |
| 44 |
static char filename[1024]; |
| 45 |
static char tmp[1024]; |
| 46 |
char tagname[1024]; |
| 47 |
|
| 48 |
filename[0] = 0; |
| 49 |
|
| 50 |
strncpy(tagname, tag, sizeof(tagname)); |
| 51 |
translate_tag_name(tagname); |
| 52 |
|
| 53 |
snprintf(tmp, sizeof(tmp), "%s::path", db->configpath); |
| 54 |
|
| 55 |
snprintf(filename, sizeof(filename), "%s/%s", |
| 56 |
db->config->get(db->config, tmp, "."), tagname); |
| 57 |
|
| 58 |
return filename; |
| 59 |
} |
| 60 |
|
| 61 |
static struct template *textdb_lookup_cached_template( |
| 62 |
const struct template_db *db, const char *tag) |
| 63 |
{ |
| 64 |
struct template_db_cache *dbdata = db->data; |
| 65 |
struct template *result; |
| 66 |
for (result = dbdata->templates; result; result = result->next) |
| 67 |
{ |
| 68 |
if (strcmp(result->tag, tag) == 0) break; |
| 69 |
} |
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
static void textdb_remove_cached_template(struct template_db *db, |
| 74 |
const char *tag) |
| 75 |
{ |
| 76 |
struct template_db_cache *dbdata = db->data; |
| 77 |
struct template **result; |
| 78 |
for (result = &dbdata->templates; *result; result = &(*result)->next) |
| 79 |
{ |
| 80 |
if (strcmp((*result)->tag, tag) == 0) |
| 81 |
{ |
| 82 |
template_deref(*result); |
| 83 |
*result = (*result)->next; |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
static struct question *textdb_lookup_cached_question( |
| 91 |
const struct database *db, const char *tag) |
| 92 |
{ |
| 93 |
struct question_db_cache *dbdata = db->data; |
| 94 |
struct question *result; |
| 95 |
for (result = dbdata->questions; result; result = result->next) |
| 96 |
{ |
| 97 |
if (strcmp(result->tag, tag) == 0) break; |
| 98 |
} |
| 99 |
return result; |
| 100 |
} |
| 101 |
*/ |
| 102 |
|
| 103 |
static int textdb_template_initialize(struct template_db *db, struct configuration *cfg) |
| 104 |
{ |
| 105 |
struct template_db_cache *dbdata; |
| 106 |
dbdata = malloc(sizeof(struct template_db_cache)); |
| 107 |
|
| 108 |
if (dbdata == NULL) |
| 109 |
return DC_NOTOK; |
| 110 |
|
| 111 |
dbdata->templates = NULL; |
| 112 |
db->data = dbdata; |
| 113 |
|
| 114 |
return DC_OK; |
| 115 |
} |
| 116 |
|
| 117 |
static int textdb_template_shutdown(struct template_db *db) |
| 118 |
{ |
| 119 |
struct template_db_cache *dbdata = db->data; |
| 120 |
struct template *t; |
| 121 |
|
| 122 |
while (dbdata->templates != NULL) |
| 123 |
{ |
| 124 |
t = dbdata->templates; |
| 125 |
dbdata->templates = dbdata->templates->next; |
| 126 |
t->next = NULL; |
| 127 |
template_deref(t); |
| 128 |
} |
| 129 |
|
| 130 |
return DC_OK; |
| 131 |
} |
| 132 |
|
| 133 |
static int textdb_template_set(struct template_db *db, struct template *t) |
| 134 |
{ |
| 135 |
FILE *outf; |
| 136 |
char *filename; |
| 137 |
const char *p, *lang; |
| 138 |
const char **field; |
| 139 |
|
| 140 |
if (template_lget(t, NULL, "tag") == NULL) return DC_NOTOK; |
| 141 |
filename = template_filename(db, template_lget(t, NULL, "tag")); |
| 142 |
|
| 143 |
if ((outf = fopen(filename, "w")) == NULL) |
| 144 |
return DC_NOTOK; |
| 145 |
|
| 146 |
fprintf(outf, "template {\n"); |
| 147 |
|
| 148 |
for (field = template_fields_list; *field != NULL; field++) |
| 149 |
{ |
| 150 |
p = template_lget(t, NULL, *field); |
| 151 |
if (p != NULL) |
| 152 |
fprintf(outf, "\t%s \"%s\";\n", *field, escapestr(p)); |
| 153 |
} |
| 154 |
|
| 155 |
lang = template_next_lang(t, NULL); |
| 156 |
while (lang) |
| 157 |
{ |
| 158 |
for (field = template_fields_list; *field != NULL; field++) |
| 159 |
{ |
| 160 |
p = template_lget(t, lang, *field); |
| 161 |
if (p != NULL && p != template_lget(t, NULL, *field)) |
| 162 |
{ |
| 163 |
if (strcmp(lang, "C") == 0) |
| 164 |
fprintf(outf, "\t%s-C \"%s\";\n", |
| 165 |
*field, escapestr(p)); |
| 166 |
else |
| 167 |
fprintf(outf, "\t%s-%s.UTF-8 \"%s\";\n", |
| 168 |
*field, lang, escapestr(p)); |
| 169 |
} |
| 170 |
} |
| 171 |
lang = template_next_lang(t, lang); |
| 172 |
} |
| 173 |
|
| 174 |
fprintf(outf, "};\n"); |
| 175 |
fclose(outf); |
| 176 |
|
| 177 |
return DC_OK; |
| 178 |
} |
| 179 |
|
| 180 |
static struct template *textdb_template_get_real(struct template_db *db, |
| 181 |
const char *ltag) |
| 182 |
{ |
| 183 |
struct configuration *rec; |
| 184 |
struct template *t; |
| 185 |
char *filename; |
| 186 |
struct configitem *node; |
| 187 |
|
| 188 |
if (ltag == NULL) return DC_NOTOK; |
| 189 |
filename = template_filename(db, ltag); |
| 190 |
|
| 191 |
rec = config_new(); |
| 192 |
if (rec->read(rec, filename) != DC_OK) |
| 193 |
{ |
| 194 |
config_delete(rec); |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
|
| 198 |
node = rec->tree(rec, "template"); |
| 199 |
if (node == 0 || node->child == 0 || node->child->value == 0) |
| 200 |
{ |
| 201 |
config_delete(rec); |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
|
| 205 |
t = template_new(0); |
| 206 |
node = node->child; |
| 207 |
while (node) |
| 208 |
{ |
| 209 |
template_lset(t, NULL, node->tag, |
| 210 |
STRDUP(unescapestr(node->value))); |
| 211 |
node = node->next; |
| 212 |
} |
| 213 |
|
| 214 |
config_delete(rec); |
| 215 |
|
| 216 |
return t; |
| 217 |
} |
| 218 |
|
| 219 |
static struct template *textdb_template_get(struct template_db *db, |
| 220 |
const char *ltag) |
| 221 |
{ |
| 222 |
struct template *result; |
| 223 |
|
| 224 |
result = textdb_lookup_cached_template(db, ltag); |
| 225 |
if (!result && (result = textdb_template_get_real(db, ltag))) |
| 226 |
{ |
| 227 |
struct template_db_cache *dbdata = db->data; |
| 228 |
result->next = dbdata->templates; |
| 229 |
dbdata->templates = result; |
| 230 |
} |
| 231 |
if (result) |
| 232 |
template_ref(result); |
| 233 |
return result; |
| 234 |
} |
| 235 |
|
| 236 |
static int textdb_template_remove(struct template_db *db, const char *tag) |
| 237 |
{ |
| 238 |
char *filename; |
| 239 |
|
| 240 |
if (tag == NULL) return DC_NOTOK; |
| 241 |
|
| 242 |
textdb_remove_cached_template(db, tag); |
| 243 |
|
| 244 |
filename = template_filename(db, tag); |
| 245 |
if (unlink(filename) == 0) |
| 246 |
return DC_OK; |
| 247 |
else |
| 248 |
return DC_NOTOK; |
| 249 |
} |
| 250 |
|
| 251 |
static struct template *textdb_template_iterate(struct template_db *db, |
| 252 |
void **iter) |
| 253 |
{ |
| 254 |
DIR *dir; |
| 255 |
struct dirent *ent; |
| 256 |
char tmp[1024]; |
| 257 |
|
| 258 |
if (*iter == NULL) |
| 259 |
{ |
| 260 |
snprintf(tmp, sizeof(tmp), "%s::path", db->configpath); |
| 261 |
dir = opendir(db->config->get(db->config, tmp, "")); |
| 262 |
if (dir == NULL) |
| 263 |
return NULL; |
| 264 |
*iter = dir; |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
dir = (DIR *)*iter; |
| 269 |
} |
| 270 |
|
| 271 |
if ((ent = readdir(dir)) == NULL) |
| 272 |
{ |
| 273 |
closedir(dir); |
| 274 |
return NULL; |
| 275 |
} |
| 276 |
|
| 277 |
return textdb_template_get(db, ent->d_name); |
| 278 |
} |
| 279 |
|
| 280 |
static int textdb_question_initialize(struct question_db *db, struct configuration *cfg) |
| 281 |
{ |
| 282 |
struct question_db_cache *dbdata; |
| 283 |
dbdata = malloc(sizeof(struct question_db_cache)); |
| 284 |
|
| 285 |
if (dbdata == NULL) |
| 286 |
return DC_NOTOK; |
| 287 |
|
| 288 |
dbdata->questions = NULL; |
| 289 |
db->data = dbdata; |
| 290 |
|
| 291 |
return DC_OK; |
| 292 |
} |
| 293 |
|
| 294 |
static int textdb_question_shutdown(struct question_db *db) |
| 295 |
{ |
| 296 |
struct question_db_cache *dbdata = db->data; |
| 297 |
struct question *q; |
| 298 |
|
| 299 |
while (dbdata->questions != NULL) |
| 300 |
{ |
| 301 |
q = dbdata->questions; |
| 302 |
dbdata->questions = dbdata->questions->next; |
| 303 |
q->next = q->prev = NULL; |
| 304 |
question_deref(q); |
| 305 |
} |
| 306 |
|
| 307 |
return DC_OK; |
| 308 |
} |
| 309 |
|
| 310 |
static int textdb_question_set(struct question_db *db, struct question *q) |
| 311 |
{ |
| 312 |
FILE *outf; |
| 313 |
char *filename; |
| 314 |
struct questionvariable *var; |
| 315 |
struct questionowner *owner; |
| 316 |
|
| 317 |
if (q->tag == NULL) return DC_NOTOK; |
| 318 |
filename = question_filename(db, q->tag); |
| 319 |
|
| 320 |
if ((outf = fopen(filename, "w")) == NULL) |
| 321 |
return DC_NOTOK; |
| 322 |
|
| 323 |
fprintf(outf, "question {\n"); |
| 324 |
fprintf(outf, "\ttag \"%s\";\n", escapestr(q->tag)); |
| 325 |
fprintf(outf, "\tvalue \"%s\";\n", (q->value ? escapestr(q->value) : "")); |
| 326 |
fprintf(outf, "\tflags 0x%08X;\n", q->flags); |
| 327 |
fprintf(outf, "\ttemplate \"%s\";\n", escapestr(q->template->tag)); |
| 328 |
if ((var = q->variables)) |
| 329 |
{ |
| 330 |
fprintf(outf, "\tvariables {\n"); |
| 331 |
do { |
| 332 |
/* escapestr uses a static buf, so we do this in |
| 333 |
* two steps */ |
| 334 |
fprintf(outf, "\t\t%s ", escapestr(var->variable)); |
| 335 |
fprintf(outf, "\"%s\";\n", escapestr(var->value)); |
| 336 |
} while ((var = var->next)); |
| 337 |
fprintf(outf, "\t};\n"); |
| 338 |
} |
| 339 |
if ((owner = q->owners)) |
| 340 |
{ |
| 341 |
fprintf(outf, "\towners:: {\n"); |
| 342 |
do { |
| 343 |
fprintf(outf, "\t\t\"%s\";\n", escapestr(owner->owner)); |
| 344 |
} while ((owner = owner->next)); |
| 345 |
fprintf(outf, "\t};\n"); |
| 346 |
} |
| 347 |
|
| 348 |
fprintf(outf, "};\n"); |
| 349 |
fclose(outf); |
| 350 |
|
| 351 |
return DC_OK; |
| 352 |
} |
| 353 |
|
| 354 |
static struct question *textdb_question_get(struct question_db *db, |
| 355 |
const char *ltag) |
| 356 |
{ |
| 357 |
struct configuration *rec; |
| 358 |
struct question *q; |
| 359 |
char *filename; |
| 360 |
struct configitem *node; |
| 361 |
|
| 362 |
if (ltag == NULL) return DC_NOTOK; |
| 363 |
filename = question_filename(db, ltag); |
| 364 |
|
| 365 |
INFO(INFO_DEBUG, "%s: filename = [%s]", __FILE__, filename); |
| 366 |
|
| 367 |
rec = config_new(); |
| 368 |
if (rec->read(rec, filename) != DC_OK) |
| 369 |
{ |
| 370 |
config_delete(rec); |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
|
| 374 |
q = question_new(0); |
| 375 |
|
| 376 |
q->tag = STRDUP(unescapestr(rec->get(rec, "question::tag", 0))); |
| 377 |
q->value = STRDUP(unescapestr(rec->get(rec, "question::value", 0))); |
| 378 |
q->flags = rec->geti(rec, "question::flags", 0); |
| 379 |
q->template = db->tdb->methods.get(db->tdb, |
| 380 |
unescapestr(rec->get(rec, "question::template", ""))); |
| 381 |
|
| 382 |
/* TODO: variables and owners */ |
| 383 |
if ((node = rec->tree(rec, "question::variables")) != 0) |
| 384 |
{ |
| 385 |
node = node->child; |
| 386 |
for (; node != 0; node = node->next) |
| 387 |
question_variable_add(q, node->tag, node->value); |
| 388 |
} |
| 389 |
|
| 390 |
if ((node = rec->tree(rec, "question::owners")) != 0) |
| 391 |
{ |
| 392 |
node = node->child; |
| 393 |
for (; node != 0; node = node->next) |
| 394 |
if (node->tag && node->tag[0] != 0 && node->tag[0] != ':') |
| 395 |
question_owner_add(q, node->tag); |
| 396 |
} |
| 397 |
|
| 398 |
INFO(INFO_DEBUG, "Read q = %s", q->tag); |
| 399 |
|
| 400 |
if (q->tag == 0 || q->value == 0 || q->template == 0) |
| 401 |
{ |
| 402 |
question_deref(q); |
| 403 |
q = 0; |
| 404 |
} |
| 405 |
|
| 406 |
config_delete(rec); |
| 407 |
|
| 408 |
return q; |
| 409 |
} |
| 410 |
|
| 411 |
static int textdb_question_disown(struct question_db *db, const char *tag, |
| 412 |
const char *owner) |
| 413 |
{ |
| 414 |
struct question *q = textdb_question_get(db, tag); |
| 415 |
if (q == NULL) return DC_NOTOK; |
| 416 |
question_owner_delete(q, owner); |
| 417 |
textdb_question_set(db, q); |
| 418 |
question_deref(q); |
| 419 |
return DC_OK; |
| 420 |
} |
| 421 |
|
| 422 |
static int textdb_question_remove(struct question_db *db, const char *tag) |
| 423 |
{ |
| 424 |
char *filename; |
| 425 |
|
| 426 |
if (tag == NULL) return DC_NOTOK; |
| 427 |
|
| 428 |
/* questions are not cached at present */ |
| 429 |
|
| 430 |
filename = question_filename(db, tag); |
| 431 |
if (unlink(filename) == 0) |
| 432 |
return DC_OK; |
| 433 |
else |
| 434 |
return DC_NOTOK; |
| 435 |
} |
| 436 |
|
| 437 |
static struct question *textdb_question_iterate(struct question_db *db, |
| 438 |
void **iter) |
| 439 |
{ |
| 440 |
DIR *dir; |
| 441 |
struct dirent *ent; |
| 442 |
static const char *path = ""; |
| 443 |
char tmp[1024]; |
| 444 |
int ret; |
| 445 |
struct stat st; |
| 446 |
|
| 447 |
if (*iter == NULL) |
| 448 |
{ |
| 449 |
snprintf(tmp, sizeof(tmp), "%s::path", db->configpath); |
| 450 |
path = db->config->get(db->config, tmp, "."); |
| 451 |
INFO(INFO_VERBOSE, "Checking %s -> %s", tmp, path); |
| 452 |
|
| 453 |
dir = opendir(path); |
| 454 |
if (dir == NULL) |
| 455 |
return NULL; |
| 456 |
*iter = dir; |
| 457 |
} |
| 458 |
else |
| 459 |
{ |
| 460 |
dir = (DIR *)*iter; |
| 461 |
} |
| 462 |
|
| 463 |
do { |
| 464 |
if ((ent = readdir(dir)) == NULL) |
| 465 |
{ |
| 466 |
INFO(INFO_DEBUG, "readdir returned NULL"); |
| 467 |
closedir(dir); |
| 468 |
return NULL; |
| 469 |
} |
| 470 |
|
| 471 |
snprintf(tmp, sizeof(tmp), "%s/%s", path, ent->d_name); |
| 472 |
ret = stat(tmp, &st); |
| 473 |
} while (ret < 0 || S_ISDIR(st.st_mode)); |
| 474 |
|
| 475 |
INFO(INFO_DEBUG, "Getting %s", ent->d_name); |
| 476 |
return textdb_question_get(db, ent->d_name); |
| 477 |
} |
| 478 |
|
| 479 |
struct template_db_module debconf_template_db_module = { |
| 480 |
.initialize = textdb_template_initialize, |
| 481 |
.shutdown = textdb_template_shutdown, |
| 482 |
.set = textdb_template_set, |
| 483 |
.get = textdb_template_get, |
| 484 |
.remove = textdb_template_remove, |
| 485 |
.iterate = textdb_template_iterate, |
| 486 |
}; |
| 487 |
|
| 488 |
struct question_db_module debconf_question_db_module = { |
| 489 |
.initialize = textdb_question_initialize, |
| 490 |
.shutdown = textdb_question_shutdown, |
| 491 |
.set = textdb_question_set, |
| 492 |
.get = textdb_question_get, |
| 493 |
.disown = textdb_question_disown, |
| 494 |
.remove = textdb_question_remove, |
| 495 |
.iterate = textdb_question_iterate, |
| 496 |
}; |
| 497 |
|
| 498 |
/* |
| 499 |
Local Variables: |
| 500 |
c-basic-offset: 8 |
| 501 |
tab-width: 8 |
| 502 |
indent-tabs-mode: t |
| 503 |
End: |
| 504 |
*/ |