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