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