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