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);
216 static void compound_depend_deinit(compound_depend_t * depends)
219 for (i = 0; i < depends->possibility_count; i++) {
221 d = depends->possibilities[i];
225 free(depends->possibilities);
228 void pkg_deinit(pkg_t * pkg)
230 compound_depend_t *deps, *dep;
236 /* owned by opkg_conf_t */
238 /* owned by opkg_conf_t */
241 pkg->state_want = SW_UNKNOWN;
242 pkg->state_flag = SF_OK;
243 pkg->state_status = SS_NOT_INSTALLED;
245 deps = pkg_get_ptr(pkg, PKG_DEPENDS);
248 for (dep = deps; dep->type; dep++)
249 compound_depend_deinit(dep);
252 pkg_set_ptr(pkg, PKG_DEPENDS, NULL);
255 deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
258 for (dep = deps; dep->type; dep++)
259 compound_depend_deinit(dep);
262 pkg_set_ptr(pkg, PKG_CONFLICTS, NULL);
265 //conffile_list_deinit(&pkg->conffiles);
267 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
268 since if they are calling deinit, they should know. Maybe do an
269 assertion here instead? */
270 pkg->installed_files_ref_cnt = 1;
271 pkg_free_installed_files(pkg);
274 //blob_buf_free(&pkg->blob);
277 int pkg_init_from_file(pkg_t * pkg, const char *filename)
281 char *control_path, *tmp;
285 pkg_set_string(pkg, PKG_LOCAL_FILENAME, filename);
287 tmp = xstrdup(filename);
288 sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
289 conf->tmp_dir, basename(tmp));
291 fd = mkstemp(control_path);
293 opkg_perror(ERROR, "Failed to make temp file %s", control_path);
298 control_file = fdopen(fd, "r+");
299 if (control_file == NULL) {
300 opkg_perror(ERROR, "Failed to fdopen %s", control_path);
306 err = pkg_extract_control_file_to_stream(pkg, control_file);
308 opkg_msg(ERROR, "Failed to extract control file from %s.\n",
313 rewind(control_file);
315 if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
317 opkg_msg(ERROR, "Malformed package file %s.\n",
324 fclose(control_file);
326 unlink(control_path);
333 /* Merge any new information in newpkg into oldpkg */
334 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg)
337 conffile_list_t *cf, head;
339 if (oldpkg == newpkg) {
343 if (!oldpkg->auto_installed)
344 oldpkg->auto_installed = newpkg->auto_installed;
347 oldpkg->src = newpkg->src;
349 oldpkg->dest = newpkg->dest;
350 if (!oldpkg->arch_index)
351 oldpkg->arch_index = newpkg->arch_index;
352 if (!pkg_get_string(oldpkg, PKG_SECTION))
353 pkg_set_string(oldpkg, PKG_SECTION, pkg_get_string(newpkg, PKG_SECTION));
354 if (!pkg_get_string(oldpkg, PKG_MAINTAINER))
355 pkg_set_string(oldpkg, PKG_MAINTAINER, pkg_get_string(newpkg, PKG_MAINTAINER));
356 if (!pkg_get_string(oldpkg, PKG_DESCRIPTION))
357 pkg_set_string(oldpkg, PKG_DESCRIPTION, pkg_get_string(newpkg, PKG_DESCRIPTION));
359 if (!pkg_get_ptr(oldpkg, PKG_DEPENDS)) {
360 pkg_set_ptr(oldpkg, PKG_DEPENDS, pkg_get_ptr(newpkg, PKG_DEPENDS));
361 pkg_set_ptr(newpkg, PKG_DEPENDS, NULL);
364 ab = pkg_get_ptr(oldpkg, PKG_PROVIDES);
366 if (!ab || !ab[0] || !ab[1]) {
367 pkg_set_ptr(oldpkg, PKG_PROVIDES, pkg_get_ptr(newpkg, PKG_PROVIDES));
368 pkg_set_ptr(newpkg, PKG_PROVIDES, NULL);
371 if (!pkg_get_ptr(oldpkg, PKG_CONFLICTS)) {
372 pkg_set_ptr(oldpkg, PKG_CONFLICTS, pkg_get_ptr(newpkg, PKG_CONFLICTS));
373 pkg_set_ptr(newpkg, PKG_CONFLICTS, NULL);
376 if (!pkg_get_ptr(oldpkg, PKG_REPLACES)) {
377 pkg_set_ptr(oldpkg, PKG_REPLACES, pkg_get_ptr(newpkg, PKG_REPLACES));
378 pkg_set_ptr(newpkg, PKG_REPLACES, NULL);
381 if (!pkg_get_string(oldpkg, PKG_FILENAME))
382 pkg_set_string(oldpkg, PKG_FILENAME, pkg_get_string(newpkg, PKG_FILENAME));
383 if (!pkg_get_string(oldpkg, PKG_LOCAL_FILENAME))
384 pkg_set_string(oldpkg, PKG_LOCAL_FILENAME, pkg_get_string(newpkg, PKG_LOCAL_FILENAME));
385 if (!pkg_get_string(oldpkg, PKG_TMP_UNPACK_DIR))
386 pkg_set_string(oldpkg, PKG_TMP_UNPACK_DIR, pkg_get_string(newpkg, PKG_TMP_UNPACK_DIR));
387 if (!pkg_get_string(oldpkg, PKG_MD5SUM))
388 pkg_set_string(oldpkg, PKG_MD5SUM, pkg_get_string(newpkg, PKG_MD5SUM));
389 if (!pkg_get_string(oldpkg, PKG_SHA256SUM))
390 pkg_set_string(oldpkg, PKG_SHA256SUM, pkg_get_string(newpkg, PKG_SHA256SUM));
391 if (!pkg_get_int(oldpkg, PKG_SIZE))
392 pkg_set_int(oldpkg, PKG_SIZE, pkg_get_int(newpkg, PKG_SIZE));
393 if (!pkg_get_int(oldpkg, PKG_INSTALLED_SIZE))
394 pkg_set_int(oldpkg, PKG_INSTALLED_SIZE, pkg_get_int(newpkg, PKG_INSTALLED_SIZE));
395 if (!pkg_get_string(oldpkg, PKG_PRIORITY))
396 pkg_set_string(oldpkg, PKG_PRIORITY, pkg_get_string(newpkg, PKG_PRIORITY));
397 if (!pkg_get_string(oldpkg, PKG_SOURCE))
398 pkg_set_string(oldpkg, PKG_SOURCE, pkg_get_string(newpkg, PKG_SOURCE));
400 if (!pkg_get_ptr(oldpkg, PKG_CONFFILES)) {
401 cf = pkg_get_ptr(newpkg, PKG_CONFFILES);
403 conffile_list_init(&head);
404 list_splice_init(&cf->head, &head.head);
405 pkg_set_raw(oldpkg, PKG_CONFFILES, &head, sizeof(head));
409 if (!oldpkg->installed_files) {
410 oldpkg->installed_files = newpkg->installed_files;
411 oldpkg->installed_files_ref_cnt =
412 newpkg->installed_files_ref_cnt;
413 newpkg->installed_files = NULL;
416 if (!oldpkg->essential)
417 oldpkg->essential = newpkg->essential;
422 static void abstract_pkg_init(abstract_pkg_t * ab_pkg)
424 ab_pkg->provided_by = abstract_pkg_vec_alloc();
425 ab_pkg->dependencies_checked = 0;
426 ab_pkg->state_status = SS_NOT_INSTALLED;
429 abstract_pkg_t *abstract_pkg_new(void)
431 abstract_pkg_t *ab_pkg;
433 ab_pkg = xcalloc(1, sizeof(abstract_pkg_t));
434 abstract_pkg_init(ab_pkg);
439 void set_flags_from_control(pkg_t * pkg)
444 sprintf_alloc(&file_name, "%s/%s.control", pkg->dest->info_dir,
447 fp = fopen(file_name, "r");
449 opkg_perror(ERROR, "Failed to open %s", file_name);
456 if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) {
458 "Unable to read control file for %s. May be empty.\n",
467 static const char *pkg_state_want_to_str(pkg_state_want_t sw)
471 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
472 if (pkg_state_want_map[i].value == sw) {
473 return pkg_state_want_map[i].str;
477 opkg_msg(ERROR, "Internal error: state_want=%d\n", sw);
478 return "<STATE_WANT_UNKNOWN>";
481 pkg_state_want_t pkg_state_want_from_str(char *str)
485 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
486 if (strcmp(str, pkg_state_want_map[i].str) == 0) {
487 return pkg_state_want_map[i].value;
491 opkg_msg(ERROR, "Internal error: state_want=%s\n", str);
495 static char *pkg_state_flag_to_str(pkg_state_flag_t sf)
501 /* clear the temporary flags before converting to string */
502 sf &= SF_NONVOLATILE_FLAGS;
505 return xstrdup("ok");
508 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
509 if (sf & pkg_state_flag_map[i].value)
510 len += strlen(pkg_state_flag_map[i].str) + 1;
513 str = xmalloc(len + 1);
516 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
517 if (sf & pkg_state_flag_map[i].value) {
518 strncat(str, pkg_state_flag_map[i].str, len);
519 strncat(str, ",", len);
524 str[len - 1] = '\0'; /* squash last comma */
529 pkg_state_flag_t pkg_state_flag_from_str(const char *str)
534 unsigned int sfname_len;
536 if (strcmp(str, "ok") == 0) {
539 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
540 sfname = pkg_state_flag_map[i].str;
541 sfname_len = strlen(sfname);
542 if (strncmp(str, sfname, sfname_len) == 0) {
543 sf |= pkg_state_flag_map[i].value;
556 static const char *pkg_state_status_to_str(pkg_state_status_t ss)
560 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
561 if (pkg_state_status_map[i].value == ss) {
562 return pkg_state_status_map[i].str;
566 opkg_msg(ERROR, "Internal error: state_status=%d\n", ss);
567 return "<STATE_STATUS_UNKNOWN>";
570 pkg_state_status_t pkg_state_status_from_str(const char *str)
574 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
575 if (strcmp(str, pkg_state_status_map[i].str) == 0) {
576 return pkg_state_status_map[i].value;
580 opkg_msg(ERROR, "Internal error: state_status=%s\n", str);
581 return SS_NOT_INSTALLED;
584 void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field)
589 compound_depend_t *dep;
590 abstract_pkg_t **ab_pkg;
592 if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
593 goto UNKNOWN_FMT_FIELD;
599 if (strcasecmp(field, "Architecture") == 0) {
600 p = pkg_get_architecture(pkg);
602 fprintf(fp, "Architecture: %s\n",
605 } else if (strcasecmp(field, "Auto-Installed") == 0) {
606 if (pkg->auto_installed)
607 fprintf(fp, "Auto-Installed: yes\n");
609 goto UNKNOWN_FMT_FIELD;
614 if (strcasecmp(field, "Conffiles") == 0) {
616 conffile_list_elt_t *iter;
618 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
620 if (!cl || nv_pair_list_empty(cl))
623 fprintf(fp, "Conffiles:\n");
624 for (iter = nv_pair_list_first(cl); iter;
625 iter = nv_pair_list_next(cl, iter)) {
626 if (((conffile_t *) iter->data)->name
627 && ((conffile_t *) iter->data)->value) {
628 fprintf(fp, " %s %s\n",
629 ((conffile_t *) iter->data)->
631 ((conffile_t *) iter->data)->
635 } else if (strcasecmp(field, "Conflicts") == 0) {
637 compound_depend_t *deps, *dep;
638 deps = pkg_get_ptr(pkg, PKG_CONFLICTS);
640 fprintf(fp, "Conflicts:");
641 for (i = 0, dep = deps; dep->type; dep++, i++) {
642 cdep = dep->possibilities[0];
643 fprintf(fp, "%s %s", i == 0 ? "" : ",",
646 fprintf(fp, " (%s%s)",
647 constraint_to_str(cdep->
655 goto UNKNOWN_FMT_FIELD;
660 if (strcasecmp(field, "Depends") == 0) {
661 dep = pkg_get_depends(pkg, DEPEND);
663 fprintf(fp, "Depends:");
664 for (i = 0, j = 0; dep && dep->type; i++, dep++) {
665 if (dep->type != DEPEND)
667 str = pkg_depend_str(pkg, i);
668 fprintf(fp, "%s %s", j == 0 ? "" : ",",
675 } else if (strcasecmp(field, "Description") == 0) {
676 p = pkg_get_string(pkg, PKG_DESCRIPTION);
678 fprintf(fp, "Description: %s\n",
682 goto UNKNOWN_FMT_FIELD;
687 if (pkg->essential) {
688 fprintf(fp, "Essential: yes\n");
693 p = pkg_get_string(pkg, PKG_FILENAME);
695 fprintf(fp, "Filename: %s\n", p);
700 if (strcasecmp(field, "Installed-Size") == 0) {
701 fprintf(fp, "Installed-Size: %lu\n",
702 (unsigned long) pkg_get_int(pkg, PKG_INSTALLED_SIZE));
703 } else if (strcasecmp(field, "Installed-Time") == 0) {
704 i = pkg_get_int(pkg, PKG_INSTALLED_TIME);
706 fprintf(fp, "Installed-Time: %lu\n",
713 if (strcasecmp(field, "Maintainer") == 0) {
714 p = pkg_get_string(pkg, PKG_MAINTAINER);
716 fprintf(fp, "Maintainer: %s\n", p);
718 } else if (strcasecmp(field, "MD5sum") == 0) {
719 p = pkg_get_string(pkg, PKG_MD5SUM);
721 fprintf(fp, "MD5Sum: %s\n", p);
724 goto UNKNOWN_FMT_FIELD;
729 if (strcasecmp(field, "Package") == 0) {
730 fprintf(fp, "Package: %s\n", pkg->name);
731 } else if (strcasecmp(field, "Priority") == 0) {
732 fprintf(fp, "Priority: %s\n", pkg_get_string(pkg, PKG_PRIORITY));
733 } else if (strcasecmp(field, "Provides") == 0) {
734 ab_pkg = pkg_get_ptr(pkg, PKG_PROVIDES);
735 if (ab_pkg && ab_pkg[0] && ab_pkg[1]) {
736 fprintf(fp, "Provides:");
737 for (i = 0, ab_pkg++; *ab_pkg; i++, ab_pkg++) {
738 fprintf(fp, "%s %s", i == 0 ? "" : ",",
745 goto UNKNOWN_FMT_FIELD;
750 if (strcasecmp(field, "Replaces") == 0) {
751 ab_pkg = pkg_get_ptr(pkg, PKG_REPLACES);
752 if (ab_pkg && *ab_pkg) {
753 fprintf(fp, "Replaces:");
754 for (i = 0; *ab_pkg; i++, ab_pkg++) {
755 fprintf(fp, "%s %s", i == 0 ? "" : ",",
760 } else if (strcasecmp(field, "Recommends") == 0) {
761 dep = pkg_get_depends(pkg, RECOMMEND);
763 fprintf(fp, "Recommends:");
764 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
765 if (dep->type != RECOMMEND)
767 str = pkg_depend_str(pkg, i);
768 fprintf(fp, "%s %s", j == 0 ? "" : ",",
776 goto UNKNOWN_FMT_FIELD;
781 if (strcasecmp(field, "Section") == 0) {
782 p = pkg_get_string(pkg, PKG_SECTION);
784 fprintf(fp, "Section: %s\n", p);
786 #if defined HAVE_SHA256
787 } else if (strcasecmp(field, "SHA256sum") == 0) {
788 p = pkg_get_string(pkg, PKG_SHA256SUM);
790 fprintf(fp, "SHA256sum: %s\n", p);
793 } else if (strcasecmp(field, "Size") == 0) {
794 i = pkg_get_int(pkg, PKG_SIZE);
796 fprintf(fp, "Size: %lu\n", (unsigned long) i);
798 } else if (strcasecmp(field, "Source") == 0) {
799 p = pkg_get_string(pkg, PKG_SOURCE);
801 fprintf(fp, "Source: %s\n", p);
803 } else if (strcasecmp(field, "Status") == 0) {
804 char *pflag = pkg_state_flag_to_str(pkg->state_flag);
805 fprintf(fp, "Status: %s %s %s\n",
806 pkg_state_want_to_str(pkg->state_want),
808 pkg_state_status_to_str(pkg->state_status));
810 } else if (strcasecmp(field, "Suggests") == 0) {
811 dep = pkg_get_depends(pkg, SUGGEST);
813 fprintf(fp, "Suggests:");
814 for (j = 0, i = 0; dep && dep->type; i++, dep++) {
815 if (dep->type != SUGGEST)
817 str = pkg_depend_str(pkg, i);
818 fprintf(fp, "%s %s", j == 0 ? "" : ",",
826 goto UNKNOWN_FMT_FIELD;
831 if (strcasecmp(field, "Tags") == 0) {
832 p = pkg_get_string(pkg, PKG_TAGS);
834 fprintf(fp, "Tags: %s\n", p);
841 char *version = pkg_version_str_alloc(pkg);
844 fprintf(fp, "Version: %s\n", version);
849 goto UNKNOWN_FMT_FIELD;
855 opkg_msg(ERROR, "Internal error: field=%s\n", field);
858 void pkg_formatted_info(FILE * fp, pkg_t * pkg)
860 pkg_formatted_field(fp, pkg, "Package");
861 pkg_formatted_field(fp, pkg, "Version");
862 pkg_formatted_field(fp, pkg, "Depends");
863 pkg_formatted_field(fp, pkg, "Recommends");
864 pkg_formatted_field(fp, pkg, "Suggests");
865 pkg_formatted_field(fp, pkg, "Provides");
866 pkg_formatted_field(fp, pkg, "Replaces");
867 pkg_formatted_field(fp, pkg, "Conflicts");
868 pkg_formatted_field(fp, pkg, "Status");
869 pkg_formatted_field(fp, pkg, "Section");
870 pkg_formatted_field(fp, pkg, "Essential");
871 pkg_formatted_field(fp, pkg, "Architecture");
872 pkg_formatted_field(fp, pkg, "Maintainer");
873 pkg_formatted_field(fp, pkg, "MD5sum");
874 pkg_formatted_field(fp, pkg, "Size");
875 pkg_formatted_field(fp, pkg, "Filename");
876 pkg_formatted_field(fp, pkg, "Conffiles");
877 pkg_formatted_field(fp, pkg, "Source");
878 pkg_formatted_field(fp, pkg, "Description");
879 pkg_formatted_field(fp, pkg, "Installed-Time");
880 pkg_formatted_field(fp, pkg, "Tags");
884 void pkg_print_status(pkg_t * pkg, FILE * file)
890 pkg_formatted_field(file, pkg, "Package");
891 pkg_formatted_field(file, pkg, "Version");
892 pkg_formatted_field(file, pkg, "Depends");
893 pkg_formatted_field(file, pkg, "Recommends");
894 pkg_formatted_field(file, pkg, "Suggests");
895 pkg_formatted_field(file, pkg, "Provides");
896 pkg_formatted_field(file, pkg, "Replaces");
897 pkg_formatted_field(file, pkg, "Conflicts");
898 pkg_formatted_field(file, pkg, "Status");
899 pkg_formatted_field(file, pkg, "Essential");
900 pkg_formatted_field(file, pkg, "Architecture");
901 pkg_formatted_field(file, pkg, "Conffiles");
902 pkg_formatted_field(file, pkg, "Installed-Time");
903 pkg_formatted_field(file, pkg, "Auto-Installed");
908 * libdpkg - Debian packaging suite library routines
909 * vercmp.c - comparison of version numbers
911 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
914 /* assume ascii; warning: evaluates x multiple times! */
915 #define order(x) ((x) == '~' ? -1 \
918 : isalpha((x)) ? (x) \
921 static int verrevcmp(const char *val, const char *ref)
928 while (*val || *ref) {
931 while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) {
932 int vc = order(*val), rc = order(*ref);
943 while (isdigit(*val) && isdigit(*ref)) {
945 first_diff = *val - *ref;
959 int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg)
961 unsigned int epoch1 = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
962 unsigned int epoch2 = (unsigned int) pkg_get_int(ref_pkg, PKG_EPOCH);
963 char *revision1 = pkg_get_string(pkg, PKG_REVISION);
964 char *revision2 = pkg_get_string(ref_pkg, PKG_REVISION);
965 const char *version1 = pkg_get_string(pkg, PKG_VERSION);
966 const char *version2 = pkg_get_string(ref_pkg, PKG_VERSION);
969 if (epoch1 > epoch2) {
973 if (epoch1 < epoch2) {
977 r = verrevcmp(version1, version2);
982 r = verrevcmp(revision1, revision2);
990 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op)
994 r = pkg_compare_versions(it, ref);
996 if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
1000 if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
1004 if (strcmp(op, "<<") == 0) {
1008 if (strcmp(op, ">>") == 0) {
1012 if (strcmp(op, "=") == 0) {
1016 opkg_msg(ERROR, "Unknown operator: %s.\n", op);
1020 int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
1022 const pkg_t * a = *(const pkg_t **)p1;
1023 const pkg_t * b = *(const pkg_t **)p2;
1026 int arch_prio1, arch_prio2;
1027 if (!a->name || !b->name) {
1028 opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n",
1033 namecmp = strcmp(a->name, b->name);
1036 vercmp = pkg_compare_versions(a, b);
1039 arch_prio1 = pkg_get_arch_priority(a);
1040 arch_prio2 = pkg_get_arch_priority(b);
1041 if (!arch_prio1 || !arch_prio2) {
1043 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1044 arch_prio1, arch_prio2);
1047 if (arch_prio1 > arch_prio2)
1049 if (arch_prio1 < arch_prio2)
1054 int abstract_pkg_name_compare(const void *p1, const void *p2)
1056 const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
1057 const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
1058 if (!a->name || !b->name) {
1059 opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n",
1063 return strcmp(a->name, b->name);
1066 char *pkg_version_str_alloc(pkg_t * pkg)
1069 char *version, *revptr;
1070 unsigned int epoch = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1072 revptr = pkg_get_string(pkg, PKG_REVISION);
1073 verstr = pkg_get_string(pkg, PKG_VERSION);
1077 sprintf_alloc(&version, "%d:%s-%s",
1078 epoch, verstr, revptr);
1080 sprintf_alloc(&version, "%d:%s",
1084 sprintf_alloc(&version, "%s-%s",
1087 version = xstrdup(verstr);
1094 * XXX: this should be broken into two functions
1096 str_list_t *pkg_get_installed_files(pkg_t * pkg)
1099 char *list_file_name = NULL;
1100 FILE *list_file = NULL;
1102 char *installed_file_name;
1103 unsigned int rootdirlen = 0;
1104 int list_from_package;
1105 const char *local_filename;
1107 pkg->installed_files_ref_cnt++;
1109 if (pkg->installed_files) {
1110 return pkg->installed_files;
1113 pkg->installed_files = str_list_alloc();
1116 * For installed packages, look at the package.list file in the database.
1117 * For uninstalled packages, get the file list directly from the package.
1119 if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
1120 list_from_package = 1;
1122 list_from_package = 0;
1124 if (list_from_package) {
1125 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1127 if (!local_filename) {
1128 return pkg->installed_files;
1130 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1131 file. In other words, change deb_extract so that it can
1132 simply return the file list as a char *[] rather than
1133 insisting on writing it to a FILE * as it does now. */
1134 sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
1135 conf->tmp_dir, pkg->name);
1136 fd = mkstemp(list_file_name);
1138 opkg_perror(ERROR, "Failed to make temp file %s.",
1140 free(list_file_name);
1141 return pkg->installed_files;
1143 list_file = fdopen(fd, "r+");
1144 if (list_file == NULL) {
1145 opkg_perror(ERROR, "Failed to fdopen temp file %s.",
1148 unlink(list_file_name);
1149 free(list_file_name);
1150 return pkg->installed_files;
1152 err = pkg_extract_data_file_names_to_stream(pkg, list_file);
1154 opkg_msg(ERROR, "Error extracting file list from %s.\n",
1157 unlink(list_file_name);
1158 free(list_file_name);
1159 str_list_deinit(pkg->installed_files);
1160 pkg->installed_files = NULL;
1165 sprintf_alloc(&list_file_name, "%s/%s.list",
1166 pkg->dest->info_dir, pkg->name);
1167 list_file = fopen(list_file_name, "r");
1168 if (list_file == NULL) {
1169 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1170 free(list_file_name);
1171 return pkg->installed_files;
1173 free(list_file_name);
1176 if (conf->offline_root)
1177 rootdirlen = strlen(conf->offline_root);
1182 line = file_read_line_alloc(list_file);
1188 if (list_from_package) {
1189 if (*file_name == '.') {
1192 if (*file_name == '/') {
1195 sprintf_alloc(&installed_file_name, "%s%s",
1196 pkg->dest->root_dir, file_name);
1198 if (conf->offline_root &&
1199 strncmp(conf->offline_root, file_name,
1201 sprintf_alloc(&installed_file_name, "%s%s",
1202 conf->offline_root, file_name);
1204 // already contains root_dir as header -> ABSOLUTE
1205 sprintf_alloc(&installed_file_name, "%s",
1209 str_list_append(pkg->installed_files, installed_file_name);
1210 free(installed_file_name);
1216 if (list_from_package) {
1217 unlink(list_file_name);
1218 free(list_file_name);
1221 return pkg->installed_files;
1224 /* XXX: CLEANUP: This function and it's counterpart,
1225 (pkg_get_installed_files), do not match our init/deinit naming
1226 convention. Nor the alloc/free convention. But, then again, neither
1227 of these conventions currrently fit the way these two functions
1229 void pkg_free_installed_files(pkg_t * pkg)
1231 pkg->installed_files_ref_cnt--;
1233 if (pkg->installed_files_ref_cnt > 0)
1236 if (pkg->installed_files) {
1237 str_list_purge(pkg->installed_files);
1240 pkg->installed_files = NULL;
1243 void pkg_remove_installed_files_list(pkg_t * pkg)
1245 char *list_file_name;
1247 sprintf_alloc(&list_file_name, "%s/%s.list",
1248 pkg->dest->info_dir, pkg->name);
1250 if (!conf->noaction)
1251 (void)unlink(list_file_name);
1253 free(list_file_name);
1256 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name)
1258 conffile_list_elt_t *iter;
1259 conffile_list_t *cl;
1260 conffile_t *conffile;
1266 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
1268 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
1269 iter = nv_pair_list_next(cl, iter)) {
1270 conffile = (conffile_t *) iter->data;
1272 if (strcmp(conffile->name, file_name) == 0) {
1280 int pkg_run_script(pkg_t * pkg, const char *script, const char *args)
1285 char *tmp_unpack_dir;
1290 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1291 maintainer script within a chroot environment. */
1292 if (conf->offline_root && !conf->force_postinstall) {
1293 opkg_msg(INFO, "Offline root mode: not running %s.%s.\n",
1298 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1299 have scripts in tmp_unpack_dir. */
1300 if (pkg->state_status == SS_INSTALLED
1301 || pkg->state_status == SS_UNPACKED) {
1302 if (pkg->dest == NULL) {
1303 opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n",
1307 sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name,
1310 tmp_unpack_dir = pkg_get_string(pkg, PKG_TMP_UNPACK_DIR);
1311 if (tmp_unpack_dir == NULL) {
1313 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1317 sprintf_alloc(&path, "%s/%s", tmp_unpack_dir, script);
1320 opkg_msg(INFO, "Running script %s.\n", path);
1323 pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir,
1326 if (pkg->is_upgrade)
1327 setenv("PKG_UPGRADE", "1", 1);
1329 setenv("PKG_UPGRADE", "0", 1);
1331 if (!file_exists(path)) {
1336 sprintf_alloc(&cmd, "%s %s", path, args);
1339 const char *argv[] = { "sh", "-c", cmd, NULL };
1340 err = xsystem(argv);
1346 "package \"%s\" %s script returned status %d.\n",
1347 pkg->name, script, err);
1354 int pkg_arch_supported(pkg_t * pkg)
1356 nv_pair_list_elt_t *l;
1357 char *architecture = pkg_get_architecture(pkg);
1362 list_for_each_entry(l, &conf->arch_list.head, node) {
1363 nv_pair_t *nv = (nv_pair_t *) l->data;
1364 if (strcmp(nv->name, architecture) == 0) {
1366 "Arch %s (priority %s) supported for pkg %s.\n",
1367 nv->name, nv->value, pkg->name);
1372 opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n",
1373 architecture, pkg->name);
1377 void pkg_info_preinstall_check(void)
1380 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1382 /* update the file owner data structure */
1383 opkg_msg(INFO, "Updating file owner list.\n");
1384 pkg_hash_fetch_all_installed(installed_pkgs);
1385 for (i = 0; i < installed_pkgs->len; i++) {
1386 pkg_t *pkg = installed_pkgs->pkgs[i];
1387 str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
1388 str_list_elt_t *iter, *niter;
1389 if (installed_files == NULL) {
1390 opkg_msg(ERROR, "Failed to determine installed "
1391 "files for pkg %s.\n", pkg->name);
1394 for (iter = str_list_first(installed_files), niter =
1395 str_list_next(installed_files, iter); iter;
1396 iter = niter, niter =
1397 str_list_next(installed_files, iter)) {
1398 char *installed_file = (char *)iter->data;
1399 file_hash_set_file_owner(installed_file, pkg);
1401 pkg_free_installed_files(pkg);
1403 pkg_vec_free(installed_pkgs);
1406 struct pkg_write_filelist_data {
1412 pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
1414 struct pkg_write_filelist_data *data = data_;
1415 pkg_t *entry = entry_;
1416 if (entry == data->pkg) {
1417 fprintf(data->stream, "%s\n", key);
1421 int pkg_write_filelist(pkg_t * pkg)
1423 struct pkg_write_filelist_data data;
1424 char *list_file_name;
1426 sprintf_alloc(&list_file_name, "%s/%s.list",
1427 pkg->dest->info_dir, pkg->name);
1429 opkg_msg(INFO, "Creating %s file for pkg %s.\n",
1430 list_file_name, pkg->name);
1432 data.stream = fopen(list_file_name, "w");
1434 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1435 free(list_file_name);
1440 hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
1441 fclose(data.stream);
1442 free(list_file_name);
1444 pkg->state_flag &= ~SF_FILELIST_CHANGED;
1449 int pkg_write_changed_filelists(void)
1451 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1452 int i, err, ret = 0;
1457 opkg_msg(INFO, "Saving changed filelists.\n");
1459 pkg_hash_fetch_all_installed(installed_pkgs);
1460 for (i = 0; i < installed_pkgs->len; i++) {
1461 pkg_t *pkg = installed_pkgs->pkgs[i];
1462 if (pkg->state_flag & SF_FILELIST_CHANGED) {
1463 err = pkg_write_filelist(pkg);
1469 pkg_vec_free(installed_pkgs);