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