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