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