File: suite_config.c

package info (click to toggle)
cdebootstrap 0.3.4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 816 kB
  • ctags: 308
  • sloc: sh: 3,408; ansic: 2,522; makefile: 243
file content (454 lines) | stat: -rw-r--r-- 11,040 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * config.c
 *
 * Copyright (C) 2003 Bastian Blank <waldi@debian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * $LastChangedBy: bastian $
 * $LastChangedDate: 2004-09-09 20:00:10 +0200 (Do, 09 Sep 2004) $
 * $LastChangedRevision: 624 $
 */

#include <config.h>

#include "frontend.h"
#include "suite.h"
#include "suite_config.h"

#include <debian-installer.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>

static di_parser_fields_function_read
  parser_read_action_flags,
  parser_read_packages_section,
  parser_read_list;

const di_parser_fieldinfo
  suite_config_action_field_action =
    DI_PARSER_FIELDINFO
    (
      "Action",
      di_parser_read_string,
      NULL,
      offsetof (suite_config_action, action)
    ),
  suite_config_action_field_comment =
    DI_PARSER_FIELDINFO
    (
      "Comment",
      di_parser_read_string,
      NULL,
      offsetof (suite_config_action, comment)
    ),
  suite_config_action_field_flags =
    DI_PARSER_FIELDINFO
    (
      "Flags",
      parser_read_action_flags,
      NULL,
      0
    ),
  suite_config_action_field_flavour =
    DI_PARSER_FIELDINFO
    (
      "Flavour",
      parser_read_list,
      NULL,
      offsetof (suite_config_action, flavour)
    ),
  suite_config_action_field_what =
    DI_PARSER_FIELDINFO
    (
      "What",
      di_parser_read_string,
      NULL,
      offsetof (suite_config_action, what)
    ),
  suite_config_packages_field_section =
    DI_PARSER_FIELDINFO
    (
      "Section",
      parser_read_packages_section,
      NULL,
      0
    ),
  suite_config_packages_field_arch =
    DI_PARSER_FIELDINFO
    (
      "Arch",
      parser_read_list,
      NULL,
      offsetof (suite_config_packages, arch)
    ),
  suite_config_packages_field_packages =
    DI_PARSER_FIELDINFO
    (
      "Packages",
      parser_read_list,
      NULL,
      offsetof (suite_config_packages, packages)
    ),
  suite_config_sections_field_section =
    DI_PARSER_FIELDINFO
    (
      "Section",
      di_parser_read_string,
      NULL,
      offsetof (suite_config_section, section)
    ),
  suite_config_sections_field_flavour =
    DI_PARSER_FIELDINFO
    (
      "Flavour",
      parser_read_list,
      NULL,
      offsetof (suite_config_section, flavour)
    ),
  suites_config_field_suite =
    DI_PARSER_FIELDINFO
    (
      "Suite",
      di_parser_read_string,
      NULL,
      offsetof (suites_config_suite, suite)
    ),
  suites_config_field_config =
    DI_PARSER_FIELDINFO
    (
      "Config",
      di_parser_read_string,
      NULL,
      offsetof (suites_config_suite, config)
    );

static const di_parser_fieldinfo *suite_config_action_fieldinfo[] =
{
  &suite_config_action_field_action,
  &suite_config_action_field_flags,
  &suite_config_action_field_flavour,
  &suite_config_action_field_what,
  NULL
};

static const di_parser_fieldinfo *suite_config_packages_fieldinfo[] =
{
  &suite_config_packages_field_section,
  &suite_config_packages_field_arch,
  &suite_config_packages_field_packages,
  NULL
};

static const di_parser_fieldinfo *suite_config_sections_fieldinfo[] =
{
  &suite_config_sections_field_section,
  &suite_config_sections_field_flavour,
  NULL
};

static const di_parser_fieldinfo *suites_config_fieldinfo[] =
{
  &suites_config_field_suite,
  &suites_config_field_config,
  NULL
};

static di_parser_read_entry_new suite_config_action_new;
static di_parser_read_entry_finish suite_config_action_finish;

static di_parser_read_entry_new suite_config_packages_new;

static di_parser_read_entry_new suite_config_sections_new;
static di_parser_read_entry_finish suite_config_sections_finish;

static di_parser_read_entry_new suites_config_new;
static di_parser_read_entry_finish suites_config_finish;

suite_config *suite_config_alloc (void)
{
  suite_config *ret;

  ret = di_new0 (suite_config, 1);
  ret->sections = di_hash_table_new (di_rstring_hash, di_rstring_equal);

  return ret;
}

void suite_config_free (suite_config *config)
{
  /* TODO */
  di_free (config);
}

void suites_config_free (suites_config *config)
{
  /* TODO */
  di_free (config);
}

static int suite_config_init_common (const char *suite_name)
{
  suites_config *suites;

  suites = suites_config_read (configdir);

  if (suites)
  {
    di_slist_node *node;

    for (node = suites->suite.head; node; node = node->next)
    {
      suites_config_suite *config_suite = node->data;
      if (!strcmp (suite_name, config_suite->suite))
      {
        if (config_suite->config)
        {
          char dir_new[PATH_MAX];
          snprintf (dir_new, PATH_MAX, "%s/%s", configdir, config_suite->config);
          suite = suite_config_read (config_suite->suite, dir_new);
          return 0;
        }
        return 1;
      }
    }

    log_text (DI_LOG_LEVEL_ERROR, "Unknown suite %s", suite_name);
  }

  return 2;
}

int suite_config_init (const char *suite_name)
{
  if (suite_config_init_common (suite_name) > 1)
  {
    suite = suite_config_read (suite_name, configdir);
    if (!suite)
      return 1;
  }

  return 0;
}

