Debian Codegraph @

src/modules/db/stack/stack.c

raw
1 /***********************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * File: database.c
7 *
8 * Description: database interface routines
9 *
10 * cdebconf is (c) 2000-2001 Randolph Chung and others under the following
11 * license.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 ***********************************************************************/
37 #include "common.h"
38 #include "database.h"
39 #include "configuration.h"
40 #include "question.h"
41 #include "template.h"
42 #include "priority.h"
43 #include "stack.h"
44
45 #include <dlfcn.h>
46 #include <unistd.h>
47 #include <string.h>
48
49 /****************************************************************************
50 *
51 * Template database
52 *
53 ***************************************************************************/
54
55 static int stack_template_db_initialize(struct template_db *db, struct configuration *cfg)
56 {
57 struct template_stack *tstack = NULL;
58 char *configpath;
59 struct configitem *tmp, *child;
60
61 if (asprintf(&configpath, "%s::stack", db->configpath) == -1)
62 return DC_NOTOK;
63 tmp = cfg->tree(cfg, configpath);
64 free(configpath);
65
66 for (child = tmp->child; child != NULL; child = child->next) {
67 if (tstack != NULL) {
68 tstack->next = malloc(sizeof(struct template_stack));
69 tstack = tstack->next;
70 } else {
71 tstack = malloc(sizeof(struct template_stack));
72 db->data = tstack;
73 }
74 tstack->db = template_db_new(cfg, child->value);
75 tstack->next = NULL;
76 }
77
78 return DC_OK;
79 }
80
81 static int stack_template_db_shutdown(struct template_db *db)
82 {
83 struct template_stack *tstack = (struct template_stack *)db->data;
84 while (tstack) {
85 struct template_stack *next = tstack->next;
86 template_db_delete(tstack->db);
87 DELETE(tstack);
88 tstack = next;
89 }
90 return DC_OK;
91 }
92
93 static int stack_template_db_load(struct template_db *db) {
94 int ret = DC_NOTOK;
95 struct template_stack *tstack = (struct template_stack *)db->data;
96 while (tstack) {
97 /* ignore errors */
98 if ((tstack->db->methods.load(tstack->db)) == DC_OK)
99 ret = DC_OK;
100 tstack = tstack->next;
101 }
102 return ret;
103 }
104
105 static int stack_template_db_reload(struct template_db *db) {
106 int ret = DC_NOTOK;
107 struct template_stack *tstack = (struct template_stack *)db->data;
108 while (tstack) {
109 /* ignore errors */
110 if ((tstack->db->methods.reload(tstack->db)) == DC_OK)
111 ret = DC_OK;
112 tstack = tstack->next;
113 }
114 return ret;
115 }
116
117 static int stack_template_db_save(struct template_db *db) {
118 struct template_stack *tstack = (struct template_stack *)db->data;
119 int ret = DC_NOTOK;
120 while (tstack) {
121 /* ignore errors */
122 if (tstack->db->methods.save(tstack->db) == DC_OK)
123 ret = DC_OK;
124 tstack = tstack->next;
125 }
126 return ret;
127 }
128
129 /* TODO: compare with what we get from db_get and see if nothing has
130 * changed. if so, just return.*/
131
132 static int stack_template_db_set(struct template_db *db, struct template *t)
133 {
134 struct template_stack *tstack = (struct template_stack *)db->data;
135 int ret;
136 while (tstack) {
137 ret = tstack->db->methods.accept(tstack->db, t->tag, t->type);
138 if (ret == DC_REJECT) {
139 tstack->db->methods.remove(tstack->db, t->tag);
140 tstack = tstack->next;
141 continue;
142 }
143 ret = tstack->db->methods.set(tstack->db, t);
144 switch (ret) {
145 case DC_OK:
146 return DC_OK;
147 case DC_NOTOK:
148 return DC_NOTOK;
149 case DC_REJECT: /* obsolete as return code from set() */
150 tstack->db->methods.remove(tstack->db, t->tag);
151 tstack = tstack->next;
152 continue;
153 }
154 }
155 /* everybody rejected it, it seems */
156 return DC_REJECT;
157 }
158
159 static struct template *stack_template_db_get(struct template_db *db,
160 const char *name)
161 {
162 struct template_stack *tstack = (struct template_stack *)db->data;
163 struct template *t;
164 while (tstack) {
165 if ((t = tstack->db->methods.get(tstack->db, name)) != NULL)
166 return t;
167 tstack = tstack->next;
168 }
169 return NULL;
170 }
171
172 static int stack_template_db_remove(struct template_db *db, const char *name)
173 {
174 struct template_stack *tstack = (struct template_stack *)db->data;
175 struct template *t;
176 while (tstack) {
177 t = tstack->db->methods.get(tstack->db, name);
178 if (t) {
179 template_deref(t);
180 return tstack->db->methods.remove(tstack->db, name);
181 }
182 tstack = tstack->next;
183 }
184 /* nobody had it? fail. */
185 return DC_NOTOK;
186 }
187
188 static int stack_template_db_lock(struct template_db *db, const char *name)
189 {
190 return DC_NOTIMPL;
191 }
192
193 static int stack_template_db_unlock(struct template_db *db, const char *name)
194 {
195 return DC_NOTIMPL;
196 }
197
198 static struct template *stack_template_db_iterate(struct template_db *db,
199 void **iter)
200 {
201 struct template_stack_iterator *tsi;
202 struct template *t;
203
204 tsi = *(struct template_stack_iterator **) iter;
205 if (tsi == NULL) {
206 *iter = tsi = malloc(sizeof *tsi);
207 tsi->stack = (struct template_stack *) db->data;;
208 tsi->child_iterator = NULL;
209 }
210
211 while (tsi->stack) {
212 t = tsi->stack->db->methods.iterate(tsi->stack->db,
213 &tsi->child_iterator);
214 if (t)
215 return t;
216 tsi->stack = tsi->stack->next;
217 tsi->child_iterator = NULL;
218 }
219
220 free(tsi);
221 return NULL;
222 }
223
224 /****************************************************************************
225 *
226 * Config database
227 *
228 ***************************************************************************/
229
230 static int stack_question_db_initialize(struct question_db *db, struct configuration *cfg)
231 {
232 struct question_stack *qstack = NULL;
233 char *configpath;
234 struct configitem *tmp, *child;
235
236 if (asprintf(&configpath, "%s::stack", db->configpath) == -1)
237 return DC_NOTOK;
238 tmp = cfg->tree(cfg, configpath);
239 free(configpath);
240
241 for (child = tmp->child; child != NULL; child = child->next) {
242 if (qstack != NULL) {
243 qstack->next = malloc(sizeof(struct question_stack));
244 qstack = qstack->next;
245 } else {
246 qstack = malloc(sizeof(struct question_stack));
247 db->data = qstack;
248 }
249 qstack->db = question_db_new(cfg, db->tdb, child->value);
250 qstack->next = NULL;
251 }
252 return DC_OK;
253 }
254
255 static int stack_question_db_shutdown(struct question_db *db)
256 {
257 struct question_stack *qstack = (struct question_stack *)db->data;
258 while (qstack) {
259 struct question_stack *next = qstack->next;
260 question_db_delete(qstack->db);
261 DELETE(qstack);
262 qstack = next;
263 }
264 return DC_OK;
265 }
266
267 static int stack_question_db_load(struct question_db *db)
268 {
269 struct question_stack *qstack = (struct question_stack *)db->data;
270 while (qstack) {
271 if (qstack->db->methods.load(qstack->db) != DC_OK)
272 return DC_NOTOK;
273 qstack = qstack->next;
274 }
275 return DC_OK;
276 }
277
278 static int stack_question_db_save(struct question_db *db)
279 {
280 struct question_stack *qstack = (struct question_stack *)db->data;
281 while (qstack) {
282 if (qstack->db->methods.save(qstack->db) == DC_NOTOK)
283 return DC_NOTOK;
284 qstack = qstack->next;
285 }
286 return DC_OK;
287 }
288
289 /* TODO: compare with what we get from db_get and see if nothing has
290 * changed. if so, just return.*/
291
292 static int stack_question_db_set(struct question_db *db, struct question *t) {
293 struct question_stack *qstack = (struct question_stack *)db->data;
294 int ret;
295 while (qstack) {
296 const char *type = t->template ? t->template->type : "";
297 ret = qstack->db->methods.accept(qstack->db, t->tag, type);
298 if (ret == DC_REJECT) {
299 qstack->db->methods.remove(qstack->db, t->tag);
300 qstack = qstack->next;
301 continue;
302 }
303 ret = qstack->db->methods.set(qstack->db, t);
304 switch (ret) {
305 case DC_OK:
306 return DC_OK;
307 case DC_NOTOK:
308 return DC_NOTOK;
309 case DC_REJECT: /* obsolete as return code from set() */
310 qstack->db->methods.remove(qstack->db, t->tag);
311 qstack = qstack->next;
312 continue;
313 }
314 }
315 /* everybody rejected it, it seems */
316 return DC_REJECT;
317 }
318
319 static struct question *stack_question_db_get(struct question_db *db,
320 const char *name)
321 {
322 struct question_stack *qstack = (struct question_stack *)db->data;
323 struct question *q;
324 while (qstack) {
325 if ((q = qstack->db->methods.get(qstack->db, name)) != NULL)
326 return q;
327 qstack = qstack->next;
328 }
329 return NULL;
330 }
331
332 static int stack_question_db_disown(struct question_db *db, const char *name,
333 const char *owner)
334 {
335 struct question_stack *qstack = (struct question_stack *)db->data;
336 struct question *q;
337 int ret;
338 while (qstack) {
339 /* only disown those which actually exist in the db */
340 if ((q = qstack->db->methods.get(qstack->db, name)) != NULL) {
341 question_deref(q);
342 ret = qstack->db->methods.disown(qstack->db, name, owner);
343 if (ret != DC_OK)
344 return ret;
345 }
346 qstack = qstack->next;
347 }
348 return DC_OK;
349 }
350
351 static int stack_question_db_disownall(struct question_db *db, const char *owner)
352 {
353 struct question_stack *qstack = (struct question_stack *)db->data;
354 int ret;
355 while (qstack) {
356 ret = qstack->db->methods.disownall(qstack->db, owner);
357 if (ret != DC_OK)
358 return ret;
359 qstack = qstack->next;
360 }
361 return DC_OK;
362 }
363
364 static int stack_question_db_remove(struct question_db *db, const char *name)
365 {
366 struct question_stack *qstack = (struct question_stack *)db->data;
367 struct question *q;
368 while (qstack) {
369 q = qstack->db->methods.get(qstack->db, name);
370 if (q) {
371 question_deref(q);
372 return qstack->db->methods.remove(qstack->db, name);
373 }
374 qstack = qstack->next;
375 }
376 /* nobody had it? fail. */
377 return DC_NOTOK;
378 }
379
380 static int stack_question_db_lock(struct question_db *db, const char *name)
381 {
382 return DC_NOTIMPL;
383 }
384
385 static int stack_question_db_unlock(struct question_db *db, const char *name)
386 {
387 return DC_NOTIMPL;
388 }
389
390 static struct question *stack_question_db_iterate(struct question_db *db,
391 void **iter)
392 {
393 struct question_stack_iterator *qsi;
394 struct question *q;
395
396 qsi = *(struct question_stack_iterator **) iter;
397 if (qsi == NULL) {
398 *iter = qsi = malloc(sizeof *qsi);
399 qsi->stack = (struct question_stack *) db->data;;
400 qsi->child_iterator = NULL;
401 }
402
403 while (qsi->stack) {
404 q = qsi->stack->db->methods.iterate(qsi->stack->db,
405 &qsi->child_iterator);
406 if (q)
407 return q;
408 qsi->stack = qsi->stack->next;
409 qsi->child_iterator = NULL;
410 }
411
412 free(qsi);
413 return NULL;
414 }
415
416 struct template_db_module debconf_template_db_module = {
417 .initialize = stack_template_db_initialize,
418 .shutdown = stack_template_db_shutdown,
419 .load = stack_template_db_load,
420 .reload = stack_template_db_reload,
421 .save = stack_template_db_save,
422 .set = stack_template_db_set,
423 .get = stack_template_db_get,
424 .remove = stack_template_db_remove,
425 .iterate = stack_template_db_iterate,
426 .lock = stack_template_db_lock,
427 .unlock = stack_template_db_unlock,
428 };
429
430 struct question_db_module debconf_question_db_module = {
431 .initialize = stack_question_db_initialize,
432 .shutdown = stack_question_db_shutdown,
433 .load = stack_question_db_load,
434 .save = stack_question_db_save,
435 .set = stack_question_db_set,
436 .get = stack_question_db_get,
437 .disown = stack_question_db_disown,
438 .disownall = stack_question_db_disownall,
439 .remove = stack_question_db_remove,
440 .iterate = stack_question_db_iterate,
441 .lock = stack_question_db_lock,
442 .unlock = stack_question_db_unlock,
443 };