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