int suite_config_init_second (const char *suite_name)
{
  return suite_config_init_common (suite_name);
}

static int suite_config_read_one (void *config, const char *dir, const char *name, const di_parser_fieldinfo *fieldinfo[], di_parser_read_entry_new parser_new, di_parser_read_entry_finish parser_finish)
{
  char file[PATH_MAX];
  di_parser_info *info;
  int ret = 0;

  info = di_parser_info_alloc ();
  di_parser_info_add (info, fieldinfo);
  snprintf (file, PATH_MAX, "%s/%s", dir, name);
  if (di_parser_rfc822_read_file (file, info, parser_new, parser_finish, config) < 0)
    ret = 1;
  di_parser_info_free (info);
  return ret;
}

suite_config *suite_config_read (const char *name, const char *dir)
{
  suite_config *config;

  config = suite_config_alloc ();
  config->name = strdup (name);

  if (suite_config_read_one (config, dir, "action", suite_config_action_fieldinfo, suite_config_action_new, suite_config_action_finish))
    goto error;
  if (suite_config_read_one (config, dir, "sections", suite_config_sections_fieldinfo, suite_config_sections_new, suite_config_sections_finish))
    goto error;
  if (suite_config_read_one (config, dir, "packages", suite_config_packages_fieldinfo, suite_config_packages_new, NULL))
    goto error;

  return config;

error:
  suite_config_free (config);
  return NULL;
}

suites_config *suites_config_read (const char *dir)
{
  suites_config *config;

  config = di_new0 (suites_config, 1);

  if (suite_config_read_one (config, dir, "suites", suites_config_fieldinfo, suites_config_new, suites_config_finish))
  {
    suites_config_free (config);
    return NULL;
  }

  return config;
}

static void *suite_config_action_new (void *user_data __attribute__ ((unused)))
{
  return di_new0 (suite_config_action, 1);
}

static int suite_config_action_finish (void *data, void *user_data)
{
  suite_config *config = user_data;
  suite_config_action *action = data;
  if (action->action)
    di_slist_append (&config->actions, action);
#if 0
  else
    suite_config_action_free (action);
#endif
  return 0;
}

static void *suite_config_packages_new (void *user_data __attribute__ ((unused)))
{
  return di_new0 (suite_config_packages, 1);
}

static void *suite_config_sections_new (void *user_data __attribute__ ((unused)))
{
  return di_new0 (suite_config_section, 1);
}

static int suite_config_sections_finish (void *data, void *user_data)
{
  suite_config *config = user_data;
  suite_config_section *section = data;
  if (section->section)
  {
    section->key.size = strlen (section->key.string);
    di_hash_table_insert (config->sections, &section->key, section);
  }
  return 0;
}

static void *suites_config_new (void *user_data __attribute__ ((unused)))
{
  return di_new0 (suites_config_suite, 1);
}

static int suites_config_finish (void *data, void *user_data)
{
  suites_config *config = user_data;
  suites_config_suite *suite = data;
  if (suite->suite)
    di_slist_append (&config->suite, suite);
  return 0;
}

suite_config_section *suite_config_get_section (suite_config *config, char *name, size_t n)
{
  di_rstring key;
  size_t size;

  if (n)
    size = n;
  else
    size = strlen (name);

  key.string = name;
  key.size = size;

  return di_hash_table_lookup (config->sections, &key);
}

static void parser_read_action_flags (
  void **data,
  const di_parser_fieldinfo *fip __attribute__ ((unused)),
  di_rstring *field_modifier __attribute__ ((unused)),
  di_rstring *value,
  void *user_data __attribute__ ((unused)))
{
  suite_config_action *action = *data;
  char *begin = value->string, *next, *end = value->string + value->size;

  while (1)
  {
    next = memchr (begin, ',', end - begin);

    if (!next)
    {
      if (begin < end)
        next = end;
      else
        break;
    }

    if (!strncasecmp ("force", begin, next - begin))
      action->flags |= SUITE_CONFIG_ACTION_FLAG_FORCE;
    else if (!strncasecmp ("only", begin, next - begin))
      action->flags |= SUITE_CONFIG_ACTION_FLAG_ONLY;

    if (next < end)
      begin = next + 2;
    else
      break;
  }
}

static void parser_read_packages_section (
  void **data,
  const di_parser_fieldinfo *fip __attribute__ ((unused)),
  di_rstring *field_modifier __attribute__ ((unused)),
  di_rstring *value,
  void *user_data __attribute__ ((unused)))
{
  suite_config *config = user_data;
  suite_config_packages *package = *data;
  suite_config_section *section = suite_config_get_section (config, value->string, value->size);
  
  if (!section)
    log_text (DI_LOG_LEVEL_WARNING, "unknown section");
  else
    di_slist_append (&section->packages, package);
}

static void parser_read_list (
  void **data,
  const di_parser_fieldinfo *fip,
  di_rstring *field_modifier __attribute__ ((unused)),
  di_rstring *value,
  void *user_data __attribute__ ((unused)))
{
  di_slist *list = (di_slist *)((char *)*data + fip->integer);
  char *str = di_stradup (value->string, value->size);
  char *begin = str, *next, *end = str + value->size;
  char *temp;

  if (!data)
    return;

  while (begin < end)
  {
    begin += strspn (begin, " \t\n,");
    next = begin;
    next += strcspn (next, " \t\n,");

    temp = di_stradup (begin, next - begin);
    di_slist_append (list, temp);

    begin = next;
  }

  di_free (str);
}