libopkg: remove Curl support
[oweals/opkg-lede.git] / libopkg / opkg_conf.c
1 /* opkg_conf.c - the opkg package management system
2
3    Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5    Carl D. Worth
6    Copyright (C) 2001 University of Southern California
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <glob.h>
24 #include <unistd.h>
25
26 #include "opkg_conf.h"
27 #include "pkg_vec.h"
28 #include "pkg.h"
29 #include "xregex.h"
30 #include "sprintf_alloc.h"
31 #include "opkg_message.h"
32 #include "file_util.h"
33 #include "opkg_defines.h"
34 #include "libbb/libbb.h"
35
36 static int lock_fd;
37 static char *lock_file = NULL;
38
39 static opkg_conf_t _conf;
40 opkg_conf_t *conf = &_conf;
41
42 /*
43  * Config file options
44  */
45 opkg_option_t options[] = {
46         {"cache", OPKG_OPT_TYPE_STRING, &_conf.cache},
47         {"force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults},
48         {"force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer},
49         {"force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends},
50         {"force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite},
51         {"force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade},
52         {"force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall},
53         {"force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space},
54         {"force_postinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_postinstall},
55         {"force_checksum", OPKG_OPT_TYPE_BOOL, &_conf.force_checksum},
56         {"check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature},
57         {"ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy},
58         {"http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy},
59         {"no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy},
60         {"test", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
61         {"noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
62         {"download_only", OPKG_OPT_TYPE_BOOL, &_conf.download_only},
63         {"nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps},
64         {"nocase", OPKG_OPT_TYPE_BOOL, &_conf.nocase},
65         {"offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root},
66         {"overlay_root", OPKG_OPT_TYPE_STRING, &_conf.overlay_root},
67         {"proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd},
68         {"proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user},
69         {"query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all},
70         {"size", OPKG_OPT_TYPE_BOOL, &_conf.size},
71         {"tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir},
72         {"verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity},
73 #if defined(HAVE_OPENSSL)
74         {"signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file},
75         {"signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path},
76 #endif
77         {NULL, 0, NULL}
78 };
79
80 static int resolve_pkg_dest_list(void)
81 {
82         nv_pair_list_elt_t *iter;
83         nv_pair_t *nv_pair;
84         pkg_dest_t *dest;
85         char *root_dir;
86
87         for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter;
88              iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) {
89                 nv_pair = (nv_pair_t *) iter->data;
90
91                 if (conf->offline_root) {
92                         sprintf_alloc(&root_dir, "%s%s", conf->offline_root,
93                                       nv_pair->value);
94                 } else {
95                         root_dir = xstrdup(nv_pair->value);
96                 }
97
98                 dest =
99                     pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name,
100                                          root_dir, conf->lists_dir);
101                 free(root_dir);
102
103                 if (conf->default_dest == NULL)
104                         conf->default_dest = dest;
105
106                 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
107                         conf->default_dest = dest;
108                         conf->restrict_to_default_dest = 1;
109                 }
110         }
111
112         if (conf->dest_str && !conf->restrict_to_default_dest) {
113                 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
114                 return -1;
115         }
116
117         return 0;
118 }
119
120 static int opkg_conf_set_option(const char *name, const char *value)
121 {
122         int i = 0;
123
124         while (options[i].name) {
125                 if (strcmp(options[i].name, name) == 0) {
126                         switch (options[i].type) {
127                         case OPKG_OPT_TYPE_BOOL:
128                                 if (*(int *)options[i].value) {
129                                         opkg_msg(ERROR,
130                                                  "Duplicate boolean option %s, "
131                                                  "leaving this option on.\n",
132                                                  name);
133                                         return 0;
134                                 }
135                                 *((int *const)options[i].value) = 1;
136                                 return 0;
137                         case OPKG_OPT_TYPE_INT:
138                                 if (value) {
139                                         if (*(int *)options[i].value) {
140                                                 opkg_msg(ERROR,
141                                                          "Duplicate option %s, "
142                                                          "using first seen value \"%d\".\n",
143                                                          name,
144                                                          *((int *)options[i].
145                                                            value));
146                                                 return 0;
147                                         }
148                                         *((int *const)options[i].value) =
149                                             atoi(value);
150                                         return 0;
151                                 } else {
152                                         opkg_msg(ERROR,
153                                                  "Option %s needs an argument\n",
154                                                  name);
155                                         return -1;
156                                 }
157                         case OPKG_OPT_TYPE_STRING:
158                                 if (value) {
159                                         if (*(char **)options[i].value) {
160                                                 opkg_msg(ERROR,
161                                                          "Duplicate option %s, "
162                                                          "using first seen value \"%s\".\n",
163                                                          name,
164                                                          *((char **)options[i].
165                                                            value));
166                                                 return 0;
167                                         }
168                                         *((char **const)options[i].value) =
169                                             xstrdup(value);
170                                         return 0;
171                                 } else {
172                                         opkg_msg(ERROR,
173                                                  "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:]]*)$", 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:]]+(.*))?[[: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) ==
235                     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
269                             && regmatch[13].rm_so != regmatch[13].rm_eo)
270                                 extra =
271                                     xstrndup(line + regmatch[11].rm_so,
272                                              regmatch[13].rm_eo -
273                                              regmatch[11].rm_so);
274                         else
275                                 extra = xstrndup(line + regmatch[11].rm_so,
276                                                  regmatch[11].rm_eo -
277                                                  regmatch[11].rm_so);
278                 }
279
280                 if (regmatch[13].rm_so != regmatch[13].rm_eo
281                     && strncmp(type, "dist", 4) != 0) {
282                         opkg_msg(ERROR,
283                                  "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
284                                  filename, line_num, line);
285                 } else {
286
287                         /* We use the conf->tmp_dest_list below instead of
288                            conf->pkg_dest_list because we might encounter an
289                            offline_root option later and that would invalidate the
290                            directories we would have computed in
291                            pkg_dest_list_init. (We do a similar thing with
292                            tmp_src_nv_pair_list for sake of symmetry.) */
293                         if (strcmp(type, "option") == 0) {
294                                 opkg_conf_set_option(name, value);
295                         } else if (strcmp(type, "dist") == 0) {
296                                 if (!nv_pair_list_find
297                                     ((nv_pair_list_t *) dist_src_list, name)) {
298                                         pkg_src_list_append(dist_src_list, name,
299                                                             value, extra, 0);
300                                 } else {
301                                         opkg_msg(ERROR,
302                                                  "Duplicate dist declaration (%s %s). "
303                                                  "Skipping.\n", name, value);
304                                 }
305                         } else if (strcmp(type, "dist/gz") == 0) {
306                                 if (!nv_pair_list_find
307                                     ((nv_pair_list_t *) dist_src_list, name)) {
308                                         pkg_src_list_append(dist_src_list, name,
309                                                             value, extra, 1);
310                                 } else {
311                                         opkg_msg(ERROR,
312                                                  "Duplicate dist declaration (%s %s). "
313                                                  "Skipping.\n", name, value);
314                                 }
315                         } else if (strcmp(type, "src") == 0) {
316                                 if (!nv_pair_list_find
317                                     ((nv_pair_list_t *) pkg_src_list, name)) {
318                                         pkg_src_list_append(pkg_src_list, name,
319                                                             value, extra, 0);
320                                 } else {
321                                         opkg_msg(ERROR,
322                                                  "Duplicate src declaration (%s %s). "
323                                                  "Skipping.\n", name, value);
324                                 }
325                         } else if (strcmp(type, "src/gz") == 0) {
326                                 if (!nv_pair_list_find
327                                     ((nv_pair_list_t *) pkg_src_list, name)) {
328                                         pkg_src_list_append(pkg_src_list, name,
329                                                             value, extra, 1);
330                                 } else {
331                                         opkg_msg(ERROR,
332                                                  "Duplicate src declaration (%s %s). "
333                                                  "Skipping.\n", name, value);
334                                 }
335                         } else if (strcmp(type, "dest") == 0) {
336                                 nv_pair_list_append(&conf->tmp_dest_list, name,
337                                                     value);
338                         } else if (strcmp(type, "lists_dir") == 0) {
339                                 conf->lists_dir = xstrdup(value);
340                         } else if (strcmp(type, "arch") == 0) {
341                                 opkg_msg(INFO,
342                                          "Supported arch %s priority (%s)\n",
343                                          name, value);
344                                 if (!value) {
345                                         opkg_msg(NOTICE,
346                                                  "No priority given for architecture %s,"
347                                                  "defaulting to 10\n", name);
348                                         value = xstrdup("10");
349                                 }
350                                 nv_pair_list_append(&conf->arch_list, name,
351                                                     value);
352                         } else {
353                                 opkg_msg(ERROR,
354                                          "%s:%d: Ignoring invalid line: `%s'\n",
355                                          filename, line_num, line);
356                         }
357
358                 }
359
360                 free(type);
361                 free(name);
362                 free(value);
363                 if (extra)
364                         free(extra);
365
366 NEXT_LINE:
367                 free(line);
368         }
369
370         regfree(&valid_line_re);
371 err2:
372         regfree(&comment_re);
373 err1:
374         if (fclose(file) == EOF) {
375                 opkg_perror(ERROR, "Couldn't close %s", filename);
376                 err = -1;
377         }
378 err0:
379         return err;
380 }
381
382 int opkg_conf_write_status_files(void)
383 {
384         pkg_dest_list_elt_t *iter;
385         pkg_dest_t *dest;
386         pkg_vec_t *all;
387         pkg_t *pkg;
388         int i, ret = 0;
389
390         if (conf->noaction)
391                 return 0;
392
393         list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
394                 dest = (pkg_dest_t *) iter->data;
395
396                 dest->status_fp = fopen(dest->status_file_name, "w");
397                 if (dest->status_fp == NULL && errno != EROFS) {
398                         opkg_perror(ERROR, "Can't open status file %s",
399                                     dest->status_file_name);
400                         ret = -1;
401                 }
402         }
403
404         all = pkg_vec_alloc();
405         pkg_hash_fetch_available(all);
406
407         for (i = 0; i < all->len; i++) {
408                 pkg = all->pkgs[i];
409                 /* We don't need most uninstalled packages in the status file */
410                 if (pkg->state_status == SS_NOT_INSTALLED
411                     && (pkg->state_want == SW_UNKNOWN
412                         || (pkg->state_want == SW_DEINSTALL
413                             && pkg->state_flag != SF_HOLD)
414                         || pkg->state_want == SW_PURGE)) {
415                         continue;
416                 }
417                 if (pkg->dest == NULL) {
418                         opkg_msg(ERROR,
419                                  "Internal error: package %s has a NULL dest\n",
420                                  pkg->name);
421                         continue;
422                 }
423                 if (pkg->dest->status_fp)
424                         pkg_print_status(pkg, pkg->dest->status_fp);
425         }
426
427         pkg_vec_free(all);
428
429         list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
430                 dest = (pkg_dest_t *) iter->data;
431                 if (dest->status_fp && fclose(dest->status_fp) == EOF) {
432                         opkg_perror(ERROR, "Couldn't close %s",
433                                     dest->status_file_name);
434                         ret = -1;
435                 }
436         }
437
438         return ret;
439 }
440
441 char *root_filename_alloc(char *filename)
442 {
443         char *root_filename;
444         sprintf_alloc(&root_filename, "%s%s",
445                       (conf->offline_root ? conf->offline_root : ""), filename);
446         return root_filename;
447 }
448
449 static int glob_errfunc(const char *epath, int eerrno)
450 {
451         if (eerrno == ENOENT)
452                 /* If leading dir does not exist, we get GLOB_NOMATCH. */
453                 return 0;
454
455         opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
456         return 0;
457 }
458
459 int opkg_conf_init(void)
460 {
461         pkg_src_list_init(&conf->pkg_src_list);
462         pkg_src_list_init(&conf->dist_src_list);
463         pkg_dest_list_init(&conf->pkg_dest_list);
464         pkg_dest_list_init(&conf->tmp_dest_list);
465         nv_pair_list_init(&conf->arch_list);
466
467         return 0;
468 }
469
470 int opkg_conf_load(void)
471 {
472         int i, glob_ret;
473         char *tmp, *tmp_dir_base, **tmp_val;
474         glob_t globbuf;
475         char *etc_opkg_conf_pattern;
476
477         conf->restrict_to_default_dest = 0;
478         conf->default_dest = NULL;
479
480         if (!conf->offline_root)
481                 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
482
483         if (conf->conf_file) {
484                 struct stat st;
485                 if (stat(conf->conf_file, &st) == -1) {
486                         opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
487                         goto err0;
488                 }
489                 if (opkg_conf_parse_file(conf->conf_file,
490                                          &conf->pkg_src_list,
491                                          &conf->dist_src_list))
492                         goto err1;
493         }
494
495         if (conf->offline_root)
496                 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf",
497                               conf->offline_root);
498         else {
499                 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
500                 if (conf_file_dir == NULL)
501                         conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
502                 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf",
503                               conf_file_dir);
504         }
505
506         memset(&globbuf, 0, sizeof(globbuf));
507         glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
508         if (glob_ret && glob_ret != GLOB_NOMATCH) {
509                 free(etc_opkg_conf_pattern);
510                 globfree(&globbuf);
511                 goto err1;
512         }
513
514         free(etc_opkg_conf_pattern);
515
516         for (i = 0; i < globbuf.gl_pathc; i++) {
517                 if (globbuf.gl_pathv[i])
518                         if (conf->conf_file &&
519                             !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
520                                 continue;
521                 if (opkg_conf_parse_file(globbuf.gl_pathv[i],
522                                          &conf->pkg_src_list,
523                                          &conf->dist_src_list) < 0) {
524                         globfree(&globbuf);
525                         goto err1;
526                 }
527         }
528
529         globfree(&globbuf);
530
531         if (conf->offline_root)
532                 sprintf_alloc(&lock_file, "%s/%s", conf->offline_root,
533                               OPKGLOCKFILE);
534         else
535                 sprintf_alloc(&lock_file, "%s", OPKGLOCKFILE);
536
537         lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
538         if (lock_fd == -1) {
539                 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
540                 goto err2;
541         }
542
543         if (lockf(lock_fd, F_TLOCK, (off_t) 0) == -1) {
544                 opkg_perror(ERROR, "Could not lock %s", lock_file);
545                 if (close(lock_fd) == -1)
546                         opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
547                                     lock_fd, lock_file);
548                 lock_fd = -1;
549                 goto err2;
550         }
551
552         if (conf->tmp_dir)
553                 tmp_dir_base = conf->tmp_dir;
554         else
555                 tmp_dir_base = getenv("TMPDIR");
556
557         sprintf_alloc(&tmp, "%s/%s",
558                       tmp_dir_base ? tmp_dir_base :
559                       OPKG_CONF_DEFAULT_TMP_DIR_BASE, OPKG_CONF_TMP_DIR_SUFFIX);
560         if (conf->tmp_dir)
561                 free(conf->tmp_dir);
562         conf->tmp_dir = mkdtemp(tmp);
563         if (conf->tmp_dir == NULL) {
564                 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
565                 goto err3;
566         }
567
568         pkg_hash_init();
569         hash_table_init("file-hash", &conf->file_hash,
570                         OPKG_CONF_DEFAULT_HASH_LEN);
571         hash_table_init("obs-file-hash", &conf->obs_file_hash,
572                         OPKG_CONF_DEFAULT_HASH_LEN / 16);
573
574         if (conf->lists_dir == NULL)
575                 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
576
577         if (conf->offline_root) {
578                 sprintf_alloc(&tmp, "%s/%s", conf->offline_root,
579                               conf->lists_dir);
580                 free(conf->lists_dir);
581                 conf->lists_dir = tmp;
582         }
583
584         /* if no architectures were defined, then default all, noarch, and host architecture */
585         if (nv_pair_list_empty(&conf->arch_list)) {
586                 nv_pair_list_append(&conf->arch_list, "all", "1");
587                 nv_pair_list_append(&conf->arch_list, "noarch", "1");
588                 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
589         }
590
591         /* Even if there is no conf file, we'll need at least one dest. */
592         if (nv_pair_list_empty(&conf->tmp_dest_list)) {
593                 nv_pair_list_append(&conf->tmp_dest_list,
594                                     OPKG_CONF_DEFAULT_DEST_NAME,
595                                     OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
596         }
597
598         if (resolve_pkg_dest_list())
599                 goto err4;
600
601         nv_pair_list_deinit(&conf->tmp_dest_list);
602
603         return 0;
604
605 err4:
606         free(conf->lists_dir);
607
608         pkg_hash_deinit();
609         hash_table_deinit(&conf->file_hash);
610         hash_table_deinit(&conf->obs_file_hash);
611
612         if (rmdir(conf->tmp_dir) == -1)
613                 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
614 err3:
615         if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
616                 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
617
618         if (close(lock_fd) == -1)
619                 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
620                             lock_fd, lock_file);
621         if (unlink(lock_file) == -1)
622                 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
623 err2:
624         if (lock_file) {
625                 free(lock_file);
626                 lock_file = NULL;
627         }
628 err1:
629         pkg_src_list_deinit(&conf->pkg_src_list);
630         pkg_src_list_deinit(&conf->dist_src_list);
631         pkg_dest_list_deinit(&conf->pkg_dest_list);
632         nv_pair_list_deinit(&conf->arch_list);
633
634         for (i = 0; options[i].name; i++) {
635                 if (options[i].type == OPKG_OPT_TYPE_STRING) {
636                         tmp_val = (char **)options[i].value;
637                         if (*tmp_val) {
638                                 free(*tmp_val);
639                                 *tmp_val = NULL;
640                         }
641                 }
642         }
643 err0:
644         nv_pair_list_deinit(&conf->tmp_dest_list);
645         if (conf->dest_str)
646                 free(conf->dest_str);
647         if (conf->conf_file)
648                 free(conf->conf_file);
649
650         return -1;
651 }
652
653 void opkg_conf_deinit(void)
654 {
655         int i;
656         char **tmp;
657
658         if (conf->tmp_dir)
659                 rm_r(conf->tmp_dir);
660
661         if (conf->lists_dir)
662                 free(conf->lists_dir);
663
664         if (conf->dest_str)
665                 free(conf->dest_str);
666
667         if (conf->conf_file)
668                 free(conf->conf_file);
669
670         pkg_src_list_deinit(&conf->pkg_src_list);
671         pkg_src_list_deinit(&conf->dist_src_list);
672         pkg_dest_list_deinit(&conf->pkg_dest_list);
673         nv_pair_list_deinit(&conf->arch_list);
674
675         for (i = 0; options[i].name; i++) {
676                 if (options[i].type == OPKG_OPT_TYPE_STRING) {
677                         tmp = (char **)options[i].value;
678                         if (*tmp) {
679                                 free(*tmp);
680                                 *tmp = NULL;
681                         }
682                 }
683         }
684
685         if (conf->verbosity >= DEBUG) {
686                 hash_print_stats(&conf->pkg_hash);
687                 hash_print_stats(&conf->file_hash);
688                 hash_print_stats(&conf->obs_file_hash);
689         }
690
691         pkg_hash_deinit();
692         hash_table_deinit(&conf->file_hash);
693         hash_table_deinit(&conf->obs_file_hash);
694
695         if (lock_fd != -1) {
696                 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
697                         opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
698
699                 if (close(lock_fd) == -1)
700                         opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
701                                     lock_fd, lock_file);
702
703         }
704
705         if (lock_file) {
706                 if (unlink(lock_file) == -1)
707                         opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
708
709                 free(lock_file);
710         }
711 }