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);
327 //conffile_list_deinit(&pkg->conffiles);
329 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
330 since if they are calling deinit, they should know. Maybe do an
331 assertion here instead? */
332 pkg->installed_files_ref_cnt = 1;
333 pkg_free_installed_files(pkg);
336 blob_buf_free(&pkg->blob);
339 int pkg_init_from_file(pkg_t * pkg, const char *filename)
343 char *control_path, *tmp;
347 if (!(pkg->state_flag & SF_NEED_DETAIL)) {
348 opkg_msg(DEBUG, "applying abpkg flag to %s\n", filename);
349 pkg->state_flag |= SF_NEED_DETAIL;
352 pkg_set_string(pkg, PKG_LOCAL_FILENAME, filename);
354 tmp = xstrdup(filename);
355 sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
356 conf->tmp_dir, basename(tmp));
358 fd = mkstemp(control_path);
360 opkg_perror(ERROR, "Failed to make temp file %s", control_path);
365 control_file = fdopen(fd, "r+");
366 if (control_file == NULL) {
367 opkg_perror(ERROR, "Failed to fdopen %s", control_path);
373 err = pkg_extract_control_file_to_stream(pkg, control_file);
375 opkg_msg(ERROR, "Failed to extract control file from %s.\n",
380 rewind(control_file);
382 if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
384 opkg_msg(ERROR, "Malformed package file %s.\n",
391 fclose(control_file);
393 unlink(control_path);
400 /* Merge any new information in newpkg into oldpkg */
401 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg)
405 if (oldpkg == newpkg) {
409 if (!oldpkg->auto_installed)
410 oldpkg->auto_installed = newpkg->auto_installed;
413 oldpkg->src = newpkg->src;
415 oldpkg->dest = newpkg->dest;
416 if (!oldpkg->arch_index)
417 oldpkg->arch_index = newpkg->arch_index;
418 if (!pkg_get_string(oldpkg, PKG_SECTION))
419 pkg_set_string(oldpkg, PKG_SECTION, pkg_get_string(newpkg, PKG_SECTION));
420 if (!pkg_get_string(oldpkg, PKG_MAINTAINER))
421 pkg_set_string(oldpkg, PKG_MAINTAINER, pkg_get_string(newpkg, PKG_MAINTAINER));
422 if (!pkg_get_string(oldpkg, PKG_DESCRIPTION))
423 pkg_set_string(oldpkg, PKG_DESCRIPTION, pkg_get_string(newpkg, PKG_DESCRIPTION));
425 if (!pkg_get_ptr(oldpkg, PKG_DEPENDS)) {
426 pkg_set_ptr(oldpkg, PKG_DEPENDS, pkg_get_ptr(newpkg, PKG_DEPENDS));
427 pkg_set_ptr(newpkg, PKG_DEPENDS, NULL);
430 ab = pkg_get_ptr(oldpkg, PKG_PROVIDES);
432 if (!ab || !ab[0] || !ab[1]) {
433 pkg_set_ptr(oldpkg, PKG_PROVIDES, pkg_get_ptr(newpkg, PKG_PROVIDES));
434 pkg_set_ptr(newpkg, PKG_PROVIDES, NULL);
440 if (!pkg_get_ptr(oldpkg, PKG_CONFLICTS)) {
441 pkg_set_ptr(oldpkg, PKG_CONFLICTS, pkg_get_ptr(newpkg, PKG_CONFLICTS));
442 pkg_set_ptr(newpkg, PKG_CONFLICTS, NULL);
445 if (!pkg_get_ptr(oldpkg, PKG_REPLACES)) {
446 pkg_set_ptr(oldpkg, PKG_REPLACES, pkg_get_ptr(newpkg, PKG_REPLACES));
447 pkg_set_ptr(newpkg, PKG_REPLACES, NULL);
450 if (!pkg_get_string(oldpkg, PKG_FILENAME))
451 pkg_set_string(oldpkg, PKG_FILENAME, pkg_get_string(newpkg, PKG_FILENAME));
452 if (!pkg_get_string(oldpkg, PKG_LOCAL_FILENAME))
453 pkg_set_string(oldpkg, PKG_LOCAL_FILENAME, pkg_get_string(newpkg, PKG_LOCAL_FILENAME));
454 if (!pkg_get_string(oldpkg, PKG_TMP_UNPACK_DIR))
455 pkg_set_string(oldpkg, PKG_TMP_UNPACK_DIR, pkg_get_string(newpkg, PKG_TMP_UNPACK_DIR));
456 if (!pkg_get_md5(oldpkg))
457 pkg_set_md5(oldpkg, pkg_get_md5(newpkg));
458 if (!pkg_get_sha256(oldpkg))
459 pkg_set_sha256(oldpkg, pkg_get_sha256(newpkg));
460 if (!pkg_get_int(oldpkg, PKG_SIZE))
461 pkg_set_int(oldpkg, PKG_SIZE, pkg_get_int(newpkg, PKG_SIZE));
462 if (!pkg_get_int(oldpkg, PKG_INSTALLED_SIZE))
463 pkg_set_int(oldpkg, PKG_INSTALLED_SIZE, pkg_get_int(newpkg, PKG_INSTALLED_SIZE));
464 if (!pkg_get_string(oldpkg, PKG_PRIORITY))
465 pkg_set_string(oldpkg, PKG_PRIORITY, pkg_get_string(newpkg, PKG_PRIORITY));
466 if (!pkg_get_string(oldpkg, PKG_SOURCE))
467 pkg_set_string(oldpkg, PKG_SOURCE, pkg_get_string(newpkg, PKG_SOURCE));
469 if (!pkg_get_ptr(oldpkg, PKG_CONFFILES)) {
470 pkg_set_ptr(oldpkg, PKG_CONFFILES, pkg_get_ptr(newpkg, PKG_CONFFILES));
471 pkg_set_ptr(newpkg, PKG_CONFFILES, NULL);
474 if (!oldpkg->installed_files) {
475 oldpkg->installed_files = newpkg->installed_files;
476 oldpkg->installed_files_ref_cnt =
477 newpkg->installed_files_ref_cnt;
478 newpkg->installed_files = NULL;
481 if (!oldpkg->essential)
482 oldpkg->essential = newpkg->essential;
484 if (!oldpkg->provided_by_hand)
485 oldpkg->provided_by_hand = newpkg->provided_by_hand;
490 static void abstract_pkg_init(abstract_pkg_t * ab_pkg)
492 ab_pkg->provided_by = abstract_pkg_vec_alloc();
493 ab_pkg->dependencies_checked = 0;
494 ab_pkg->state_status = SS_NOT_INSTALLED;
497 abstract_pkg_t *abstract_pkg_new(void)
499 abstract_pkg_t *ab_pkg;
501 ab_pkg = xcalloc(1, sizeof(abstract_pkg_t));
502 abstract_pkg_init(ab_pkg);
507 static const char *pkg_state_want_to_str(pkg_state_want_t sw)
511 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
512 if (pkg_state_want_map[i].value == sw) {
513 return pkg_state_want_map[i].str;
517 opkg_msg(ERROR, "Internal error: state_want=%d\n", sw);
518 return "<STATE_WANT_UNKNOWN>";
521 pkg_state_want_t pkg_state_want_from_str(char *str)
525 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
526 if (strcmp(str, pkg_state_want_map[i].str) == 0) {
527 return pkg_state_want_map[i].value;
531 opkg_msg(ERROR, "Internal error: state_want=%s\n", str);
535 static char *pkg_state_flag_to_str(pkg_state_flag_t sf)
541 /* clear the temporary flags before converting to string */
542 sf &= SF_NONVOLATILE_FLAGS;
545 return xstrdup("ok");
548 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
549 if (sf & pkg_state_flag_map[i].value)
550 len += strlen(pkg_state_flag_map[i].str) + 1;
553 str = xmalloc(len + 1);
556 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
557 if (sf & pkg_state_flag_map[i].value) {
558 strncat(str, pkg_state_flag_map[i].str, len);
559 strncat(str, ",", len);
564 str[len - 1] = '\0'; /* squash last comma */
569 pkg_state_flag_t pkg_state_flag_from_str(const char *str)
574 unsigned int sfname_len;
576 if (strcmp(str, "ok") == 0) {
579 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
580 sfname = pkg_state_flag_map[i].str;
581 sfname_len = strlen(sfname);
582 if (strncmp(str, sfname, sfname_len) == 0) {
583 sf |= pkg_state_flag_map[i].value;
596 static const char *pkg_state_status_to_str(pkg_state_status_t ss)
600 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
601 if (pkg_state_status_map[i].value == ss) {
602 return pkg_state_status_map[i].str;
606 opkg_msg(ERROR, "Internal error: state_status=%d\n", ss);
607 return "<STATE_STATUS_UNKNOWN>";
610 pkg_state_status_t pkg_state_status_from_str(const char *str)
614 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
615 if (strcmp(str, pkg_state_status_map[i].str) == 0) {
616 return pkg_state_status_map[i].value;
620 opkg_msg(ERROR, "Internal error: state_status=%s\n", str);
621 return SS_NOT_INSTALLED;
624 void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field)
629 compound_depend_t *dep;
630 abstract_pkg_t **ab_pkg;
632 if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
633 goto UNKNOWN_FMT_FIELD;
639 if (strcasecmp(field, "Architecture") == 0) {
640 p = pkg_get_architecture(pkg);
642 fprintf(fp, "Architecture: %s\n",
645 } else if (strcasecmp(field, "Auto-Installed") == 0) {
646 if (pkg->auto_installed)
647 fprintf(fp, "Auto-Installed: yes\n");
649 goto UNKNOWN_FMT_FIELD;
654 if (strcasecmp(field, "Conffiles") == 0) {
656 conffile_list_elt_t *iter;
658 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
660 if (!cl || nv_pair_list_empty(cl))
663 fprintf(fp, "Conffiles:\n");
664 for (iter = nv_pair_list_first(cl); iter;
665 iter = nv_pair_list_next(cl, iter)) {
666 if (((conffile_t *) iter->data)->name
667 && ((conffile_t *) iter->data)->value) {
668 fprintf(fp, " %s %s\n",
669 ((conffile_t *) iter->data)->
671 ((conffile_t *) iter->data)->
675 } else if (strcasecmp(field, "Conflicts") == 0) {
677 compound_depend_t *deps, *dep;
678 deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
680 fprintf(fp, "Conflicts:");
681 for (i = 0, dep = deps; dep->type; dep++, i++) {
682 cdep = dep->possibilities[0];
683 fprintf(fp, "%s %s", i == 0 ? "" : ",",
686 fprintf(fp, " (%s%s)",
687 constraint_to_str(cdep->
695 goto UNKNOWN_FMT_FIELD;
700 if (strcasecmp(field, "Depends") == 0) {
701 dep = pkg_get_depends(pkg, DEPEND);
703 fprintf(fp, "Depends:");
704 for (i = 0, j = 0; dep && dep->type; i++, dep++) {
705 if (dep->type != DEPEND)
707 str = pkg_depend_str(pkg, i);
708 fprintf(fp, "%s %s", j == 0 ? "" : ",",
715 } else if (strcasecmp(field, "Description") == 0) {
716 p = pkg_get_string(pkg, PKG_DESCRIPTION);
718 fprintf(fp, "Description: %s\n",
722 goto UNKNOWN_FMT_FIELD;
727 if (pkg->essential) {
728 fprintf(fp, "Essential: yes\n");
733 p = pkg_get_string(pkg, PKG_FILENAME);
735 fprintf(fp, "Filename: %s\n", p);
740 if (strcasecmp(field, "Installed-Size") == 0) {
741 fprintf(fp, "Installed-Size: %lu\n",
742 (unsigned long) pkg_get_int(pkg, PKG_INSTALLED_SIZE));
743 } else if (strcasecmp(field, "Installed-Time") == 0) {
744 i = pkg_get_int(pkg, PKG_INSTALLED_TIME);
746 fprintf(fp, "Installed-Time: %lu\n",
753 if (strcasecmp(field, "Maintainer") == 0) {
754 p = pkg_get_string(pkg, PKG_MAINTAINER);
756 fprintf(fp, "Maintainer: %s\n", p);
758 } else if (strcasecmp(field, "MD5sum") == 0) {
759 p = pkg_get_md5(pkg);
761 fprintf(fp, "MD5Sum: %s\n", p);
764 goto UNKNOWN_FMT_FIELD;
769 if (strcasecmp(field, "Package") == 0) {
770 fprintf(fp, "Package: %s\n", pkg->name);
771 } else if (strcasecmp(field, "Priority") == 0) {
772 fprintf(fp, "Priority: %s\n", pkg_get_string(pkg, PKG_PRIORITY));
773 } else if (strcasecmp(field, "Provides") == 0) {
774 ab_pkg = pkg_get_ptr(pkg, PKG_PROVIDES);
775 if (ab_pkg && ab_pkg[0] && ab_pkg[1]) {
776 fprintf(fp, "Provides:");
777 for (i = 1; ab_pkg[i]; i++) {
778 fprintf(fp, "%s %s", i == 1 ? "" : ",",
784 goto UNKNOWN_FMT_FIELD;
789 if (strcasecmp(field, "Replaces") == 0) {
790 ab_pkg = pkg_get_ptr(pkg, PKG_REPLACES);
791 if (ab_pkg && *ab_pkg) {
792 fprintf(fp, "Replaces:");
793 for (i = 0; *ab_pkg; i++, ab_pkg++) {
794 fprintf(fp, "%s %s", i == 0 ? "" : ",",
799 } else if (strcasecmp(field, "Recommends") == 0) {
800 dep = pkg_get_depends(pkg, RECOMMEND);
802 fprintf(fp, "Recommends:");
803 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
804 if (dep->type != RECOMMEND)
806 str = pkg_depend_str(pkg, i);
807 fprintf(fp, "%s %s", j == 0 ? "" : ",",
815 goto UNKNOWN_FMT_FIELD;
820 if (strcasecmp(field, "Section") == 0) {
821 p = pkg_get_string(pkg, PKG_SECTION);
823 fprintf(fp, "Section: %s\n", p);
825 } else if (strcasecmp(field, "SHA256sum") == 0) {
826 p = pkg_get_string(pkg, PKG_SHA256SUM);
828 fprintf(fp, "SHA256sum: %s\n", p);
830 } else if (strcasecmp(field, "Size") == 0) {
831 i = pkg_get_int(pkg, PKG_SIZE);
833 fprintf(fp, "Size: %lu\n", (unsigned long) i);
835 } else if (strcasecmp(field, "Source") == 0) {
836 p = pkg_get_string(pkg, PKG_SOURCE);
838 fprintf(fp, "Source: %s\n", p);
840 } else if (strcasecmp(field, "Status") == 0) {
841 char *pflag = pkg_state_flag_to_str(pkg->state_flag);
842 fprintf(fp, "Status: %s %s %s\n",
843 pkg_state_want_to_str(pkg->state_want),
845 pkg_state_status_to_str(pkg->state_status));
847 } else if (strcasecmp(field, "Suggests") == 0) {
848 dep = pkg_get_depends(pkg, SUGGEST);
850 fprintf(fp, "Suggests:");
851 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
852 if (dep->type != SUGGEST)
854 str = pkg_depend_str(pkg, i);
855 fprintf(fp, "%s %s", j == 0 ? "" : ",",
863 goto UNKNOWN_FMT_FIELD;
868 if (strcasecmp(field, "Tags") == 0) {
869 p = pkg_get_string(pkg, PKG_TAGS);
871 fprintf(fp, "Tags: %s\n", p);
878 char *version = pkg_version_str_alloc(pkg);
881 fprintf(fp, "Version: %s\n", version);
886 goto UNKNOWN_FMT_FIELD;
892 opkg_msg(ERROR, "Internal error: field=%s\n", field);
895 void pkg_formatted_info(FILE * fp, pkg_t * pkg)
897 pkg_formatted_field(fp, pkg, "Package");
898 pkg_formatted_field(fp, pkg, "Version");
899 pkg_formatted_field(fp, pkg, "Depends");
900 pkg_formatted_field(fp, pkg, "Recommends");
901 pkg_formatted_field(fp, pkg, "Suggests");
902 pkg_formatted_field(fp, pkg, "Provides");
903 pkg_formatted_field(fp, pkg, "Replaces");
904 pkg_formatted_field(fp, pkg, "Conflicts");
905 pkg_formatted_field(fp, pkg, "Status");
906 pkg_formatted_field(fp, pkg, "Section");
907 pkg_formatted_field(fp, pkg, "Essential");
908 pkg_formatted_field(fp, pkg, "Architecture");
909 pkg_formatted_field(fp, pkg, "Maintainer");
910 pkg_formatted_field(fp, pkg, "MD5sum");
911 pkg_formatted_field(fp, pkg, "Size");
912 pkg_formatted_field(fp, pkg, "Filename");
913 pkg_formatted_field(fp, pkg, "Conffiles");
914 pkg_formatted_field(fp, pkg, "Source");
915 pkg_formatted_field(fp, pkg, "Description");
916 pkg_formatted_field(fp, pkg, "Installed-Time");
917 pkg_formatted_field(fp, pkg, "Tags");
921 void pkg_print_status(pkg_t * pkg, FILE * file)
927 pkg_formatted_field(file, pkg, "Package");
928 pkg_formatted_field(file, pkg, "Version");
929 pkg_formatted_field(file, pkg, "Depends");
930 pkg_formatted_field(file, pkg, "Recommends");
931 pkg_formatted_field(file, pkg, "Suggests");
932 pkg_formatted_field(file, pkg, "Provides");
933 pkg_formatted_field(file, pkg, "Replaces");
934 pkg_formatted_field(file, pkg, "Conflicts");
935 pkg_formatted_field(file, pkg, "Status");
936 pkg_formatted_field(file, pkg, "Essential");
937 pkg_formatted_field(file, pkg, "Architecture");
938 pkg_formatted_field(file, pkg, "Conffiles");
939 pkg_formatted_field(file, pkg, "Installed-Time");
940 pkg_formatted_field(file, pkg, "Auto-Installed");
945 * libdpkg - Debian packaging suite library routines
946 * vercmp.c - comparison of version numbers
948 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
951 /* assume ascii; warning: evaluates x multiple times! */
952 #define order(x) ((x) == '~' ? -1 \
955 : isalpha((x)) ? (x) \
958 static int verrevcmp(const char *val, const char *ref)
965 while (*val || *ref) {
968 while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) {
969 int vc = order(*val), rc = order(*ref);
980 while (isdigit(*val) && isdigit(*ref)) {
982 first_diff = *val - *ref;
996 int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg)
998 unsigned int epoch1 = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
999 unsigned int epoch2 = (unsigned int) pkg_get_int(ref_pkg, PKG_EPOCH);
1000 char *revision1 = pkg_get_string(pkg, PKG_REVISION);
1001 char *revision2 = pkg_get_string(ref_pkg, PKG_REVISION);
1002 const char *version1 = pkg_get_string(pkg, PKG_VERSION);
1003 const char *version2 = pkg_get_string(ref_pkg, PKG_VERSION);
1006 if (epoch1 > epoch2) {
1010 if (epoch1 < epoch2) {
1014 r = verrevcmp(version1, version2);
1019 r = verrevcmp(revision1, revision2);
1027 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op)
1031 r = pkg_compare_versions(it, ref);
1033 if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
1037 if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
1041 if (strcmp(op, "<<") == 0) {
1045 if (strcmp(op, ">>") == 0) {
1049 if (strcmp(op, "=") == 0) {
1053 opkg_msg(ERROR, "Unknown operator: %s.\n", op);
1057 int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
1059 const pkg_t * a = *(const pkg_t **)p1;
1060 const pkg_t * b = *(const pkg_t **)p2;
1063 int arch_prio1, arch_prio2;
1064 if (!a->name || !b->name) {
1065 opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n",
1070 namecmp = strcmp(a->name, b->name);
1073 vercmp = pkg_compare_versions(a, b);
1076 arch_prio1 = pkg_get_arch_priority(a);
1077 arch_prio2 = pkg_get_arch_priority(b);
1078 if (!arch_prio1 || !arch_prio2) {
1080 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1081 arch_prio1, arch_prio2);
1084 if (arch_prio1 > arch_prio2)
1086 if (arch_prio1 < arch_prio2)
1091 int abstract_pkg_name_compare(const void *p1, const void *p2)
1093 const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
1094 const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
1095 if (!a->name || !b->name) {
1096 opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n",
1100 return strcmp(a->name, b->name);
1103 char *pkg_version_str_alloc(pkg_t * pkg)
1106 char *version, *revptr;
1107 unsigned int epoch = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1109 revptr = pkg_get_string(pkg, PKG_REVISION);
1110 verstr = pkg_get_string(pkg, PKG_VERSION);
1114 sprintf_alloc(&version, "%d:%s-%s",
1115 epoch, verstr, revptr);
1117 sprintf_alloc(&version, "%d:%s",
1121 sprintf_alloc(&version, "%s-%s",
1124 version = xstrdup(verstr);
1131 * XXX: this should be broken into two functions
1133 str_list_t *pkg_get_installed_files(pkg_t * pkg)
1136 char *list_file_name = NULL;
1137 FILE *list_file = NULL;
1139 char *installed_file_name;
1140 unsigned int rootdirlen = 0;
1141 int list_from_package;
1142 const char *local_filename;
1144 pkg->installed_files_ref_cnt++;
1146 if (pkg->installed_files) {
1147 return pkg->installed_files;
1150 pkg->installed_files = str_list_alloc();
1153 * For installed packages, look at the package.list file in the database.
1154 * For uninstalled packages, get the file list directly from the package.
1156 if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
1157 list_from_package = 1;
1159 list_from_package = 0;
1161 if (list_from_package) {
1162 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1164 if (!local_filename) {
1165 return pkg->installed_files;
1167 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1168 file. In other words, change deb_extract so that it can
1169 simply return the file list as a char *[] rather than
1170 insisting on writing it to a FILE * as it does now. */
1171 sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
1172 conf->tmp_dir, pkg->name);
1173 fd = mkstemp(list_file_name);
1175 opkg_perror(ERROR, "Failed to make temp file %s.",
1177 free(list_file_name);
1178 return pkg->installed_files;
1180 list_file = fdopen(fd, "r+");
1181 if (list_file == NULL) {
1182 opkg_perror(ERROR, "Failed to fdopen temp file %s.",
1185 unlink(list_file_name);
1186 free(list_file_name);
1187 return pkg->installed_files;
1189 err = pkg_extract_data_file_names_to_stream(pkg, list_file);
1191 opkg_msg(ERROR, "Error extracting file list from %s.\n",
1194 unlink(list_file_name);
1195 free(list_file_name);
1196 str_list_deinit(pkg->installed_files);
1197 pkg->installed_files = NULL;
1202 sprintf_alloc(&list_file_name, "%s/%s.list",
1203 pkg->dest->info_dir, pkg->name);
1204 list_file = fopen(list_file_name, "r");
1205 if (list_file == NULL) {
1206 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1207 free(list_file_name);
1208 return pkg->installed_files;
1210 free(list_file_name);
1213 if (conf->offline_root)
1214 rootdirlen = strlen(conf->offline_root);
1219 line = file_read_line_alloc(list_file);
1225 if (list_from_package) {
1226 if (*file_name == '.') {
1229 if (*file_name == '/') {
1232 sprintf_alloc(&installed_file_name, "%s%s",
1233 pkg->dest->root_dir, file_name);
1235 if (conf->offline_root &&
1236 strncmp(conf->offline_root, file_name,
1238 sprintf_alloc(&installed_file_name, "%s%s",
1239 conf->offline_root, file_name);
1241 // already contains root_dir as header -> ABSOLUTE
1242 sprintf_alloc(&installed_file_name, "%s",
1246 str_list_append(pkg->installed_files, installed_file_name);
1247 free(installed_file_name);
1253 if (list_from_package) {
1254 unlink(list_file_name);
1255 free(list_file_name);
1258 return pkg->installed_files;
1261 /* XXX: CLEANUP: This function and it's counterpart,
1262 (pkg_get_installed_files), do not match our init/deinit naming
1263 convention. Nor the alloc/free convention. But, then again, neither
1264 of these conventions currrently fit the way these two functions
1266 void pkg_free_installed_files(pkg_t * pkg)
1268 pkg->installed_files_ref_cnt--;
1270 if (pkg->installed_files_ref_cnt > 0)
1273 if (pkg->installed_files) {
1274 str_list_purge(pkg->installed_files);
1277 pkg->installed_files = NULL;
1280 void pkg_remove_installed_files_list(pkg_t * pkg)
1282 char *list_file_name;
1284 sprintf_alloc(&list_file_name, "%s/%s.list",
1285 pkg->dest->info_dir, pkg->name);
1287 if (!conf->noaction)
1288 (void)unlink(list_file_name);
1290 free(list_file_name);
1293 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name)
1295 conffile_list_elt_t *iter;
1296 conffile_list_t *cl;
1297 conffile_t *conffile;
1303 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
1305 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
1306 iter = nv_pair_list_next(cl, iter)) {
1307 conffile = (conffile_t *) iter->data;
1309 if (strcmp(conffile->name, file_name) == 0) {
1317 int pkg_run_script(pkg_t * pkg, const char *script, const char *args)
1322 char *tmp_unpack_dir;
1327 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1328 maintainer script within a chroot environment. */
1329 if (conf->offline_root && !conf->force_postinstall) {
1330 opkg_msg(INFO, "Offline root mode: not running %s.%s.\n",
1335 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1336 have scripts in tmp_unpack_dir. */
1337 if (pkg->state_status == SS_INSTALLED
1338 || pkg->state_status == SS_UNPACKED) {
1339 if (pkg->dest == NULL) {
1340 opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n",
1344 sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name,
1347 tmp_unpack_dir = pkg_get_string(pkg, PKG_TMP_UNPACK_DIR);
1348 if (tmp_unpack_dir == NULL) {
1350 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1354 sprintf_alloc(&path, "%s/%s", tmp_unpack_dir, script);
1357 opkg_msg(INFO, "Running script %s.\n", path);
1360 pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir,
1363 if (pkg->is_upgrade)
1364 setenv("PKG_UPGRADE", "1", 1);
1366 setenv("PKG_UPGRADE", "0", 1);
1368 if (!file_exists(path)) {
1373 sprintf_alloc(&cmd, "%s %s", path, args);
1376 const char *argv[] = { "/bin/sh", "-c", cmd, NULL };
1377 err = xsystem(argv);
1383 "package \"%s\" %s script returned status %d.\n",
1384 pkg->name, script, err);
1391 int pkg_arch_supported(pkg_t * pkg)
1393 nv_pair_list_elt_t *l;
1394 char *architecture = pkg_get_architecture(pkg);
1399 list_for_each_entry(l, &conf->arch_list.head, node) {
1400 nv_pair_t *nv = (nv_pair_t *) l->data;
1401 if (strcmp(nv->name, architecture) == 0) {
1403 "Arch %s (priority %s) supported for pkg %s.\n",
1404 nv->name, nv->value, pkg->name);
1409 opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n",
1410 architecture, pkg->name);
1414 void pkg_info_preinstall_check(void)
1417 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1419 /* update the file owner data structure */
1420 opkg_msg(INFO, "Updating file owner list.\n");
1421 pkg_hash_fetch_all_installed(installed_pkgs);
1422 for (i = 0; i < installed_pkgs->len; i++) {
1423 pkg_t *pkg = installed_pkgs->pkgs[i];
1424 str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
1425 str_list_elt_t *iter, *niter;
1426 if (installed_files == NULL) {
1427 opkg_msg(ERROR, "Failed to determine installed "
1428 "files for pkg %s.\n", pkg->name);
1431 for (iter = str_list_first(installed_files), niter =
1432 str_list_next(installed_files, iter); iter;
1433 iter = niter, niter =
1434 str_list_next(installed_files, iter)) {
1435 char *installed_file = (char *)iter->data;
1436 file_hash_set_file_owner(installed_file, pkg);
1438 pkg_free_installed_files(pkg);
1440 pkg_vec_free(installed_pkgs);
1443 struct pkg_write_filelist_data {
1449 pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
1451 struct pkg_write_filelist_data *data = data_;
1452 pkg_t *entry = entry_;
1453 if (entry == data->pkg) {
1454 fprintf(data->stream, "%s\n", key);
1458 int pkg_write_filelist(pkg_t * pkg)
1460 struct pkg_write_filelist_data data;
1461 char *list_file_name;
1463 sprintf_alloc(&list_file_name, "%s/%s.list",
1464 pkg->dest->info_dir, pkg->name);
1466 opkg_msg(INFO, "Creating %s file for pkg %s.\n",
1467 list_file_name, pkg->name);
1469 data.stream = fopen(list_file_name, "w");
1471 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1472 free(list_file_name);
1477 hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
1478 fclose(data.stream);
1479 free(list_file_name);
1481 pkg->state_flag &= ~SF_FILELIST_CHANGED;
1486 int pkg_write_changed_filelists(void)
1488 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1489 int i, err, ret = 0;
1494 opkg_msg(INFO, "Saving changed filelists.\n");
1496 pkg_hash_fetch_all_installed(installed_pkgs);
1497 for (i = 0; i < installed_pkgs->len; i++) {
1498 pkg_t *pkg = installed_pkgs->pkgs[i];
1499 if (pkg->state_flag & SF_FILELIST_CHANGED) {
1500 err = pkg_write_filelist(pkg);
1506 pkg_vec_free(installed_pkgs);