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