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)
272 compound_depend_t *deps, *dep;
278 /* owned by opkg_conf_t */
280 /* owned by opkg_conf_t */
283 pkg->state_want = SW_UNKNOWN;
284 pkg->state_flag = SF_OK;
285 pkg->state_status = SS_NOT_INSTALLED;
287 deps = pkg_get_ptr(pkg, PKG_DEPENDS);
290 for (dep = deps; dep->type; dep++)
291 compound_depend_deinit(dep);
294 pkg_set_ptr(pkg, PKG_DEPENDS, NULL);
297 deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
300 for (dep = deps; dep->type; dep++)
301 compound_depend_deinit(dep);
304 pkg_set_ptr(pkg, PKG_CONFLICTS, NULL);
307 //conffile_list_deinit(&pkg->conffiles);
309 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
310 since if they are calling deinit, they should know. Maybe do an
311 assertion here instead? */
312 pkg->installed_files_ref_cnt = 1;
313 pkg_free_installed_files(pkg);
316 //blob_buf_free(&pkg->blob);
319 int pkg_init_from_file(pkg_t * pkg, const char *filename)
323 char *control_path, *tmp;
327 pkg_set_string(pkg, PKG_LOCAL_FILENAME, filename);
329 tmp = xstrdup(filename);
330 sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
331 conf->tmp_dir, basename(tmp));
333 fd = mkstemp(control_path);
335 opkg_perror(ERROR, "Failed to make temp file %s", control_path);
340 control_file = fdopen(fd, "r+");
341 if (control_file == NULL) {
342 opkg_perror(ERROR, "Failed to fdopen %s", control_path);
348 err = pkg_extract_control_file_to_stream(pkg, control_file);
350 opkg_msg(ERROR, "Failed to extract control file from %s.\n",
355 rewind(control_file);
357 if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
359 opkg_msg(ERROR, "Malformed package file %s.\n",
366 fclose(control_file);
368 unlink(control_path);
375 /* Merge any new information in newpkg into oldpkg */
376 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg)
380 if (oldpkg == newpkg) {
384 if (!oldpkg->auto_installed)
385 oldpkg->auto_installed = newpkg->auto_installed;
388 oldpkg->src = newpkg->src;
390 oldpkg->dest = newpkg->dest;
391 if (!oldpkg->arch_index)
392 oldpkg->arch_index = newpkg->arch_index;
393 if (!pkg_get_string(oldpkg, PKG_SECTION))
394 pkg_set_string(oldpkg, PKG_SECTION, pkg_get_string(newpkg, PKG_SECTION));
395 if (!pkg_get_string(oldpkg, PKG_MAINTAINER))
396 pkg_set_string(oldpkg, PKG_MAINTAINER, pkg_get_string(newpkg, PKG_MAINTAINER));
397 if (!pkg_get_string(oldpkg, PKG_DESCRIPTION))
398 pkg_set_string(oldpkg, PKG_DESCRIPTION, pkg_get_string(newpkg, PKG_DESCRIPTION));
400 if (!pkg_get_ptr(oldpkg, PKG_DEPENDS)) {
401 pkg_set_ptr(oldpkg, PKG_DEPENDS, pkg_get_ptr(newpkg, PKG_DEPENDS));
402 pkg_set_ptr(newpkg, PKG_DEPENDS, NULL);
405 ab = pkg_get_ptr(oldpkg, PKG_PROVIDES);
407 if (!ab || !ab[0] || !ab[1]) {
408 pkg_set_ptr(oldpkg, PKG_PROVIDES, pkg_get_ptr(newpkg, PKG_PROVIDES));
409 pkg_set_ptr(newpkg, PKG_PROVIDES, NULL);
415 if (!pkg_get_ptr(oldpkg, PKG_CONFLICTS)) {
416 pkg_set_ptr(oldpkg, PKG_CONFLICTS, pkg_get_ptr(newpkg, PKG_CONFLICTS));
417 pkg_set_ptr(newpkg, PKG_CONFLICTS, NULL);
420 if (!pkg_get_ptr(oldpkg, PKG_REPLACES)) {
421 pkg_set_ptr(oldpkg, PKG_REPLACES, pkg_get_ptr(newpkg, PKG_REPLACES));
422 pkg_set_ptr(newpkg, PKG_REPLACES, NULL);
425 if (!pkg_get_string(oldpkg, PKG_FILENAME))
426 pkg_set_string(oldpkg, PKG_FILENAME, pkg_get_string(newpkg, PKG_FILENAME));
427 if (!pkg_get_string(oldpkg, PKG_LOCAL_FILENAME))
428 pkg_set_string(oldpkg, PKG_LOCAL_FILENAME, pkg_get_string(newpkg, PKG_LOCAL_FILENAME));
429 if (!pkg_get_string(oldpkg, PKG_TMP_UNPACK_DIR))
430 pkg_set_string(oldpkg, PKG_TMP_UNPACK_DIR, pkg_get_string(newpkg, PKG_TMP_UNPACK_DIR));
431 if (!pkg_get_md5(oldpkg))
432 pkg_set_md5(oldpkg, pkg_get_md5(newpkg));
433 if (!pkg_get_sha256(oldpkg))
434 pkg_set_sha256(oldpkg, pkg_get_sha256(newpkg));
435 if (!pkg_get_int(oldpkg, PKG_SIZE))
436 pkg_set_int(oldpkg, PKG_SIZE, pkg_get_int(newpkg, PKG_SIZE));
437 if (!pkg_get_int(oldpkg, PKG_INSTALLED_SIZE))
438 pkg_set_int(oldpkg, PKG_INSTALLED_SIZE, pkg_get_int(newpkg, PKG_INSTALLED_SIZE));
439 if (!pkg_get_string(oldpkg, PKG_PRIORITY))
440 pkg_set_string(oldpkg, PKG_PRIORITY, pkg_get_string(newpkg, PKG_PRIORITY));
441 if (!pkg_get_string(oldpkg, PKG_SOURCE))
442 pkg_set_string(oldpkg, PKG_SOURCE, pkg_get_string(newpkg, PKG_SOURCE));
444 if (!pkg_get_ptr(oldpkg, PKG_CONFFILES)) {
445 pkg_set_ptr(oldpkg, PKG_CONFFILES, pkg_get_ptr(newpkg, PKG_CONFFILES));
446 pkg_set_ptr(newpkg, PKG_CONFFILES, NULL);
449 if (!oldpkg->installed_files) {
450 oldpkg->installed_files = newpkg->installed_files;
451 oldpkg->installed_files_ref_cnt =
452 newpkg->installed_files_ref_cnt;
453 newpkg->installed_files = NULL;
456 if (!oldpkg->essential)
457 oldpkg->essential = newpkg->essential;
462 static void abstract_pkg_init(abstract_pkg_t * ab_pkg)
464 ab_pkg->provided_by = abstract_pkg_vec_alloc();
465 ab_pkg->dependencies_checked = 0;
466 ab_pkg->state_status = SS_NOT_INSTALLED;
469 abstract_pkg_t *abstract_pkg_new(void)
471 abstract_pkg_t *ab_pkg;
473 ab_pkg = xcalloc(1, sizeof(abstract_pkg_t));
474 abstract_pkg_init(ab_pkg);
479 void set_flags_from_control(pkg_t * pkg)
484 sprintf_alloc(&file_name, "%s/%s.control", pkg->dest->info_dir,
487 fp = fopen(file_name, "r");
489 opkg_perror(ERROR, "Failed to open %s", file_name);
496 if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) {
498 "Unable to read control file for %s. May be empty.\n",
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 = 0, ab_pkg++; *ab_pkg; i++, ab_pkg++) {
778 fprintf(fp, "%s %s", i == 0 ? "" : ",",
785 goto UNKNOWN_FMT_FIELD;
790 if (strcasecmp(field, "Replaces") == 0) {
791 ab_pkg = pkg_get_ptr(pkg, PKG_REPLACES);
792 if (ab_pkg && *ab_pkg) {
793 fprintf(fp, "Replaces:");
794 for (i = 0; *ab_pkg; i++, ab_pkg++) {
795 fprintf(fp, "%s %s", i == 0 ? "" : ",",
800 } else if (strcasecmp(field, "Recommends") == 0) {
801 dep = pkg_get_depends(pkg, RECOMMEND);
803 fprintf(fp, "Recommends:");
804 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
805 if (dep->type != RECOMMEND)
807 str = pkg_depend_str(pkg, i);
808 fprintf(fp, "%s %s", j == 0 ? "" : ",",
816 goto UNKNOWN_FMT_FIELD;
821 if (strcasecmp(field, "Section") == 0) {
822 p = pkg_get_string(pkg, PKG_SECTION);
824 fprintf(fp, "Section: %s\n", p);
826 #if defined HAVE_SHA256
827 } else if (strcasecmp(field, "SHA256sum") == 0) {
828 p = pkg_get_string(pkg, PKG_SHA256SUM);
830 fprintf(fp, "SHA256sum: %s\n", p);
833 } else if (strcasecmp(field, "Size") == 0) {
834 i = pkg_get_int(pkg, PKG_SIZE);
836 fprintf(fp, "Size: %lu\n", (unsigned long) i);
838 } else if (strcasecmp(field, "Source") == 0) {
839 p = pkg_get_string(pkg, PKG_SOURCE);
841 fprintf(fp, "Source: %s\n", p);
843 } else if (strcasecmp(field, "Status") == 0) {
844 char *pflag = pkg_state_flag_to_str(pkg->state_flag);
845 fprintf(fp, "Status: %s %s %s\n",
846 pkg_state_want_to_str(pkg->state_want),
848 pkg_state_status_to_str(pkg->state_status));
850 } else if (strcasecmp(field, "Suggests") == 0) {
851 dep = pkg_get_depends(pkg, SUGGEST);
853 fprintf(fp, "Suggests:");
854 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
855 if (dep->type != SUGGEST)
857 str = pkg_depend_str(pkg, i);
858 fprintf(fp, "%s %s", j == 0 ? "" : ",",
866 goto UNKNOWN_FMT_FIELD;
871 if (strcasecmp(field, "Tags") == 0) {
872 p = pkg_get_string(pkg, PKG_TAGS);
874 fprintf(fp, "Tags: %s\n", p);
881 char *version = pkg_version_str_alloc(pkg);
884 fprintf(fp, "Version: %s\n", version);
889 goto UNKNOWN_FMT_FIELD;
895 opkg_msg(ERROR, "Internal error: field=%s\n", field);
898 void pkg_formatted_info(FILE * fp, pkg_t * pkg)
900 pkg_formatted_field(fp, pkg, "Package");
901 pkg_formatted_field(fp, pkg, "Version");
902 pkg_formatted_field(fp, pkg, "Depends");
903 pkg_formatted_field(fp, pkg, "Recommends");
904 pkg_formatted_field(fp, pkg, "Suggests");
905 pkg_formatted_field(fp, pkg, "Provides");
906 pkg_formatted_field(fp, pkg, "Replaces");
907 pkg_formatted_field(fp, pkg, "Conflicts");
908 pkg_formatted_field(fp, pkg, "Status");
909 pkg_formatted_field(fp, pkg, "Section");
910 pkg_formatted_field(fp, pkg, "Essential");
911 pkg_formatted_field(fp, pkg, "Architecture");
912 pkg_formatted_field(fp, pkg, "Maintainer");
913 pkg_formatted_field(fp, pkg, "MD5sum");
914 pkg_formatted_field(fp, pkg, "Size");
915 pkg_formatted_field(fp, pkg, "Filename");
916 pkg_formatted_field(fp, pkg, "Conffiles");
917 pkg_formatted_field(fp, pkg, "Source");
918 pkg_formatted_field(fp, pkg, "Description");
919 pkg_formatted_field(fp, pkg, "Installed-Time");
920 pkg_formatted_field(fp, pkg, "Tags");
924 void pkg_print_status(pkg_t * pkg, FILE * file)
930 pkg_formatted_field(file, pkg, "Package");
931 pkg_formatted_field(file, pkg, "Version");
932 pkg_formatted_field(file, pkg, "Depends");
933 pkg_formatted_field(file, pkg, "Recommends");
934 pkg_formatted_field(file, pkg, "Suggests");
935 pkg_formatted_field(file, pkg, "Provides");
936 pkg_formatted_field(file, pkg, "Replaces");
937 pkg_formatted_field(file, pkg, "Conflicts");
938 pkg_formatted_field(file, pkg, "Status");
939 pkg_formatted_field(file, pkg, "Essential");
940 pkg_formatted_field(file, pkg, "Architecture");
941 pkg_formatted_field(file, pkg, "Conffiles");
942 pkg_formatted_field(file, pkg, "Installed-Time");
943 pkg_formatted_field(file, pkg, "Auto-Installed");
948 * libdpkg - Debian packaging suite library routines
949 * vercmp.c - comparison of version numbers
951 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
954 /* assume ascii; warning: evaluates x multiple times! */
955 #define order(x) ((x) == '~' ? -1 \
958 : isalpha((x)) ? (x) \
961 static int verrevcmp(const char *val, const char *ref)
968 while (*val || *ref) {
971 while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) {
972 int vc = order(*val), rc = order(*ref);
983 while (isdigit(*val) && isdigit(*ref)) {
985 first_diff = *val - *ref;
999 int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg)
1001 unsigned int epoch1 = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1002 unsigned int epoch2 = (unsigned int) pkg_get_int(ref_pkg, PKG_EPOCH);
1003 char *revision1 = pkg_get_string(pkg, PKG_REVISION);
1004 char *revision2 = pkg_get_string(ref_pkg, PKG_REVISION);
1005 const char *version1 = pkg_get_string(pkg, PKG_VERSION);
1006 const char *version2 = pkg_get_string(ref_pkg, PKG_VERSION);
1009 if (epoch1 > epoch2) {
1013 if (epoch1 < epoch2) {
1017 r = verrevcmp(version1, version2);
1022 r = verrevcmp(revision1, revision2);
1030 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op)
1034 r = pkg_compare_versions(it, ref);
1036 if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
1040 if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
1044 if (strcmp(op, "<<") == 0) {
1048 if (strcmp(op, ">>") == 0) {
1052 if (strcmp(op, "=") == 0) {
1056 opkg_msg(ERROR, "Unknown operator: %s.\n", op);
1060 int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
1062 const pkg_t * a = *(const pkg_t **)p1;
1063 const pkg_t * b = *(const pkg_t **)p2;
1066 int arch_prio1, arch_prio2;
1067 if (!a->name || !b->name) {
1068 opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n",
1073 namecmp = strcmp(a->name, b->name);
1076 vercmp = pkg_compare_versions(a, b);
1079 arch_prio1 = pkg_get_arch_priority(a);
1080 arch_prio2 = pkg_get_arch_priority(b);
1081 if (!arch_prio1 || !arch_prio2) {
1083 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1084 arch_prio1, arch_prio2);
1087 if (arch_prio1 > arch_prio2)
1089 if (arch_prio1 < arch_prio2)
1094 int abstract_pkg_name_compare(const void *p1, const void *p2)
1096 const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
1097 const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
1098 if (!a->name || !b->name) {
1099 opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n",
1103 return strcmp(a->name, b->name);
1106 char *pkg_version_str_alloc(pkg_t * pkg)
1109 char *version, *revptr;
1110 unsigned int epoch = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1112 revptr = pkg_get_string(pkg, PKG_REVISION);
1113 verstr = pkg_get_string(pkg, PKG_VERSION);
1117 sprintf_alloc(&version, "%d:%s-%s",
1118 epoch, verstr, revptr);
1120 sprintf_alloc(&version, "%d:%s",
1124 sprintf_alloc(&version, "%s-%s",
1127 version = xstrdup(verstr);
1134 * XXX: this should be broken into two functions
1136 str_list_t *pkg_get_installed_files(pkg_t * pkg)
1139 char *list_file_name = NULL;
1140 FILE *list_file = NULL;
1142 char *installed_file_name;
1143 unsigned int rootdirlen = 0;
1144 int list_from_package;
1145 const char *local_filename;
1147 pkg->installed_files_ref_cnt++;
1149 if (pkg->installed_files) {
1150 return pkg->installed_files;
1153 pkg->installed_files = str_list_alloc();
1156 * For installed packages, look at the package.list file in the database.
1157 * For uninstalled packages, get the file list directly from the package.
1159 if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
1160 list_from_package = 1;
1162 list_from_package = 0;
1164 if (list_from_package) {
1165 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1167 if (!local_filename) {
1168 return pkg->installed_files;
1170 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1171 file. In other words, change deb_extract so that it can
1172 simply return the file list as a char *[] rather than
1173 insisting on writing it to a FILE * as it does now. */
1174 sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
1175 conf->tmp_dir, pkg->name);
1176 fd = mkstemp(list_file_name);
1178 opkg_perror(ERROR, "Failed to make temp file %s.",
1180 free(list_file_name);
1181 return pkg->installed_files;
1183 list_file = fdopen(fd, "r+");
1184 if (list_file == NULL) {
1185 opkg_perror(ERROR, "Failed to fdopen temp file %s.",
1188 unlink(list_file_name);
1189 free(list_file_name);
1190 return pkg->installed_files;
1192 err = pkg_extract_data_file_names_to_stream(pkg, list_file);
1194 opkg_msg(ERROR, "Error extracting file list from %s.\n",
1197 unlink(list_file_name);
1198 free(list_file_name);
1199 str_list_deinit(pkg->installed_files);
1200 pkg->installed_files = NULL;
1205 sprintf_alloc(&list_file_name, "%s/%s.list",
1206 pkg->dest->info_dir, pkg->name);
1207 list_file = fopen(list_file_name, "r");
1208 if (list_file == NULL) {
1209 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1210 free(list_file_name);
1211 return pkg->installed_files;
1213 free(list_file_name);
1216 if (conf->offline_root)
1217 rootdirlen = strlen(conf->offline_root);
1222 line = file_read_line_alloc(list_file);
1228 if (list_from_package) {
1229 if (*file_name == '.') {
1232 if (*file_name == '/') {
1235 sprintf_alloc(&installed_file_name, "%s%s",
1236 pkg->dest->root_dir, file_name);
1238 if (conf->offline_root &&
1239 strncmp(conf->offline_root, file_name,
1241 sprintf_alloc(&installed_file_name, "%s%s",
1242 conf->offline_root, file_name);
1244 // already contains root_dir as header -> ABSOLUTE
1245 sprintf_alloc(&installed_file_name, "%s",
1249 str_list_append(pkg->installed_files, installed_file_name);
1250 free(installed_file_name);
1256 if (list_from_package) {
1257 unlink(list_file_name);
1258 free(list_file_name);
1261 return pkg->installed_files;
1264 /* XXX: CLEANUP: This function and it's counterpart,
1265 (pkg_get_installed_files), do not match our init/deinit naming
1266 convention. Nor the alloc/free convention. But, then again, neither
1267 of these conventions currrently fit the way these two functions
1269 void pkg_free_installed_files(pkg_t * pkg)
1271 pkg->installed_files_ref_cnt--;
1273 if (pkg->installed_files_ref_cnt > 0)
1276 if (pkg->installed_files) {
1277 str_list_purge(pkg->installed_files);
1280 pkg->installed_files = NULL;
1283 void pkg_remove_installed_files_list(pkg_t * pkg)
1285 char *list_file_name;
1287 sprintf_alloc(&list_file_name, "%s/%s.list",
1288 pkg->dest->info_dir, pkg->name);
1290 if (!conf->noaction)
1291 (void)unlink(list_file_name);
1293 free(list_file_name);
1296 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name)
1298 conffile_list_elt_t *iter;
1299 conffile_list_t *cl;
1300 conffile_t *conffile;
1306 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
1308 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
1309 iter = nv_pair_list_next(cl, iter)) {
1310 conffile = (conffile_t *) iter->data;
1312 if (strcmp(conffile->name, file_name) == 0) {
1320 int pkg_run_script(pkg_t * pkg, const char *script, const char *args)
1325 char *tmp_unpack_dir;
1330 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1331 maintainer script within a chroot environment. */
1332 if (conf->offline_root && !conf->force_postinstall) {
1333 opkg_msg(INFO, "Offline root mode: not running %s.%s.\n",
1338 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1339 have scripts in tmp_unpack_dir. */
1340 if (pkg->state_status == SS_INSTALLED
1341 || pkg->state_status == SS_UNPACKED) {
1342 if (pkg->dest == NULL) {
1343 opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n",
1347 sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name,
1350 tmp_unpack_dir = pkg_get_string(pkg, PKG_TMP_UNPACK_DIR);
1351 if (tmp_unpack_dir == NULL) {
1353 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1357 sprintf_alloc(&path, "%s/%s", tmp_unpack_dir, script);
1360 opkg_msg(INFO, "Running script %s.\n", path);
1363 pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir,
1366 if (pkg->is_upgrade)
1367 setenv("PKG_UPGRADE", "1", 1);
1369 setenv("PKG_UPGRADE", "0", 1);
1371 if (!file_exists(path)) {
1376 sprintf_alloc(&cmd, "%s %s", path, args);
1379 const char *argv[] = { "sh", "-c", cmd, NULL };
1380 err = xsystem(argv);
1386 "package \"%s\" %s script returned status %d.\n",
1387 pkg->name, script, err);
1394 int pkg_arch_supported(pkg_t * pkg)
1396 nv_pair_list_elt_t *l;
1397 char *architecture = pkg_get_architecture(pkg);
1402 list_for_each_entry(l, &conf->arch_list.head, node) {
1403 nv_pair_t *nv = (nv_pair_t *) l->data;
1404 if (strcmp(nv->name, architecture) == 0) {
1406 "Arch %s (priority %s) supported for pkg %s.\n",
1407 nv->name, nv->value, pkg->name);
1412 opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n",
1413 architecture, pkg->name);
1417 void pkg_info_preinstall_check(void)
1420 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1422 /* update the file owner data structure */
1423 opkg_msg(INFO, "Updating file owner list.\n");
1424 pkg_hash_fetch_all_installed(installed_pkgs);
1425 for (i = 0; i < installed_pkgs->len; i++) {
1426 pkg_t *pkg = installed_pkgs->pkgs[i];
1427 str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
1428 str_list_elt_t *iter, *niter;
1429 if (installed_files == NULL) {
1430 opkg_msg(ERROR, "Failed to determine installed "
1431 "files for pkg %s.\n", pkg->name);
1434 for (iter = str_list_first(installed_files), niter =
1435 str_list_next(installed_files, iter); iter;
1436 iter = niter, niter =
1437 str_list_next(installed_files, iter)) {
1438 char *installed_file = (char *)iter->data;
1439 file_hash_set_file_owner(installed_file, pkg);
1441 pkg_free_installed_files(pkg);
1443 pkg_vec_free(installed_pkgs);
1446 struct pkg_write_filelist_data {
1452 pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
1454 struct pkg_write_filelist_data *data = data_;
1455 pkg_t *entry = entry_;
1456 if (entry == data->pkg) {
1457 fprintf(data->stream, "%s\n", key);
1461 int pkg_write_filelist(pkg_t * pkg)
1463 struct pkg_write_filelist_data data;
1464 char *list_file_name;
1466 sprintf_alloc(&list_file_name, "%s/%s.list",
1467 pkg->dest->info_dir, pkg->name);
1469 opkg_msg(INFO, "Creating %s file for pkg %s.\n",
1470 list_file_name, pkg->name);
1472 data.stream = fopen(list_file_name, "w");
1474 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1475 free(list_file_name);
1480 hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
1481 fclose(data.stream);
1482 free(list_file_name);
1484 pkg->state_flag &= ~SF_FILELIST_CHANGED;
1489 int pkg_write_changed_filelists(void)
1491 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1492 int i, err, ret = 0;
1497 opkg_msg(INFO, "Saving changed filelists.\n");
1499 pkg_hash_fetch_all_installed(installed_pkgs);
1500 for (i = 0; i < installed_pkgs->len; i++) {
1501 pkg_t *pkg = installed_pkgs->pkgs[i];
1502 if (pkg->state_flag & SF_FILELIST_CHANGED) {
1503 err = pkg_write_filelist(pkg);
1509 pkg_vec_free(installed_pkgs);