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.
28 #include "pkg_parse.h"
29 #include "pkg_extract.h"
30 #include "opkg_message.h"
31 #include "opkg_utils.h"
33 #include "libbb/libbb.h"
34 #include "sprintf_alloc.h"
35 #include "file_util.h"
37 #include "opkg_conf.h"
39 typedef struct enum_map enum_map_t;
45 static const enum_map_t pkg_state_want_map[] = {
46 {SW_UNKNOWN, "unknown"},
47 {SW_INSTALL, "install"},
48 {SW_DEINSTALL, "deinstall"},
52 static const enum_map_t pkg_state_flag_map[] = {
54 {SF_REINSTREQ, "reinstreq"},
56 {SF_REPLACE, "replace"},
57 {SF_NOPRUNE, "noprune"},
58 {SF_PREFER, "prefer"},
59 {SF_OBSOLETE, "obsolete"},
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"}
74 static void pkg_init(pkg_t * pkg)
79 pkg->state_want = SW_UNKNOWN;
80 pkg->state_flag = SF_OK;
81 pkg->state_status = SS_NOT_INSTALLED;
83 pkg->installed_files = NULL;
84 pkg->installed_files_ref_cnt = 0;
86 pkg->provided_by_hand = 0;
90 blob_buf_init(&pkg->blob, 0);
97 pkg = xcalloc(1, sizeof(pkg_t));
103 void *pkg_set_raw(pkg_t *pkg, int id, const void *val, size_t len)
106 struct blob_attr *cur;
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));
114 memcpy(blob_data(cur), val, blob_len(cur));
115 return blob_data(cur);
119 cur = blob_put(&pkg->blob, id, val, len);
120 return cur ? blob_data(cur) : NULL;
123 void *pkg_get_raw(const pkg_t * pkg, int id)
126 struct blob_attr *cur;
128 blob_for_each_attr(cur, pkg->blob.head, rem)
129 if (blob_id(cur) == id)
130 return blob_data(cur);
135 char *pkg_set_string(pkg_t *pkg, int id, const char *s)
145 while (isspace(*s)) {
150 while (len > 0 && isspace(s[len - 1]))
156 p = pkg_set_raw(pkg, id, s, len + 1);
162 char *pkg_get_architecture(const pkg_t *pkg)
164 nv_pair_list_elt_t *l;
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)
176 char *pkg_set_architecture(pkg_t *pkg, const char *architecture, ssize_t len)
178 nv_pair_list_elt_t *l;
181 list_for_each_entry(l, &conf->arch_list.head, node) {
182 nv_pair_t *nv = (nv_pair_t *) l->data;
184 if (!strncmp(nv->name, architecture, len) && nv->name[len] == '\0') {
186 opkg_msg(ERROR, "Internal error: too many different architectures\n");
201 int pkg_get_arch_priority(const pkg_t *pkg)
203 nv_pair_list_elt_t *l;
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);
215 char *pkg_get_md5(const pkg_t *pkg)
217 char *p = pkg_get_raw(pkg, PKG_MD5SUM);
222 return checksum_bin2hex(p, 16);
225 char *pkg_set_md5(pkg_t *pkg, const char *cksum)
228 char *p = checksum_hex2bin(cksum, &len);
233 return pkg_set_raw(pkg, PKG_MD5SUM, p, len);
236 char *pkg_get_sha256(const pkg_t *pkg)
238 char *p = pkg_get_raw(pkg, PKG_SHA256SUM);
243 return checksum_bin2hex(p, 32);
246 char *pkg_set_sha256(pkg_t *pkg, const char *cksum)
249 char *p = checksum_hex2bin(cksum, &len);
254 return pkg_set_raw(pkg, PKG_SHA256SUM, p, len);
258 static void compound_depend_deinit(compound_depend_t * depends)
261 for (i = 0; i < depends->possibility_count; i++) {
263 d = depends->possibilities[i];
267 free(depends->possibilities);
270 void pkg_deinit(pkg_t * pkg)
273 struct blob_attr *cur;
274 compound_depend_t *deps, *dep;
281 /* owned by opkg_conf_t */
283 /* owned by opkg_conf_t */
286 pkg->state_want = SW_UNKNOWN;
287 pkg->state_flag = SF_OK;
288 pkg->state_status = SS_NOT_INSTALLED;
290 blob_for_each_attr(cur, pkg->blob.head, rem) {
291 switch (blob_id(cur)) {
294 deps = pkg_get_ptr(pkg, blob_id(cur));
297 for (dep = deps; dep->type; dep++)
298 compound_depend_deinit(dep);
303 pkg_set_ptr(pkg, blob_id(cur), NULL);
308 ptr = pkg_get_ptr(pkg, blob_id(cur));
313 pkg_set_ptr(pkg, blob_id(cur), NULL);
317 ptr = pkg_get_ptr(pkg, blob_id(cur));
320 conffile_list_deinit(ptr);
324 pkg_set_ptr(pkg, blob_id(cur), NULL);
329 //conffile_list_deinit(&pkg->conffiles);
331 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
332 since if they are calling deinit, they should know. Maybe do an
333 assertion here instead? */
334 pkg->installed_files_ref_cnt = 1;
335 pkg_free_installed_files(pkg);
338 blob_buf_free(&pkg->blob);
341 int pkg_init_from_file(pkg_t * pkg, const char *filename)
345 char *control_path, *tmp;
349 if (!(pkg->state_flag & SF_NEED_DETAIL)) {
350 opkg_msg(DEBUG, "applying abpkg flag to %s\n", filename);
351 pkg->state_flag |= SF_NEED_DETAIL;
354 pkg_set_string(pkg, PKG_LOCAL_FILENAME, filename);
356 tmp = xstrdup(filename);
357 sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
358 conf->tmp_dir, basename(tmp));
360 fd = mkstemp(control_path);
362 opkg_perror(ERROR, "Failed to make temp file %s", control_path);
367 control_file = fdopen(fd, "r+");
368 if (control_file == NULL) {
369 opkg_perror(ERROR, "Failed to fdopen %s", control_path);
375 err = pkg_extract_control_file_to_stream(pkg, control_file);
377 opkg_msg(ERROR, "Failed to extract control file from %s.\n",
382 rewind(control_file);
384 if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
386 opkg_msg(ERROR, "Malformed package file %s.\n",
393 fclose(control_file);
395 unlink(control_path);
402 /* Merge any new information in newpkg into oldpkg */
403 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg)
407 if (oldpkg == newpkg) {
411 if (!oldpkg->auto_installed)
412 oldpkg->auto_installed = newpkg->auto_installed;
415 oldpkg->src = newpkg->src;
417 oldpkg->dest = newpkg->dest;
418 if (!oldpkg->arch_index)
419 oldpkg->arch_index = newpkg->arch_index;
420 if (!pkg_get_string(oldpkg, PKG_SECTION))
421 pkg_set_string(oldpkg, PKG_SECTION, pkg_get_string(newpkg, PKG_SECTION));
422 if (!pkg_get_string(oldpkg, PKG_MAINTAINER))
423 pkg_set_string(oldpkg, PKG_MAINTAINER, pkg_get_string(newpkg, PKG_MAINTAINER));
424 if (!pkg_get_string(oldpkg, PKG_DESCRIPTION))
425 pkg_set_string(oldpkg, PKG_DESCRIPTION, pkg_get_string(newpkg, PKG_DESCRIPTION));
427 if (!pkg_get_ptr(oldpkg, PKG_DEPENDS)) {
428 pkg_set_ptr(oldpkg, PKG_DEPENDS, pkg_get_ptr(newpkg, PKG_DEPENDS));
429 pkg_set_ptr(newpkg, PKG_DEPENDS, NULL);
432 ab = pkg_get_ptr(oldpkg, PKG_PROVIDES);
434 if (!ab || !ab[0] || !ab[1]) {
435 pkg_set_ptr(oldpkg, PKG_PROVIDES, pkg_get_ptr(newpkg, PKG_PROVIDES));
436 pkg_set_ptr(newpkg, PKG_PROVIDES, NULL);
442 if (!pkg_get_ptr(oldpkg, PKG_CONFLICTS)) {
443 pkg_set_ptr(oldpkg, PKG_CONFLICTS, pkg_get_ptr(newpkg, PKG_CONFLICTS));
444 pkg_set_ptr(newpkg, PKG_CONFLICTS, NULL);
447 if (!pkg_get_ptr(oldpkg, PKG_REPLACES)) {
448 pkg_set_ptr(oldpkg, PKG_REPLACES, pkg_get_ptr(newpkg, PKG_REPLACES));
449 pkg_set_ptr(newpkg, PKG_REPLACES, NULL);
452 if (!pkg_get_string(oldpkg, PKG_FILENAME))
453 pkg_set_string(oldpkg, PKG_FILENAME, pkg_get_string(newpkg, PKG_FILENAME));
454 if (!pkg_get_string(oldpkg, PKG_LOCAL_FILENAME))
455 pkg_set_string(oldpkg, PKG_LOCAL_FILENAME, pkg_get_string(newpkg, PKG_LOCAL_FILENAME));
456 if (!pkg_get_string(oldpkg, PKG_TMP_UNPACK_DIR))
457 pkg_set_string(oldpkg, PKG_TMP_UNPACK_DIR, pkg_get_string(newpkg, PKG_TMP_UNPACK_DIR));
458 if (!pkg_get_md5(oldpkg))
459 pkg_set_md5(oldpkg, pkg_get_md5(newpkg));
460 if (!pkg_get_sha256(oldpkg))
461 pkg_set_sha256(oldpkg, pkg_get_sha256(newpkg));
462 if (!pkg_get_int(oldpkg, PKG_SIZE))
463 pkg_set_int(oldpkg, PKG_SIZE, pkg_get_int(newpkg, PKG_SIZE));
464 if (!pkg_get_int(oldpkg, PKG_INSTALLED_SIZE))
465 pkg_set_int(oldpkg, PKG_INSTALLED_SIZE, pkg_get_int(newpkg, PKG_INSTALLED_SIZE));
466 if (!pkg_get_string(oldpkg, PKG_PRIORITY))
467 pkg_set_string(oldpkg, PKG_PRIORITY, pkg_get_string(newpkg, PKG_PRIORITY));
468 if (!pkg_get_string(oldpkg, PKG_SOURCE))
469 pkg_set_string(oldpkg, PKG_SOURCE, pkg_get_string(newpkg, PKG_SOURCE));
471 if (!pkg_get_ptr(oldpkg, PKG_CONFFILES)) {
472 pkg_set_ptr(oldpkg, PKG_CONFFILES, pkg_get_ptr(newpkg, PKG_CONFFILES));
473 pkg_set_ptr(newpkg, PKG_CONFFILES, NULL);
476 if (!oldpkg->installed_files) {
477 oldpkg->installed_files = newpkg->installed_files;
478 oldpkg->installed_files_ref_cnt =
479 newpkg->installed_files_ref_cnt;
480 newpkg->installed_files = NULL;
483 if (!oldpkg->essential)
484 oldpkg->essential = newpkg->essential;
489 static void abstract_pkg_init(abstract_pkg_t * ab_pkg)
491 ab_pkg->provided_by = abstract_pkg_vec_alloc();
492 ab_pkg->dependencies_checked = 0;
493 ab_pkg->state_status = SS_NOT_INSTALLED;
496 abstract_pkg_t *abstract_pkg_new(void)
498 abstract_pkg_t *ab_pkg;
500 ab_pkg = xcalloc(1, sizeof(abstract_pkg_t));
501 abstract_pkg_init(ab_pkg);
506 void set_flags_from_control(pkg_t * pkg)
511 sprintf_alloc(&file_name, "%s/%s.control", pkg->dest->info_dir,
514 fp = fopen(file_name, "r");
516 opkg_perror(ERROR, "Failed to open %s", file_name);
523 if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) {
525 "Unable to read control file for %s. May be empty.\n",
534 static const char *pkg_state_want_to_str(pkg_state_want_t sw)
538 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
539 if (pkg_state_want_map[i].value == sw) {
540 return pkg_state_want_map[i].str;
544 opkg_msg(ERROR, "Internal error: state_want=%d\n", sw);
545 return "<STATE_WANT_UNKNOWN>";
548 pkg_state_want_t pkg_state_want_from_str(char *str)
552 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
553 if (strcmp(str, pkg_state_want_map[i].str) == 0) {
554 return pkg_state_want_map[i].value;
558 opkg_msg(ERROR, "Internal error: state_want=%s\n", str);
562 static char *pkg_state_flag_to_str(pkg_state_flag_t sf)
568 /* clear the temporary flags before converting to string */
569 sf &= SF_NONVOLATILE_FLAGS;
572 return xstrdup("ok");
575 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
576 if (sf & pkg_state_flag_map[i].value)
577 len += strlen(pkg_state_flag_map[i].str) + 1;
580 str = xmalloc(len + 1);
583 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
584 if (sf & pkg_state_flag_map[i].value) {
585 strncat(str, pkg_state_flag_map[i].str, len);
586 strncat(str, ",", len);
591 str[len - 1] = '\0'; /* squash last comma */
596 pkg_state_flag_t pkg_state_flag_from_str(const char *str)
601 unsigned int sfname_len;
603 if (strcmp(str, "ok") == 0) {
606 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
607 sfname = pkg_state_flag_map[i].str;
608 sfname_len = strlen(sfname);
609 if (strncmp(str, sfname, sfname_len) == 0) {
610 sf |= pkg_state_flag_map[i].value;
623 static const char *pkg_state_status_to_str(pkg_state_status_t ss)
627 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
628 if (pkg_state_status_map[i].value == ss) {
629 return pkg_state_status_map[i].str;
633 opkg_msg(ERROR, "Internal error: state_status=%d\n", ss);
634 return "<STATE_STATUS_UNKNOWN>";
637 pkg_state_status_t pkg_state_status_from_str(const char *str)
641 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
642 if (strcmp(str, pkg_state_status_map[i].str) == 0) {
643 return pkg_state_status_map[i].value;
647 opkg_msg(ERROR, "Internal error: state_status=%s\n", str);
648 return SS_NOT_INSTALLED;
651 void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field)
656 compound_depend_t *dep;
657 abstract_pkg_t **ab_pkg;
659 if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
660 goto UNKNOWN_FMT_FIELD;
666 if (strcasecmp(field, "Architecture") == 0) {
667 p = pkg_get_architecture(pkg);
669 fprintf(fp, "Architecture: %s\n",
672 } else if (strcasecmp(field, "Auto-Installed") == 0) {
673 if (pkg->auto_installed)
674 fprintf(fp, "Auto-Installed: yes\n");
676 goto UNKNOWN_FMT_FIELD;
681 if (strcasecmp(field, "Conffiles") == 0) {
683 conffile_list_elt_t *iter;
685 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
687 if (!cl || nv_pair_list_empty(cl))
690 fprintf(fp, "Conffiles:\n");
691 for (iter = nv_pair_list_first(cl); iter;
692 iter = nv_pair_list_next(cl, iter)) {
693 if (((conffile_t *) iter->data)->name
694 && ((conffile_t *) iter->data)->value) {
695 fprintf(fp, " %s %s\n",
696 ((conffile_t *) iter->data)->
698 ((conffile_t *) iter->data)->
702 } else if (strcasecmp(field, "Conflicts") == 0) {
704 compound_depend_t *deps, *dep;
705 deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
707 fprintf(fp, "Conflicts:");
708 for (i = 0, dep = deps; dep->type; dep++, i++) {
709 cdep = dep->possibilities[0];
710 fprintf(fp, "%s %s", i == 0 ? "" : ",",
713 fprintf(fp, " (%s%s)",
714 constraint_to_str(cdep->
722 goto UNKNOWN_FMT_FIELD;
727 if (strcasecmp(field, "Depends") == 0) {
728 dep = pkg_get_depends(pkg, DEPEND);
730 fprintf(fp, "Depends:");
731 for (i = 0, j = 0; dep && dep->type; i++, dep++) {
732 if (dep->type != DEPEND)
734 str = pkg_depend_str(pkg, i);
735 fprintf(fp, "%s %s", j == 0 ? "" : ",",
742 } else if (strcasecmp(field, "Description") == 0) {
743 p = pkg_get_string(pkg, PKG_DESCRIPTION);
745 fprintf(fp, "Description: %s\n",
749 goto UNKNOWN_FMT_FIELD;
754 if (pkg->essential) {
755 fprintf(fp, "Essential: yes\n");
760 p = pkg_get_string(pkg, PKG_FILENAME);
762 fprintf(fp, "Filename: %s\n", p);
767 if (strcasecmp(field, "Installed-Size") == 0) {
768 fprintf(fp, "Installed-Size: %lu\n",
769 (unsigned long) pkg_get_int(pkg, PKG_INSTALLED_SIZE));
770 } else if (strcasecmp(field, "Installed-Time") == 0) {
771 i = pkg_get_int(pkg, PKG_INSTALLED_TIME);
773 fprintf(fp, "Installed-Time: %lu\n",
780 if (strcasecmp(field, "Maintainer") == 0) {
781 p = pkg_get_string(pkg, PKG_MAINTAINER);
783 fprintf(fp, "Maintainer: %s\n", p);
785 } else if (strcasecmp(field, "MD5sum") == 0) {
786 p = pkg_get_md5(pkg);
788 fprintf(fp, "MD5Sum: %s\n", p);
791 goto UNKNOWN_FMT_FIELD;
796 if (strcasecmp(field, "Package") == 0) {
797 fprintf(fp, "Package: %s\n", pkg->name);
798 } else if (strcasecmp(field, "Priority") == 0) {
799 fprintf(fp, "Priority: %s\n", pkg_get_string(pkg, PKG_PRIORITY));
800 } else if (strcasecmp(field, "Provides") == 0) {
801 ab_pkg = pkg_get_ptr(pkg, PKG_PROVIDES);
802 if (ab_pkg && ab_pkg[0] && ab_pkg[1]) {
803 fprintf(fp, "Provides:");
804 for (i = 1; ab_pkg[i]; i++) {
805 fprintf(fp, "%s %s", i == 1 ? "" : ",",
811 goto UNKNOWN_FMT_FIELD;
816 if (strcasecmp(field, "Replaces") == 0) {
817 ab_pkg = pkg_get_ptr(pkg, PKG_REPLACES);
818 if (ab_pkg && *ab_pkg) {
819 fprintf(fp, "Replaces:");
820 for (i = 0; *ab_pkg; i++, ab_pkg++) {
821 fprintf(fp, "%s %s", i == 0 ? "" : ",",
826 } else if (strcasecmp(field, "Recommends") == 0) {
827 dep = pkg_get_depends(pkg, RECOMMEND);
829 fprintf(fp, "Recommends:");
830 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
831 if (dep->type != RECOMMEND)
833 str = pkg_depend_str(pkg, i);
834 fprintf(fp, "%s %s", j == 0 ? "" : ",",
842 goto UNKNOWN_FMT_FIELD;
847 if (strcasecmp(field, "Section") == 0) {
848 p = pkg_get_string(pkg, PKG_SECTION);
850 fprintf(fp, "Section: %s\n", p);
852 #if defined HAVE_SHA256
853 } else if (strcasecmp(field, "SHA256sum") == 0) {
854 p = pkg_get_string(pkg, PKG_SHA256SUM);
856 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");
974 * libdpkg - Debian packaging suite library routines
975 * vercmp.c - comparison of version numbers
977 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
980 /* assume ascii; warning: evaluates x multiple times! */
981 #define order(x) ((x) == '~' ? -1 \
984 : isalpha((x)) ? (x) \
987 static int verrevcmp(const char *val, const char *ref)
994 while (*val || *ref) {
997 while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) {
998 int vc = order(*val), rc = order(*ref);
1009 while (isdigit(*val) && isdigit(*ref)) {
1011 first_diff = *val - *ref;
1025 int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg)
1027 unsigned int epoch1 = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1028 unsigned int epoch2 = (unsigned int) pkg_get_int(ref_pkg, PKG_EPOCH);
1029 char *revision1 = pkg_get_string(pkg, PKG_REVISION);
1030 char *revision2 = pkg_get_string(ref_pkg, PKG_REVISION);
1031 const char *version1 = pkg_get_string(pkg, PKG_VERSION);
1032 const char *version2 = pkg_get_string(ref_pkg, PKG_VERSION);
1035 if (epoch1 > epoch2) {
1039 if (epoch1 < epoch2) {
1043 r = verrevcmp(version1, version2);
1048 r = verrevcmp(revision1, revision2);
1056 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op)
1060 r = pkg_compare_versions(it, ref);
1062 if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
1066 if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
1070 if (strcmp(op, "<<") == 0) {
1074 if (strcmp(op, ">>") == 0) {
1078 if (strcmp(op, "=") == 0) {
1082 opkg_msg(ERROR, "Unknown operator: %s.\n", op);
1086 int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
1088 const pkg_t * a = *(const pkg_t **)p1;
1089 const pkg_t * b = *(const pkg_t **)p2;
1092 int arch_prio1, arch_prio2;
1093 if (!a->name || !b->name) {
1094 opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n",
1099 namecmp = strcmp(a->name, b->name);
1102 vercmp = pkg_compare_versions(a, b);
1105 arch_prio1 = pkg_get_arch_priority(a);
1106 arch_prio2 = pkg_get_arch_priority(b);
1107 if (!arch_prio1 || !arch_prio2) {
1109 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1110 arch_prio1, arch_prio2);
1113 if (arch_prio1 > arch_prio2)
1115 if (arch_prio1 < arch_prio2)
1120 int abstract_pkg_name_compare(const void *p1, const void *p2)
1122 const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
1123 const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
1124 if (!a->name || !b->name) {
1125 opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n",
1129 return strcmp(a->name, b->name);
1132 char *pkg_version_str_alloc(pkg_t * pkg)
1135 char *version, *revptr;
1136 unsigned int epoch = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1138 revptr = pkg_get_string(pkg, PKG_REVISION);
1139 verstr = pkg_get_string(pkg, PKG_VERSION);
1143 sprintf_alloc(&version, "%d:%s-%s",
1144 epoch, verstr, revptr);
1146 sprintf_alloc(&version, "%d:%s",
1150 sprintf_alloc(&version, "%s-%s",
1153 version = xstrdup(verstr);
1160 * XXX: this should be broken into two functions
1162 str_list_t *pkg_get_installed_files(pkg_t * pkg)
1165 char *list_file_name = NULL;
1166 FILE *list_file = NULL;
1168 char *installed_file_name;
1169 unsigned int rootdirlen = 0;
1170 int list_from_package;
1171 const char *local_filename;
1173 pkg->installed_files_ref_cnt++;
1175 if (pkg->installed_files) {
1176 return pkg->installed_files;
1179 pkg->installed_files = str_list_alloc();
1182 * For installed packages, look at the package.list file in the database.
1183 * For uninstalled packages, get the file list directly from the package.
1185 if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
1186 list_from_package = 1;
1188 list_from_package = 0;
1190 if (list_from_package) {
1191 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1193 if (!local_filename) {
1194 return pkg->installed_files;
1196 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1197 file. In other words, change deb_extract so that it can
1198 simply return the file list as a char *[] rather than
1199 insisting on writing it to a FILE * as it does now. */
1200 sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
1201 conf->tmp_dir, pkg->name);
1202 fd = mkstemp(list_file_name);
1204 opkg_perror(ERROR, "Failed to make temp file %s.",
1206 free(list_file_name);
1207 return pkg->installed_files;
1209 list_file = fdopen(fd, "r+");
1210 if (list_file == NULL) {
1211 opkg_perror(ERROR, "Failed to fdopen temp file %s.",
1214 unlink(list_file_name);
1215 free(list_file_name);
1216 return pkg->installed_files;
1218 err = pkg_extract_data_file_names_to_stream(pkg, list_file);
1220 opkg_msg(ERROR, "Error extracting file list from %s.\n",
1223 unlink(list_file_name);
1224 free(list_file_name);
1225 str_list_deinit(pkg->installed_files);
1226 pkg->installed_files = NULL;
1231 sprintf_alloc(&list_file_name, "%s/%s.list",
1232 pkg->dest->info_dir, pkg->name);
1233 list_file = fopen(list_file_name, "r");
1234 if (list_file == NULL) {
1235 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1236 free(list_file_name);
1237 return pkg->installed_files;
1239 free(list_file_name);
1242 if (conf->offline_root)
1243 rootdirlen = strlen(conf->offline_root);
1248 line = file_read_line_alloc(list_file);
1254 if (list_from_package) {
1255 if (*file_name == '.') {
1258 if (*file_name == '/') {
1261 sprintf_alloc(&installed_file_name, "%s%s",
1262 pkg->dest->root_dir, file_name);
1264 if (conf->offline_root &&
1265 strncmp(conf->offline_root, file_name,
1267 sprintf_alloc(&installed_file_name, "%s%s",
1268 conf->offline_root, file_name);
1270 // already contains root_dir as header -> ABSOLUTE
1271 sprintf_alloc(&installed_file_name, "%s",
1275 str_list_append(pkg->installed_files, installed_file_name);
1276 free(installed_file_name);
1282 if (list_from_package) {
1283 unlink(list_file_name);
1284 free(list_file_name);
1287 return pkg->installed_files;
1290 /* XXX: CLEANUP: This function and it's counterpart,
1291 (pkg_get_installed_files), do not match our init/deinit naming
1292 convention. Nor the alloc/free convention. But, then again, neither
1293 of these conventions currrently fit the way these two functions
1295 void pkg_free_installed_files(pkg_t * pkg)
1297 pkg->installed_files_ref_cnt--;
1299 if (pkg->installed_files_ref_cnt > 0)
1302 if (pkg->installed_files) {
1303 str_list_purge(pkg->installed_files);
1306 pkg->installed_files = NULL;
1309 void pkg_remove_installed_files_list(pkg_t * pkg)
1311 char *list_file_name;
1313 sprintf_alloc(&list_file_name, "%s/%s.list",
1314 pkg->dest->info_dir, pkg->name);
1316 if (!conf->noaction)
1317 (void)unlink(list_file_name);
1319 free(list_file_name);
1322 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name)
1324 conffile_list_elt_t *iter;
1325 conffile_list_t *cl;
1326 conffile_t *conffile;
1332 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
1334 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
1335 iter = nv_pair_list_next(cl, iter)) {
1336 conffile = (conffile_t *) iter->data;
1338 if (strcmp(conffile->name, file_name) == 0) {
1346 int pkg_run_script(pkg_t * pkg, const char *script, const char *args)
1351 char *tmp_unpack_dir;
1356 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1357 maintainer script within a chroot environment. */
1358 if (conf->offline_root && !conf->force_postinstall) {
1359 opkg_msg(INFO, "Offline root mode: not running %s.%s.\n",
1364 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1365 have scripts in tmp_unpack_dir. */
1366 if (pkg->state_status == SS_INSTALLED
1367 || pkg->state_status == SS_UNPACKED) {
1368 if (pkg->dest == NULL) {
1369 opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n",
1373 sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name,
1376 tmp_unpack_dir = pkg_get_string(pkg, PKG_TMP_UNPACK_DIR);
1377 if (tmp_unpack_dir == NULL) {
1379 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1383 sprintf_alloc(&path, "%s/%s", tmp_unpack_dir, script);
1386 opkg_msg(INFO, "Running script %s.\n", path);
1389 pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir,
1392 if (pkg->is_upgrade)
1393 setenv("PKG_UPGRADE", "1", 1);
1395 setenv("PKG_UPGRADE", "0", 1);
1397 if (!file_exists(path)) {
1402 sprintf_alloc(&cmd, "%s %s", path, args);
1405 const char *argv[] = { "sh", "-c", cmd, NULL };
1406 err = xsystem(argv);
1412 "package \"%s\" %s script returned status %d.\n",
1413 pkg->name, script, err);
1420 int pkg_arch_supported(pkg_t * pkg)
1422 nv_pair_list_elt_t *l;
1423 char *architecture = pkg_get_architecture(pkg);
1428 list_for_each_entry(l, &conf->arch_list.head, node) {
1429 nv_pair_t *nv = (nv_pair_t *) l->data;
1430 if (strcmp(nv->name, architecture) == 0) {
1432 "Arch %s (priority %s) supported for pkg %s.\n",
1433 nv->name, nv->value, pkg->name);
1438 opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n",
1439 architecture, pkg->name);
1443 void pkg_info_preinstall_check(void)
1446 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1448 /* update the file owner data structure */
1449 opkg_msg(INFO, "Updating file owner list.\n");
1450 pkg_hash_fetch_all_installed(installed_pkgs);
1451 for (i = 0; i < installed_pkgs->len; i++) {
1452 pkg_t *pkg = installed_pkgs->pkgs[i];
1453 str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
1454 str_list_elt_t *iter, *niter;
1455 if (installed_files == NULL) {
1456 opkg_msg(ERROR, "Failed to determine installed "
1457 "files for pkg %s.\n", pkg->name);
1460 for (iter = str_list_first(installed_files), niter =
1461 str_list_next(installed_files, iter); iter;
1462 iter = niter, niter =
1463 str_list_next(installed_files, iter)) {
1464 char *installed_file = (char *)iter->data;
1465 file_hash_set_file_owner(installed_file, pkg);
1467 pkg_free_installed_files(pkg);
1469 pkg_vec_free(installed_pkgs);
1472 struct pkg_write_filelist_data {
1478 pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
1480 struct pkg_write_filelist_data *data = data_;
1481 pkg_t *entry = entry_;
1482 if (entry == data->pkg) {
1483 fprintf(data->stream, "%s\n", key);
1487 int pkg_write_filelist(pkg_t * pkg)
1489 struct pkg_write_filelist_data data;
1490 char *list_file_name;
1492 sprintf_alloc(&list_file_name, "%s/%s.list",
1493 pkg->dest->info_dir, pkg->name);
1495 opkg_msg(INFO, "Creating %s file for pkg %s.\n",
1496 list_file_name, pkg->name);
1498 data.stream = fopen(list_file_name, "w");
1500 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1501 free(list_file_name);
1506 hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
1507 fclose(data.stream);
1508 free(list_file_name);
1510 pkg->state_flag &= ~SF_FILELIST_CHANGED;
1515 int pkg_write_changed_filelists(void)
1517 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1518 int i, err, ret = 0;
1523 opkg_msg(INFO, "Saving changed filelists.\n");
1525 pkg_hash_fetch_all_installed(installed_pkgs);
1526 for (i = 0; i < installed_pkgs->len; i++) {
1527 pkg_t *pkg = installed_pkgs->pkgs[i];
1528 if (pkg->state_flag & SF_FILELIST_CHANGED) {
1529 err = pkg_write_filelist(pkg);
1535 pkg_vec_free(installed_pkgs);