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 pkg_set_string(pkg, PKG_LOCAL_FILENAME, filename);
351 tmp = xstrdup(filename);
352 sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
353 conf->tmp_dir, basename(tmp));
355 fd = mkstemp(control_path);
357 opkg_perror(ERROR, "Failed to make temp file %s", control_path);
362 control_file = fdopen(fd, "r+");
363 if (control_file == NULL) {
364 opkg_perror(ERROR, "Failed to fdopen %s", control_path);
370 err = pkg_extract_control_file_to_stream(pkg, control_file);
372 opkg_msg(ERROR, "Failed to extract control file from %s.\n",
377 rewind(control_file);
379 if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
381 opkg_msg(ERROR, "Malformed package file %s.\n",
388 fclose(control_file);
390 unlink(control_path);
397 /* Merge any new information in newpkg into oldpkg */
398 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg)
402 if (oldpkg == newpkg) {
406 if (!oldpkg->auto_installed)
407 oldpkg->auto_installed = newpkg->auto_installed;
410 oldpkg->src = newpkg->src;
412 oldpkg->dest = newpkg->dest;
413 if (!oldpkg->arch_index)
414 oldpkg->arch_index = newpkg->arch_index;
415 if (!pkg_get_string(oldpkg, PKG_SECTION))
416 pkg_set_string(oldpkg, PKG_SECTION, pkg_get_string(newpkg, PKG_SECTION));
417 if (!pkg_get_string(oldpkg, PKG_MAINTAINER))
418 pkg_set_string(oldpkg, PKG_MAINTAINER, pkg_get_string(newpkg, PKG_MAINTAINER));
419 if (!pkg_get_string(oldpkg, PKG_DESCRIPTION))
420 pkg_set_string(oldpkg, PKG_DESCRIPTION, pkg_get_string(newpkg, PKG_DESCRIPTION));
422 if (!pkg_get_ptr(oldpkg, PKG_DEPENDS)) {
423 pkg_set_ptr(oldpkg, PKG_DEPENDS, pkg_get_ptr(newpkg, PKG_DEPENDS));
424 pkg_set_ptr(newpkg, PKG_DEPENDS, NULL);
427 ab = pkg_get_ptr(oldpkg, PKG_PROVIDES);
429 if (!ab || !ab[0] || !ab[1]) {
430 pkg_set_ptr(oldpkg, PKG_PROVIDES, pkg_get_ptr(newpkg, PKG_PROVIDES));
431 pkg_set_ptr(newpkg, PKG_PROVIDES, NULL);
437 if (!pkg_get_ptr(oldpkg, PKG_CONFLICTS)) {
438 pkg_set_ptr(oldpkg, PKG_CONFLICTS, pkg_get_ptr(newpkg, PKG_CONFLICTS));
439 pkg_set_ptr(newpkg, PKG_CONFLICTS, NULL);
442 if (!pkg_get_ptr(oldpkg, PKG_REPLACES)) {
443 pkg_set_ptr(oldpkg, PKG_REPLACES, pkg_get_ptr(newpkg, PKG_REPLACES));
444 pkg_set_ptr(newpkg, PKG_REPLACES, NULL);
447 if (!pkg_get_string(oldpkg, PKG_FILENAME))
448 pkg_set_string(oldpkg, PKG_FILENAME, pkg_get_string(newpkg, PKG_FILENAME));
449 if (!pkg_get_string(oldpkg, PKG_LOCAL_FILENAME))
450 pkg_set_string(oldpkg, PKG_LOCAL_FILENAME, pkg_get_string(newpkg, PKG_LOCAL_FILENAME));
451 if (!pkg_get_string(oldpkg, PKG_TMP_UNPACK_DIR))
452 pkg_set_string(oldpkg, PKG_TMP_UNPACK_DIR, pkg_get_string(newpkg, PKG_TMP_UNPACK_DIR));
453 if (!pkg_get_md5(oldpkg))
454 pkg_set_md5(oldpkg, pkg_get_md5(newpkg));
455 if (!pkg_get_sha256(oldpkg))
456 pkg_set_sha256(oldpkg, pkg_get_sha256(newpkg));
457 if (!pkg_get_int(oldpkg, PKG_SIZE))
458 pkg_set_int(oldpkg, PKG_SIZE, pkg_get_int(newpkg, PKG_SIZE));
459 if (!pkg_get_int(oldpkg, PKG_INSTALLED_SIZE))
460 pkg_set_int(oldpkg, PKG_INSTALLED_SIZE, pkg_get_int(newpkg, PKG_INSTALLED_SIZE));
461 if (!pkg_get_string(oldpkg, PKG_PRIORITY))
462 pkg_set_string(oldpkg, PKG_PRIORITY, pkg_get_string(newpkg, PKG_PRIORITY));
463 if (!pkg_get_string(oldpkg, PKG_SOURCE))
464 pkg_set_string(oldpkg, PKG_SOURCE, pkg_get_string(newpkg, PKG_SOURCE));
466 if (!pkg_get_ptr(oldpkg, PKG_CONFFILES)) {
467 pkg_set_ptr(oldpkg, PKG_CONFFILES, pkg_get_ptr(newpkg, PKG_CONFFILES));
468 pkg_set_ptr(newpkg, PKG_CONFFILES, NULL);
471 if (!oldpkg->installed_files) {
472 oldpkg->installed_files = newpkg->installed_files;
473 oldpkg->installed_files_ref_cnt =
474 newpkg->installed_files_ref_cnt;
475 newpkg->installed_files = NULL;
478 if (!oldpkg->essential)
479 oldpkg->essential = newpkg->essential;
484 static void abstract_pkg_init(abstract_pkg_t * ab_pkg)
486 ab_pkg->provided_by = abstract_pkg_vec_alloc();
487 ab_pkg->dependencies_checked = 0;
488 ab_pkg->state_status = SS_NOT_INSTALLED;
491 abstract_pkg_t *abstract_pkg_new(void)
493 abstract_pkg_t *ab_pkg;
495 ab_pkg = xcalloc(1, sizeof(abstract_pkg_t));
496 abstract_pkg_init(ab_pkg);
501 void set_flags_from_control(pkg_t * pkg)
506 sprintf_alloc(&file_name, "%s/%s.control", pkg->dest->info_dir,
509 fp = fopen(file_name, "r");
511 opkg_perror(ERROR, "Failed to open %s", file_name);
518 if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) {
520 "Unable to read control file for %s. May be empty.\n",
529 static const char *pkg_state_want_to_str(pkg_state_want_t sw)
533 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
534 if (pkg_state_want_map[i].value == sw) {
535 return pkg_state_want_map[i].str;
539 opkg_msg(ERROR, "Internal error: state_want=%d\n", sw);
540 return "<STATE_WANT_UNKNOWN>";
543 pkg_state_want_t pkg_state_want_from_str(char *str)
547 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
548 if (strcmp(str, pkg_state_want_map[i].str) == 0) {
549 return pkg_state_want_map[i].value;
553 opkg_msg(ERROR, "Internal error: state_want=%s\n", str);
557 static char *pkg_state_flag_to_str(pkg_state_flag_t sf)
563 /* clear the temporary flags before converting to string */
564 sf &= SF_NONVOLATILE_FLAGS;
567 return xstrdup("ok");
570 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
571 if (sf & pkg_state_flag_map[i].value)
572 len += strlen(pkg_state_flag_map[i].str) + 1;
575 str = xmalloc(len + 1);
578 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
579 if (sf & pkg_state_flag_map[i].value) {
580 strncat(str, pkg_state_flag_map[i].str, len);
581 strncat(str, ",", len);
586 str[len - 1] = '\0'; /* squash last comma */
591 pkg_state_flag_t pkg_state_flag_from_str(const char *str)
596 unsigned int sfname_len;
598 if (strcmp(str, "ok") == 0) {
601 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
602 sfname = pkg_state_flag_map[i].str;
603 sfname_len = strlen(sfname);
604 if (strncmp(str, sfname, sfname_len) == 0) {
605 sf |= pkg_state_flag_map[i].value;
618 static const char *pkg_state_status_to_str(pkg_state_status_t ss)
622 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
623 if (pkg_state_status_map[i].value == ss) {
624 return pkg_state_status_map[i].str;
628 opkg_msg(ERROR, "Internal error: state_status=%d\n", ss);
629 return "<STATE_STATUS_UNKNOWN>";
632 pkg_state_status_t pkg_state_status_from_str(const char *str)
636 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
637 if (strcmp(str, pkg_state_status_map[i].str) == 0) {
638 return pkg_state_status_map[i].value;
642 opkg_msg(ERROR, "Internal error: state_status=%s\n", str);
643 return SS_NOT_INSTALLED;
646 void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field)
651 compound_depend_t *dep;
652 abstract_pkg_t **ab_pkg;
654 if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
655 goto UNKNOWN_FMT_FIELD;
661 if (strcasecmp(field, "Architecture") == 0) {
662 p = pkg_get_architecture(pkg);
664 fprintf(fp, "Architecture: %s\n",
667 } else if (strcasecmp(field, "Auto-Installed") == 0) {
668 if (pkg->auto_installed)
669 fprintf(fp, "Auto-Installed: yes\n");
671 goto UNKNOWN_FMT_FIELD;
676 if (strcasecmp(field, "Conffiles") == 0) {
678 conffile_list_elt_t *iter;
680 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
682 if (!cl || nv_pair_list_empty(cl))
685 fprintf(fp, "Conffiles:\n");
686 for (iter = nv_pair_list_first(cl); iter;
687 iter = nv_pair_list_next(cl, iter)) {
688 if (((conffile_t *) iter->data)->name
689 && ((conffile_t *) iter->data)->value) {
690 fprintf(fp, " %s %s\n",
691 ((conffile_t *) iter->data)->
693 ((conffile_t *) iter->data)->
697 } else if (strcasecmp(field, "Conflicts") == 0) {
699 compound_depend_t *deps, *dep;
700 deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
702 fprintf(fp, "Conflicts:");
703 for (i = 0, dep = deps; dep->type; dep++, i++) {
704 cdep = dep->possibilities[0];
705 fprintf(fp, "%s %s", i == 0 ? "" : ",",
708 fprintf(fp, " (%s%s)",
709 constraint_to_str(cdep->
717 goto UNKNOWN_FMT_FIELD;
722 if (strcasecmp(field, "Depends") == 0) {
723 dep = pkg_get_depends(pkg, DEPEND);
725 fprintf(fp, "Depends:");
726 for (i = 0, j = 0; dep && dep->type; i++, dep++) {
727 if (dep->type != DEPEND)
729 str = pkg_depend_str(pkg, i);
730 fprintf(fp, "%s %s", j == 0 ? "" : ",",
737 } else if (strcasecmp(field, "Description") == 0) {
738 p = pkg_get_string(pkg, PKG_DESCRIPTION);
740 fprintf(fp, "Description: %s\n",
744 goto UNKNOWN_FMT_FIELD;
749 if (pkg->essential) {
750 fprintf(fp, "Essential: yes\n");
755 p = pkg_get_string(pkg, PKG_FILENAME);
757 fprintf(fp, "Filename: %s\n", p);
762 if (strcasecmp(field, "Installed-Size") == 0) {
763 fprintf(fp, "Installed-Size: %lu\n",
764 (unsigned long) pkg_get_int(pkg, PKG_INSTALLED_SIZE));
765 } else if (strcasecmp(field, "Installed-Time") == 0) {
766 i = pkg_get_int(pkg, PKG_INSTALLED_TIME);
768 fprintf(fp, "Installed-Time: %lu\n",
775 if (strcasecmp(field, "Maintainer") == 0) {
776 p = pkg_get_string(pkg, PKG_MAINTAINER);
778 fprintf(fp, "Maintainer: %s\n", p);
780 } else if (strcasecmp(field, "MD5sum") == 0) {
781 p = pkg_get_md5(pkg);
783 fprintf(fp, "MD5Sum: %s\n", p);
786 goto UNKNOWN_FMT_FIELD;
791 if (strcasecmp(field, "Package") == 0) {
792 fprintf(fp, "Package: %s\n", pkg->name);
793 } else if (strcasecmp(field, "Priority") == 0) {
794 fprintf(fp, "Priority: %s\n", pkg_get_string(pkg, PKG_PRIORITY));
795 } else if (strcasecmp(field, "Provides") == 0) {
796 ab_pkg = pkg_get_ptr(pkg, PKG_PROVIDES);
797 if (ab_pkg && ab_pkg[0] && ab_pkg[1]) {
798 fprintf(fp, "Provides:");
799 for (i = 0, ab_pkg++; *ab_pkg; i++, ab_pkg++) {
800 fprintf(fp, "%s %s", i == 0 ? "" : ",",
807 goto UNKNOWN_FMT_FIELD;
812 if (strcasecmp(field, "Replaces") == 0) {
813 ab_pkg = pkg_get_ptr(pkg, PKG_REPLACES);
814 if (ab_pkg && *ab_pkg) {
815 fprintf(fp, "Replaces:");
816 for (i = 0; *ab_pkg; i++, ab_pkg++) {
817 fprintf(fp, "%s %s", i == 0 ? "" : ",",
822 } else if (strcasecmp(field, "Recommends") == 0) {
823 dep = pkg_get_depends(pkg, RECOMMEND);
825 fprintf(fp, "Recommends:");
826 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
827 if (dep->type != RECOMMEND)
829 str = pkg_depend_str(pkg, i);
830 fprintf(fp, "%s %s", j == 0 ? "" : ",",
838 goto UNKNOWN_FMT_FIELD;
843 if (strcasecmp(field, "Section") == 0) {
844 p = pkg_get_string(pkg, PKG_SECTION);
846 fprintf(fp, "Section: %s\n", p);
848 #if defined HAVE_SHA256
849 } else if (strcasecmp(field, "SHA256sum") == 0) {
850 p = pkg_get_string(pkg, PKG_SHA256SUM);
852 fprintf(fp, "SHA256sum: %s\n", p);
855 } else if (strcasecmp(field, "Size") == 0) {
856 i = pkg_get_int(pkg, PKG_SIZE);
858 fprintf(fp, "Size: %lu\n", (unsigned long) i);
860 } else if (strcasecmp(field, "Source") == 0) {
861 p = pkg_get_string(pkg, PKG_SOURCE);
863 fprintf(fp, "Source: %s\n", p);
865 } else if (strcasecmp(field, "Status") == 0) {
866 char *pflag = pkg_state_flag_to_str(pkg->state_flag);
867 fprintf(fp, "Status: %s %s %s\n",
868 pkg_state_want_to_str(pkg->state_want),
870 pkg_state_status_to_str(pkg->state_status));
872 } else if (strcasecmp(field, "Suggests") == 0) {
873 dep = pkg_get_depends(pkg, SUGGEST);
875 fprintf(fp, "Suggests:");
876 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
877 if (dep->type != SUGGEST)
879 str = pkg_depend_str(pkg, i);
880 fprintf(fp, "%s %s", j == 0 ? "" : ",",
888 goto UNKNOWN_FMT_FIELD;
893 if (strcasecmp(field, "Tags") == 0) {
894 p = pkg_get_string(pkg, PKG_TAGS);
896 fprintf(fp, "Tags: %s\n", p);
903 char *version = pkg_version_str_alloc(pkg);
906 fprintf(fp, "Version: %s\n", version);
911 goto UNKNOWN_FMT_FIELD;
917 opkg_msg(ERROR, "Internal error: field=%s\n", field);
920 void pkg_formatted_info(FILE * fp, pkg_t * pkg)
922 pkg_formatted_field(fp, pkg, "Package");
923 pkg_formatted_field(fp, pkg, "Version");
924 pkg_formatted_field(fp, pkg, "Depends");
925 pkg_formatted_field(fp, pkg, "Recommends");
926 pkg_formatted_field(fp, pkg, "Suggests");
927 pkg_formatted_field(fp, pkg, "Provides");
928 pkg_formatted_field(fp, pkg, "Replaces");
929 pkg_formatted_field(fp, pkg, "Conflicts");
930 pkg_formatted_field(fp, pkg, "Status");
931 pkg_formatted_field(fp, pkg, "Section");
932 pkg_formatted_field(fp, pkg, "Essential");
933 pkg_formatted_field(fp, pkg, "Architecture");
934 pkg_formatted_field(fp, pkg, "Maintainer");
935 pkg_formatted_field(fp, pkg, "MD5sum");
936 pkg_formatted_field(fp, pkg, "Size");
937 pkg_formatted_field(fp, pkg, "Filename");
938 pkg_formatted_field(fp, pkg, "Conffiles");
939 pkg_formatted_field(fp, pkg, "Source");
940 pkg_formatted_field(fp, pkg, "Description");
941 pkg_formatted_field(fp, pkg, "Installed-Time");
942 pkg_formatted_field(fp, pkg, "Tags");
946 void pkg_print_status(pkg_t * pkg, FILE * file)
952 pkg_formatted_field(file, pkg, "Package");
953 pkg_formatted_field(file, pkg, "Version");
954 pkg_formatted_field(file, pkg, "Depends");
955 pkg_formatted_field(file, pkg, "Recommends");
956 pkg_formatted_field(file, pkg, "Suggests");
957 pkg_formatted_field(file, pkg, "Provides");
958 pkg_formatted_field(file, pkg, "Replaces");
959 pkg_formatted_field(file, pkg, "Conflicts");
960 pkg_formatted_field(file, pkg, "Status");
961 pkg_formatted_field(file, pkg, "Essential");
962 pkg_formatted_field(file, pkg, "Architecture");
963 pkg_formatted_field(file, pkg, "Conffiles");
964 pkg_formatted_field(file, pkg, "Installed-Time");
965 pkg_formatted_field(file, pkg, "Auto-Installed");
970 * libdpkg - Debian packaging suite library routines
971 * vercmp.c - comparison of version numbers
973 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
976 /* assume ascii; warning: evaluates x multiple times! */
977 #define order(x) ((x) == '~' ? -1 \
980 : isalpha((x)) ? (x) \
983 static int verrevcmp(const char *val, const char *ref)
990 while (*val || *ref) {
993 while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) {
994 int vc = order(*val), rc = order(*ref);
1005 while (isdigit(*val) && isdigit(*ref)) {
1007 first_diff = *val - *ref;
1021 int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg)
1023 unsigned int epoch1 = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1024 unsigned int epoch2 = (unsigned int) pkg_get_int(ref_pkg, PKG_EPOCH);
1025 char *revision1 = pkg_get_string(pkg, PKG_REVISION);
1026 char *revision2 = pkg_get_string(ref_pkg, PKG_REVISION);
1027 const char *version1 = pkg_get_string(pkg, PKG_VERSION);
1028 const char *version2 = pkg_get_string(ref_pkg, PKG_VERSION);
1031 if (epoch1 > epoch2) {
1035 if (epoch1 < epoch2) {
1039 r = verrevcmp(version1, version2);
1044 r = verrevcmp(revision1, revision2);
1052 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op)
1056 r = pkg_compare_versions(it, ref);
1058 if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
1062 if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
1066 if (strcmp(op, "<<") == 0) {
1070 if (strcmp(op, ">>") == 0) {
1074 if (strcmp(op, "=") == 0) {
1078 opkg_msg(ERROR, "Unknown operator: %s.\n", op);
1082 int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
1084 const pkg_t * a = *(const pkg_t **)p1;
1085 const pkg_t * b = *(const pkg_t **)p2;
1088 int arch_prio1, arch_prio2;
1089 if (!a->name || !b->name) {
1090 opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n",
1095 namecmp = strcmp(a->name, b->name);
1098 vercmp = pkg_compare_versions(a, b);
1101 arch_prio1 = pkg_get_arch_priority(a);
1102 arch_prio2 = pkg_get_arch_priority(b);
1103 if (!arch_prio1 || !arch_prio2) {
1105 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1106 arch_prio1, arch_prio2);
1109 if (arch_prio1 > arch_prio2)
1111 if (arch_prio1 < arch_prio2)
1116 int abstract_pkg_name_compare(const void *p1, const void *p2)
1118 const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
1119 const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
1120 if (!a->name || !b->name) {
1121 opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n",
1125 return strcmp(a->name, b->name);
1128 char *pkg_version_str_alloc(pkg_t * pkg)
1131 char *version, *revptr;
1132 unsigned int epoch = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1134 revptr = pkg_get_string(pkg, PKG_REVISION);
1135 verstr = pkg_get_string(pkg, PKG_VERSION);
1139 sprintf_alloc(&version, "%d:%s-%s",
1140 epoch, verstr, revptr);
1142 sprintf_alloc(&version, "%d:%s",
1146 sprintf_alloc(&version, "%s-%s",
1149 version = xstrdup(verstr);
1156 * XXX: this should be broken into two functions
1158 str_list_t *pkg_get_installed_files(pkg_t * pkg)
1161 char *list_file_name = NULL;
1162 FILE *list_file = NULL;
1164 char *installed_file_name;
1165 unsigned int rootdirlen = 0;
1166 int list_from_package;
1167 const char *local_filename;
1169 pkg->installed_files_ref_cnt++;
1171 if (pkg->installed_files) {
1172 return pkg->installed_files;
1175 pkg->installed_files = str_list_alloc();
1178 * For installed packages, look at the package.list file in the database.
1179 * For uninstalled packages, get the file list directly from the package.
1181 if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
1182 list_from_package = 1;
1184 list_from_package = 0;
1186 if (list_from_package) {
1187 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1189 if (!local_filename) {
1190 return pkg->installed_files;
1192 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1193 file. In other words, change deb_extract so that it can
1194 simply return the file list as a char *[] rather than
1195 insisting on writing it to a FILE * as it does now. */
1196 sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
1197 conf->tmp_dir, pkg->name);
1198 fd = mkstemp(list_file_name);
1200 opkg_perror(ERROR, "Failed to make temp file %s.",
1202 free(list_file_name);
1203 return pkg->installed_files;
1205 list_file = fdopen(fd, "r+");
1206 if (list_file == NULL) {
1207 opkg_perror(ERROR, "Failed to fdopen temp file %s.",
1210 unlink(list_file_name);
1211 free(list_file_name);
1212 return pkg->installed_files;
1214 err = pkg_extract_data_file_names_to_stream(pkg, list_file);
1216 opkg_msg(ERROR, "Error extracting file list from %s.\n",
1219 unlink(list_file_name);
1220 free(list_file_name);
1221 str_list_deinit(pkg->installed_files);
1222 pkg->installed_files = NULL;
1227 sprintf_alloc(&list_file_name, "%s/%s.list",
1228 pkg->dest->info_dir, pkg->name);
1229 list_file = fopen(list_file_name, "r");
1230 if (list_file == NULL) {
1231 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1232 free(list_file_name);
1233 return pkg->installed_files;
1235 free(list_file_name);
1238 if (conf->offline_root)
1239 rootdirlen = strlen(conf->offline_root);
1244 line = file_read_line_alloc(list_file);
1250 if (list_from_package) {
1251 if (*file_name == '.') {
1254 if (*file_name == '/') {
1257 sprintf_alloc(&installed_file_name, "%s%s",
1258 pkg->dest->root_dir, file_name);
1260 if (conf->offline_root &&
1261 strncmp(conf->offline_root, file_name,
1263 sprintf_alloc(&installed_file_name, "%s%s",
1264 conf->offline_root, file_name);
1266 // already contains root_dir as header -> ABSOLUTE
1267 sprintf_alloc(&installed_file_name, "%s",
1271 str_list_append(pkg->installed_files, installed_file_name);
1272 free(installed_file_name);
1278 if (list_from_package) {
1279 unlink(list_file_name);
1280 free(list_file_name);
1283 return pkg->installed_files;
1286 /* XXX: CLEANUP: This function and it's counterpart,
1287 (pkg_get_installed_files), do not match our init/deinit naming
1288 convention. Nor the alloc/free convention. But, then again, neither
1289 of these conventions currrently fit the way these two functions
1291 void pkg_free_installed_files(pkg_t * pkg)
1293 pkg->installed_files_ref_cnt--;
1295 if (pkg->installed_files_ref_cnt > 0)
1298 if (pkg->installed_files) {
1299 str_list_purge(pkg->installed_files);
1302 pkg->installed_files = NULL;
1305 void pkg_remove_installed_files_list(pkg_t * pkg)
1307 char *list_file_name;
1309 sprintf_alloc(&list_file_name, "%s/%s.list",
1310 pkg->dest->info_dir, pkg->name);
1312 if (!conf->noaction)
1313 (void)unlink(list_file_name);
1315 free(list_file_name);
1318 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name)
1320 conffile_list_elt_t *iter;
1321 conffile_list_t *cl;
1322 conffile_t *conffile;
1328 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
1330 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
1331 iter = nv_pair_list_next(cl, iter)) {
1332 conffile = (conffile_t *) iter->data;
1334 if (strcmp(conffile->name, file_name) == 0) {
1342 int pkg_run_script(pkg_t * pkg, const char *script, const char *args)
1347 char *tmp_unpack_dir;
1352 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1353 maintainer script within a chroot environment. */
1354 if (conf->offline_root && !conf->force_postinstall) {
1355 opkg_msg(INFO, "Offline root mode: not running %s.%s.\n",
1360 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1361 have scripts in tmp_unpack_dir. */
1362 if (pkg->state_status == SS_INSTALLED
1363 || pkg->state_status == SS_UNPACKED) {
1364 if (pkg->dest == NULL) {
1365 opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n",
1369 sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name,
1372 tmp_unpack_dir = pkg_get_string(pkg, PKG_TMP_UNPACK_DIR);
1373 if (tmp_unpack_dir == NULL) {
1375 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1379 sprintf_alloc(&path, "%s/%s", tmp_unpack_dir, script);
1382 opkg_msg(INFO, "Running script %s.\n", path);
1385 pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir,
1388 if (pkg->is_upgrade)
1389 setenv("PKG_UPGRADE", "1", 1);
1391 setenv("PKG_UPGRADE", "0", 1);
1393 if (!file_exists(path)) {
1398 sprintf_alloc(&cmd, "%s %s", path, args);
1401 const char *argv[] = { "sh", "-c", cmd, NULL };
1402 err = xsystem(argv);
1408 "package \"%s\" %s script returned status %d.\n",
1409 pkg->name, script, err);
1416 int pkg_arch_supported(pkg_t * pkg)
1418 nv_pair_list_elt_t *l;
1419 char *architecture = pkg_get_architecture(pkg);
1424 list_for_each_entry(l, &conf->arch_list.head, node) {
1425 nv_pair_t *nv = (nv_pair_t *) l->data;
1426 if (strcmp(nv->name, architecture) == 0) {
1428 "Arch %s (priority %s) supported for pkg %s.\n",
1429 nv->name, nv->value, pkg->name);
1434 opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n",
1435 architecture, pkg->name);
1439 void pkg_info_preinstall_check(void)
1442 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1444 /* update the file owner data structure */
1445 opkg_msg(INFO, "Updating file owner list.\n");
1446 pkg_hash_fetch_all_installed(installed_pkgs);
1447 for (i = 0; i < installed_pkgs->len; i++) {
1448 pkg_t *pkg = installed_pkgs->pkgs[i];
1449 str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
1450 str_list_elt_t *iter, *niter;
1451 if (installed_files == NULL) {
1452 opkg_msg(ERROR, "Failed to determine installed "
1453 "files for pkg %s.\n", pkg->name);
1456 for (iter = str_list_first(installed_files), niter =
1457 str_list_next(installed_files, iter); iter;
1458 iter = niter, niter =
1459 str_list_next(installed_files, iter)) {
1460 char *installed_file = (char *)iter->data;
1461 file_hash_set_file_owner(installed_file, pkg);
1463 pkg_free_installed_files(pkg);
1465 pkg_vec_free(installed_pkgs);
1468 struct pkg_write_filelist_data {
1474 pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
1476 struct pkg_write_filelist_data *data = data_;
1477 pkg_t *entry = entry_;
1478 if (entry == data->pkg) {
1479 fprintf(data->stream, "%s\n", key);
1483 int pkg_write_filelist(pkg_t * pkg)
1485 struct pkg_write_filelist_data data;
1486 char *list_file_name;
1488 sprintf_alloc(&list_file_name, "%s/%s.list",
1489 pkg->dest->info_dir, pkg->name);
1491 opkg_msg(INFO, "Creating %s file for pkg %s.\n",
1492 list_file_name, pkg->name);
1494 data.stream = fopen(list_file_name, "w");
1496 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1497 free(list_file_name);
1502 hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
1503 fclose(data.stream);
1504 free(list_file_name);
1506 pkg->state_flag &= ~SF_FILELIST_CHANGED;
1511 int pkg_write_changed_filelists(void)
1513 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1514 int i, err, ret = 0;
1519 opkg_msg(INFO, "Saving changed filelists.\n");
1521 pkg_hash_fetch_all_installed(installed_pkgs);
1522 for (i = 0; i < installed_pkgs->len; i++) {
1523 pkg_t *pkg = installed_pkgs->pkgs[i];
1524 if (pkg->state_flag & SF_FILELIST_CHANGED) {
1525 err = pkg_write_filelist(pkg);
1531 pkg_vec_free(installed_pkgs);