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