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