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