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;
82 pkg->depends_str = NULL;
83 pkg->provides_str = NULL;
84 pkg->depends_count = 0;
86 pkg->suggests_str = NULL;
87 pkg->recommends_str = NULL;
88 pkg->suggests_count = 0;
89 pkg->recommends_count = 0;
91 active_list_init(&pkg->list);
93 pkg->conflicts = NULL;
94 pkg->conflicts_count = 0;
97 pkg->replaces_count = 0;
99 pkg->pre_depends_count = 0;
100 pkg->pre_depends_str = NULL;
101 pkg->provides_count = 0;
102 pkg->provides = NULL;
103 pkg->tmp_unpack_dir = NULL;
105 pkg->installed_size = 0;
106 conffile_list_init(&pkg->conffiles);
107 pkg->installed_files = NULL;
108 pkg->installed_files_ref_cnt = 0;
110 pkg->provided_by_hand = 0;
112 blob_buf_init(&pkg->blob, 0);
119 pkg = xcalloc(1, sizeof(pkg_t));
125 void *pkg_set_raw(pkg_t *pkg, int id, const void *val, size_t len)
128 struct blob_attr *cur;
130 blob_for_each_attr(cur, pkg->blob.head, rem) {
131 if (blob_id(cur) == id) {
132 if (blob_len(cur) < len) {
133 fprintf(stderr, "ERROR: truncating field %d <%p> to %d byte",
134 id, val, blob_len(cur));
136 memcpy(blob_data(cur), val, blob_len(cur));
137 return blob_data(cur);
141 cur = blob_put(&pkg->blob, id, val, len);
142 return cur ? blob_data(cur) : NULL;
145 void *pkg_get_raw(const pkg_t * pkg, int id)
148 struct blob_attr *cur;
150 blob_for_each_attr(cur, pkg->blob.head, rem)
151 if (blob_id(cur) == id)
152 return blob_data(cur);
157 char *pkg_set_string(pkg_t *pkg, int id, const char *s)
167 while (isspace(*s)) {
172 while (len > 0 && isspace(s[len - 1]))
178 p = pkg_set_raw(pkg, id, s, len + 1);
185 static void compound_depend_deinit(compound_depend_t * depends)
188 for (i = 0; i < depends->possibility_count; i++) {
190 d = depends->possibilities[i];
194 free(depends->possibilities);
197 void pkg_deinit(pkg_t * pkg)
205 /* owned by opkg_conf_t */
207 /* owned by opkg_conf_t */
210 pkg->state_want = SW_UNKNOWN;
211 pkg->state_flag = SF_OK;
212 pkg->state_status = SS_NOT_INSTALLED;
214 active_list_clear(&pkg->list);
218 pkg->replaces = NULL;
221 int count = pkg->pre_depends_count
223 + pkg->recommends_count + pkg->suggests_count;
225 for (i = 0; i < count; i++)
226 compound_depend_deinit(&pkg->depends[i]);
230 if (pkg->conflicts) {
231 for (i = 0; i < pkg->conflicts_count; i++)
232 compound_depend_deinit(&pkg->conflicts[i]);
233 free(pkg->conflicts);
239 pkg->pre_depends_count = 0;
240 pkg->provides_count = 0;
242 /* CLEANUP: It'd be nice to pullin the cleanup function from
243 opkg_install.c here. See comment in
244 opkg_install.c:cleanup_temporary_files */
245 if (pkg->tmp_unpack_dir)
246 free(pkg->tmp_unpack_dir);
247 pkg->tmp_unpack_dir = NULL;
249 conffile_list_deinit(&pkg->conffiles);
251 /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
252 since if they are calling deinit, they should know. Maybe do an
253 assertion here instead? */
254 pkg->installed_files_ref_cnt = 1;
255 pkg_free_installed_files(pkg);
258 blob_buf_free(&pkg->blob);
261 int pkg_init_from_file(pkg_t * pkg, const char *filename)
265 char *control_path, *tmp;
269 pkg_set_string(pkg, PKG_LOCAL_FILENAME, filename);
271 tmp = xstrdup(filename);
272 sprintf_alloc(&control_path, "%s/%s.control.XXXXXX",
273 conf->tmp_dir, basename(tmp));
275 fd = mkstemp(control_path);
277 opkg_perror(ERROR, "Failed to make temp file %s", control_path);
282 control_file = fdopen(fd, "r+");
283 if (control_file == NULL) {
284 opkg_perror(ERROR, "Failed to fdopen %s", control_path);
290 err = pkg_extract_control_file_to_stream(pkg, control_file);
292 opkg_msg(ERROR, "Failed to extract control file from %s.\n",
297 rewind(control_file);
299 if ((err = pkg_parse_from_stream(pkg, control_file, 0))) {
301 opkg_msg(ERROR, "Malformed package file %s.\n",
308 fclose(control_file);
310 unlink(control_path);
317 /* Merge any new information in newpkg into oldpkg */
318 int pkg_merge(pkg_t * oldpkg, pkg_t * newpkg)
320 if (oldpkg == newpkg) {
324 if (!oldpkg->auto_installed)
325 oldpkg->auto_installed = newpkg->auto_installed;
328 oldpkg->src = newpkg->src;
330 oldpkg->dest = newpkg->dest;
331 if (!pkg_get_string(oldpkg, PKG_ARCHITECTURE))
332 pkg_set_string(oldpkg, PKG_ARCHITECTURE, pkg_get_string(newpkg, PKG_ARCHITECTURE));
333 if (!oldpkg->arch_priority)
334 oldpkg->arch_priority = newpkg->arch_priority;
335 if (!pkg_get_string(oldpkg, PKG_SECTION))
336 pkg_set_string(oldpkg, PKG_SECTION, pkg_get_string(newpkg, PKG_SECTION));
337 if (!pkg_get_string(oldpkg, PKG_MAINTAINER))
338 pkg_set_string(oldpkg, PKG_MAINTAINER, pkg_get_string(newpkg, PKG_MAINTAINER));
339 if (!pkg_get_string(oldpkg, PKG_DESCRIPTION))
340 pkg_set_string(oldpkg, PKG_DESCRIPTION, pkg_get_string(newpkg, PKG_DESCRIPTION));
342 if (!oldpkg->depends_count && !oldpkg->pre_depends_count
343 && !oldpkg->recommends_count && !oldpkg->suggests_count) {
344 oldpkg->depends_count = newpkg->depends_count;
345 newpkg->depends_count = 0;
347 oldpkg->depends = newpkg->depends;
348 newpkg->depends = NULL;
350 oldpkg->pre_depends_count = newpkg->pre_depends_count;
351 newpkg->pre_depends_count = 0;
353 oldpkg->recommends_count = newpkg->recommends_count;
354 newpkg->recommends_count = 0;
356 oldpkg->suggests_count = newpkg->suggests_count;
357 newpkg->suggests_count = 0;
360 if (oldpkg->provides_count <= 1) {
361 oldpkg->provides_count = newpkg->provides_count;
362 newpkg->provides_count = 0;
364 if (!oldpkg->provides) {
365 oldpkg->provides = newpkg->provides;
366 newpkg->provides = NULL;
370 if (!oldpkg->conflicts_count) {
371 oldpkg->conflicts_count = newpkg->conflicts_count;
372 newpkg->conflicts_count = 0;
374 oldpkg->conflicts = newpkg->conflicts;
375 newpkg->conflicts = NULL;
378 if (!oldpkg->replaces_count) {
379 oldpkg->replaces_count = newpkg->replaces_count;
380 newpkg->replaces_count = 0;
382 oldpkg->replaces = newpkg->replaces;
383 newpkg->replaces = NULL;
386 if (!pkg_get_string(oldpkg, PKG_FILENAME))
387 pkg_set_string(oldpkg, PKG_FILENAME, pkg_get_string(newpkg, PKG_FILENAME));
388 if (!pkg_get_string(oldpkg, PKG_LOCAL_FILENAME))
389 pkg_set_string(oldpkg, PKG_LOCAL_FILENAME, pkg_get_string(newpkg, PKG_LOCAL_FILENAME));
390 if (!oldpkg->tmp_unpack_dir)
391 oldpkg->tmp_unpack_dir = xstrdup(newpkg->tmp_unpack_dir);
392 if (!pkg_get_string(oldpkg, PKG_MD5SUM))
393 pkg_set_string(oldpkg, PKG_MD5SUM, pkg_get_string(newpkg, PKG_MD5SUM));
394 if (!pkg_get_string(oldpkg, PKG_SHA256SUM))
395 pkg_set_string(oldpkg, PKG_SHA256SUM, pkg_get_string(newpkg, PKG_SHA256SUM));
397 oldpkg->size = newpkg->size;
398 if (!oldpkg->installed_size)
399 oldpkg->installed_size = newpkg->installed_size;
400 if (!pkg_get_string(oldpkg, PKG_PRIORITY))
401 pkg_set_string(oldpkg, PKG_PRIORITY, pkg_get_string(newpkg, PKG_PRIORITY));
402 if (!pkg_get_string(oldpkg, PKG_SOURCE))
403 pkg_set_string(oldpkg, PKG_SOURCE, pkg_get_string(newpkg, PKG_SOURCE));
405 if (nv_pair_list_empty(&oldpkg->conffiles)) {
406 list_splice_init(&newpkg->conffiles.head,
407 &oldpkg->conffiles.head);
410 if (!oldpkg->installed_files) {
411 oldpkg->installed_files = newpkg->installed_files;
412 oldpkg->installed_files_ref_cnt =
413 newpkg->installed_files_ref_cnt;
414 newpkg->installed_files = NULL;
417 if (!oldpkg->essential)
418 oldpkg->essential = newpkg->essential;
423 static void abstract_pkg_init(abstract_pkg_t * ab_pkg)
425 ab_pkg->provided_by = abstract_pkg_vec_alloc();
426 ab_pkg->dependencies_checked = 0;
427 ab_pkg->state_status = SS_NOT_INSTALLED;
430 abstract_pkg_t *abstract_pkg_new(void)
432 abstract_pkg_t *ab_pkg;
434 ab_pkg = xcalloc(1, sizeof(abstract_pkg_t));
435 abstract_pkg_init(ab_pkg);
440 void set_flags_from_control(pkg_t * pkg)
445 sprintf_alloc(&file_name, "%s/%s.control", pkg->dest->info_dir,
448 fp = fopen(file_name, "r");
450 opkg_perror(ERROR, "Failed to open %s", file_name);
457 if (pkg_parse_from_stream(pkg, fp, PFM_ALL ^ PFM_ESSENTIAL)) {
459 "Unable to read control file for %s. May be empty.\n",
468 static const char *pkg_state_want_to_str(pkg_state_want_t sw)
472 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
473 if (pkg_state_want_map[i].value == sw) {
474 return pkg_state_want_map[i].str;
478 opkg_msg(ERROR, "Internal error: state_want=%d\n", sw);
479 return "<STATE_WANT_UNKNOWN>";
482 pkg_state_want_t pkg_state_want_from_str(char *str)
486 for (i = 0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
487 if (strcmp(str, pkg_state_want_map[i].str) == 0) {
488 return pkg_state_want_map[i].value;
492 opkg_msg(ERROR, "Internal error: state_want=%s\n", str);
496 static char *pkg_state_flag_to_str(pkg_state_flag_t sf)
502 /* clear the temporary flags before converting to string */
503 sf &= SF_NONVOLATILE_FLAGS;
506 return xstrdup("ok");
509 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
510 if (sf & pkg_state_flag_map[i].value)
511 len += strlen(pkg_state_flag_map[i].str) + 1;
514 str = xmalloc(len + 1);
517 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
518 if (sf & pkg_state_flag_map[i].value) {
519 strncat(str, pkg_state_flag_map[i].str, len);
520 strncat(str, ",", len);
525 str[len - 1] = '\0'; /* squash last comma */
530 pkg_state_flag_t pkg_state_flag_from_str(const char *str)
535 unsigned int sfname_len;
537 if (strcmp(str, "ok") == 0) {
540 for (i = 0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
541 sfname = pkg_state_flag_map[i].str;
542 sfname_len = strlen(sfname);
543 if (strncmp(str, sfname, sfname_len) == 0) {
544 sf |= pkg_state_flag_map[i].value;
557 static const char *pkg_state_status_to_str(pkg_state_status_t ss)
561 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
562 if (pkg_state_status_map[i].value == ss) {
563 return pkg_state_status_map[i].str;
567 opkg_msg(ERROR, "Internal error: state_status=%d\n", ss);
568 return "<STATE_STATUS_UNKNOWN>";
571 pkg_state_status_t pkg_state_status_from_str(const char *str)
575 for (i = 0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
576 if (strcmp(str, pkg_state_status_map[i].str) == 0) {
577 return pkg_state_status_map[i].value;
581 opkg_msg(ERROR, "Internal error: state_status=%s\n", str);
582 return SS_NOT_INSTALLED;
585 void pkg_formatted_field(FILE * fp, pkg_t * pkg, const char *field)
590 int depends_count = pkg->pre_depends_count +
591 pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
593 if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
594 goto UNKNOWN_FMT_FIELD;
600 if (strcasecmp(field, "Architecture") == 0) {
601 p = pkg_get_string(pkg, PKG_ARCHITECTURE);
603 fprintf(fp, "Architecture: %s\n",
606 } else if (strcasecmp(field, "Auto-Installed") == 0) {
607 if (pkg->auto_installed)
608 fprintf(fp, "Auto-Installed: yes\n");
610 goto UNKNOWN_FMT_FIELD;
615 if (strcasecmp(field, "Conffiles") == 0) {
616 conffile_list_elt_t *iter;
618 if (nv_pair_list_empty(&pkg->conffiles))
621 fprintf(fp, "Conffiles:\n");
622 for (iter = nv_pair_list_first(&pkg->conffiles); iter;
623 iter = nv_pair_list_next(&pkg->conffiles, iter)) {
624 if (((conffile_t *) iter->data)->name
625 && ((conffile_t *) iter->data)->value) {
626 fprintf(fp, " %s %s\n",
627 ((conffile_t *) iter->data)->
629 ((conffile_t *) iter->data)->
633 } else if (strcasecmp(field, "Conflicts") == 0) {
635 if (pkg->conflicts_count) {
636 fprintf(fp, "Conflicts:");
637 for (i = 0; i < pkg->conflicts_count; i++) {
639 pkg->conflicts[i].possibilities[0];
640 fprintf(fp, "%s %s", i == 0 ? "" : ",",
643 fprintf(fp, " (%s%s)",
644 constraint_to_str(cdep->
652 goto UNKNOWN_FMT_FIELD;
657 if (strcasecmp(field, "Depends") == 0) {
658 if (pkg->depends_count) {
659 fprintf(fp, "Depends:");
660 for (j = 0, i = 0; i < depends_count; i++) {
661 if (pkg->depends[i].type != DEPEND)
663 str = pkg_depend_str(pkg, i);
664 fprintf(fp, "%s %s", j == 0 ? "" : ",",
671 } else if (strcasecmp(field, "Description") == 0) {
672 p = pkg_get_string(pkg, PKG_DESCRIPTION);
674 fprintf(fp, "Description: %s\n",
678 goto UNKNOWN_FMT_FIELD;
683 if (pkg->essential) {
684 fprintf(fp, "Essential: yes\n");
689 p = pkg_get_string(pkg, PKG_FILENAME);
691 fprintf(fp, "Filename: %s\n", p);
696 if (strcasecmp(field, "Installed-Size") == 0) {
697 fprintf(fp, "Installed-Size: %ld\n",
698 pkg->installed_size);
699 } else if (strcasecmp(field, "Installed-Time") == 0
700 && pkg->installed_time) {
701 fprintf(fp, "Installed-Time: %lu\n",
702 pkg->installed_time);
707 if (strcasecmp(field, "Maintainer") == 0) {
708 p = pkg_get_string(pkg, PKG_MAINTAINER);
710 fprintf(fp, "Maintainer: %s\n", p);
712 } else if (strcasecmp(field, "MD5sum") == 0) {
713 p = pkg_get_string(pkg, PKG_MD5SUM);
715 fprintf(fp, "MD5Sum: %s\n", p);
718 goto UNKNOWN_FMT_FIELD;
723 if (strcasecmp(field, "Package") == 0) {
724 fprintf(fp, "Package: %s\n", pkg->name);
725 } else if (strcasecmp(field, "Priority") == 0) {
726 fprintf(fp, "Priority: %s\n", pkg_get_string(pkg, PKG_PRIORITY));
727 } else if (strcasecmp(field, "Provides") == 0) {
728 if (pkg->provides_count > 1) {
729 fprintf(fp, "Provides:");
730 for (i = 1; i < pkg->provides_count; i++) {
731 fprintf(fp, "%s %s", i == 1 ? "" : ",",
732 pkg->provides[i]->name);
737 goto UNKNOWN_FMT_FIELD;
742 if (strcasecmp(field, "Replaces") == 0) {
743 if (pkg->replaces_count) {
744 fprintf(fp, "Replaces:");
745 for (i = 0; i < pkg->replaces_count; i++) {
746 fprintf(fp, "%s %s", i == 0 ? "" : ",",
747 pkg->replaces[i]->name);
751 } else if (strcasecmp(field, "Recommends") == 0) {
752 if (pkg->recommends_count) {
753 fprintf(fp, "Recommends:");
754 for (j = 0, i = 0; i < depends_count; i++) {
755 if (pkg->depends[i].type != RECOMMEND)
757 str = pkg_depend_str(pkg, i);
758 fprintf(fp, "%s %s", j == 0 ? "" : ",",
766 goto UNKNOWN_FMT_FIELD;
771 if (strcasecmp(field, "Section") == 0) {
772 p = pkg_get_string(pkg, PKG_SECTION);
774 fprintf(fp, "Section: %s\n", p);
776 #if defined HAVE_SHA256
777 } else if (strcasecmp(field, "SHA256sum") == 0) {
778 p = pkg_get_string(pkg, PKG_SHA256SUM);
780 fprintf(fp, "SHA256sum: %s\n", p);
783 } else if (strcasecmp(field, "Size") == 0) {
785 fprintf(fp, "Size: %ld\n", pkg->size);
787 } else if (strcasecmp(field, "Source") == 0) {
788 p = pkg_get_string(pkg, PKG_SOURCE);
790 fprintf(fp, "Source: %s\n", p);
792 } else if (strcasecmp(field, "Status") == 0) {
793 char *pflag = pkg_state_flag_to_str(pkg->state_flag);
794 fprintf(fp, "Status: %s %s %s\n",
795 pkg_state_want_to_str(pkg->state_want),
797 pkg_state_status_to_str(pkg->state_status));
799 } else if (strcasecmp(field, "Suggests") == 0) {
800 if (pkg->suggests_count) {
801 fprintf(fp, "Suggests:");
802 for (j = 0, i = 0; i < depends_count; i++) {
803 if (pkg->depends[i].type != SUGGEST)
805 str = pkg_depend_str(pkg, i);
806 fprintf(fp, "%s %s", j == 0 ? "" : ",",
814 goto UNKNOWN_FMT_FIELD;
819 if (strcasecmp(field, "Tags") == 0) {
820 p = pkg_get_string(pkg, PKG_TAGS);
822 fprintf(fp, "Tags: %s\n", p);
829 char *version = pkg_version_str_alloc(pkg);
832 fprintf(fp, "Version: %s\n", version);
837 goto UNKNOWN_FMT_FIELD;
843 opkg_msg(ERROR, "Internal error: field=%s\n", field);
846 void pkg_formatted_info(FILE * fp, pkg_t * pkg)
848 pkg_formatted_field(fp, pkg, "Package");
849 pkg_formatted_field(fp, pkg, "Version");
850 pkg_formatted_field(fp, pkg, "Depends");
851 pkg_formatted_field(fp, pkg, "Recommends");
852 pkg_formatted_field(fp, pkg, "Suggests");
853 pkg_formatted_field(fp, pkg, "Provides");
854 pkg_formatted_field(fp, pkg, "Replaces");
855 pkg_formatted_field(fp, pkg, "Conflicts");
856 pkg_formatted_field(fp, pkg, "Status");
857 pkg_formatted_field(fp, pkg, "Section");
858 pkg_formatted_field(fp, pkg, "Essential");
859 pkg_formatted_field(fp, pkg, "Architecture");
860 pkg_formatted_field(fp, pkg, "Maintainer");
861 pkg_formatted_field(fp, pkg, "MD5sum");
862 pkg_formatted_field(fp, pkg, "Size");
863 pkg_formatted_field(fp, pkg, "Filename");
864 pkg_formatted_field(fp, pkg, "Conffiles");
865 pkg_formatted_field(fp, pkg, "Source");
866 pkg_formatted_field(fp, pkg, "Description");
867 pkg_formatted_field(fp, pkg, "Installed-Time");
868 pkg_formatted_field(fp, pkg, "Tags");
872 void pkg_print_status(pkg_t * pkg, FILE * file)
878 pkg_formatted_field(file, pkg, "Package");
879 pkg_formatted_field(file, pkg, "Version");
880 pkg_formatted_field(file, pkg, "Depends");
881 pkg_formatted_field(file, pkg, "Recommends");
882 pkg_formatted_field(file, pkg, "Suggests");
883 pkg_formatted_field(file, pkg, "Provides");
884 pkg_formatted_field(file, pkg, "Replaces");
885 pkg_formatted_field(file, pkg, "Conflicts");
886 pkg_formatted_field(file, pkg, "Status");
887 pkg_formatted_field(file, pkg, "Essential");
888 pkg_formatted_field(file, pkg, "Architecture");
889 pkg_formatted_field(file, pkg, "Conffiles");
890 pkg_formatted_field(file, pkg, "Installed-Time");
891 pkg_formatted_field(file, pkg, "Auto-Installed");
896 * libdpkg - Debian packaging suite library routines
897 * vercmp.c - comparison of version numbers
899 * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
902 /* assume ascii; warning: evaluates x multiple times! */
903 #define order(x) ((x) == '~' ? -1 \
906 : isalpha((x)) ? (x) \
909 static int verrevcmp(const char *val, const char *ref)
916 while (*val || *ref) {
919 while ((*val && !isdigit(*val)) || (*ref && !isdigit(*ref))) {
920 int vc = order(*val), rc = order(*ref);
931 while (isdigit(*val) && isdigit(*ref)) {
933 first_diff = *val - *ref;
947 int pkg_compare_versions(const pkg_t * pkg, const pkg_t * ref_pkg)
949 unsigned int epoch1 = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
950 unsigned int epoch2 = (unsigned int) pkg_get_int(ref_pkg, PKG_EPOCH);
951 char *revision1 = pkg_get_ptr(pkg, PKG_REVISION);
952 char *revision2 = pkg_get_ptr(ref_pkg, PKG_REVISION);
953 const char *version1 = pkg_get_string(pkg, PKG_VERSION);
954 const char *version2 = pkg_get_string(ref_pkg, PKG_VERSION);
957 if (epoch1 > epoch2) {
961 if (epoch1 < epoch2) {
965 r = verrevcmp(version1, version2);
970 r = verrevcmp(revision1, revision2);
978 int pkg_version_satisfied(pkg_t * it, pkg_t * ref, const char *op)
982 r = pkg_compare_versions(it, ref);
984 if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
988 if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
992 if (strcmp(op, "<<") == 0) {
996 if (strcmp(op, ">>") == 0) {
1000 if (strcmp(op, "=") == 0) {
1004 opkg_msg(ERROR, "Unknown operator: %s.\n", op);
1008 int pkg_name_version_and_architecture_compare(const void *p1, const void *p2)
1010 const pkg_t * a = *(const pkg_t **)p1;
1011 const pkg_t * b = *(const pkg_t **)p2;
1014 if (!a->name || !b->name) {
1015 opkg_msg(ERROR, "Internal error: a->name=%p, b->name=%p.\n",
1020 namecmp = strcmp(a->name, b->name);
1023 vercmp = pkg_compare_versions(a, b);
1026 if (!a->arch_priority || !b->arch_priority) {
1028 "Internal error: a->arch_priority=%i b->arch_priority=%i.\n",
1029 a->arch_priority, b->arch_priority);
1032 if (a->arch_priority > b->arch_priority)
1034 if (a->arch_priority < b->arch_priority)
1039 int abstract_pkg_name_compare(const void *p1, const void *p2)
1041 const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
1042 const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
1043 if (!a->name || !b->name) {
1044 opkg_msg(ERROR, "Internal error: a->name=%p b->name=%p.\n",
1048 return strcmp(a->name, b->name);
1051 char *pkg_version_str_alloc(pkg_t * pkg)
1054 char *version, *revptr;
1055 unsigned int epoch = (unsigned int) pkg_get_int(pkg, PKG_EPOCH);
1057 revptr = pkg_get_ptr(pkg, PKG_REVISION);
1058 verstr = pkg_get_string(pkg, PKG_VERSION);
1062 sprintf_alloc(&version, "%d:%s-%s",
1063 epoch, verstr, revptr);
1065 sprintf_alloc(&version, "%d:%s",
1069 sprintf_alloc(&version, "%s-%s",
1072 version = xstrdup(verstr);
1079 * XXX: this should be broken into two functions
1081 str_list_t *pkg_get_installed_files(pkg_t * pkg)
1084 char *list_file_name = NULL;
1085 FILE *list_file = NULL;
1087 char *installed_file_name;
1088 unsigned int rootdirlen = 0;
1089 int list_from_package;
1090 const char *local_filename;
1092 pkg->installed_files_ref_cnt++;
1094 if (pkg->installed_files) {
1095 return pkg->installed_files;
1098 pkg->installed_files = str_list_alloc();
1101 * For installed packages, look at the package.list file in the database.
1102 * For uninstalled packages, get the file list directly from the package.
1104 if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL)
1105 list_from_package = 1;
1107 list_from_package = 0;
1109 if (list_from_package) {
1110 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1112 if (!local_filename) {
1113 return pkg->installed_files;
1115 /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
1116 file. In other words, change deb_extract so that it can
1117 simply return the file list as a char *[] rather than
1118 insisting on writing it to a FILE * as it does now. */
1119 sprintf_alloc(&list_file_name, "%s/%s.list.XXXXXX",
1120 conf->tmp_dir, pkg->name);
1121 fd = mkstemp(list_file_name);
1123 opkg_perror(ERROR, "Failed to make temp file %s.",
1125 free(list_file_name);
1126 return pkg->installed_files;
1128 list_file = fdopen(fd, "r+");
1129 if (list_file == NULL) {
1130 opkg_perror(ERROR, "Failed to fdopen temp file %s.",
1133 unlink(list_file_name);
1134 free(list_file_name);
1135 return pkg->installed_files;
1137 err = pkg_extract_data_file_names_to_stream(pkg, list_file);
1139 opkg_msg(ERROR, "Error extracting file list from %s.\n",
1142 unlink(list_file_name);
1143 free(list_file_name);
1144 str_list_deinit(pkg->installed_files);
1145 pkg->installed_files = NULL;
1150 sprintf_alloc(&list_file_name, "%s/%s.list",
1151 pkg->dest->info_dir, pkg->name);
1152 list_file = fopen(list_file_name, "r");
1153 if (list_file == NULL) {
1154 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1155 free(list_file_name);
1156 return pkg->installed_files;
1158 free(list_file_name);
1161 if (conf->offline_root)
1162 rootdirlen = strlen(conf->offline_root);
1167 line = file_read_line_alloc(list_file);
1173 if (list_from_package) {
1174 if (*file_name == '.') {
1177 if (*file_name == '/') {
1180 sprintf_alloc(&installed_file_name, "%s%s",
1181 pkg->dest->root_dir, file_name);
1183 if (conf->offline_root &&
1184 strncmp(conf->offline_root, file_name,
1186 sprintf_alloc(&installed_file_name, "%s%s",
1187 conf->offline_root, file_name);
1189 // already contains root_dir as header -> ABSOLUTE
1190 sprintf_alloc(&installed_file_name, "%s",
1194 str_list_append(pkg->installed_files, installed_file_name);
1195 free(installed_file_name);
1201 if (list_from_package) {
1202 unlink(list_file_name);
1203 free(list_file_name);
1206 return pkg->installed_files;
1209 /* XXX: CLEANUP: This function and it's counterpart,
1210 (pkg_get_installed_files), do not match our init/deinit naming
1211 convention. Nor the alloc/free convention. But, then again, neither
1212 of these conventions currrently fit the way these two functions
1214 void pkg_free_installed_files(pkg_t * pkg)
1216 pkg->installed_files_ref_cnt--;
1218 if (pkg->installed_files_ref_cnt > 0)
1221 if (pkg->installed_files) {
1222 str_list_purge(pkg->installed_files);
1225 pkg->installed_files = NULL;
1228 void pkg_remove_installed_files_list(pkg_t * pkg)
1230 char *list_file_name;
1232 sprintf_alloc(&list_file_name, "%s/%s.list",
1233 pkg->dest->info_dir, pkg->name);
1235 if (!conf->noaction)
1236 (void)unlink(list_file_name);
1238 free(list_file_name);
1241 conffile_t *pkg_get_conffile(pkg_t * pkg, const char *file_name)
1243 conffile_list_elt_t *iter;
1244 conffile_t *conffile;
1250 for (iter = nv_pair_list_first(&pkg->conffiles); iter;
1251 iter = nv_pair_list_next(&pkg->conffiles, iter)) {
1252 conffile = (conffile_t *) iter->data;
1254 if (strcmp(conffile->name, file_name) == 0) {
1262 int pkg_run_script(pkg_t * pkg, const char *script, const char *args)
1271 /* XXX: FEATURE: When conf->offline_root is set, we should run the
1272 maintainer script within a chroot environment. */
1273 if (conf->offline_root && !conf->force_postinstall) {
1274 opkg_msg(INFO, "Offline root mode: not running %s.%s.\n",
1279 /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
1280 have scripts in pkg->tmp_unpack_dir. */
1281 if (pkg->state_status == SS_INSTALLED
1282 || pkg->state_status == SS_UNPACKED) {
1283 if (pkg->dest == NULL) {
1284 opkg_msg(ERROR, "Internal error: %s has a NULL dest.\n",
1288 sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name,
1291 if (pkg->tmp_unpack_dir == NULL) {
1293 "Internal error: %s has a NULL tmp_unpack_dir.\n",
1297 sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script);
1300 opkg_msg(INFO, "Running script %s.\n", path);
1303 pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir,
1306 if (pkg->is_upgrade)
1307 setenv("PKG_UPGRADE", "1", 1);
1309 setenv("PKG_UPGRADE", "0", 1);
1311 if (!file_exists(path)) {
1316 sprintf_alloc(&cmd, "%s %s", path, args);
1319 const char *argv[] = { "sh", "-c", cmd, NULL };
1320 err = xsystem(argv);
1326 "package \"%s\" %s script returned status %d.\n",
1327 pkg->name, script, err);
1334 int pkg_arch_supported(pkg_t * pkg)
1336 nv_pair_list_elt_t *l;
1337 char *architecture = pkg_get_string(pkg, PKG_ARCHITECTURE);
1342 list_for_each_entry(l, &conf->arch_list.head, node) {
1343 nv_pair_t *nv = (nv_pair_t *) l->data;
1344 if (strcmp(nv->name, architecture) == 0) {
1346 "Arch %s (priority %s) supported for pkg %s.\n",
1347 nv->name, nv->value, pkg->name);
1352 opkg_msg(DEBUG, "Arch %s unsupported for pkg %s.\n",
1353 architecture, pkg->name);
1357 void pkg_info_preinstall_check(void)
1360 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1362 /* update the file owner data structure */
1363 opkg_msg(INFO, "Updating file owner list.\n");
1364 pkg_hash_fetch_all_installed(installed_pkgs);
1365 for (i = 0; i < installed_pkgs->len; i++) {
1366 pkg_t *pkg = installed_pkgs->pkgs[i];
1367 str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
1368 str_list_elt_t *iter, *niter;
1369 if (installed_files == NULL) {
1370 opkg_msg(ERROR, "Failed to determine installed "
1371 "files for pkg %s.\n", pkg->name);
1374 for (iter = str_list_first(installed_files), niter =
1375 str_list_next(installed_files, iter); iter;
1376 iter = niter, niter =
1377 str_list_next(installed_files, iter)) {
1378 char *installed_file = (char *)iter->data;
1379 file_hash_set_file_owner(installed_file, pkg);
1381 pkg_free_installed_files(pkg);
1383 pkg_vec_free(installed_pkgs);
1386 struct pkg_write_filelist_data {
1392 pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
1394 struct pkg_write_filelist_data *data = data_;
1395 pkg_t *entry = entry_;
1396 if (entry == data->pkg) {
1397 fprintf(data->stream, "%s\n", key);
1401 int pkg_write_filelist(pkg_t * pkg)
1403 struct pkg_write_filelist_data data;
1404 char *list_file_name;
1406 sprintf_alloc(&list_file_name, "%s/%s.list",
1407 pkg->dest->info_dir, pkg->name);
1409 opkg_msg(INFO, "Creating %s file for pkg %s.\n",
1410 list_file_name, pkg->name);
1412 data.stream = fopen(list_file_name, "w");
1414 opkg_perror(ERROR, "Failed to open %s", list_file_name);
1415 free(list_file_name);
1420 hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
1421 fclose(data.stream);
1422 free(list_file_name);
1424 pkg->state_flag &= ~SF_FILELIST_CHANGED;
1429 int pkg_write_changed_filelists(void)
1431 pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1432 int i, err, ret = 0;
1437 opkg_msg(INFO, "Saving changed filelists.\n");
1439 pkg_hash_fetch_all_installed(installed_pkgs);
1440 for (i = 0; i < installed_pkgs->len; i++) {
1441 pkg_t *pkg = installed_pkgs->pkgs[i];
1442 if (pkg->state_flag & SF_FILELIST_CHANGED) {
1443 err = pkg_write_filelist(pkg);
1449 pkg_vec_free(installed_pkgs);