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