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