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