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