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