libopkg: flag package with SF_NEED_DETAIL in pkg_init_from_file()
[oweals/opkg-lede.git] / libopkg / opkg_conf.c
1 /* opkg_conf.c - the opkg package management system
2
3    Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5    Carl D. Worth
6    Copyright (C) 2001 University of Southern California
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 */
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <glob.h>
26 #include <unistd.h>
27
28 #include "opkg_conf.h"
29 #include "pkg_vec.h"
30 #include "pkg.h"
31 #include "xregex.h"
32 #include "sprintf_alloc.h"
33 #include "opkg_message.h"
34 #include "file_util.h"
35 #include "opkg_defines.h"
36 #include "libbb/libbb.h"
37
38 static int lock_fd;
39 static char *lock_file = NULL;
40
41 static opkg_conf_t _conf;
42 opkg_conf_t *conf = &_conf;
43
44 /*
45  * Config file options
46  */
47 opkg_option_t options[] = {
48         {"cache", OPKG_OPT_TYPE_STRING, &_conf.cache},
49         {"force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults},
50         {"force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer},
51         {"force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends},
52         {"force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite},
53         {"force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade},
54         {"force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall},
55         {"force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space},
56         {"force_postinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_postinstall},
57         {"force_checksum", OPKG_OPT_TYPE_BOOL, &_conf.force_checksum},
58         {"check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature},
59         {"ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy},
60         {"http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy},
61         {"no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy},
62         {"test", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
63         {"noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
64         {"download_only", OPKG_OPT_TYPE_BOOL, &_conf.download_only},
65         {"nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps},
66         {"nocase", OPKG_OPT_TYPE_BOOL, &_conf.nocase},
67         {"offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root},
68         {"overlay_root", OPKG_OPT_TYPE_STRING, &_conf.overlay_root},
69         {"proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd},
70         {"proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user},
71         {"query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all},
72         {"size", OPKG_OPT_TYPE_BOOL, &_conf.size},
73         {"tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir},
74         {"verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity},
75 #if defined(HAVE_OPENSSL)
76         {"signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file},
77         {"signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path},
78 #endif
79 #if defined(HAVE_PATHFINDER)
80         {"check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path},
81 #endif
82 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
83         {"ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine},
84         {"ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert},
85         {"ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type},
86         {"ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key},
87         {"ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type},
88         {"ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd},
89         {"ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file},
90         {"ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path},
91         {"ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL,
92          &_conf.ssl_dont_verify_peer},
93 #endif
94         {NULL, 0, NULL}
95 };
96
97 static int resolve_pkg_dest_list(void)
98 {
99         nv_pair_list_elt_t *iter;
100         nv_pair_t *nv_pair;
101         pkg_dest_t *dest;
102         char *root_dir;
103
104         for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter;
105              iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) {
106                 nv_pair = (nv_pair_t *) iter->data;
107
108                 if (conf->offline_root) {
109                         sprintf_alloc(&root_dir, "%s%s", conf->offline_root,
110                                       nv_pair->value);
111                 } else {
112                         root_dir = xstrdup(nv_pair->value);
113                 }
114
115                 dest =
116                     pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name,
117                                          root_dir, conf->lists_dir);
118                 free(root_dir);
119
120                 if (conf->default_dest == NULL)
121                         conf->default_dest = dest;
122
123                 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
124                         conf->default_dest = dest;
125                         conf->restrict_to_default_dest = 1;
126                 }
127         }
128
129         if (conf->dest_str && !conf->restrict_to_default_dest) {
130                 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
131                 return -1;
132         }
133
134         return 0;
135 }
136
137 static int opkg_conf_set_option(const char *name, const char *value)
138 {
139         int i = 0;
140
141         while (options[i].name) {
142                 if (strcmp(options[i].name, name) == 0) {
143                         switch (options[i].type) {
144                         case OPKG_OPT_TYPE_BOOL:
145                                 if (*(int *)options[i].value) {
146                                         opkg_msg(ERROR,
147                                                  "Duplicate boolean option %s, "
148                                                  "leaving this option on.\n",
149                                                  name);
150                                         return 0;
151                                 }
152                                 *((int *const)options[i].value) = 1;
153                                 return 0;
154                         case OPKG_OPT_TYPE_INT:
155                                 if (value) {
156                                         if (*(int *)options[i].value) {
157                                                 opkg_msg(ERROR,
158                                                          "Duplicate option %s, "
159                                                          "using first seen value \"%d\".\n",
160                                                          name,
161                                                          *((int *)options[i].
162                                                            value));
163                                                 return 0;
164                                         }
165                                         *((int *const)options[i].value) =
166                                             atoi(value);
167                                         return 0;
168                                 } else {
169                                         opkg_msg(ERROR,
170                                                  "Option %s needs an argument\n",
171                                                  name);
172                                         return -1;
173                                 }
174                         case OPKG_OPT_TYPE_STRING:
175                                 if (value) {
176                                         if (*(char **)options[i].value) {
177                                                 opkg_msg(ERROR,
178                                                          "Duplicate option %s, "
179                                                          "using first seen value \"%s\".\n",
180                                                          name,
181                                                          *((char **)options[i].
182                                                            value));
183                                                 return 0;
184                                         }
185                                         *((char **const)options[i].value) =
186                                             xstrdup(value);
187                                         return 0;
188                                 } else {
189                                         opkg_msg(ERROR,
190                                                  "Option %s needs an argument\n",
191                                                  name);
192                                         return -1;
193                                 }
194                         }
195                 }
196                 i++;
197         }
198
199         opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
200         return -1;
201 }
202
203 static int
204 opkg_conf_parse_file(const char *filename,
205                      pkg_src_list_t * pkg_src_list,
206                      pkg_src_list_t * dist_src_list)
207 {
208         int line_num = 0;
209         int err = 0;
210         FILE *file;
211         regex_t valid_line_re, comment_re;
212 #define regmatch_size 14
213         regmatch_t regmatch[regmatch_size];
214
215         file = fopen(filename, "r");
216         if (file == NULL) {
217                 opkg_perror(ERROR, "Failed to open %s", filename);
218                 err = -1;
219                 goto err0;
220         }
221
222         opkg_msg(INFO, "Loading conf file %s.\n", filename);
223
224         err = xregcomp(&comment_re,
225                        "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED);
226         if (err)
227                 goto err1;
228
229         err = xregcomp(&valid_line_re,
230                        "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
231                        "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
232                        "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
233                        "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
234                        REG_EXTENDED);
235         if (err)
236                 goto err2;
237
238         while (1) {
239                 char *line;
240                 char *type, *name, *value, *extra;
241
242                 line_num++;
243
244                 line = file_read_line_alloc(file);
245                 if (line == NULL)
246                         break;
247
248                 if (regexec(&comment_re, line, 0, 0, 0) == 0)
249                         goto NEXT_LINE;
250
251                 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) ==
252                     REG_NOMATCH) {
253                         opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
254                                  filename, line_num, line);
255                         goto NEXT_LINE;
256                 }
257
258                 /* This has to be so ugly to deal with optional quotation marks */
259                 if (regmatch[2].rm_so > 0) {
260                         type = xstrndup(line + regmatch[2].rm_so,
261                                         regmatch[2].rm_eo - regmatch[2].rm_so);
262                 } else {
263                         type = xstrndup(line + regmatch[3].rm_so,
264                                         regmatch[3].rm_eo - regmatch[3].rm_so);
265                 }
266
267                 if (regmatch[5].rm_so > 0) {
268                         name = xstrndup(line + regmatch[5].rm_so,
269                                         regmatch[5].rm_eo - regmatch[5].rm_so);
270                 } else {
271                         name = xstrndup(line + regmatch[6].rm_so,
272                                         regmatch[6].rm_eo - regmatch[6].rm_so);
273                 }
274
275                 if (regmatch[8].rm_so > 0) {
276                         value = xstrndup(line + regmatch[8].rm_so,
277                                          regmatch[8].rm_eo - regmatch[8].rm_so);
278                 } else {
279                         value = xstrndup(line + regmatch[9].rm_so,
280                                          regmatch[9].rm_eo - regmatch[9].rm_so);
281                 }
282
283                 extra = NULL;
284                 if (regmatch[11].rm_so > 0) {
285                         if (regmatch[13].rm_so > 0
286                             && regmatch[13].rm_so != regmatch[13].rm_eo)
287                                 extra =
288                                     xstrndup(line + regmatch[11].rm_so,
289                                              regmatch[13].rm_eo -
290                                              regmatch[11].rm_so);
291                         else
292                                 extra = xstrndup(line + regmatch[11].rm_so,
293                                                  regmatch[11].rm_eo -
294                                                  regmatch[11].rm_so);
295                 }
296
297                 if (regmatch[13].rm_so != regmatch[13].rm_eo
298                     && strncmp(type, "dist", 4) != 0) {
299                         opkg_msg(ERROR,
300                                  "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
301                                  filename, line_num, line);
302                 } else {
303
304                         /* We use the conf->tmp_dest_list below instead of
305                            conf->pkg_dest_list because we might encounter an
306                            offline_root option later and that would invalidate the
307                            directories we would have computed in
308                            pkg_dest_list_init. (We do a similar thing with
309                            tmp_src_nv_pair_list for sake of symmetry.) */
310                         if (strcmp(type, "option") == 0) {
311                                 opkg_conf_set_option(name, value);
312                         } else if (strcmp(type, "dist") == 0) {
313                                 if (!nv_pair_list_find
314                                     ((nv_pair_list_t *) dist_src_list, name)) {
315                                         pkg_src_list_append(dist_src_list, name,
316                                                             value, extra, 0);
317                                 } else {
318                                         opkg_msg(ERROR,
319                                                  "Duplicate dist declaration (%s %s). "
320                                                  "Skipping.\n", name, value);
321                                 }
322                         } else if (strcmp(type, "dist/gz") == 0) {
323                                 if (!nv_pair_list_find
324                                     ((nv_pair_list_t *) dist_src_list, name)) {
325                                         pkg_src_list_append(dist_src_list, name,
326                                                             value, extra, 1);
327                                 } else {
328                                         opkg_msg(ERROR,
329                                                  "Duplicate dist declaration (%s %s). "
330                                                  "Skipping.\n", name, value);
331                                 }
332                         } else if (strcmp(type, "src") == 0) {
333                                 if (!nv_pair_list_find
334                                     ((nv_pair_list_t *) pkg_src_list, name)) {
335                                         pkg_src_list_append(pkg_src_list, name,
336                                                             value, extra, 0);
337                                 } else {
338                                         opkg_msg(ERROR,
339                                                  "Duplicate src declaration (%s %s). "
340                                                  "Skipping.\n", name, value);
341                                 }
342                         } else if (strcmp(type, "src/gz") == 0) {
343                                 if (!nv_pair_list_find
344                                     ((nv_pair_list_t *) pkg_src_list, name)) {
345                                         pkg_src_list_append(pkg_src_list, name,
346                                                             value, extra, 1);
347                                 } else {
348                                         opkg_msg(ERROR,
349                                                  "Duplicate src declaration (%s %s). "
350                                                  "Skipping.\n", name, value);
351                                 }
352                         } else if (strcmp(type, "dest") == 0) {
353                                 nv_pair_list_append(&conf->tmp_dest_list, name,
354                                                     value);
355                         } else if (strcmp(type, "lists_dir") == 0) {
356                                 conf->lists_dir = xstrdup(value);
357                         } else if (strcmp(type, "arch") == 0) {
358                                 opkg_msg(INFO,
359                                          "Supported arch %s priority (%s)\n",
360                                          name, value);
361                                 if (!value) {
362                                         opkg_msg(NOTICE,
363                                                  "No priority given for architecture %s,"
364                                                  "defaulting to 10\n", name);
365                                         value = xstrdup("10");
366                                 }
367                                 nv_pair_list_append(&conf->arch_list, name,
368                                                     value);
369                         } else {
370                                 opkg_msg(ERROR,
371                                          "%s:%d: Ignoring invalid line: `%s'\n",
372                                          filename, line_num, line);
373                         }
374
375                 }
376
377                 free(type);
378                 free(name);
379                 free(value);
380                 if (extra)
381                         free(extra);
382
383 NEXT_LINE:
384                 free(line);
385         }
386
387         regfree(&valid_line_re);
388 err2:
389         regfree(&comment_re);
390 err1:
391         if (fclose(file) == EOF) {
392                 opkg_perror(ERROR, "Couldn't close %s", filename);
393                 err = -1;
394         }
395 err0:
396         return err;
397 }
398
399 int opkg_conf_write_status_files(void)
400 {
401         pkg_dest_list_elt_t *iter;
402         pkg_dest_t *dest;
403         pkg_vec_t *all;
404         pkg_t *pkg;
405         int i, ret = 0;
406
407         if (conf->noaction)
408                 return 0;
409
410         list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
411                 dest = (pkg_dest_t *) iter->data;
412
413                 dest->status_fp = fopen(dest->status_file_name, "w");
414                 if (dest->status_fp == NULL && errno != EROFS) {
415                         opkg_perror(ERROR, "Can't open status file %s",
416                                     dest->status_file_name);
417                         ret = -1;
418                 }
419         }
420
421         all = pkg_vec_alloc();
422         pkg_hash_fetch_available(all);
423
424         for (i = 0; i < all->len; i++) {
425                 pkg = all->pkgs[i];
426                 /* We don't need most uninstalled packages in the status file */
427                 if (pkg->state_status == SS_NOT_INSTALLED
428                     && (pkg->state_want == SW_UNKNOWN
429                         || (pkg->state_want == SW_DEINSTALL
430                             && pkg->state_flag != SF_HOLD)
431                         || pkg->state_want == SW_PURGE)) {
432                         continue;
433                 }
434                 if (pkg->dest == NULL) {
435                         opkg_msg(ERROR,
436                                  "Internal error: package %s has a NULL dest\n",
437                                  pkg->name);
438                         continue;
439                 }
440                 if (pkg->dest->status_fp)
441                         pkg_print_status(pkg, pkg->dest->status_fp);
442         }
443
444         pkg_vec_free(all);
445
446         list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
447                 dest = (pkg_dest_t *) iter->data;
448                 if (dest->status_fp && fclose(dest->status_fp) == EOF) {
449                         opkg_perror(ERROR, "Couldn't close %s",
450                                     dest->status_file_name);
451                         ret = -1;
452                 }
453         }
454
455         return ret;
456 }
457
458 char *root_filename_alloc(char *filename)
459 {
460         char *root_filename;
461         sprintf_alloc(&root_filename, "%s%s",
462                       (conf->offline_root ? conf->offline_root : ""), filename);
463         return root_filename;
464 }
465
466 static int glob_errfunc(const char *epath, int eerrno)
467 {
468         if (eerrno == ENOENT)
469                 /* If leading dir does not exist, we get GLOB_NOMATCH. */
470                 return 0;
471
472         opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
473         return 0;
474 }
475
476 int opkg_conf_init(void)
477 {
478         pkg_src_list_init(&conf->pkg_src_list);
479         pkg_src_list_init(&conf->dist_src_list);
480         pkg_dest_list_init(&conf->pkg_dest_list);
481         pkg_dest_list_init(&conf->tmp_dest_list);
482         nv_pair_list_init(&conf->arch_list);
483
484         return 0;
485 }
486
487 int opkg_conf_load(void)
488 {
489         int i, glob_ret;
490         char *tmp, *tmp_dir_base, **tmp_val;
491         glob_t globbuf;
492         char *etc_opkg_conf_pattern;
493
494         conf->restrict_to_default_dest = 0;
495         conf->default_dest = NULL;
496 #if defined(HAVE_PATHFINDER)
497         conf->check_x509_path = 1;
498 #endif
499
500         if (!conf->offline_root)
501                 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
502
503         if (conf->conf_file) {
504                 struct stat st;
505                 if (stat(conf->conf_file, &st) == -1) {
506                         opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
507                         goto err0;
508                 }
509                 if (opkg_conf_parse_file(conf->conf_file,
510                                          &conf->pkg_src_list,
511                                          &conf->dist_src_list))
512                         goto err1;
513         }
514
515         if (conf->offline_root)
516                 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf",
517                               conf->offline_root);
518         else {
519                 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
520                 if (conf_file_dir == NULL)
521                         conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
522                 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf",
523                               conf_file_dir);
524         }
525
526         memset(&globbuf, 0, sizeof(globbuf));
527         glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
528         if (glob_ret && glob_ret != GLOB_NOMATCH) {
529                 free(etc_opkg_conf_pattern);
530                 globfree(&globbuf);
531                 goto err1;
532         }
533
534         free(etc_opkg_conf_pattern);
535
536         for (i = 0; i < globbuf.gl_pathc; i++) {
537                 if (globbuf.gl_pathv[i])
538                         if (conf->conf_file &&
539                             !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
540                                 continue;
541                 if (opkg_conf_parse_file(globbuf.gl_pathv[i],
542                                          &conf->pkg_src_list,
543                                          &conf->dist_src_list) < 0) {
544                         globfree(&globbuf);
545                         goto err1;
546                 }
547         }
548
549         globfree(&globbuf);
550
551         if (conf->offline_root)
552                 sprintf_alloc(&lock_file, "%s/%s", conf->offline_root,
553                               OPKGLOCKFILE);
554         else
555                 sprintf_alloc(&lock_file, "%s", OPKGLOCKFILE);
556
557         lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
558         if (lock_fd == -1) {
559                 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
560                 goto err2;
561         }
562
563         if (lockf(lock_fd, F_TLOCK, (off_t) 0) == -1) {
564                 opkg_perror(ERROR, "Could not lock %s", lock_file);
565                 if (close(lock_fd) == -1)
566                         opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
567                                     lock_fd, lock_file);
568                 lock_fd = -1;
569                 goto err2;
570         }
571
572         if (conf->tmp_dir)
573                 tmp_dir_base = conf->tmp_dir;
574         else
575                 tmp_dir_base = getenv("TMPDIR");
576
577         sprintf_alloc(&tmp, "%s/%s",
578                       tmp_dir_base ? tmp_dir_base :
579                       OPKG_CONF_DEFAULT_TMP_DIR_BASE, OPKG_CONF_TMP_DIR_SUFFIX);
580         if (conf->tmp_dir)
581                 free(conf->tmp_dir);
582         conf->tmp_dir = mkdtemp(tmp);
583         if (conf->tmp_dir == NULL) {
584                 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
585                 goto err3;
586         }
587
588         pkg_hash_init();
589         hash_table_init("file-hash", &conf->file_hash,
590                         OPKG_CONF_DEFAULT_HASH_LEN);
591         hash_table_init("obs-file-hash", &conf->obs_file_hash,
592                         OPKG_CONF_DEFAULT_HASH_LEN / 16);
593
594         if (conf->lists_dir == NULL)
595                 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
596
597         if (conf->offline_root) {
598                 sprintf_alloc(&tmp, "%s/%s", conf->offline_root,
599                               conf->lists_dir);
600                 free(conf->lists_dir);
601                 conf->lists_dir = tmp;
602         }
603
604         /* if no architectures were defined, then default all, noarch, and host architecture */
605         if (nv_pair_list_empty(&conf->arch_list)) {
606                 nv_pair_list_append(&conf->arch_list, "all", "1");
607                 nv_pair_list_append(&conf->arch_list, "noarch", "1");
608                 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
609         }
610
611         /* Even if there is no conf file, we'll need at least one dest. */
612         if (nv_pair_list_empty(&conf->tmp_dest_list)) {
613                 nv_pair_list_append(&conf->tmp_dest_list,
614                                     OPKG_CONF_DEFAULT_DEST_NAME,
615                                     OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
616         }
617
618         if (resolve_pkg_dest_list())
619                 goto err4;
620
621         nv_pair_list_deinit(&conf->tmp_dest_list);
622
623         return 0;
624
625 err4:
626         free(conf->lists_dir);
627
628         pkg_hash_deinit();
629         hash_table_deinit(&conf->file_hash);
630         hash_table_deinit(&conf->obs_file_hash);
631
632         if (rmdir(conf->tmp_dir) == -1)
633                 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
634 err3:
635         if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
636                 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
637
638         if (close(lock_fd) == -1)
639                 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
640                             lock_fd, lock_file);
641         if (unlink(lock_file) == -1)
642                 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
643 err2:
644         if (lock_file) {
645                 free(lock_file);
646                 lock_file = NULL;
647         }
648 err1:
649         pkg_src_list_deinit(&conf->pkg_src_list);
650         pkg_src_list_deinit(&conf->dist_src_list);
651         pkg_dest_list_deinit(&conf->pkg_dest_list);
652         nv_pair_list_deinit(&conf->arch_list);
653
654         for (i = 0; options[i].name; i++) {
655                 if (options[i].type == OPKG_OPT_TYPE_STRING) {
656                         tmp_val = (char **)options[i].value;
657                         if (*tmp_val) {
658                                 free(*tmp_val);
659                                 *tmp_val = NULL;
660                         }
661                 }
662         }
663 err0:
664         nv_pair_list_deinit(&conf->tmp_dest_list);
665         if (conf->dest_str)
666                 free(conf->dest_str);
667         if (conf->conf_file)
668                 free(conf->conf_file);
669
670         return -1;
671 }
672
673 void opkg_conf_deinit(void)
674 {
675         int i;
676         char **tmp;
677
678         if (conf->tmp_dir)
679                 rm_r(conf->tmp_dir);
680
681         if (conf->lists_dir)
682                 free(conf->lists_dir);
683
684         if (conf->dest_str)
685                 free(conf->dest_str);
686
687         if (conf->conf_file)
688                 free(conf->conf_file);
689
690         pkg_src_list_deinit(&conf->pkg_src_list);
691         pkg_src_list_deinit(&conf->dist_src_list);
692         pkg_dest_list_deinit(&conf->pkg_dest_list);
693         nv_pair_list_deinit(&conf->arch_list);
694
695         for (i = 0; options[i].name; i++) {
696                 if (options[i].type == OPKG_OPT_TYPE_STRING) {
697                         tmp = (char **)options[i].value;
698                         if (*tmp) {
699                                 free(*tmp);
700                                 *tmp = NULL;
701                         }
702                 }
703         }
704
705         if (conf->verbosity >= DEBUG) {
706                 hash_print_stats(&conf->pkg_hash);
707                 hash_print_stats(&conf->file_hash);
708                 hash_print_stats(&conf->obs_file_hash);
709         }
710
711         pkg_hash_deinit();
712         hash_table_deinit(&conf->file_hash);
713         hash_table_deinit(&conf->obs_file_hash);
714
715         if (lock_fd != -1) {
716                 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
717                         opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
718
719                 if (close(lock_fd) == -1)
720                         opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
721                                     lock_fd, lock_file);
722
723         }
724
725         if (lock_file) {
726                 if (unlink(lock_file) == -1)
727                         opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
728
729                 free(lock_file);
730         }
731 }