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