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