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