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