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