1 /* pkg.c - the opkg package management system
5 Copyright (C) 2001 University of Southern California
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.
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.
26 #include "pkg_parse.h"
27 #include "pkg_extract.h"
28 #include "opkg_message.h"
29 #include "opkg_utils.h"
31 #include "libbb/libbb.h"
32 #include "sprintf_alloc.h"
33 #include "file_util.h"
35 #include "opkg_conf.h"
37 typedef struct enum_map enum_map_t
;
43 static const enum_map_t pkg_state_want_map
[] = {
44 {SW_UNKNOWN
, "unknown"},
45 {SW_INSTALL
, "install"},
46 {SW_DEINSTALL
, "deinstall"},
50 static const enum_map_t pkg_state_flag_map
[] = {
52 {SF_REINSTREQ
, "reinstreq"},
54 {SF_REPLACE
, "replace"},
55 {SF_NOPRUNE
, "noprune"},
56 {SF_PREFER
, "prefer"},
57 {SF_OBSOLETE
, "obsolete"},
61 static const enum_map_t pkg_state_status_map
[] = {
62 {SS_NOT_INSTALLED
, "not-installed"},
63 {SS_UNPACKED
, "unpacked"},
64 {SS_HALF_CONFIGURED
, "half-configured"},
65 {SS_INSTALLED
, "installed"},
66 {SS_HALF_INSTALLED
, "half-installed"},
67 {SS_CONFIG_FILES
, "config-files"},
68 {SS_POST_INST_FAILED
, "post-inst-failed"},
69 {SS_REMOVAL_FAILED
, "removal-failed"}
72 static void pkg_init(pkg_t
* pkg
)
77 pkg
->state_want
= SW_UNKNOWN
;
78 pkg
->state_flag
= SF_OK
;
79 pkg
->state_status
= SS_NOT_INSTALLED
;
81 pkg
->installed_files
= NULL
;
82 pkg
->installed_files_ref_cnt
= 0;
84 pkg
->provided_by_hand
= 0;
88 blob_buf_init(&pkg
->blob
, 0);
95 pkg
= xcalloc(1, sizeof(pkg_t
));
101 void *pkg_set_raw(pkg_t
*pkg
, int id
, const void *val
, size_t len
)
104 struct blob_attr
*cur
;
106 blob_for_each_attr(cur
, pkg
->blob
.head
, rem
) {
107 if (blob_id(cur
) == id
) {
108 if (blob_len(cur
) < len
) {
109 fprintf(stderr
, "ERROR: truncating field %d <%p> to %d byte",
110 id
, val
, blob_len(cur
));
112 memcpy(blob_data(cur
), val
, blob_len(cur
));
113 return blob_data(cur
);
117 cur
= blob_put(&pkg
->blob
, id
, val
, len
);
118 return cur
? blob_data(cur
) : NULL
;
121 void *pkg_get_raw(const pkg_t
* pkg
, int id
)
124 struct blob_attr
*cur
;
126 blob_for_each_attr(cur
, pkg
->blob
.head
, rem
)
127 if (blob_id(cur
) == id
)
128 return blob_data(cur
);
133 char *pkg_set_string(pkg_t
*pkg
, int id
, const char *s
)
143 while (isspace(*s
)) {
148 while (len
> 0 && isspace(s
[len
- 1]))
154 p
= pkg_set_raw(pkg
, id
, s
, len
+ 1);
160 char *pkg_get_architecture(const pkg_t
*pkg
)
162 nv_pair_list_elt_t
*l
;
165 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
166 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
167 if (n
++ == pkg
->arch_index
)
174 char *pkg_set_architecture(pkg_t
*pkg
, const char *architecture
, ssize_t len
)
176 nv_pair_list_elt_t
*l
;
179 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
180 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
182 if (!strncmp(nv
->name
, architecture
, len
) && nv
->name
[len
] == '\0') {
184 opkg_msg(ERROR
, "Internal error: too many different architectures\n");
199 int pkg_get_arch_priority(const pkg_t
*pkg
)
201 nv_pair_list_elt_t
*l
;
204 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
205 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
206 if (n
++ == pkg
->arch_index
)
207 return strtol(nv
->value
, NULL
, 0);
213 char *pkg_get_md5(const pkg_t
*pkg
)
215 char *p
= pkg_get_raw(pkg
, PKG_MD5SUM
);
220 return checksum_bin2hex(p
, 16);
223 char *pkg_set_md5(pkg_t
*pkg
, const char *cksum
)
226 char *p
= checksum_hex2bin(cksum
, &len
);
231 return pkg_set_raw(pkg
, PKG_MD5SUM
, p
, len
);
234 char *pkg_get_sha256(const pkg_t
*pkg
)
236 char *p
= pkg_get_raw(pkg
, PKG_SHA256SUM
);
241 return checksum_bin2hex(p
, 32);
244 char *pkg_set_sha256(pkg_t
*pkg
, const char *cksum
)
247 char *p
= checksum_hex2bin(cksum
, &len
);
252 return pkg_set_raw(pkg
, PKG_SHA256SUM
, p
, len
);
256 static void compound_depend_deinit(compound_depend_t
* depends
)
259 for (i
= 0; i
< depends
->possibility_count
; i
++) {
261 d
= depends
->possibilities
[i
];
265 free(depends
->possibilities
);
268 void pkg_deinit(pkg_t
* pkg
)
271 struct blob_attr
*cur
;
272 compound_depend_t
*deps
, *dep
;
279 /* owned by opkg_conf_t */
281 /* owned by opkg_conf_t */
284 pkg
->state_want
= SW_UNKNOWN
;
285 pkg
->state_flag
= SF_OK
;
286 pkg
->state_status
= SS_NOT_INSTALLED
;
288 blob_for_each_attr(cur
, pkg
->blob
.head
, rem
) {
289 switch (blob_id(cur
)) {
292 deps
= pkg_get_ptr(pkg
, blob_id(cur
));
295 for (dep
= deps
; dep
->type
; dep
++)
296 compound_depend_deinit(dep
);
301 pkg_set_ptr(pkg
, blob_id(cur
), NULL
);
306 ptr
= pkg_get_ptr(pkg
, blob_id(cur
));
311 pkg_set_ptr(pkg
, blob_id(cur
), NULL
);
315 ptr
= pkg_get_ptr(pkg
, blob_id(cur
));
318 conffile_list_deinit(ptr
);
322 pkg_set_ptr(pkg
, blob_id(cur
), NULL
);
324 case PKG_ALTERNATIVES
:
325 ptr
= pkg_get_ptr(pkg
, blob_id(cur
));
328 struct pkg_alternatives
*pkg_alts
= ptr
;
330 while (pkg_alts
->nalts
)
331 free(pkg_alts
->alts
[--pkg_alts
->nalts
]);
332 free(pkg_alts
->alts
);
336 pkg_set_ptr(pkg
, blob_id(cur
), NULL
);
341 //conffile_list_deinit(&pkg->conffiles);
343 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
344 since if they are calling deinit, they should know. Maybe do an
345 assertion here instead? */
346 pkg
->installed_files_ref_cnt
= 1;
347 pkg_free_installed_files(pkg
);
350 blob_buf_free(&pkg
->blob
);
353 int pkg_init_from_file(pkg_t
* pkg
, const char *filename
)
357 char *control_path
, *tmp
;
361 if (!(pkg
->state_flag
& SF_NEED_DETAIL
)) {
362 opkg_msg(DEBUG
, "applying abpkg flag to %s\n", filename
);
363 pkg
->state_flag
|= SF_NEED_DETAIL
;
366 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, filename
);
368 tmp
= xstrdup(filename
);
369 sprintf_alloc(&control_path
, "%s/%s.control.XXXXXX",
370 conf
->tmp_dir
, basename(tmp
));
372 fd
= mkstemp(control_path
);
374 opkg_perror(ERROR
, "Failed to make temp file %s", control_path
);
379 control_file
= fdopen(fd
, "r+");
380 if (control_file
== NULL
) {
381 opkg_perror(ERROR
, "Failed to fdopen %s", control_path
);
387 err
= pkg_extract_control_file_to_stream(pkg
, control_file
);
389 opkg_msg(ERROR
, "Failed to extract control file from %s.\n",
394 rewind(control_file
);
396 if ((err
= pkg_parse_from_stream(pkg
, control_file
, 0))) {
398 opkg_msg(ERROR
, "Malformed package file %s.\n",
405 fclose(control_file
);
407 unlink(control_path
);
414 /* Merge any new information in newpkg into oldpkg */
415 int pkg_merge(pkg_t
* oldpkg
, pkg_t
* newpkg
)
419 if (oldpkg
== newpkg
) {
423 if (!oldpkg
->auto_installed
)
424 oldpkg
->auto_installed
= newpkg
->auto_installed
;
427 oldpkg
->src
= newpkg
->src
;
429 oldpkg
->dest
= newpkg
->dest
;
430 if (!oldpkg
->arch_index
)
431 oldpkg
->arch_index
= newpkg
->arch_index
;
432 if (!pkg_get_string(oldpkg
, PKG_SECTION
))
433 pkg_set_string(oldpkg
, PKG_SECTION
, pkg_get_string(newpkg
, PKG_SECTION
));
434 if (!pkg_get_string(oldpkg
, PKG_MAINTAINER
))
435 pkg_set_string(oldpkg
, PKG_MAINTAINER
, pkg_get_string(newpkg
, PKG_MAINTAINER
));
436 if (!pkg_get_string(oldpkg
, PKG_DESCRIPTION
))
437 pkg_set_string(oldpkg
, PKG_DESCRIPTION
, pkg_get_string(newpkg
, PKG_DESCRIPTION
));
439 if (!pkg_get_ptr(oldpkg
, PKG_DEPENDS
)) {
440 pkg_set_ptr(oldpkg
, PKG_DEPENDS
, pkg_get_ptr(newpkg
, PKG_DEPENDS
));
441 pkg_set_ptr(newpkg
, PKG_DEPENDS
, NULL
);
444 ab
= pkg_get_ptr(oldpkg
, PKG_PROVIDES
);
446 if (!ab
|| !ab
[0] || !ab
[1]) {
447 pkg_set_ptr(oldpkg
, PKG_PROVIDES
, pkg_get_ptr(newpkg
, PKG_PROVIDES
));
448 pkg_set_ptr(newpkg
, PKG_PROVIDES
, NULL
);
454 if (!pkg_get_ptr(oldpkg
, PKG_CONFLICTS
)) {
455 pkg_set_ptr(oldpkg
, PKG_CONFLICTS
, pkg_get_ptr(newpkg
, PKG_CONFLICTS
));
456 pkg_set_ptr(newpkg
, PKG_CONFLICTS
, NULL
);
459 if (!pkg_get_ptr(oldpkg
, PKG_REPLACES
)) {
460 pkg_set_ptr(oldpkg
, PKG_REPLACES
, pkg_get_ptr(newpkg
, PKG_REPLACES
));
461 pkg_set_ptr(newpkg
, PKG_REPLACES
, NULL
);
464 if (!pkg_get_string(oldpkg
, PKG_FILENAME
))
465 pkg_set_string(oldpkg
, PKG_FILENAME
, pkg_get_string(newpkg
, PKG_FILENAME
));
466 if (!pkg_get_string(oldpkg
, PKG_LOCAL_FILENAME
))
467 pkg_set_string(oldpkg
, PKG_LOCAL_FILENAME
, pkg_get_string(newpkg
, PKG_LOCAL_FILENAME
));
468 if (!pkg_get_string(oldpkg
, PKG_TMP_UNPACK_DIR
))
469 pkg_set_string(oldpkg
, PKG_TMP_UNPACK_DIR
, pkg_get_string(newpkg
, PKG_TMP_UNPACK_DIR
));
470 if (!pkg_get_md5(oldpkg
))
471 pkg_set_md5(oldpkg
, pkg_get_md5(newpkg
));
472 if (!pkg_get_sha256(oldpkg
))
473 pkg_set_sha256(oldpkg
, pkg_get_sha256(newpkg
));
474 if (!pkg_get_int(oldpkg
, PKG_SIZE
))
475 pkg_set_int(oldpkg
, PKG_SIZE
, pkg_get_int(newpkg
, PKG_SIZE
));
476 if (!pkg_get_int(oldpkg
, PKG_INSTALLED_SIZE
))
477 pkg_set_int(oldpkg
, PKG_INSTALLED_SIZE
, pkg_get_int(newpkg
, PKG_INSTALLED_SIZE
));
478 if (!pkg_get_string(oldpkg
, PKG_PRIORITY
))
479 pkg_set_string(oldpkg
, PKG_PRIORITY
, pkg_get_string(newpkg
, PKG_PRIORITY
));
480 if (!pkg_get_string(oldpkg
, PKG_SOURCE
))
481 pkg_set_string(oldpkg
, PKG_SOURCE
, pkg_get_string(newpkg
, PKG_SOURCE
));
483 if (!pkg_get_ptr(oldpkg
, PKG_CONFFILES
)) {
484 pkg_set_ptr(oldpkg
, PKG_CONFFILES
, pkg_get_ptr(newpkg
, PKG_CONFFILES
));
485 pkg_set_ptr(newpkg
, PKG_CONFFILES
, NULL
);
488 if (!oldpkg
->installed_files
) {
489 oldpkg
->installed_files
= newpkg
->installed_files
;
490 oldpkg
->installed_files_ref_cnt
=
491 newpkg
->installed_files_ref_cnt
;
492 newpkg
->installed_files
= NULL
;
495 if (!oldpkg
->essential
)
496 oldpkg
->essential
= newpkg
->essential
;
498 if (!oldpkg
->provided_by_hand
)
499 oldpkg
->provided_by_hand
= newpkg
->provided_by_hand
;
504 static void abstract_pkg_init(abstract_pkg_t
* ab_pkg
)
506 ab_pkg
->provided_by
= abstract_pkg_vec_alloc();
507 ab_pkg
->dependencies_checked
= 0;
508 ab_pkg
->state_status
= SS_NOT_INSTALLED
;
511 abstract_pkg_t
*abstract_pkg_new(void)
513 abstract_pkg_t
*ab_pkg
;
515 ab_pkg
= xcalloc(1, sizeof(abstract_pkg_t
));
516 abstract_pkg_init(ab_pkg
);
521 static const char *pkg_state_want_to_str(pkg_state_want_t sw
)
525 for (i
= 0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
526 if (pkg_state_want_map
[i
].value
== sw
) {
527 return pkg_state_want_map
[i
].str
;
531 opkg_msg(ERROR
, "Internal error: state_want=%d\n", sw
);
532 return "<STATE_WANT_UNKNOWN>";
535 pkg_state_want_t
pkg_state_want_from_str(char *str
)
539 for (i
= 0; i
< ARRAY_SIZE(pkg_state_want_map
); i
++) {
540 if (strcmp(str
, pkg_state_want_map
[i
].str
) == 0) {
541 return pkg_state_want_map
[i
].value
;
545 opkg_msg(ERROR
, "Internal error: state_want=%s\n", str
);
549 static char *pkg_state_flag_to_str(pkg_state_flag_t sf
)
555 /* clear the temporary flags before converting to string */
556 sf
&= SF_NONVOLATILE_FLAGS
;
559 return xstrdup("ok");
562 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
563 if (sf
& pkg_state_flag_map
[i
].value
)
564 len
+= strlen(pkg_state_flag_map
[i
].str
) + 1;
567 str
= xmalloc(len
+ 1);
570 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
571 if (sf
& pkg_state_flag_map
[i
].value
) {
572 strncat(str
, pkg_state_flag_map
[i
].str
, len
);
573 strncat(str
, ",", len
);
578 str
[len
- 1] = '\0'; /* squash last comma */
583 pkg_state_flag_t
pkg_state_flag_from_str(const char *str
)
588 unsigned int sfname_len
;
590 if (strcmp(str
, "ok") == 0) {
593 for (i
= 0; i
< ARRAY_SIZE(pkg_state_flag_map
); i
++) {
594 sfname
= pkg_state_flag_map
[i
].str
;
595 sfname_len
= strlen(sfname
);
596 if (strncmp(str
, sfname
, sfname_len
) == 0) {
597 sf
|= pkg_state_flag_map
[i
].value
;
610 static const char *pkg_state_status_to_str(pkg_state_status_t ss
)
614 for (i
= 0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
615 if (pkg_state_status_map
[i
].value
== ss
) {
616 return pkg_state_status_map
[i
].str
;
620 opkg_msg(ERROR
, "Internal error: state_status=%d\n", ss
);
621 return "<STATE_STATUS_UNKNOWN>";
624 pkg_state_status_t
pkg_state_status_from_str(const char *str
)
628 for (i
= 0; i
< ARRAY_SIZE(pkg_state_status_map
); i
++) {
629 if (strcmp(str
, pkg_state_status_map
[i
].str
) == 0) {
630 return pkg_state_status_map
[i
].value
;
634 opkg_msg(ERROR
, "Internal error: state_status=%s\n", str
);
635 return SS_NOT_INSTALLED
;
638 void pkg_formatted_field(FILE * fp
, pkg_t
* pkg
, const char *field
)
643 compound_depend_t
*dep
;
644 abstract_pkg_t
**ab_pkg
;
646 if (strlen(field
) < PKG_MINIMUM_FIELD_NAME_LEN
) {
647 goto UNKNOWN_FMT_FIELD
;
653 if (strcasecmp(field
, "Alternatives") == 0) {
654 struct pkg_alternatives
*pkg_alts
= pkg_get_ptr(pkg
, PKG_ALTERNATIVES
);
656 if (pkg_alts
&& pkg_alts
->nalts
> 0) {
658 struct pkg_alternative
*alt
;
660 alt
= pkg_alts
->alts
[0];
661 fprintf(fp
, "Alternatives: %d:%s:%s", alt
->prio
, alt
->path
, alt
->altpath
);
662 for (i
= 1; i
< pkg_alts
->nalts
; i
++) {
663 alt
= pkg_alts
->alts
[i
];
664 fprintf(fp
, ", %d:%s:%s", alt
->prio
, alt
->path
, alt
->altpath
);
668 } else if (strcasecmp(field
, "Architecture") == 0) {
669 p
= pkg_get_architecture(pkg
);
671 fprintf(fp
, "Architecture: %s\n",
674 } else if (strcasecmp(field
, "Auto-Installed") == 0) {
675 if (pkg
->auto_installed
)
676 fprintf(fp
, "Auto-Installed: yes\n");
678 goto UNKNOWN_FMT_FIELD
;
683 if (strcasecmp(field
, "Conffiles") == 0) {
685 conffile_list_elt_t
*iter
;
687 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
689 if (!cl
|| nv_pair_list_empty(cl
))
692 fprintf(fp
, "Conffiles:\n");
693 for (iter
= nv_pair_list_first(cl
); iter
;
694 iter
= nv_pair_list_next(cl
, iter
)) {
695 if (((conffile_t
*) iter
->data
)->name
696 && ((conffile_t
*) iter
->data
)->value
) {
697 fprintf(fp
, " %s %s\n",
698 ((conffile_t
*) iter
->data
)->
700 ((conffile_t
*) iter
->data
)->
704 } else if (strcasecmp(field
, "Conflicts") == 0) {
706 compound_depend_t
*deps
, *dep
;
707 deps
= pkg_get_ptr(pkg
, PKG_CONFLICTS
);
709 fprintf(fp
, "Conflicts:");
710 for (i
= 0, dep
= deps
; dep
->type
; dep
++, i
++) {
711 cdep
= dep
->possibilities
[0];
712 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
715 fprintf(fp
, " (%s%s)",
716 constraint_to_str(cdep
->
724 goto UNKNOWN_FMT_FIELD
;
729 if (strcasecmp(field
, "Depends") == 0) {
730 dep
= pkg_get_depends(pkg
, DEPEND
);
732 fprintf(fp
, "Depends:");
733 for (i
= 0, j
= 0; dep
&& dep
->type
; i
++, dep
++) {
734 if (dep
->type
!= DEPEND
)
736 str
= pkg_depend_str(pkg
, i
);
737 fprintf(fp
, "%s %s", j
== 0 ? "" : ",",
744 } else if (strcasecmp(field
, "Description") == 0) {
745 p
= pkg_get_string(pkg
, PKG_DESCRIPTION
);
747 fprintf(fp
, "Description: %s\n",
751 goto UNKNOWN_FMT_FIELD
;
756 if (pkg
->essential
) {
757 fprintf(fp
, "Essential: yes\n");
762 p
= pkg_get_string(pkg
, PKG_FILENAME
);
764 fprintf(fp
, "Filename: %s\n", p
);
769 if (strcasecmp(field
, "Installed-Size") == 0) {
770 fprintf(fp
, "Installed-Size: %lu\n",
771 (unsigned long) pkg_get_int(pkg
, PKG_INSTALLED_SIZE
));
772 } else if (strcasecmp(field
, "Installed-Time") == 0) {
773 i
= pkg_get_int(pkg
, PKG_INSTALLED_TIME
);
775 fprintf(fp
, "Installed-Time: %lu\n",
782 if (strcasecmp(field
, "Maintainer") == 0) {
783 p
= pkg_get_string(pkg
, PKG_MAINTAINER
);
785 fprintf(fp
, "Maintainer: %s\n", p
);
787 } else if (strcasecmp(field
, "MD5sum") == 0) {
788 p
= pkg_get_md5(pkg
);
790 fprintf(fp
, "MD5Sum: %s\n", p
);
793 goto UNKNOWN_FMT_FIELD
;
798 if (strcasecmp(field
, "Package") == 0) {
799 fprintf(fp
, "Package: %s\n", pkg
->name
);
800 } else if (strcasecmp(field
, "Priority") == 0) {
801 fprintf(fp
, "Priority: %s\n", pkg_get_string(pkg
, PKG_PRIORITY
));
802 } else if (strcasecmp(field
, "Provides") == 0) {
803 ab_pkg
= pkg_get_ptr(pkg
, PKG_PROVIDES
);
804 if (ab_pkg
&& ab_pkg
[0] && ab_pkg
[1]) {
805 fprintf(fp
, "Provides:");
806 for (i
= 1; ab_pkg
[i
]; i
++) {
807 fprintf(fp
, "%s %s", i
== 1 ? "" : ",",
813 goto UNKNOWN_FMT_FIELD
;
818 if (strcasecmp(field
, "Replaces") == 0) {
819 ab_pkg
= pkg_get_ptr(pkg
, PKG_REPLACES
);
820 if (ab_pkg
&& ab_pkg
[0]) {
821 fprintf(fp
, "Replaces:");
822 for (i
= 0; ab_pkg
[i
]; i
++) {
823 fprintf(fp
, "%s %s", i
== 0 ? "" : ",",
828 } else if (strcasecmp(field
, "Recommends") == 0) {
829 dep
= pkg_get_depends(pkg
, RECOMMEND
);
831 fprintf(fp
, "Recommends:");
832 for (j
= 0, i
= 0; dep
&& dep
->type
; i
++, dep
++) {
833 if (dep
->type
!= RECOMMEND
)
835 str
= pkg_depend_str(pkg
, i
);
836 fprintf(fp
, "%s %s", j
== 0 ? "" : ",",
844 goto UNKNOWN_FMT_FIELD
;
849 if (strcasecmp(field
, "Section") == 0) {
850 p
= pkg_get_string(pkg
, PKG_SECTION
);
852 fprintf(fp
, "Section: %s\n", p
);
854 } else if (strcasecmp(field
, "SHA256sum") == 0) {
855 p
= pkg_get_string(pkg
, PKG_SHA256SUM
);
857 fprintf(fp
, "SHA256sum: %s\n", p
);
859 } else if (strcasecmp(field
, "Size") == 0) {
860 i
= pkg_get_int(pkg
, PKG_SIZE
);
862 fprintf(fp
, "Size: %lu\n", (unsigned long) i
);
864 } else if (strcasecmp(field
, "Source") == 0) {
865 p
= pkg_get_string(pkg
, PKG_SOURCE
);
867 fprintf(fp
, "Source: %s\n", p
);
869 } else if (strcasecmp(field
, "Status") == 0) {
870 char *pflag
= pkg_state_flag_to_str(pkg
->state_flag
);
871 fprintf(fp
, "Status: %s %s %s\n",
872 pkg_state_want_to_str(pkg
->state_want
),
874 pkg_state_status_to_str(pkg
->state_status
));
876 } else if (strcasecmp(field
, "Suggests") == 0) {
877 dep
= pkg_get_depends(pkg
, SUGGEST
);
879 fprintf(fp
, "Suggests:");
880 for (j
= 0, i
= 0; dep
&& dep
->type
; i
++, dep
++) {
881 if (dep
->type
!= SUGGEST
)
883 str
= pkg_depend_str(pkg
, i
);
884 fprintf(fp
, "%s %s", j
== 0 ? "" : ",",
892 goto UNKNOWN_FMT_FIELD
;
897 if (strcasecmp(field
, "Tags") == 0) {
898 p
= pkg_get_string(pkg
, PKG_TAGS
);
900 fprintf(fp
, "Tags: %s\n", p
);
907 char *version
= pkg_version_str_alloc(pkg
);
910 fprintf(fp
, "Version: %s\n", version
);
915 goto UNKNOWN_FMT_FIELD
;
921 opkg_msg(ERROR
, "Internal error: field=%s\n", field
);
924 void pkg_formatted_info(FILE * fp
, pkg_t
* pkg
)
926 pkg_formatted_field(fp
, pkg
, "Package");
927 pkg_formatted_field(fp
, pkg
, "Version");
928 pkg_formatted_field(fp
, pkg
, "Depends");
929 pkg_formatted_field(fp
, pkg
, "Recommends");
930 pkg_formatted_field(fp
, pkg
, "Suggests");
931 pkg_formatted_field(fp
, pkg
, "Provides");
932 pkg_formatted_field(fp
, pkg
, "Replaces");
933 pkg_formatted_field(fp
, pkg
, "Conflicts");
934 pkg_formatted_field(fp
, pkg
, "Status");
935 pkg_formatted_field(fp
, pkg
, "Section");
936 pkg_formatted_field(fp
, pkg
, "Essential");
937 pkg_formatted_field(fp
, pkg
, "Architecture");
938 pkg_formatted_field(fp
, pkg
, "Maintainer");
939 pkg_formatted_field(fp
, pkg
, "MD5sum");
940 pkg_formatted_field(fp
, pkg
, "Size");
941 pkg_formatted_field(fp
, pkg
, "Filename");
942 pkg_formatted_field(fp
, pkg
, "Conffiles");
943 pkg_formatted_field(fp
, pkg
, "Source");
944 pkg_formatted_field(fp
, pkg
, "Description");
945 pkg_formatted_field(fp
, pkg
, "Installed-Time");
946 pkg_formatted_field(fp
, pkg
, "Tags");
950 void pkg_print_status(pkg_t
* pkg
, FILE * file
)
956 pkg_formatted_field(file
, pkg
, "Package");
957 pkg_formatted_field(file
, pkg
, "Version");
958 pkg_formatted_field(file
, pkg
, "Depends");
959 pkg_formatted_field(file
, pkg
, "Recommends");
960 pkg_formatted_field(file
, pkg
, "Suggests");
961 pkg_formatted_field(file
, pkg
, "Provides");
962 pkg_formatted_field(file
, pkg
, "Replaces");
963 pkg_formatted_field(file
, pkg
, "Conflicts");
964 pkg_formatted_field(file
, pkg
, "Status");
965 pkg_formatted_field(file
, pkg
, "Essential");
966 pkg_formatted_field(file
, pkg
, "Architecture");
967 pkg_formatted_field(file
, pkg
, "Conffiles");
968 pkg_formatted_field(file
, pkg
, "Installed-Time");
969 pkg_formatted_field(file
, pkg
, "Auto-Installed");
970 pkg_formatted_field(file
, pkg
, "Alternatives");
975 * libdpkg - Debian packaging suite library routines
976 * vercmp.c - comparison of version numbers
978 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
981 /* assume ascii; warning: evaluates x multiple times! */
982 #define order(x) ((x) == '~' ? -1 \
985 : isalpha((x)) ? (x) \
988 static int verrevcmp(const char *val
, const char *ref
)
995 while (*val
|| *ref
) {
998 while ((*val
&& !isdigit(*val
)) || (*ref
&& !isdigit(*ref
))) {
999 int vc
= order(*val
), rc
= order(*ref
);
1010 while (isdigit(*val
) && isdigit(*ref
)) {
1012 first_diff
= *val
- *ref
;
1026 int pkg_compare_versions(const pkg_t
* pkg
, const pkg_t
* ref_pkg
)
1028 unsigned int epoch1
= (unsigned int) pkg_get_int(pkg
, PKG_EPOCH
);
1029 unsigned int epoch2
= (unsigned int) pkg_get_int(ref_pkg
, PKG_EPOCH
);
1030 char *revision1
= pkg_get_string(pkg
, PKG_REVISION
);
1031 char *revision2
= pkg_get_string(ref_pkg
, PKG_REVISION
);
1032 const char *version1
= pkg_get_string(pkg
, PKG_VERSION
);
1033 const char *version2
= pkg_get_string(ref_pkg
, PKG_VERSION
);
1036 if (epoch1
> epoch2
) {
1040 if (epoch1
< epoch2
) {
1044 r
= verrevcmp(version1
, version2
);
1049 r
= verrevcmp(revision1
, revision2
);
1057 int pkg_version_satisfied(pkg_t
* it
, pkg_t
* ref
, const char *op
)
1061 r
= pkg_compare_versions(it
, ref
);
1063 if (strcmp(op
, "<=") == 0 || strcmp(op
, "<") == 0) {
1067 if (strcmp(op
, ">=") == 0 || strcmp(op
, ">") == 0) {
1071 if (strcmp(op
, "<<") == 0) {
1075 if (strcmp(op
, ">>") == 0) {
1079 if (strcmp(op
, "=") == 0) {
1083 opkg_msg(ERROR
, "Unknown operator: %s.\n", op
);
1087 int pkg_name_version_and_architecture_compare(const void *p1
, const void *p2
)
1089 const pkg_t
* a
= *(const pkg_t
**)p1
;
1090 const pkg_t
* b
= *(const pkg_t
**)p2
;
1093 int arch_prio1
, arch_prio2
;
1094 if (!a
->name
|| !b
->name
) {
1095 opkg_msg(ERROR
, "Internal error: a->name=%p, b->name=%p.\n",
1100 namecmp
= strcmp(a
->name
, b
->name
);
1103 vercmp
= pkg_compare_versions(a
, b
);
1106 arch_prio1
= pkg_get_arch_priority(a
);
1107 arch_prio2
= pkg_get_arch_priority(b
);
1108 if (!arch_prio1
|| !arch_prio2
) {
1110 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1111 arch_prio1
, arch_prio2
);
1114 if (arch_prio1
> arch_prio2
)
1116 if (arch_prio1
< arch_prio2
)
1121 int abstract_pkg_name_compare(const void *p1
, const void *p2
)
1123 const abstract_pkg_t
*a
= *(const abstract_pkg_t
**)p1
;
1124 const abstract_pkg_t
*b
= *(const abstract_pkg_t
**)p2
;
1125 if (!a
->name
|| !b
->name
) {
1126 opkg_msg(ERROR
, "Internal error: a->name=%p b->name=%p.\n",
1130 return strcmp(a
->name
, b
->name
);
1133 char *pkg_version_str_alloc(pkg_t
* pkg
)
1136 char *version
, *revptr
;
1137 unsigned int epoch
= (unsigned int) pkg_get_int(pkg
, PKG_EPOCH
);
1139 revptr
= pkg_get_string(pkg
, PKG_REVISION
);
1140 verstr
= pkg_get_string(pkg
, PKG_VERSION
);
1144 sprintf_alloc(&version
, "%d:%s-%s",
1145 epoch
, verstr
, revptr
);
1147 sprintf_alloc(&version
, "%d:%s",
1151 sprintf_alloc(&version
, "%s-%s",
1154 version
= xstrdup(verstr
);
1161 * XXX: this should be broken into two functions
1163 str_list_t
*pkg_get_installed_files(pkg_t
* pkg
)
1166 char *list_file_name
= NULL
;
1167 FILE *list_file
= NULL
;
1169 char *installed_file_name
;
1170 unsigned int rootdirlen
= 0;
1171 int list_from_package
;
1172 const char *local_filename
;
1174 pkg
->installed_files_ref_cnt
++;
1176 if (pkg
->installed_files
) {
1177 return pkg
->installed_files
;
1180 pkg
->installed_files
= str_list_alloc();
1183 * For installed packages, look at the package.list file in the database.
1184 * For uninstalled packages, get the file list directly from the package.
1186 if (pkg
->state_status
== SS_NOT_INSTALLED
|| pkg
->dest
== NULL
)
1187 list_from_package
= 1;
1189 list_from_package
= 0;
1191 if (list_from_package
) {
1192 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1194 if (!local_filename
) {
1195 return pkg
->installed_files
;
1197 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1198 file. In other words, change deb_extract so that it can
1199 simply return the file list as a char *[] rather than
1200 insisting on writing it to a FILE * as it does now. */
1201 sprintf_alloc(&list_file_name
, "%s/%s.list.XXXXXX",
1202 conf
->tmp_dir
, pkg
->name
);
1203 fd
= mkstemp(list_file_name
);
1205 opkg_perror(ERROR
, "Failed to make temp file %s.",
1207 free(list_file_name
);
1208 return pkg
->installed_files
;
1210 list_file
= fdopen(fd
, "r+");
1211 if (list_file
== NULL
) {
1212 opkg_perror(ERROR
, "Failed to fdopen temp file %s.",
1215 unlink(list_file_name
);
1216 free(list_file_name
);
1217 return pkg
->installed_files
;
1219 err
= pkg_extract_data_file_names_to_stream(pkg
, list_file
);
1221 opkg_msg(ERROR
, "Error extracting file list from %s.\n",
1224 unlink(list_file_name
);
1225 free(list_file_name
);
1226 str_list_deinit(pkg
->installed_files
);
1227 pkg
->installed_files
= NULL
;
1232 sprintf_alloc(&list_file_name
, "%s/%s.list",
1233 pkg
->dest
->info_dir
, pkg
->name
);
1234 list_file
= fopen(list_file_name
, "r");
1235 if (list_file
== NULL
) {
1236 opkg_perror(ERROR
, "Failed to open %s", list_file_name
);
1237 free(list_file_name
);
1238 return pkg
->installed_files
;
1240 free(list_file_name
);
1243 if (conf
->offline_root
)
1244 rootdirlen
= strlen(conf
->offline_root
);
1249 line
= file_read_line_alloc(list_file
);
1255 if (list_from_package
) {
1256 if (*file_name
== '.') {
1259 if (*file_name
== '/') {
1262 sprintf_alloc(&installed_file_name
, "%s%s",
1263 pkg
->dest
->root_dir
, file_name
);
1265 if (conf
->offline_root
&&
1266 strncmp(conf
->offline_root
, file_name
,
1268 sprintf_alloc(&installed_file_name
, "%s%s",
1269 conf
->offline_root
, file_name
);
1271 // already contains root_dir as header -> ABSOLUTE
1272 sprintf_alloc(&installed_file_name
, "%s",
1276 str_list_append(pkg
->installed_files
, installed_file_name
);
1277 free(installed_file_name
);
1283 if (list_from_package
) {
1284 unlink(list_file_name
);
1285 free(list_file_name
);
1288 return pkg
->installed_files
;
1291 /* XXX: CLEANUP: This function and it's counterpart,
1292 (pkg_get_installed_files), do not match our init/deinit naming
1293 convention. Nor the alloc/free convention. But, then again, neither
1294 of these conventions currrently fit the way these two functions
1296 void pkg_free_installed_files(pkg_t
* pkg
)
1298 pkg
->installed_files_ref_cnt
--;
1300 if (pkg
->installed_files_ref_cnt
> 0)
1303 if (pkg
->installed_files
) {
1304 str_list_purge(pkg
->installed_files
);
1307 pkg
->installed_files
= NULL
;
1310 void pkg_remove_installed_files_list(pkg_t
* pkg
)
1312 char *list_file_name
;
1314 sprintf_alloc(&list_file_name
, "%s/%s.list",
1315 pkg
->dest
->info_dir
, pkg
->name
);
1317 if (!conf
->noaction
)
1318 (void)unlink(list_file_name
);
1320 free(list_file_name
);
1323 conffile_t
*pkg_get_conffile(pkg_t
* pkg
, const char *file_name
)
1325 conffile_list_elt_t
*iter
;
1326 conffile_list_t
*cl
;
1327 conffile_t
*conffile
;
1333 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
1335 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
1336 iter
= nv_pair_list_next(cl
, iter
)) {
1337 conffile
= (conffile_t
*) iter
->data
;
1339 if (strcmp(conffile
->name
, file_name
) == 0) {
1347 int pkg_run_script(pkg_t
* pkg
, const char *script
, const char *args
)
1352 char *tmp_unpack_dir
;
1357 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1358 maintainer script within a chroot environment. */
1359 if (conf
->offline_root
&& !conf
->force_postinstall
) {
1360 opkg_msg(INFO
, "Offline root mode: not running %s.%s.\n",
1365 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1366 have scripts in tmp_unpack_dir. */
1367 if (pkg
->state_status
== SS_INSTALLED
1368 || pkg
->state_status
== SS_UNPACKED
1369 || pkg
->state_status
== SS_HALF_INSTALLED
) {
1370 if (pkg
->dest
== NULL
) {
1371 opkg_msg(ERROR
, "Internal error: %s has a NULL dest.\n",
1375 sprintf_alloc(&path
, "%s/%s.%s", pkg
->dest
->info_dir
, pkg
->name
,
1378 tmp_unpack_dir
= pkg_get_string(pkg
, PKG_TMP_UNPACK_DIR
);
1379 if (tmp_unpack_dir
== NULL
) {
1381 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1385 sprintf_alloc(&path
, "%s/%s", tmp_unpack_dir
, script
);
1388 opkg_msg(INFO
, "Running script %s.\n", path
);
1391 pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
,
1394 if (pkg
->is_upgrade
)
1395 setenv("PKG_UPGRADE", "1", 1);
1397 setenv("PKG_UPGRADE", "0", 1);
1399 if (!file_exists(path
)) {
1404 sprintf_alloc(&cmd
, "%s %s", path
, args
);
1407 const char *argv
[] = { "/bin/sh", "-c", cmd
, NULL
};
1408 err
= xsystem(argv
);
1414 "package \"%s\" %s script returned status %d.\n",
1415 pkg
->name
, script
, err
);
1422 int pkg_arch_supported(pkg_t
* pkg
)
1424 nv_pair_list_elt_t
*l
;
1425 char *architecture
= pkg_get_architecture(pkg
);
1430 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
1431 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
1432 if (strcmp(nv
->name
, architecture
) == 0) {
1434 "Arch %s (priority %s) supported for pkg %s.\n",
1435 nv
->name
, nv
->value
, pkg
->name
);
1440 opkg_msg(DEBUG
, "Arch %s unsupported for pkg %s.\n",
1441 architecture
, pkg
->name
);
1445 void pkg_info_preinstall_check(void)
1448 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1450 /* update the file owner data structure */
1451 opkg_msg(INFO
, "Updating file owner list.\n");
1452 pkg_hash_fetch_all_installed(installed_pkgs
);
1453 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1454 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1455 str_list_t
*installed_files
= pkg_get_installed_files(pkg
); /* this causes installed_files to be cached */
1456 str_list_elt_t
*iter
, *niter
;
1457 if (installed_files
== NULL
) {
1458 opkg_msg(ERROR
, "Failed to determine installed "
1459 "files for pkg %s.\n", pkg
->name
);
1462 for (iter
= str_list_first(installed_files
), niter
=
1463 str_list_next(installed_files
, iter
); iter
;
1464 iter
= niter
, niter
=
1465 str_list_next(installed_files
, iter
)) {
1466 char *installed_file
= (char *)iter
->data
;
1467 file_hash_set_file_owner(installed_file
, pkg
);
1469 pkg_free_installed_files(pkg
);
1471 pkg_vec_free(installed_pkgs
);
1474 struct pkg_write_filelist_data
{
1480 pkg_write_filelist_helper(const char *key
, void *entry_
, void *data_
)
1482 struct pkg_write_filelist_data
*data
= data_
;
1483 pkg_t
*entry
= entry_
;
1484 if (entry
== data
->pkg
) {
1485 fprintf(data
->stream
, "%s\n", key
);
1489 int pkg_write_filelist(pkg_t
* pkg
)
1491 struct pkg_write_filelist_data data
;
1492 char *list_file_name
;
1494 sprintf_alloc(&list_file_name
, "%s/%s.list",
1495 pkg
->dest
->info_dir
, pkg
->name
);
1497 opkg_msg(INFO
, "Creating %s file for pkg %s.\n",
1498 list_file_name
, pkg
->name
);
1500 data
.stream
= fopen(list_file_name
, "w");
1502 opkg_perror(ERROR
, "Failed to open %s", list_file_name
);
1503 free(list_file_name
);
1508 hash_table_foreach(&conf
->file_hash
, pkg_write_filelist_helper
, &data
);
1509 fclose(data
.stream
);
1510 free(list_file_name
);
1512 pkg
->state_flag
&= ~SF_FILELIST_CHANGED
;
1517 int pkg_write_changed_filelists(void)
1519 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc();
1520 int i
, err
, ret
= 0;
1525 opkg_msg(INFO
, "Saving changed filelists.\n");
1527 pkg_hash_fetch_all_installed(installed_pkgs
);
1528 for (i
= 0; i
< installed_pkgs
->len
; i
++) {
1529 pkg_t
*pkg
= installed_pkgs
->pkgs
[i
];
1530 if (pkg
->state_flag
& SF_FILELIST_CHANGED
) {
1531 err
= pkg_write_filelist(pkg
);
1537 pkg_vec_free(installed_pkgs
);