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