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