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