pkg: convert most other struct members into dynamic blob buffer fields
[oweals/opkg-lede.git] / libopkg / pkg.c
1 /* pkg.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "config.h"
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <unistd.h>
24 #include <libgen.h>
25
26 #include "pkg.h"
27
28 #include "pkg_parse.h"
29 #include "pkg_extract.h"
30 #include "opkg_message.h"
31 #include "opkg_utils.h"
32
33 #include "libbb/libbb.h"
34 #include "sprintf_alloc.h"
35 #include "file_util.h"
36 #include "xsystem.h"
37 #include "opkg_conf.h"
38
39 typedef struct enum_map enum_map_t;
40 struct enum_map {
41         unsigned int value;
42         const char *str;
43 };
44
45 static const enum_map_t pkg_state_want_map[] = {
46         {SW_UNKNOWN, "unknown"},
47         {SW_INSTALL, "install"},
48         {SW_DEINSTALL, "deinstall"},
49         {SW_PURGE, "purge"}
50 };
51
52 static const enum_map_t pkg_state_flag_map[] = {
53         {SF_OK, "ok"},
54         {SF_REINSTREQ, "reinstreq"},
55         {SF_HOLD, "hold"},
56         {SF_REPLACE, "replace"},
57         {SF_NOPRUNE, "noprune"},
58         {SF_PREFER, "prefer"},
59         {SF_OBSOLETE, "obsolete"},
60         {SF_USER, "user"},
61 };
62
63 static const enum_map_t pkg_state_status_map[] = {
64         {SS_NOT_INSTALLED, "not-installed"},
65         {SS_UNPACKED, "unpacked"},
66         {SS_HALF_CONFIGURED, "half-configured"},
67         {SS_INSTALLED, "installed"},
68         {SS_HALF_INSTALLED, "half-installed"},
69         {SS_CONFIG_FILES, "config-files"},
70         {SS_POST_INST_FAILED, "post-inst-failed"},
71         {SS_REMOVAL_FAILED, "removal-failed"}
72 };
73
74 static void pkg_init(pkg_t * pkg)
75 {
76         pkg->name = NULL;
77         pkg->dest = NULL;
78         pkg->src = NULL;
79         pkg->state_want = SW_UNKNOWN;
80         pkg->state_flag = SF_OK;
81         pkg->state_status = SS_NOT_INSTALLED;
82
83         active_list_init(&pkg->list);
84
85         pkg->installed_files = NULL;
86         pkg->installed_files_ref_cnt = 0;
87         pkg->essential = 0;
88         pkg->provided_by_hand = 0;
89
90         blob_buf_init(&pkg->blob, 0);
91 }
92
93 pkg_t *pkg_new(void)
94 {
95         pkg_t *pkg;
96
97         pkg = xcalloc(1, sizeof(pkg_t));
98         pkg_init(pkg);
99
100         return pkg;
101 }
102
103 void *pkg_set_raw(pkg_t *pkg, int id, const void *val, size_t len)
104 {
105         int rem;
106         struct blob_attr *cur;
107
108         blob_for_each_attr(cur, pkg->blob.head, rem) {
109                 if (blob_id(cur) == id) {
110                         if (blob_len(cur) < len) {
111                                 fprintf(stderr, "ERROR: truncating field %d <%p> to %d byte",
112                                         id, val, blob_len(cur));
113                         }
114                         memcpy(blob_data(cur), val, blob_len(cur));
115                         return blob_data(cur);
116                 }
117         }
118
119         cur = blob_put(&pkg->blob, id, val, len);
120         return cur ? blob_data(cur) : NULL;
121 }
122
123 void *pkg_get_raw(const pkg_t * pkg, int id)
124 {
125         int rem;
126         struct blob_attr *cur;
127
128         blob_for_each_attr(cur, pkg->blob.head, rem)
129                 if (blob_id(cur) == id)
130                         return blob_data(cur);
131
132         return NULL;
133 }
134
135 char *pkg_set_string(pkg_t *pkg, int id, const char *s)
136 {
137         size_t len;
138         char *p;
139
140         if (!s)
141                 return NULL;
142
143         len = strlen(s);
144
145         while (isspace(*s)) {
146                 s++;
147                 len--;
148         }
149
150         while (len > 0 && isspace(s[len - 1]))
151                 len--;
152
153         if (!len)
154                 return NULL;
155
156         p = pkg_set_raw(pkg, id, s, len + 1);
157         p[len] = 0;
158
159         return p;
160 }
161
162
163 static void compound_depend_deinit(compound_depend_t * depends)
164 {
165         int i;
166         for (i = 0; i < depends->possibility_count; i++) {
167                 depend_t *d;
168                 d = depends->possibilities[i];
169                 free(d->version);
170                 free(d);
171         }
172         free(depends->possibilities);
173 }
174
175 void pkg_deinit(pkg_t * pkg)
176 {
177         compound_depend_t *deps, *dep;
178
179         if (pkg->name)
180                 free(pkg->name);
181         pkg->name = NULL;
182
183         /* owned by opkg_conf_t */
184         pkg->dest = NULL;
185         /* owned by opkg_conf_t */
186         pkg->src = NULL;
187
188         pkg->state_want = SW_UNKNOWN;
189         pkg->state_flag = SF_OK;
190         pkg->state_status = SS_NOT_INSTALLED;
191
192         active_list_clear(&pkg->list);
193
194         deps = pkg_get_ptr(pkg, PKG_DEPENDS);
195
196         if (deps) {
197                 for (dep = deps; dep->type; dep++)
198                         compound_depend_deinit(dep);
199
200                 free(deps);
201                 pkg_set_ptr(pkg, PKG_DEPENDS, NULL);
202         }
203
204         deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
205
206         if (deps) {
207                 for (dep = deps; dep->type; dep++)
208                         compound_depend_deinit(dep);
209
210                 free(deps);
211                 pkg_set_ptr(pkg, PKG_CONFLICTS, NULL);
212         }
213
214         //conffile_list_deinit(&pkg->conffiles);
215
216         /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
217            since if they are calling deinit, they should know. Maybe do an
218            assertion here instead? */
219         pkg->installed_files_ref_cnt = 1;
220         pkg_free_installed_files(pkg);
221         pkg->essential = 0;
222
223         //blob_buf_free(&pkg->blob);
224 }
225
226 int pkg_init_from_file(pkg_t * pkg, const char *filename)
227 {
228         int fd, err = 0;
229         FILE *control_file;
230         char *control_path, *tmp;
231
232         pkg_init(pkg);
233
234         pkg_set_string(pkg, PKG_LOCAL_FILENAME, filename);
235
236         tmp = xstrdup(filename);
237         sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
238                       conf->tmp_dir, basename(tmp));
239         free(tmp);
240         fd = mkstemp(control_path);
241         if (fd == -1) {
242                 opkg_perror(ERROR, "Failed to make temp file %s", control_path);
243                 err = -1;
244                 goto err0;
245         }
246
247         control_file = fdopen(fd, "r+");
248         if (control_file == NULL) {
249                 opkg_perror(ERROR, "Failed to fdopen %s", control_path);
250                 close(fd);
251                 err = -1;
252                 goto err1;
253         }
254
255         err = pkg_extract_control_file_to_stream(pkg, control_file);
256         if (err) {
257                 opkg_msg(ERROR, "Failed to extract control file from %s.\n",
258                          filename);
259                 goto err2;
260         }
261
262         rewind(control_file);
263
264         if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
265                 if (err == 1) {
266                         opkg_msg(ERROR, "Malformed package file %s.\n",
267                                  filename);
268                 }
269                 err = -1;
270         }
271
272 err2:
273         fclose(control_file);
274 err1:
275         unlink(control_path);
276 err0:
277         free(control_path);
278
279         return err;
280 }
281
282 /* Merge any new information in newpkg into oldpkg */
283 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg)
284 {
285         abstract_pkg_t **ab;
286         conffile_list_t *cf, head;
287
288         if (oldpkg == newpkg) {
289                 return 0;
290         }
291
292         if (!oldpkg->auto_installed)
293                 oldpkg->auto_installed = newpkg->auto_installed;
294
295         if (!oldpkg->src)
296                 oldpkg->src = newpkg->src;
297         if (!oldpkg->dest)
298                 oldpkg->dest = newpkg->dest;
299         if (!pkg_get_string(oldpkg, PKG_ARCHITECTURE))
300                 pkg_set_string(oldpkg, PKG_ARCHITECTURE, pkg_get_string(newpkg, PKG_ARCHITECTURE));
301         if (!pkg_get_int(oldpkg, PKG_ARCH_PRIORITY))
302                 pkg_set_int(oldpkg, PKG_ARCH_PRIORITY, pkg_get_int(newpkg, PKG_ARCH_PRIORITY));
303         if (!pkg_get_string(oldpkg, PKG_SECTION))
304                 pkg_set_string(oldpkg, PKG_SECTION, pkg_get_string(newpkg, PKG_SECTION));
305         if (!pkg_get_string(oldpkg, PKG_MAINTAINER))
306                 pkg_set_string(oldpkg, PKG_MAINTAINER, pkg_get_string(newpkg, PKG_MAINTAINER));
307         if (!pkg_get_string(oldpkg, PKG_DESCRIPTION))
308                 pkg_set_string(oldpkg, PKG_DESCRIPTION, pkg_get_string(newpkg, PKG_DESCRIPTION));
309
310         if (!pkg_get_ptr(oldpkg, PKG_DEPENDS)) {
311                 pkg_set_ptr(oldpkg, PKG_DEPENDS, pkg_get_ptr(newpkg, PKG_DEPENDS));
312                 pkg_set_ptr(newpkg, PKG_DEPENDS, NULL);
313         }
314
315         ab = pkg_get_ptr(oldpkg, PKG_PROVIDES);
316
317         if (!ab || !ab[0] || !ab[1]) {
318                 pkg_set_ptr(oldpkg, PKG_PROVIDES, pkg_get_ptr(newpkg, PKG_PROVIDES));
319                 pkg_set_ptr(newpkg, PKG_PROVIDES, NULL);
320         }
321
322         if (!pkg_get_ptr(oldpkg, PKG_CONFLICTS)) {
323                 pkg_set_ptr(oldpkg, PKG_CONFLICTS, pkg_get_ptr(newpkg, PKG_CONFLICTS));
324                 pkg_set_ptr(newpkg, PKG_CONFLICTS, NULL);
325         }
326
327         if (!pkg_get_ptr(oldpkg, PKG_REPLACES)) {
328                 pkg_set_ptr(oldpkg, PKG_REPLACES, pkg_get_ptr(newpkg, PKG_REPLACES));
329                 pkg_set_ptr(newpkg, PKG_REPLACES, NULL);
330         }
331
332         if (!pkg_get_string(oldpkg, PKG_FILENAME))
333                 pkg_set_string(oldpkg, PKG_FILENAME, pkg_get_string(newpkg, PKG_FILENAME));
334         if (!pkg_get_string(oldpkg, PKG_LOCAL_FILENAME))
335                 pkg_set_string(oldpkg, PKG_LOCAL_FILENAME, pkg_get_string(newpkg, PKG_LOCAL_FILENAME));
336         if (!pkg_get_string(oldpkg, PKG_TMP_UNPACK_DIR))
337                 pkg_set_string(oldpkg, PKG_TMP_UNPACK_DIR, pkg_get_string(newpkg, PKG_TMP_UNPACK_DIR));
338         if (!pkg_get_string(oldpkg, PKG_MD5SUM))
339                 pkg_set_string(oldpkg, PKG_MD5SUM, pkg_get_string(newpkg, PKG_MD5SUM));
340         if (!pkg_get_string(oldpkg, PKG_SHA256SUM))
341                 pkg_set_string(oldpkg, PKG_SHA256SUM, pkg_get_string(newpkg, PKG_SHA256SUM));
342         if (!pkg_get_int(oldpkg, PKG_SIZE))
343                 pkg_set_int(oldpkg, PKG_SIZE, pkg_get_int(newpkg, PKG_SIZE));
344         if (!pkg_get_int(oldpkg, PKG_INSTALLED_SIZE))
345                 pkg_set_int(oldpkg, PKG_INSTALLED_SIZE, pkg_get_int(newpkg, PKG_INSTALLED_SIZE));
346         if (!pkg_get_string(oldpkg, PKG_PRIORITY))
347                 pkg_set_string(oldpkg, PKG_PRIORITY, pkg_get_string(newpkg, PKG_PRIORITY));
348         if (!pkg_get_string(oldpkg, PKG_SOURCE))
349                 pkg_set_string(oldpkg, PKG_SOURCE, pkg_get_string(newpkg, PKG_SOURCE));
350
351         if (!pkg_get_ptr(oldpkg, PKG_CONFFILES)) {
352                 cf = pkg_get_ptr(newpkg, PKG_CONFFILES);
353                 if (cf) {
354                         conffile_list_init(&head);
355                         list_splice_init(&cf->head, &head.head);
356                         pkg_set_raw(oldpkg, PKG_CONFFILES, &head, sizeof(head));
357                 }
358         }
359
360         if (!oldpkg->installed_files) {
361                 oldpkg->installed_files = newpkg->installed_files;
362                 oldpkg->installed_files_ref_cnt =
363                     newpkg->installed_files_ref_cnt;
364                 newpkg->installed_files = NULL;
365         }
366
367         if (!oldpkg->essential)
368                 oldpkg->essential = newpkg->essential;
369
370         return 0;
371 }
372
373 static void abstract_pkg_init(abstract_pkg_t * ab_pkg)
374 {
375         ab_pkg->provided_by = abstract_pkg_vec_alloc();
376         ab_pkg->dependencies_checked = 0;
377         ab_pkg->state_status = SS_NOT_INSTALLED;
378 }
379
380 abstract_pkg_t *abstract_pkg_new(void)
381 {
382         abstract_pkg_t *ab_pkg;
383
384         ab_pkg = xcalloc(1, sizeof(abstract_pkg_t));
385         abstract_pkg_init(ab_pkg);
386
387         return ab_pkg;
388 }
389
390 void set_flags_from_control(pkg_t * pkg)
391 {
392         char *file_name;
393         FILE *fp;
394
395         sprintf_alloc(&file_name, "%s/%s.control", pkg->dest->info_dir,
396                       pkg->name);
397
398         fp = fopen(file_name, "r");
399         if (fp == NULL) {
400                 opkg_perror(ERROR, "Failed to open %s", file_name);
401                 free(file_name);
402                 return;
403         }
404
405         free(file_name);
406
407         if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) {
408                 opkg_msg(DEBUG,
409                          "Unable to read control file for %s. May be empty.\n",
410                          pkg->name);
411         }
412
413         fclose(fp);
414
415         return;
416 }
417
418 static const char *pkg_state_want_to_str(pkg_state_want_t sw)
419 {
420         int i;
421
422         for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
423                 if (pkg_state_want_map[i].value == sw) {
424                         return pkg_state_want_map[i].str;
425                 }
426         }
427
428         opkg_msg(ERROR, "Internal error: state_want=%d\n", sw);
429         return "<STATE_WANT_UNKNOWN>";
430 }
431
432 pkg_state_want_t pkg_state_want_from_str(char *str)
433 {
434         int i;
435
436         for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
437                 if (strcmp(str, pkg_state_want_map[i].str) == 0) {
438                         return pkg_state_want_map[i].value;
439                 }
440         }
441
442         opkg_msg(ERROR, "Internal error: state_want=%s\n", str);
443         return SW_UNKNOWN;
444 }
445
446 static char *pkg_state_flag_to_str(pkg_state_flag_t sf)
447 {
448         int i;
449         unsigned int len;
450         char *str;
451
452         /* clear the temporary flags before converting to string */
453         sf &= SF_NONVOLATILE_FLAGS;
454
455         if (sf == 0)
456                 return xstrdup("ok");
457
458         len = 0;
459         for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
460                 if (sf & pkg_state_flag_map[i].value)
461                         len += strlen(pkg_state_flag_map[i].str) + 1;
462         }
463
464         str = xmalloc(len + 1);
465         str[0] = '\0';
466
467         for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
468                 if (sf & pkg_state_flag_map[i].value) {
469                         strncat(str, pkg_state_flag_map[i].str, len);
470                         strncat(str, ",", len);
471                 }
472         }
473
474         len = strlen(str);
475         str[len - 1] = '\0';    /* squash last comma */
476
477         return str;
478 }
479
480 pkg_state_flag_t pkg_state_flag_from_str(const char *str)
481 {
482         int i;
483         int sf = SF_OK;
484         const char *sfname;
485         unsigned int sfname_len;
486
487         if (strcmp(str, "ok") == 0) {
488                 return SF_OK;
489         }
490         for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
491                 sfname = pkg_state_flag_map[i].str;
492                 sfname_len = strlen(sfname);
493                 if (strncmp(str, sfname, sfname_len) == 0) {
494                         sf |= pkg_state_flag_map[i].value;
495                         str += sfname_len;
496                         if (str[0] == ',') {
497                                 str++;
498                         } else {
499                                 break;
500                         }
501                 }
502         }
503
504         return sf;
505 }
506
507 static const char *pkg_state_status_to_str(pkg_state_status_t ss)
508 {
509         int i;
510
511         for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
512                 if (pkg_state_status_map[i].value == ss) {
513                         return pkg_state_status_map[i].str;
514                 }
515         }
516
517         opkg_msg(ERROR, "Internal error: state_status=%d\n", ss);
518         return "<STATE_STATUS_UNKNOWN>";
519 }
520
521 pkg_state_status_t pkg_state_status_from_str(const char *str)
522 {
523         int i;
524
525         for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
526                 if (strcmp(str, pkg_state_status_map[i].str) == 0) {
527                         return pkg_state_status_map[i].value;
528                 }
529         }
530
531         opkg_msg(ERROR, "Internal error: state_status=%s\n", str);
532         return SS_NOT_INSTALLED;
533 }
534
535 void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field)
536 {
537         int i, j;
538         char *str;
539         const char *p;
540         compound_depend_t *dep;
541         abstract_pkg_t **ab_pkg;
542
543         if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
544                 goto UNKNOWN_FMT_FIELD;
545         }
546
547         switch (field[0]) {
548         case 'a':
549         case 'A':
550                 if (strcasecmp(field, "Architecture") == 0) {
551                         p = pkg_get_string(pkg, PKG_ARCHITECTURE);
552                         if (p) {
553                                 fprintf(fp, "Architecture: %s\n",
554                                         p);
555                         }
556                 } else if (strcasecmp(field, "Auto-Installed") == 0) {
557                         if (pkg->auto_installed)
558                                 fprintf(fp, "Auto-Installed: yes\n");
559                 } else {
560                         goto UNKNOWN_FMT_FIELD;
561                 }
562                 break;
563         case 'c':
564         case 'C':
565                 if (strcasecmp(field, "Conffiles") == 0) {
566                         conffile_list_t *cl;
567                         conffile_list_elt_t *iter;
568
569                         cl = pkg_get_ptr(pkg, PKG_CONFFILES);
570
571                         if (!cl || nv_pair_list_empty(cl))
572                                 return;
573
574                         fprintf(fp, "Conffiles:\n");
575                         for (iter = nv_pair_list_first(cl); iter;
576                              iter = nv_pair_list_next(cl, iter)) {
577                                 if (((conffile_t *) iter->data)->name
578                                     && ((conffile_t *) iter->data)->value) {
579                                         fprintf(fp, " %s %s\n",
580                                                 ((conffile_t *) iter->data)->
581                                                 name,
582                                                 ((conffile_t *) iter->data)->
583                                                 value);
584                                 }
585                         }
586                 } else if (strcasecmp(field, "Conflicts") == 0) {
587                         struct depend *cdep;
588                         compound_depend_t *deps, *dep;
589                         deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
590                         if (deps) {
591                                 fprintf(fp, "Conflicts:");
592                                 for (i = 0, dep = deps; dep->type; dep++, i++) {
593                                         cdep = dep->possibilities[0];
594                                         fprintf(fp, "%s %s", i == 0 ? "" : ",",
595                                                 cdep->pkg->name);
596                                         if (cdep->version) {
597                                                 fprintf(fp, " (%s%s)",
598                                                         constraint_to_str(cdep->
599                                                                           constraint),
600                                                         cdep->version);
601                                         }
602                                 }
603                                 fprintf(fp, "\n");
604                         }
605                 } else {
606                         goto UNKNOWN_FMT_FIELD;
607                 }
608                 break;
609         case 'd':
610         case 'D':
611                 if (strcasecmp(field, "Depends") == 0) {
612                         dep = pkg_get_depends(pkg, DEPEND);
613                         if (dep) {
614                                 fprintf(fp, "Depends:");
615                                 for (i = 0, j = 0; dep && dep->type; i++, dep++) {
616                                         if (dep->type != DEPEND)
617                                                 continue;
618                                         str = pkg_depend_str(pkg, i);
619                                         fprintf(fp, "%s %s", j == 0 ? "" : ",",
620                                                 str);
621                                         free(str);
622                                         j++;
623                                 }
624                                 fprintf(fp, "\n");
625                         }
626                 } else if (strcasecmp(field, "Description") == 0) {
627                         p = pkg_get_string(pkg, PKG_DESCRIPTION);
628                         if (p) {
629                                 fprintf(fp, "Description: %s\n",
630                                         p);
631                         }
632                 } else {
633                         goto UNKNOWN_FMT_FIELD;
634                 }
635                 break;
636         case 'e':
637         case 'E':
638                 if (pkg->essential) {
639                         fprintf(fp, "Essential: yes\n");
640                 }
641                 break;
642         case 'f':
643         case 'F':
644                 p = pkg_get_string(pkg, PKG_FILENAME);
645                 if (p) {
646                         fprintf(fp, "Filename: %s\n", p);
647                 }
648                 break;
649         case 'i':
650         case 'I':
651                 if (strcasecmp(field, "Installed-Size") == 0) {
652                         fprintf(fp, "Installed-Size: %lu\n",
653                                 (unsigned long) pkg_get_int(pkg, PKG_INSTALLED_SIZE));
654                 } else if (strcasecmp(field, "Installed-Time") == 0) {
655                         i = pkg_get_int(pkg, PKG_INSTALLED_TIME);
656                         if (i) {
657                                 fprintf(fp, "Installed-Time: %lu\n",
658                                         (unsigned long) i);
659                         }
660                 }
661                 break;
662         case 'm':
663         case 'M':
664                 if (strcasecmp(field, "Maintainer") == 0) {
665                         p = pkg_get_string(pkg, PKG_MAINTAINER);
666                         if (p) {
667                                 fprintf(fp, "Maintainer: %s\n", p);
668                         }
669                 } else if (strcasecmp(field, "MD5sum") == 0) {
670                         p = pkg_get_string(pkg, PKG_MD5SUM);
671                         if (p) {
672                                 fprintf(fp, "MD5Sum: %s\n", p);
673                         }
674                 } else {
675                         goto UNKNOWN_FMT_FIELD;
676                 }
677                 break;
678         case 'p':
679         case 'P':
680                 if (strcasecmp(field, "Package") == 0) {
681                         fprintf(fp, "Package: %s\n", pkg->name);
682                 } else if (strcasecmp(field, "Priority") == 0) {
683                         fprintf(fp, "Priority: %s\n", pkg_get_string(pkg, PKG_PRIORITY));
684                 } else if (strcasecmp(field, "Provides") == 0) {
685                         ab_pkg = pkg_get_ptr(pkg, PKG_PROVIDES);
686                         if (ab_pkg && ab_pkg[0] && ab_pkg[1]) {
687                                 fprintf(fp, "Provides:");
688                                 for (i = 0, ab_pkg++; *ab_pkg; i++, ab_pkg++) {
689                                         fprintf(fp, "%s %s", i == 0 ? "" : ",",
690                                                 (*ab_pkg)->name);
691                                         ab_pkg++;
692                                 }
693                                 fprintf(fp, "\n");
694                         }
695                 } else {
696                         goto UNKNOWN_FMT_FIELD;
697                 }
698                 break;
699         case 'r':
700         case 'R':
701                 if (strcasecmp(field, "Replaces") == 0) {
702                         ab_pkg = pkg_get_ptr(pkg, PKG_REPLACES);
703                         if (ab_pkg && *ab_pkg) {
704                                 fprintf(fp, "Replaces:");
705                                 for (i = 0; *ab_pkg; i++, ab_pkg++) {
706                                         fprintf(fp, "%s %s", i == 0 ? "" : ",",
707                                                 (*ab_pkg)->name);
708                                 }
709                                 fprintf(fp, "\n");
710                         }
711                 } else if (strcasecmp(field, "Recommends") == 0) {
712                         dep = pkg_get_depends(pkg, RECOMMEND);
713                         if (dep) {
714                                 fprintf(fp, "Recommends:");
715                                 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
716                                         if (dep->type != RECOMMEND)
717                                                 continue;
718                                         str = pkg_depend_str(pkg, i);
719                                         fprintf(fp, "%s %s", j == 0 ? "" : ",",
720                                                 str);
721                                         free(str);
722                                         j++;
723                                 }
724                                 fprintf(fp, "\n");
725                         }
726                 } else {
727                         goto UNKNOWN_FMT_FIELD;
728                 }
729                 break;
730         case 's':
731         case 'S':
732                 if (strcasecmp(field, "Section") == 0) {
733                         p = pkg_get_string(pkg, PKG_SECTION);
734                         if (p) {
735                                 fprintf(fp, "Section: %s\n", p);
736                         }
737 #if defined HAVE_SHA256
738                 } else if (strcasecmp(field, "SHA256sum") == 0) {
739                         p = pkg_get_string(pkg, PKG_SHA256SUM);
740                         if (p) {
741                                 fprintf(fp, "SHA256sum: %s\n", p);
742                         }
743 #endif
744                 } else if (strcasecmp(field, "Size") == 0) {
745                         i = pkg_get_int(pkg, PKG_SIZE);
746                         if (i) {
747                                 fprintf(fp, "Size: %lu\n", (unsigned long) i);
748                         }
749                 } else if (strcasecmp(field, "Source") == 0) {
750                         p = pkg_get_string(pkg, PKG_SOURCE);
751                         if (p) {
752                                 fprintf(fp, "Source: %s\n", p);
753                         }
754                 } else if (strcasecmp(field, "Status") == 0) {
755                         char *pflag = pkg_state_flag_to_str(pkg->state_flag);
756                         fprintf(fp, "Status: %s %s %s\n",
757                                 pkg_state_want_to_str(pkg->state_want),
758                                 pflag,
759                                 pkg_state_status_to_str(pkg->state_status));
760                         free(pflag);
761                 } else if (strcasecmp(field, "Suggests") == 0) {
762                         dep = pkg_get_depends(pkg, SUGGEST);
763                         if (dep) {
764                                 fprintf(fp, "Suggests:");
765                                 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
766                                         if (dep->type != SUGGEST)
767                                                 continue;
768                                         str = pkg_depend_str(pkg, i);
769                                         fprintf(fp, "%s %s", j == 0 ? "" : ",",
770                                                 str);
771                                         free(str);
772                                         j++;
773                                 }
774                                 fprintf(fp, "\n");
775                         }
776                 } else {
777                         goto UNKNOWN_FMT_FIELD;
778                 }
779                 break;
780         case 't':
781         case 'T':
782                 if (strcasecmp(field, "Tags") == 0) {
783                         p = pkg_get_string(pkg, PKG_TAGS);
784                         if (p) {
785                                 fprintf(fp, "Tags: %s\n", p);
786                         }
787                 }
788                 break;
789         case 'v':
790         case 'V':
791                 {
792                         char *version = pkg_version_str_alloc(pkg);
793                         if (version == NULL)
794                                 return;
795                         fprintf(fp, "Version: %s\n", version);
796                         free(version);
797                 }
798                 break;
799         default:
800                 goto UNKNOWN_FMT_FIELD;
801         }
802
803         return;
804
805 UNKNOWN_FMT_FIELD:
806         opkg_msg(ERROR, "Internal error: field=%s\n", field);
807 }
808
809 void pkg_formatted_info(FILE * fp, pkg_t * pkg)
810 {
811         pkg_formatted_field(fp, pkg, "Package");
812         pkg_formatted_field(fp, pkg, "Version");
813         pkg_formatted_field(fp, pkg, "Depends");
814         pkg_formatted_field(fp, pkg, "Recommends");
815         pkg_formatted_field(fp, pkg, "Suggests");
816         pkg_formatted_field(fp, pkg, "Provides");
817         pkg_formatted_field(fp, pkg, "Replaces");
818         pkg_formatted_field(fp, pkg, "Conflicts");
819         pkg_formatted_field(fp, pkg, "Status");
820         pkg_formatted_field(fp, pkg, "Section");
821         pkg_formatted_field(fp, pkg, "Essential");
822         pkg_formatted_field(fp, pkg, "Architecture");
823         pkg_formatted_field(fp, pkg, "Maintainer");
824         pkg_formatted_field(fp, pkg, "MD5sum");
825         pkg_formatted_field(fp, pkg, "Size");
826         pkg_formatted_field(fp, pkg, "Filename");
827         pkg_formatted_field(fp, pkg, "Conffiles");
828         pkg_formatted_field(fp, pkg, "Source");
829         pkg_formatted_field(fp, pkg, "Description");
830         pkg_formatted_field(fp, pkg, "Installed-Time");
831         pkg_formatted_field(fp, pkg, "Tags");
832         fputs("\n", fp);
833 }
834
835 void pkg_print_status(pkg_t * pkg, FILE * file)
836 {
837         if (pkg == NULL) {
838                 return;
839         }
840
841         pkg_formatted_field(file, pkg, "Package");
842         pkg_formatted_field(file, pkg, "Version");
843         pkg_formatted_field(file, pkg, "Depends");
844         pkg_formatted_field(file, pkg, "Recommends");
845         pkg_formatted_field(file, pkg, "Suggests");
846         pkg_formatted_field(file, pkg, "Provides");
847         pkg_formatted_field(file, pkg, "Replaces");
848         pkg_formatted_field(file, pkg, "Conflicts");
849         pkg_formatted_field(file, pkg, "Status");
850         pkg_formatted_field(file, pkg, "Essential");
851         pkg_formatted_field(file, pkg, "Architecture");
852         pkg_formatted_field(file, pkg, "Conffiles");
853         pkg_formatted_field(file, pkg, "Installed-Time");
854         pkg_formatted_field(file, pkg, "Auto-Installed");
855         fputs("\n", file);
856 }
857
858 /*
859  * libdpkg - Debian packaging suite library routines
860  * vercmp.c - comparison of version numbers
861  *
862  * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
863  */
864
865 /* assume ascii; warning: evaluates x multiple times! */
866 #define order(x) ((x) == '~' ? -1 \
867                 : isdigit((x)) ? 0 \
868                 : !(x) ? 0 \
869                 : isalpha((x)) ? (x) \
870                 : (x) + 256)
871
872 static int verrevcmp(const char *val, const char *ref)
873 {
874         if (!val)
875                 val = "";
876         if (!ref)
877                 ref = "";
878
879         while (*val || *ref) {
880                 int first_diff = 0;
881
882                 while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) {
883                         int vc = order(*val), rc = order(*ref);
884                         if (vc != rc)
885                                 return vc - rc;
886                         val++;
887                         ref++;
888                 }
889
890                 while (*val == '0')
891                         val++;
892                 while (*ref == '0')
893                         ref++;
894                 while (isdigit(*val) && isdigit(*ref)) {
895                         if (!first_diff)
896                                 first_diff = *val - *ref;
897                         val++;
898                         ref++;
899                 }
900                 if (isdigit(*val))
901                         return 1;
902                 if (isdigit(*ref))
903                         return -1;
904                 if (first_diff)
905                         return first_diff;
906         }
907         return 0;
908 }
909
910 int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg)
911 {
912         unsigned int epoch1 = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
913         unsigned int epoch2 = (unsigned int) pkg_get_int(ref_pkg, PKG_EPOCH);
914         char *revision1 = pkg_get_string(pkg, PKG_REVISION);
915         char *revision2 = pkg_get_string(ref_pkg, PKG_REVISION);
916         const char *version1 = pkg_get_string(pkg, PKG_VERSION);
917         const char *version2 = pkg_get_string(ref_pkg, PKG_VERSION);
918         int r;
919
920         if (epoch1 > epoch2) {
921                 return 1;
922         }
923
924         if (epoch1 < epoch2) {
925                 return -1;
926         }
927
928         r = verrevcmp(version1, version2);
929         if (r) {
930                 return r;
931         }
932
933         r = verrevcmp(revision1, revision2);
934         if (r) {
935                 return r;
936         }
937
938         return r;
939 }
940
941 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op)
942 {
943         int r;
944
945         r = pkg_compare_versions(it, ref);
946
947         if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
948                 return r <= 0;
949         }
950
951         if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
952                 return r >= 0;
953         }
954
955         if (strcmp(op, "<<") == 0) {
956                 return r < 0;
957         }
958
959         if (strcmp(op, ">>") == 0) {
960                 return r > 0;
961         }
962
963         if (strcmp(op, "=") == 0) {
964                 return r == 0;
965         }
966
967         opkg_msg(ERROR, "Unknown operator: %s.\n", op);
968         return 0;
969 }
970
971 int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
972 {
973         const pkg_t * a = *(const pkg_t **)p1;
974         const pkg_t * b = *(const pkg_t **)p2;
975         int namecmp;
976         int vercmp;
977         int arch_prio1, arch_prio2;
978         if (!a->name || !b->name) {
979                 opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n",
980                          a->name, b->name);
981                 return 0;
982         }
983
984         namecmp = strcmp(a->name, b->name);
985         if (namecmp)
986                 return namecmp;
987         vercmp = pkg_compare_versions(a, b);
988         if (vercmp)
989                 return vercmp;
990         arch_prio1 = pkg_get_int(a, PKG_ARCH_PRIORITY);
991         arch_prio2 = pkg_get_int(b, PKG_ARCH_PRIORITY);
992         if (!arch_prio1 || !arch_prio2) {
993                 opkg_msg(ERROR,
994                          "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
995                          arch_prio1, arch_prio2);
996                 return 0;
997         }
998         if (arch_prio1 > arch_prio2)
999                 return 1;
1000         if (arch_prio1 < arch_prio2)
1001                 return -1;
1002         return 0;
1003 }
1004
1005 int abstract_pkg_name_compare(const void *p1, const void *p2)
1006 {
1007         const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
1008         const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
1009         if (!a->name || !b->name) {
1010                 opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n",
1011                          a->name, b->name);
1012                 return 0;
1013         }
1014         return strcmp(a->name, b->name);
1015 }
1016
1017 char *pkg_version_str_alloc(pkg_t * pkg)
1018 {
1019         const char *verstr;
1020         char *version, *revptr;
1021         unsigned int epoch = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1022
1023         revptr = pkg_get_string(pkg, PKG_REVISION);
1024         verstr = pkg_get_string(pkg, PKG_VERSION);
1025
1026         if (epoch) {
1027                 if (revptr)
1028                         sprintf_alloc(&version, "%d:%s-%s",
1029                                       epoch, verstr, revptr);
1030                 else
1031                         sprintf_alloc(&version, "%d:%s",
1032                                       epoch, verstr);
1033         } else {
1034                 if (revptr)
1035                         sprintf_alloc(&version, "%s-%s",
1036                                       verstr, revptr);
1037                 else
1038                         version = xstrdup(verstr);
1039         }
1040
1041         return version;
1042 }
1043
1044 /*
1045  * XXX: this should be broken into two functions
1046  */
1047 str_list_t *pkg_get_installed_files(pkg_t * pkg)
1048 {
1049         int err, fd;
1050         char *list_file_name = NULL;
1051         FILE *list_file = NULL;
1052         char *line;
1053         char *installed_file_name;
1054         unsigned int rootdirlen = 0;
1055         int list_from_package;
1056         const char *local_filename;
1057
1058         pkg->installed_files_ref_cnt++;
1059
1060         if (pkg->installed_files) {
1061                 return pkg->installed_files;
1062         }
1063
1064         pkg->installed_files = str_list_alloc();
1065
1066         /*
1067          * For installed packages, look at the package.list file in the database.
1068          * For uninstalled packages, get the file list directly from the package.
1069          */
1070         if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
1071                 list_from_package = 1;
1072         else
1073                 list_from_package = 0;
1074
1075         if (list_from_package) {
1076                 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1077
1078                 if (!local_filename) {
1079                         return pkg->installed_files;
1080                 }
1081                 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1082                    file. In other words, change deb_extract so that it can
1083                    simply return the file list as a char *[] rather than
1084                    insisting on writing it to a FILE * as it does now. */
1085                 sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
1086                               conf->tmp_dir, pkg->name);
1087                 fd = mkstemp(list_file_name);
1088                 if (fd == -1) {
1089                         opkg_perror(ERROR, "Failed to make temp file %s.",
1090                                     list_file_name);
1091                         free(list_file_name);
1092                         return pkg->installed_files;
1093                 }
1094                 list_file = fdopen(fd, "r+");
1095                 if (list_file == NULL) {
1096                         opkg_perror(ERROR, "Failed to fdopen temp file %s.",
1097                                     list_file_name);
1098                         close(fd);
1099                         unlink(list_file_name);
1100                         free(list_file_name);
1101                         return pkg->installed_files;
1102                 }
1103                 err = pkg_extract_data_file_names_to_stream(pkg, list_file);
1104                 if (err) {
1105                         opkg_msg(ERROR, "Error extracting file list from %s.\n",
1106                                  local_filename);
1107                         fclose(list_file);
1108                         unlink(list_file_name);
1109                         free(list_file_name);
1110                         str_list_deinit(pkg->installed_files);
1111                         pkg->installed_files = NULL;
1112                         return NULL;
1113                 }
1114                 rewind(list_file);
1115         } else {
1116                 sprintf_alloc(&list_file_name, "%s/%s.list",
1117                               pkg->dest->info_dir, pkg->name);
1118                 list_file = fopen(list_file_name, "r");
1119                 if (list_file == NULL) {
1120                         opkg_perror(ERROR, "Failed to open %s", list_file_name);
1121                         free(list_file_name);
1122                         return pkg->installed_files;
1123                 }
1124                 free(list_file_name);
1125         }
1126
1127         if (conf->offline_root)
1128                 rootdirlen = strlen(conf->offline_root);
1129
1130         while (1) {
1131                 char *file_name;
1132
1133                 line = file_read_line_alloc(list_file);
1134                 if (line == NULL) {
1135                         break;
1136                 }
1137                 file_name = line;
1138
1139                 if (list_from_package) {
1140                         if (*file_name == '.') {
1141                                 file_name++;
1142                         }
1143                         if (*file_name == '/') {
1144                                 file_name++;
1145                         }
1146                         sprintf_alloc(&installed_file_name, "%s%s",
1147                                       pkg->dest->root_dir, file_name);
1148                 } else {
1149                         if (conf->offline_root &&
1150                             strncmp(conf->offline_root, file_name,
1151                                     rootdirlen)) {
1152                                 sprintf_alloc(&installed_file_name, "%s%s",
1153                                               conf->offline_root, file_name);
1154                         } else {
1155                                 // already contains root_dir as header -> ABSOLUTE
1156                                 sprintf_alloc(&installed_file_name, "%s",
1157                                               file_name);
1158                         }
1159                 }
1160                 str_list_append(pkg->installed_files, installed_file_name);
1161                 free(installed_file_name);
1162                 free(line);
1163         }
1164
1165         fclose(list_file);
1166
1167         if (list_from_package) {
1168                 unlink(list_file_name);
1169                 free(list_file_name);
1170         }
1171
1172         return pkg->installed_files;
1173 }
1174
1175 /* XXX: CLEANUP: This function and it's counterpart,
1176    (pkg_get_installed_files), do not match our init/deinit naming
1177    convention. Nor the alloc/free convention. But, then again, neither
1178    of these conventions currrently fit the way these two functions
1179    work. */
1180 void pkg_free_installed_files(pkg_t * pkg)
1181 {
1182         pkg->installed_files_ref_cnt--;
1183
1184         if (pkg->installed_files_ref_cnt > 0)
1185                 return;
1186
1187         if (pkg->installed_files) {
1188                 str_list_purge(pkg->installed_files);
1189         }
1190
1191         pkg->installed_files = NULL;
1192 }
1193
1194 void pkg_remove_installed_files_list(pkg_t * pkg)
1195 {
1196         char *list_file_name;
1197
1198         sprintf_alloc(&list_file_name, "%s/%s.list",
1199                       pkg->dest->info_dir, pkg->name);
1200
1201         if (!conf->noaction)
1202                 (void)unlink(list_file_name);
1203
1204         free(list_file_name);
1205 }
1206
1207 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name)
1208 {
1209         conffile_list_elt_t *iter;
1210         conffile_list_t *cl;
1211         conffile_t *conffile;
1212
1213         if (pkg == NULL) {
1214                 return NULL;
1215         }
1216
1217         cl = pkg_get_ptr(pkg, PKG_CONFFILES);
1218
1219         for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
1220              iter = nv_pair_list_next(cl, iter)) {
1221                 conffile = (conffile_t *) iter->data;
1222
1223                 if (strcmp(conffile->name, file_name) == 0) {
1224                         return conffile;
1225                 }
1226         }
1227
1228         return NULL;
1229 }
1230
1231 int pkg_run_script(pkg_t * pkg, const char *script, const char *args)
1232 {
1233         int err;
1234         char *path;
1235         char *cmd;
1236         char *tmp_unpack_dir;
1237
1238         if (conf->noaction)
1239                 return 0;
1240
1241         /* XXX: FEATURE: When conf->offline_root is set, we should run the
1242            maintainer script within a chroot environment. */
1243         if (conf->offline_root && !conf->force_postinstall) {
1244                 opkg_msg(INFO, "Offline root mode: not running %s.%s.\n",
1245                          pkg->name, script);
1246                 return 0;
1247         }
1248
1249         /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1250            have scripts in tmp_unpack_dir. */
1251         if (pkg->state_status == SS_INSTALLED
1252             || pkg->state_status == SS_UNPACKED) {
1253                 if (pkg->dest == NULL) {
1254                         opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n",
1255                                  pkg->name);
1256                         return -1;
1257                 }
1258                 sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name,
1259                               script);
1260         } else {
1261                 tmp_unpack_dir = pkg_get_string(pkg, PKG_TMP_UNPACK_DIR);
1262                 if (tmp_unpack_dir == NULL) {
1263                         opkg_msg(ERROR,
1264                                  "Internal error: %s has a NULL tmp_unpack_dir.\n",
1265                                  pkg->name);
1266                         return -1;
1267                 }
1268                 sprintf_alloc(&path, "%s/%s", tmp_unpack_dir, script);
1269         }
1270
1271         opkg_msg(INFO, "Running script %s.\n", path);
1272
1273         setenv("PKG_ROOT",
1274                pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir,
1275                1);
1276
1277         if (pkg->is_upgrade)
1278                 setenv("PKG_UPGRADE", "1", 1);
1279         else
1280                 setenv("PKG_UPGRADE", "0", 1);
1281
1282         if (!file_exists(path)) {
1283                 free(path);
1284                 return 0;
1285         }
1286
1287         sprintf_alloc(&cmd, "%s %s", path, args);
1288         free(path);
1289         {
1290                 const char *argv[] = { "sh", "-c", cmd, NULL };
1291                 err = xsystem(argv);
1292         }
1293         free(cmd);
1294
1295         if (err) {
1296                 opkg_msg(ERROR,
1297                          "package \"%s\" %s script returned status %d.\n",
1298                          pkg->name, script, err);
1299                 return err;
1300         }
1301
1302         return 0;
1303 }
1304
1305 int pkg_arch_supported(pkg_t * pkg)
1306 {
1307         nv_pair_list_elt_t *l;
1308         char *architecture = pkg_get_string(pkg, PKG_ARCHITECTURE);
1309
1310         if (!architecture)
1311                 return 1;
1312
1313         list_for_each_entry(l, &conf->arch_list.head, node) {
1314                 nv_pair_t *nv = (nv_pair_t *) l->data;
1315                 if (strcmp(nv->name, architecture) == 0) {
1316                         opkg_msg(DEBUG,
1317                                  "Arch %s (priority %s) supported for pkg %s.\n",
1318                                  nv->name, nv->value, pkg->name);
1319                         return 1;
1320                 }
1321         }
1322
1323         opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n",
1324                  architecture, pkg->name);
1325         return 0;
1326 }
1327
1328 void pkg_info_preinstall_check(void)
1329 {
1330         int i;
1331         pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1332
1333         /* update the file owner data structure */
1334         opkg_msg(INFO, "Updating file owner list.\n");
1335         pkg_hash_fetch_all_installed(installed_pkgs);
1336         for (i = 0; i < installed_pkgs->len; i++) {
1337                 pkg_t *pkg = installed_pkgs->pkgs[i];
1338                 str_list_t *installed_files = pkg_get_installed_files(pkg);     /* this causes installed_files to be cached */
1339                 str_list_elt_t *iter, *niter;
1340                 if (installed_files == NULL) {
1341                         opkg_msg(ERROR, "Failed to determine installed "
1342                                  "files for pkg %s.\n", pkg->name);
1343                         break;
1344                 }
1345                 for (iter = str_list_first(installed_files), niter =
1346                      str_list_next(installed_files, iter); iter;
1347                      iter = niter, niter =
1348                      str_list_next(installed_files, iter)) {
1349                         char *installed_file = (char *)iter->data;
1350                         file_hash_set_file_owner(installed_file, pkg);
1351                 }
1352                 pkg_free_installed_files(pkg);
1353         }
1354         pkg_vec_free(installed_pkgs);
1355 }
1356
1357 struct pkg_write_filelist_data {
1358         pkg_t *pkg;
1359         FILE *stream;
1360 };
1361
1362 static void
1363 pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
1364 {
1365         struct pkg_write_filelist_data *data = data_;
1366         pkg_t *entry = entry_;
1367         if (entry == data->pkg) {
1368                 fprintf(data->stream, "%s\n", key);
1369         }
1370 }
1371
1372 int pkg_write_filelist(pkg_t * pkg)
1373 {
1374         struct pkg_write_filelist_data data;
1375         char *list_file_name;
1376
1377         sprintf_alloc(&list_file_name, "%s/%s.list",
1378                       pkg->dest->info_dir, pkg->name);
1379
1380         opkg_msg(INFO, "Creating %s file for pkg %s.\n",
1381                  list_file_name, pkg->name);
1382
1383         data.stream = fopen(list_file_name, "w");
1384         if (!data.stream) {
1385                 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1386                 free(list_file_name);
1387                 return -1;
1388         }
1389
1390         data.pkg = pkg;
1391         hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
1392         fclose(data.stream);
1393         free(list_file_name);
1394
1395         pkg->state_flag &= ~SF_FILELIST_CHANGED;
1396
1397         return 0;
1398 }
1399
1400 int pkg_write_changed_filelists(void)
1401 {
1402         pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1403         int i, err, ret = 0;
1404
1405         if (conf->noaction)
1406                 return 0;
1407
1408         opkg_msg(INFO, "Saving changed filelists.\n");
1409
1410         pkg_hash_fetch_all_installed(installed_pkgs);
1411         for (i = 0; i < installed_pkgs->len; i++) {
1412                 pkg_t *pkg = installed_pkgs->pkgs[i];
1413                 if (pkg->state_flag & SF_FILELIST_CHANGED) {
1414                         err = pkg_write_filelist(pkg);
1415                         if (err)
1416                                 ret = -1;
1417                 }
1418         }
1419
1420         pkg_vec_free(installed_pkgs);
1421
1422         return ret;
1423 }