Add a function for recursive directory removal and use that instead of xsystem.
[project/opkg-lede.git] / libopkg / opkg_conf.c
1 /* opkg_conf.c - the opkg package management system
2
3 Carl D. Worth
4
5 Copyright (C) 2001 University of Southern California
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include "opkg_conf.h"
20 #include "opkg_error.h"
21
22 #include "xregex.h"
23 #include "sprintf_alloc.h"
24 #include "args.h"
25 #include "opkg_message.h"
26 #include "file_util.h"
27 #include "str_util.h"
28 #include "xsystem.h"
29 #include "opkg_defines.h"
30 #include "libbb/libbb.h"
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <glob.h>
37
38 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
39 pkg_src_list_t *pkg_src_list,
40 nv_pair_list_t *tmp_dest_nv_pair_list);
41 static int opkg_conf_set_option(const opkg_option_t *options,
42 const char *name, const char *value);
43 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
44 const char *default_dest_name);
45 static int set_and_load_pkg_src_list(opkg_conf_t *conf,
46 pkg_src_list_t *nv_pair_list);
47 static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
48 nv_pair_list_t *nv_pair_list);
49
50 void opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
51 {
52 opkg_option_t tmp[] = {
53 { "cache", OPKG_OPT_TYPE_STRING, &conf->cache},
54 { "force_defaults", OPKG_OPT_TYPE_BOOL, &conf->force_defaults },
55 { "force_maintainer", OPKG_OPT_TYPE_BOOL, &conf->force_maintainer },
56 { "force_depends", OPKG_OPT_TYPE_BOOL, &conf->force_depends },
57 { "force_overwrite", OPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
58 { "force_downgrade", OPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
59 { "force_reinstall", OPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
60 { "force_space", OPKG_OPT_TYPE_BOOL, &conf->force_space },
61 { "check_signature", OPKG_OPT_TYPE_INT, &conf->check_signature },
62 { "ftp_proxy", OPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
63 { "http_proxy", OPKG_OPT_TYPE_STRING, &conf->http_proxy },
64 { "no_proxy", OPKG_OPT_TYPE_STRING, &conf->no_proxy },
65 { "test", OPKG_OPT_TYPE_INT, &conf->noaction },
66 { "noaction", OPKG_OPT_TYPE_INT, &conf->noaction },
67 { "nodeps", OPKG_OPT_TYPE_BOOL, &conf->nodeps },
68 { "offline_root", OPKG_OPT_TYPE_STRING, &conf->offline_root },
69 { "offline_root_path", OPKG_OPT_TYPE_STRING, &conf->offline_root_path },
70 { "offline_root_post_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
71 { "offline_root_pre_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
72 { "proxy_passwd", OPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
73 { "proxy_user", OPKG_OPT_TYPE_STRING, &conf->proxy_user },
74 { "query-all", OPKG_OPT_TYPE_BOOL, &conf->query_all },
75 { "verbosity", OPKG_OPT_TYPE_BOOL, &conf->verbosity },
76 #if defined(HAVE_OPENSSL)
77 { "signature_ca_file", OPKG_OPT_TYPE_STRING, &conf->signature_ca_file },
78 { "signature_ca_path", OPKG_OPT_TYPE_STRING, &conf->signature_ca_path },
79 #endif
80 #if defined(HAVE_PATHFINDER)
81 { "check_x509_path", OPKG_OPT_TYPE_INT, &conf->check_x509_path },
82 #endif
83 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
84 { "ssl_engine", OPKG_OPT_TYPE_STRING, &conf->ssl_engine },
85 { "ssl_cert", OPKG_OPT_TYPE_STRING, &conf->ssl_cert },
86 { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &conf->ssl_cert_type },
87 { "ssl_key", OPKG_OPT_TYPE_STRING, &conf->ssl_key },
88 { "ssl_key_type", OPKG_OPT_TYPE_STRING, &conf->ssl_key_type },
89 { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &conf->ssl_key_passwd },
90 { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_file },
91 { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_path },
92 { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &conf->ssl_dont_verify_peer },
93 #endif
94 { NULL }
95 };
96
97 *options = xcalloc(1, sizeof(tmp));
98 memcpy(*options, tmp, sizeof(tmp));
99 };
100
101 static void opkg_conf_override_string(char **conf_str, char *arg_str)
102 {
103 if (arg_str) {
104 if (*conf_str) {
105 free(*conf_str);
106 }
107 *conf_str = xstrdup(arg_str);
108 }
109 }
110
111 static void opkg_conf_free_string(char **conf_str)
112 {
113 if (*conf_str) {
114 free(*conf_str);
115 *conf_str = NULL;
116 }
117 }
118
119 int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
120 {
121 int err;
122 int errno_copy;
123 char *tmp_dir_base;
124 nv_pair_list_t tmp_dest_nv_pair_list;
125 char *lock_file = NULL;
126 glob_t globbuf;
127 char *etc_opkg_conf_pattern;
128 char *offline_root = NULL;
129
130 memset(conf, 0, sizeof(opkg_conf_t));
131
132 #if defined(HAVE_PATHFINDER)
133 conf->check_x509_path = 1;
134 #endif
135
136 pkg_src_list_init(&conf->pkg_src_list);
137
138 nv_pair_list_init(&tmp_dest_nv_pair_list);
139 pkg_dest_list_init(&conf->pkg_dest_list);
140
141 nv_pair_list_init(&conf->arch_list);
142
143 conf->restrict_to_default_dest = 0;
144 conf->default_dest = NULL;
145
146 if (args->conf_file) {
147 struct stat stat_buf;
148 err = stat(args->conf_file, &stat_buf);
149 if (err == 0)
150 if (opkg_conf_parse_file(conf, args->conf_file,
151 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
152 /* Memory leakage from opkg_conf_parse-file */
153 return OPKG_CONF_ERR_PARSE;
154 }
155 }
156
157 opkg_conf_override_string(&conf->offline_root, args->offline_root);
158 offline_root = conf->offline_root;
159
160 if (conf->offline_root)
161 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", conf->offline_root);
162 else {
163 char *conf_file_dir = getenv("OPKG_CONF_DIR");
164 if (conf_file_dir == NULL)
165 conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
166 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
167 }
168 memset(&globbuf, 0, sizeof(globbuf));
169 err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
170 free (etc_opkg_conf_pattern);
171 if (!err) {
172 int i;
173 for (i = 0; i < globbuf.gl_pathc; i++) {
174 if (globbuf.gl_pathv[i])
175 if (args->conf_file &&
176 !strcmp(args->conf_file, globbuf.gl_pathv[i]))
177 continue;
178 if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i],
179 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
180 /* Memory leakage from opkg_conf_parse-file */
181 return OPKG_CONF_ERR_PARSE;
182 }
183 if (offline_root != conf->offline_root) {
184 opkg_message(conf, OPKG_ERROR,
185 "Config file %s, within an offline "
186 "root contains option offline_root.\n",
187 globbuf.gl_pathv[i]);
188 return OPKG_CONF_ERR_PARSE;
189 }
190 }
191 }
192 globfree(&globbuf);
193
194 opkg_conf_override_string(&conf->offline_root_path,
195 args->offline_root_path);
196 opkg_conf_override_string(&conf->offline_root_pre_script_cmd,
197 args->offline_root_pre_script_cmd);
198 opkg_conf_override_string(&conf->offline_root_post_script_cmd,
199 args->offline_root_post_script_cmd);
200
201 opkg_conf_override_string(&conf->cache, args->cache);
202
203 /* check for lock file */
204 if (conf->offline_root)
205 sprintf_alloc (&lock_file, "%s/%s/lock", conf->offline_root, OPKG_STATE_DIR_PREFIX);
206 else
207 sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
208
209 err = conf->lock_fd = creat (lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
210 if (err != -1)
211 err = lockf (conf->lock_fd, F_TLOCK, 0);
212 errno_copy = errno;
213
214 if (err) {
215 opkg_message (conf, OPKG_ERROR, "Could not lock %s: %s\n",
216 lock_file, strerror(errno_copy));
217 free(lock_file);
218 return OPKG_CONF_ERR_LOCK;
219 }
220 free(lock_file);
221
222 if (args->tmp_dir)
223 tmp_dir_base = args->tmp_dir;
224 else
225 tmp_dir_base = getenv("TMPDIR");
226 sprintf_alloc(&conf->tmp_dir, "%s/%s",
227 tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
228 OPKG_CONF_TMP_DIR_SUFFIX);
229 conf->tmp_dir = mkdtemp(conf->tmp_dir);
230 if (conf->tmp_dir == NULL) {
231 opkg_message(conf, OPKG_ERROR,
232 "%s: Creating temp dir %s failed: %s\n",
233 conf->tmp_dir, strerror(errno));
234 return OPKG_CONF_ERR_TMP_DIR;
235 }
236
237 pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN);
238 hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
239 hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
240
241 if (conf->lists_dir == NULL)
242 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
243
244 if (conf->offline_root) {
245 char *tmp;
246 sprintf_alloc(&tmp, "%s/%s", conf->offline_root, conf->lists_dir);
247 free(conf->lists_dir);
248 conf->lists_dir = tmp;
249 }
250
251 sprintf_alloc(&conf->pending_dir, "%s/pending", conf->lists_dir);
252
253 /* if no architectures were defined, then default all, noarch, and host architecture */
254 if (nv_pair_list_empty(&conf->arch_list)) {
255 nv_pair_list_append(&conf->arch_list, "all", "1");
256 nv_pair_list_append(&conf->arch_list, "noarch", "1");
257 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
258 }
259
260 /* Even if there is no conf file, we'll need at least one dest. */
261 if (nv_pair_list_empty(&tmp_dest_nv_pair_list)) {
262 nv_pair_list_append(&tmp_dest_nv_pair_list,
263 OPKG_CONF_DEFAULT_DEST_NAME,
264 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
265 }
266
267 /* After parsing the file, set options from command-line, (so that
268 command-line arguments take precedence) */
269 /* XXX: CLEANUP: The interaction between args.c and opkg_conf.c
270 really needs to be cleaned up. There is so much duplication
271 right now it is ridiculous. Maybe opkg_conf_t should just save
272 a pointer to args_t (which could then not be freed), rather
273 than duplicating every field here? */
274 if (args->autoremove) {
275 conf->autoremove = 1;
276 }
277 if (args->force_depends) {
278 conf->force_depends = 1;
279 }
280 if (args->force_defaults) {
281 conf->force_defaults = 1;
282 }
283 if (args->force_maintainer) {
284 conf->force_maintainer = 1;
285 }
286 if (args->force_overwrite) {
287 conf->force_overwrite = 1;
288 }
289 if (args->force_downgrade) {
290 conf->force_downgrade = 1;
291 }
292 if (args->force_space) {
293 conf->force_space = 1;
294 }
295 if (args->force_reinstall) {
296 conf->force_reinstall = 1;
297 }
298 if (args->force_removal_of_dependent_packages) {
299 conf->force_removal_of_dependent_packages = 1;
300 }
301 if (args->force_removal_of_essential_packages) {
302 conf->force_removal_of_essential_packages = 1;
303 }
304 if (args->nodeps) {
305 conf->nodeps = 1;
306 }
307 if (args->noaction) {
308 conf->noaction = 1;
309 }
310 if (args->query_all) {
311 conf->query_all = 1;
312 }
313 if (args->verbosity != conf->verbosity) {
314 conf->verbosity = args->verbosity;
315 }
316
317 /* Pigi: added a flag to disable the checking of structures if the command does not need to
318 read anything from there.
319 */
320 if ( !(args->nocheckfordirorfile)){
321 /* need to run load the source list before dest list -Jamey */
322 if ( !(args->noreadfeedsfile))
323 set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
324
325 /* Now that we have resolved conf->offline_root, we can commit to
326 the directory names for the dests and load in all the package
327 lists. */
328 set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list);
329
330 if (args->dest) {
331 err = opkg_conf_set_default_dest(conf, args->dest);
332 if (err) {
333 return OPKG_CONF_ERR_DEFAULT_DEST;
334 }
335 }
336 }
337 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
338
339 return 0;
340 }
341
342 void opkg_conf_deinit(opkg_conf_t *conf)
343 {
344 rm_r(conf->tmp_dir);
345
346 free(conf->tmp_dir);
347 free(conf->lists_dir);
348 free(conf->pending_dir);
349
350 pkg_src_list_deinit(&conf->pkg_src_list);
351 pkg_dest_list_deinit(&conf->pkg_dest_list);
352 nv_pair_list_deinit(&conf->arch_list);
353
354 opkg_conf_free_string(&conf->cache);
355
356 opkg_conf_free_string(&conf->ftp_proxy);
357 opkg_conf_free_string(&conf->http_proxy);
358 opkg_conf_free_string(&conf->no_proxy);
359
360 opkg_conf_free_string(&conf->offline_root);
361 opkg_conf_free_string(&conf->offline_root_path);
362 opkg_conf_free_string(&conf->offline_root_pre_script_cmd);
363 opkg_conf_free_string(&conf->offline_root_post_script_cmd);
364
365 opkg_conf_free_string(&conf->proxy_passwd);
366 opkg_conf_free_string(&conf->proxy_user);
367
368 #if defined(HAVE_OPENSSL)
369 opkg_conf_free_string(&conf->signature_ca_file);
370 opkg_conf_free_string(&conf->signature_ca_path);
371 #endif
372
373 #if defined(HAVE_SSLCURL)
374 opkg_conf_free_string(&conf->ssl_engine);
375 opkg_conf_free_string(&conf->ssl_cert);
376 opkg_conf_free_string(&conf->ssl_cert_type);
377 opkg_conf_free_string(&conf->ssl_key);
378 opkg_conf_free_string(&conf->ssl_key_type);
379 opkg_conf_free_string(&conf->ssl_key_passwd);
380 opkg_conf_free_string(&conf->ssl_ca_file);
381 opkg_conf_free_string(&conf->ssl_ca_path);
382 #endif
383
384 if (conf->verbosity >= OPKG_DEBUG) {
385 int i;
386 hash_table_t *hashes[] = {
387 &conf->pkg_hash,
388 &conf->file_hash,
389 &conf->obs_file_hash };
390 for (i = 0; i < 3; i++) {
391 hash_table_t *hash = hashes[i];
392 int c = 0;
393 int n_conflicts = 0;
394 int j;
395 for (j = 0; j < hash->n_entries; j++) {
396 int len = 0;
397 hash_entry_t *e = &hash->entries[j];
398 if (e->next)
399 n_conflicts++;
400 while (e && e->key) {
401 len++;
402 e = e->next;
403 }
404 if (len > c)
405 c = len;
406 }
407 opkg_message(conf, OPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n",
408 hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
409 }
410 }
411
412 if (&conf->pkg_hash)
413 pkg_hash_deinit(&conf->pkg_hash);
414 if (&conf->file_hash)
415 hash_table_deinit(&conf->file_hash);
416 if (&conf->obs_file_hash)
417 hash_table_deinit(&conf->obs_file_hash);
418
419 lockf(conf->lock_fd, F_ULOCK, 0);
420 close(conf->lock_fd);
421 }
422
423 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
424 const char *default_dest_name)
425 {
426 pkg_dest_list_elt_t *iter;
427 pkg_dest_t *dest;
428
429 for (iter = void_list_first(&conf->pkg_dest_list); iter; iter = void_list_next(&conf->pkg_dest_list, iter)) {
430 dest = (pkg_dest_t *)iter->data;
431 if (strcmp(dest->name, default_dest_name) == 0) {
432 conf->default_dest = dest;
433 conf->restrict_to_default_dest = 1;
434 return 0;
435 }
436 }
437
438 fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
439
440 return 1;
441 }
442
443 static int set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
444 {
445 pkg_src_list_elt_t *iter;
446 pkg_src_t *src;
447 char *list_file;
448
449 for (iter = void_list_first(pkg_src_list); iter; iter = void_list_next(pkg_src_list, iter)) {
450 src = (pkg_src_t *)iter->data;
451 if (src == NULL) {
452 continue;
453 }
454
455 sprintf_alloc(&list_file, "%s/%s",
456 conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir,
457 src->name);
458
459 if (file_exists(list_file)) {
460 pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
461 }
462 free(list_file);
463 }
464
465 return 0;
466 }
467
468 static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list)
469 {
470 nv_pair_list_elt_t *iter;
471 nv_pair_t *nv_pair;
472 pkg_dest_t *dest;
473 char *root_dir;
474
475 for (iter = nv_pair_list_first(nv_pair_list); iter; iter = nv_pair_list_next(nv_pair_list, iter)) {
476 nv_pair = (nv_pair_t *)iter->data;
477
478 if (conf->offline_root) {
479 sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
480 } else {
481 root_dir = xstrdup(nv_pair->value);
482 }
483 dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir);
484 free(root_dir);
485 if (dest == NULL) {
486 continue;
487 }
488 if (conf->default_dest == NULL) {
489 conf->default_dest = dest;
490 }
491 if (file_exists(dest->status_file_name)) {
492 pkg_hash_add_from_file(conf, dest->status_file_name,
493 NULL, dest, 1);
494 }
495 }
496
497 return 0;
498 }
499
500 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
501 pkg_src_list_t *pkg_src_list,
502 nv_pair_list_t *tmp_dest_nv_pair_list)
503 {
504 int err;
505 opkg_option_t * options;
506 FILE *file;
507 regex_t valid_line_re, comment_re;
508 #define regmatch_size 12
509 regmatch_t regmatch[regmatch_size];
510
511 opkg_init_options_array(conf, &options);
512
513 file = fopen(filename, "r");
514 if (file == NULL) {
515 fprintf(stderr, "%s: failed to open %s: %s\n",
516 __FUNCTION__, filename, strerror(errno));
517 free(options);
518 return -1;
519 }
520 opkg_message(conf, OPKG_NOTICE, "loading conf file %s\n", filename);
521
522 err = xregcomp(&comment_re,
523 "^[[:space:]]*(#.*|[[:space:]]*)$",
524 REG_EXTENDED);
525 if (err) {
526 free(options);
527 return -1;
528 }
529 err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
530 if (err) {
531 free(options);
532 return -1;
533 }
534
535 while(1) {
536 int line_num = 0;
537 char *line;
538 char *type, *name, *value, *extra;
539
540 line = file_read_line_alloc(file);
541 line_num++;
542 if (line == NULL) {
543 break;
544 }
545
546 str_chomp(line);
547
548 if (regexec(&comment_re, line, 0, 0, 0) == 0) {
549 goto NEXT_LINE;
550 }
551
552 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
553 str_chomp(line);
554 fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
555 filename, line_num, line);
556 goto NEXT_LINE;
557 }
558
559 /* This has to be so ugly to deal with optional quotation marks */
560 if (regmatch[2].rm_so > 0) {
561 type = xstrndup(line + regmatch[2].rm_so,
562 regmatch[2].rm_eo - regmatch[2].rm_so);
563 } else {
564 type = xstrndup(line + regmatch[3].rm_so,
565 regmatch[3].rm_eo - regmatch[3].rm_so);
566 }
567 if (regmatch[5].rm_so > 0) {
568 name = xstrndup(line + regmatch[5].rm_so,
569 regmatch[5].rm_eo - regmatch[5].rm_so);
570 } else {
571 name = xstrndup(line + regmatch[6].rm_so,
572 regmatch[6].rm_eo - regmatch[6].rm_so);
573 }
574 if (regmatch[8].rm_so > 0) {
575 value = xstrndup(line + regmatch[8].rm_so,
576 regmatch[8].rm_eo - regmatch[8].rm_so);
577 } else {
578 value = xstrndup(line + regmatch[9].rm_so,
579 regmatch[9].rm_eo - regmatch[9].rm_so);
580 }
581 extra = NULL;
582 if (regmatch[11].rm_so > 0) {
583 extra = xstrndup (line + regmatch[11].rm_so,
584 regmatch[11].rm_eo - regmatch[11].rm_so);
585 }
586
587 /* We use the tmp_dest_nv_pair_list below instead of
588 conf->pkg_dest_list because we might encounter an
589 offline_root option later and that would invalidate the
590 directories we would have computed in
591 pkg_dest_list_init. (We do a similar thing with
592 tmp_src_nv_pair_list for sake of symmetry.) */
593 if (strcmp(type, "option") == 0) {
594 opkg_conf_set_option(options, name, value);
595 } else if (strcmp(type, "src") == 0) {
596 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
597 pkg_src_list_append (pkg_src_list, name, value, extra, 0);
598 } else {
599 opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
600 name, value);
601 }
602 } else if (strcmp(type, "src/gz") == 0) {
603 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
604 pkg_src_list_append (pkg_src_list, name, value, extra, 1);
605 } else {
606 opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
607 name, value);
608 }
609 } else if (strcmp(type, "dest") == 0) {
610 nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
611 } else if (strcmp(type, "lists_dir") == 0) {
612 conf->lists_dir = xstrdup(value);
613 } else if (strcmp(type, "arch") == 0) {
614 opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value);
615 if (!value) {
616 opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
617 value = xstrdup("10");
618 }
619 nv_pair_list_append(&conf->arch_list, name, value);
620 } else {
621 fprintf(stderr, "WARNING: Ignoring unknown configuration "
622 "parameter: %s %s %s\n", type, name, value);
623 free(options);
624 return -1;
625 }
626
627 free(type);
628 free(name);
629 free(value);
630 if (extra)
631 free (extra);
632
633 NEXT_LINE:
634 free(line);
635 }
636
637 free(options);
638 regfree(&comment_re);
639 regfree(&valid_line_re);
640 fclose(file);
641
642 return 0;
643 }
644
645 static int opkg_conf_set_option(const opkg_option_t *options,
646 const char *name, const char *value)
647 {
648 int i = 0;
649 while (options[i].name) {
650 if (strcmp(options[i].name, name) == 0) {
651 switch (options[i].type) {
652 case OPKG_OPT_TYPE_BOOL:
653 *((int *)options[i].value) = 1;
654 return 0;
655 case OPKG_OPT_TYPE_INT:
656 if (value) {
657 *((int *)options[i].value) = atoi(value);
658 return 0;
659 } else {
660 printf("%s: Option %s need an argument\n",
661 __FUNCTION__, name);
662 return EINVAL;
663 }
664 case OPKG_OPT_TYPE_STRING:
665 if (value) {
666 *((char **)options[i].value) = xstrdup(value);
667 return 0;
668 } else {
669 printf("%s: Option %s need an argument\n",
670 __FUNCTION__, name);
671 return EINVAL;
672 }
673 }
674 }
675 i++;
676 }
677
678 fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
679 __FUNCTION__, name, value);
680 return EINVAL;
681 }
682
683 int opkg_conf_write_status_files(opkg_conf_t *conf)
684 {
685 pkg_dest_list_elt_t *iter;
686 pkg_dest_t *dest;
687 pkg_vec_t *all;
688 pkg_t *pkg;
689 int i, ret = 0;
690
691 if (conf->noaction)
692 return 0;
693
694 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
695 dest = (pkg_dest_t *)iter->data;
696
697 dest->status_fp = fopen(dest->status_file_name, "w");
698 if (dest->status_fp == NULL) {
699 fprintf(stderr, "%s: Can't open status file: %s: %s\n",
700 __FUNCTION__, dest->status_file_name, strerror(errno));
701 ret = -1;
702 }
703 }
704
705 all = pkg_vec_alloc();
706 pkg_hash_fetch_available(&conf->pkg_hash, all);
707
708 for(i = 0; i < all->len; i++) {
709 pkg = all->pkgs[i];
710 /* We don't need most uninstalled packages in the status file */
711 if (pkg->state_status == SS_NOT_INSTALLED
712 && (pkg->state_want == SW_UNKNOWN
713 || pkg->state_want == SW_DEINSTALL
714 || pkg->state_want == SW_PURGE)) {
715 continue;
716 }
717 if (pkg->dest == NULL) {
718 fprintf(stderr, "%s: ERROR: Can't write status for "
719 "package %s since it has a NULL dest\n",
720 __FUNCTION__, pkg->name);
721 continue;
722 }
723 if (pkg->dest->status_fp)
724 pkg_print_status(pkg, pkg->dest->status_fp);
725 }
726
727 pkg_vec_free(all);
728
729 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
730 dest = (pkg_dest_t *)iter->data;
731 fclose(dest->status_fp);
732 }
733
734 return ret;
735 }
736
737
738 char *root_filename_alloc(opkg_conf_t *conf, char *filename)
739 {
740 char *root_filename;
741 sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
742 return root_filename;
743 }