1 /* pkg_depends.c - the opkg package management system
5 Copyright (C) 2002 Compaq Computer Corporation
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.
22 #include "opkg_utils.h"
24 #include "opkg_message.h"
25 #include "pkg_parse.h"
26 #include "hash_table.h"
27 #include "libbb/libbb.h"
29 static int parseDepends(compound_depend_t * compound_depend, char *depend_str, enum depend_type type);
30 static depend_t *depend_init(void);
31 static char **add_unresolved_dep(pkg_t * pkg, char **the_lost, int ref_ndx);
32 static char **merge_unresolved(char **oldstuff, char **newstuff);
33 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
35 static int pkg_installed_and_constraint_satisfied(pkg_t * pkg, void *cdata)
37 depend_t *depend = (depend_t *) cdata;
38 if ((pkg->state_status == SS_INSTALLED
39 || pkg->state_status == SS_UNPACKED)
40 && version_constraints_satisfied(depend, pkg)
41 && !(pkg->state_flag & SF_IGNORE))
47 static int pkg_constraint_satisfied(pkg_t * pkg, void *cdata)
49 depend_t *depend = (depend_t *) cdata;
50 if (version_constraints_satisfied(depend, pkg)
51 && !(pkg->state_flag & SF_IGNORE))
57 /* returns ndependencies or negative error value */
59 pkg_hash_fetch_unsatisfied_dependencies(pkg_t * pkg, pkg_vec_t * unsatisfied,
62 pkg_t *satisfier_entry_pkg;
66 abstract_pkg_t *ab_pkg;
67 compound_depend_t *compound_depend;
70 * this is a setup to check for redundant/cyclic dependency checks,
71 * which are marked at the abstract_pkg level
73 if (!(ab_pkg = pkg->parent)) {
74 opkg_msg(ERROR, "Internal error, with pkg %s.\n", pkg->name);
78 if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */
82 ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */
85 compound_depend = pkg_get_ptr(pkg, PKG_DEPENDS);
87 if (!compound_depend || !compound_depend->type) {
94 /* foreach dependency */
95 for (i = 0; compound_depend && compound_depend->type; compound_depend++, i++) {
96 depend_t **possible_satisfiers =
97 compound_depend->possibilities;;
99 satisfier_entry_pkg = NULL;
101 if (compound_depend->type == GREEDY_DEPEND) {
102 /* foreach possible satisfier */
103 for (j = 0; j < compound_depend->possibility_count; j++) {
104 /* foreach provided_by, which includes the abstract_pkg itself */
105 abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
106 abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
107 int nposs = ab_provider_vec->len;
108 abstract_pkg_t **ab_providers = ab_provider_vec->pkgs;
110 for (l = 0; l < nposs; l++) {
111 pkg_vec_t *test_vec = ab_providers[l]->pkgs;
112 /* if no depends on this one, try the first package that Provides this one */
113 if (!test_vec) { /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */
117 /* cruise this possiblity's pkg_vec looking for an installed version */
118 for (k = 0; k < test_vec->len; k++) {
119 pkg_t *pkg_scout = test_vec->pkgs[k];
120 /* not installed, and not already known about? */
121 if ((pkg_scout->state_want != SW_INSTALL)
122 && !pkg_scout->parent->dependencies_checked
123 && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
124 char **newstuff = NULL;
126 pkg_vec_t *tmp_vec = pkg_vec_alloc();
127 /* check for not-already-installed dependencies */
128 rc = pkg_hash_fetch_unsatisfied_dependencies(
129 pkg_scout, tmp_vec, &newstuff);
130 if (newstuff == NULL) {
133 for (m = 0; m < rc; m++) {
134 pkg_t *p = tmp_vec->pkgs[m];
135 if (p->state_want == SW_INSTALL)
138 "Not installing %s due"
139 " to requirement for %s.\n",
140 pkg_scout->name, p->name);
144 pkg_vec_free(tmp_vec);
146 /* mark this one for installation */
148 "Adding satisfier for greedy"
151 pkg_vec_insert(unsatisfied, pkg_scout);
155 "Not installing %s due to "
168 /* foreach possible satisfier, look for installed package */
169 for (j = 0; j < compound_depend->possibility_count; j++) {
170 /* foreach provided_by, which includes the abstract_pkg itself */
171 depend_t *dependence_to_satisfy = possible_satisfiers[j];
172 abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
173 pkg_t *satisfying_pkg =
174 pkg_hash_fetch_best_installation_candidate(satisfying_apkg,
175 pkg_installed_and_constraint_satisfied,
176 dependence_to_satisfy, 1);
177 /* Being that I can't test constraing in pkg_hash, I will test it here */
178 if (satisfying_pkg != NULL) {
179 if (!pkg_installed_and_constraint_satisfied
180 (satisfying_pkg, dependence_to_satisfy)) {
181 satisfying_pkg = NULL;
184 opkg_msg(DEBUG, "satisfying_pkg=%p\n", satisfying_pkg);
185 if (satisfying_pkg != NULL) {
191 /* if nothing installed matches, then look for uninstalled satisfier */
193 /* foreach possible satisfier, look for installed package */
194 for (j = 0; j < compound_depend->possibility_count; j++) {
195 /* foreach provided_by, which includes the abstract_pkg itself */
196 depend_t *dependence_to_satisfy = possible_satisfiers[j];
197 abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
198 pkg_t *satisfying_pkg =
199 pkg_hash_fetch_best_installation_candidate(satisfying_apkg,
200 pkg_constraint_satisfied, dependence_to_satisfy, 1);
201 /* Being that I can't test constraing in pkg_hash, I will test it here too */
202 if (satisfying_pkg != NULL) {
203 if (!pkg_constraint_satisfied(satisfying_pkg,
204 dependence_to_satisfy)) {
205 satisfying_pkg = NULL;
209 /* user request overrides package recommendation */
210 if (satisfying_pkg != NULL
211 && (compound_depend->type == RECOMMEND
212 || compound_depend->type == SUGGEST)
213 && (satisfying_pkg->state_want == SW_DEINSTALL
214 || satisfying_pkg->state_want == SW_PURGE)) {
216 "%s: ignoring recommendation for "
217 "%s at user request\n",
218 pkg->name, satisfying_pkg->name);
222 opkg_msg(DEBUG, "satisfying_pkg=%p\n",
224 if (satisfying_pkg != NULL) {
225 satisfier_entry_pkg = satisfying_pkg;
231 /* we didn't find one, add something to the unsatisfied vector */
233 if (!satisfier_entry_pkg) {
234 /* failure to meet recommendations is not an error */
235 if (compound_depend->type != RECOMMEND
236 && compound_depend->type != SUGGEST)
237 the_lost = add_unresolved_dep(pkg, the_lost, i);
240 "%s: unsatisfied recommendation for %s\n",
242 compound_depend->possibilities[0]->pkg->name);
244 if (compound_depend->type == SUGGEST) {
245 /* just mention it politely */
247 "package %s suggests installing %s\n",
248 pkg->name, satisfier_entry_pkg->name);
250 char **newstuff = NULL;
252 if (satisfier_entry_pkg != pkg &&
253 !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg))
255 pkg_hash_fetch_unsatisfied_dependencies(
256 satisfier_entry_pkg, unsatisfied, &newstuff);
257 pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
258 the_lost = merge_unresolved(the_lost, newstuff);
266 *unresolved = the_lost;
268 return unsatisfied->len;
271 /*checking for conflicts !in replaces
272 If a packages conflicts with another but is also replacing it, I should not consider it a
274 returns 0 if conflicts <> replaces or 1 if conflicts == replaces
276 static int is_pkg_a_replaces(pkg_t * pkg_scout, pkg_t * pkg)
278 abstract_pkg_t **replaces = pkg_get_ptr(pkg, PKG_REPLACES);
280 if (!replaces || !*replaces)
284 if (strcmp(pkg_scout->name, (*replaces)->name) == 0) { // Found
285 opkg_msg(DEBUG2, "Seems I've found a replace %s %s\n",
286 pkg_scout->name, (*replaces)->name);
295 pkg_vec_t *pkg_hash_fetch_conflicts(pkg_t * pkg)
297 pkg_vec_t *installed_conflicts, *test_vec;
298 compound_depend_t *conflicts, *conflict;
299 depend_t **possible_satisfiers;
300 depend_t *possible_satisfier;
302 abstract_pkg_t *ab_pkg;
307 * this is a setup to check for redundant/cyclic dependency checks,
308 * which are marked at the abstract_pkg level
310 if (!(ab_pkg = pkg->parent)) {
311 opkg_msg(ERROR, "Internal error: %s not in hash table\n",
313 return (pkg_vec_t *) NULL;
316 conflicts = pkg_get_ptr(pkg, PKG_CONFLICTS);
318 return (pkg_vec_t *) NULL;
320 installed_conflicts = pkg_vec_alloc();
322 /* foreach conflict */
323 for (conflict = conflicts; conflict->type; conflict++ ) {
324 possible_satisfiers = conflicts->possibilities;
326 /* foreach possible satisfier */
327 for (j = 0; j < conflicts->possibility_count; j++) {
328 possible_satisfier = possible_satisfiers[j];
329 if (!possible_satisfier)
331 "Internal error: possible_satisfier=NULL\n");
332 if (!possible_satisfier->pkg)
334 "Internal error: possible_satisfier->pkg=NULL\n");
335 test_vec = possible_satisfier->pkg->pkgs;
337 /* pkg_vec found, it is an actual package conflict
338 * cruise this possiblity's pkg_vec looking for an installed version */
339 pkg_scouts = test_vec->pkgs;
340 for (k = 0; k < test_vec->len; k++) {
341 pkg_scout = pkg_scouts[k];
344 "Internal error: pkg_scout=NULL\n");
347 if ((pkg_scout->state_status == SS_INSTALLED
348 || pkg_scout->state_want == SW_INSTALL)
349 && version_constraints_satisfied(possible_satisfier,
351 && !is_pkg_a_replaces(pkg_scout, pkg)) {
352 if (!is_pkg_in_pkg_vec(installed_conflicts,
354 pkg_vec_insert(installed_conflicts, pkg_scout);
363 if (installed_conflicts->len)
364 return installed_conflicts;
365 pkg_vec_free(installed_conflicts);
366 return (pkg_vec_t *) NULL;
369 int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
374 if (depends->constraint == NONE)
379 parse_version(temp, depends->version);
381 comparison = pkg_compare_versions(pkg, temp);
386 if ((depends->constraint == EARLIER) && (comparison < 0))
388 else if ((depends->constraint == LATER) && (comparison > 0))
390 else if (comparison == 0)
392 else if ((depends->constraint == LATER_EQUAL) && (comparison >= 0))
394 else if ((depends->constraint == EARLIER_EQUAL) && (comparison <= 0))
400 int pkg_dependence_satisfiable(depend_t * depend)
402 abstract_pkg_t *apkg = depend->pkg;
403 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
404 int n_providers = provider_apkgs->len;
405 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
411 for (i = 0; i < n_providers; i++) {
412 abstract_pkg_t *papkg = apkgs[i];
413 pkg_vec = papkg->pkgs;
415 n_pkgs = pkg_vec->len;
416 for (j = 0; j < n_pkgs; j++) {
417 pkg_t *pkg = pkg_vec->pkgs[j];
418 if (version_constraints_satisfied(depend, pkg)) {
427 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
431 pkg_t **pkgs = vec->pkgs;
432 arch1 = pkg_get_architecture(pkg);
434 for (i = 0; i < vec->len; i++) {
435 arch2 = pkg_get_architecture(*(pkgs + i));
437 if ((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
438 && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
439 && (strcmp(arch1, arch2) == 0))
446 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
449 int pkg_replaces(pkg_t * pkg, pkg_t * replacee)
451 abstract_pkg_t **replaces = pkg_get_ptr(pkg, PKG_REPLACES);
452 abstract_pkg_t **provides = pkg_get_ptr(replacee, PKG_PROVIDES);
453 abstract_pkg_t **r, **p;
455 for (r = replaces; r && *r; r++)
456 for (p = provides; p && *p; p++)
464 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
467 int pkg_conflicts_abstract(pkg_t * pkg, abstract_pkg_t * conflictee)
469 compound_depend_t *conflicts, *conflict;
471 conflicts = pkg_get_ptr(pkg, PKG_CONFLICTS);
474 for (conflict = conflicts; conflict->type; conflict++) {
475 int possibility_count = conflict->possibility_count;
476 struct depend **possibilities = conflict->possibilities;
477 for (j = 0; j < possibility_count; j++) {
478 if (possibilities[j]->pkg == conflictee) {
487 * pkg_conflicts returns 1 if pkg->conflicts contains one of
488 * conflictee's provides and 0 otherwise.
490 int pkg_conflicts(pkg_t * pkg, pkg_t * conflictee)
493 int possibility_count;
494 struct depend **possibilities;
495 compound_depend_t *conflicts, *conflict;
496 abstract_pkg_t **conflictee_provides, **provider, *possibility;
498 conflicts = pkg_get_ptr(pkg, PKG_CONFLICTS);
499 conflictee_provides = pkg_get_ptr(conflictee, PKG_PROVIDES);
501 for (conflict = conflicts; conflict->type; conflict++) {
502 possibility_count = conflict->possibility_count;
503 possibilities = conflict->possibilities;
504 for (j = 0; j < possibility_count; j++) {
505 possibility = possibilities[j]->pkg;
506 for (provider = conflictee_provides; provider && *provider; provider++) {
507 if (possibility == *provider) {
516 static char **merge_unresolved(char **oldstuff, char **newstuff)
518 int oldlen = 0, newlen = 0;
525 while (oldstuff && oldstuff[oldlen])
527 while (newstuff && newstuff[newlen])
530 result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
532 for (i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
533 *(result + i) = *(newstuff + j);
535 *(result + i) = NULL;
541 * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
542 * this is null terminated, no count is carried around
544 char **add_unresolved_dep(pkg_t * pkg, char **the_lost, int ref_ndx)
550 while (the_lost && the_lost[count])
553 count++; /* need one to hold the null */
554 resized = xrealloc(the_lost, sizeof(char *) * (count + 1));
555 resized[count - 1] = pkg_depend_str(pkg, ref_ndx);
556 resized[count] = NULL;
561 static void flag_related_packages(pkg_t *pkg, int state_flags)
564 compound_depend_t *deps;
566 for (deps = pkg_get_ptr(pkg, PKG_DEPENDS), i = 0; deps && deps[i].type; i++)
567 for (j = 0; j < deps[i].possibility_count; j++) {
568 if ((deps[i].possibilities[j]->pkg->state_flag & state_flags) != state_flags) {
569 opkg_msg(DEBUG, "propagating pkg flag to dependent abpkg %s\n",
570 deps[i].possibilities[j]->pkg->name);
571 deps[i].possibilities[j]->pkg->state_flag |= state_flags;
575 for (deps = pkg_get_ptr(pkg, PKG_CONFLICTS), i = 0; deps && deps[i].type; i++)
576 for (j = 0; j < deps[i].possibility_count; j++) {
577 if ((deps[i].possibilities[j]->pkg->state_flag & state_flags) != state_flags) {
578 opkg_msg(DEBUG, "propagating pkg flag to conflicting abpkg %s\n",
579 deps[i].possibilities[j]->pkg->name);
580 deps[i].possibilities[j]->pkg->state_flag |= state_flags;
585 abstract_pkg_t **init_providelist(pkg_t *pkg, int *count)
587 abstract_pkg_t *ab_pkg;
588 abstract_pkg_t **provides = pkg_get_ptr(pkg, PKG_PROVIDES);
591 provides = calloc(2, sizeof(abstract_pkg_t *));
600 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
603 ab_pkg->pkgs = pkg_vec_alloc();
605 if (!abstract_pkg_vec_contains(ab_pkg->provided_by, ab_pkg))
606 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
608 provides[0] = ab_pkg;
614 pkg_set_ptr(pkg, PKG_PROVIDES, provides);
617 for (*count = 1; *provides; provides++) {
618 if (pkg->state_flag & SF_NEED_DETAIL) {
619 if (!((*provides)->state_flag & SF_NEED_DETAIL)) {
620 opkg_msg(DEBUG, "propagating pkg flag to provided abpkg %s\n",
622 (*provides)->state_flag |= SF_NEED_DETAIL;
629 flag_related_packages(pkg, SF_NEED_DETAIL);
634 void parse_providelist(pkg_t *pkg, char *list)
638 abstract_pkg_t *ab_pkg, *provided_abpkg, **tmp, **provides;
640 provides = init_providelist(pkg, &count);
641 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
643 if (!provides || !ab_pkg)
646 for (item = strtok_r(list, ", ", &tok); item;
647 count++, item = strtok_r(NULL, ", ", &tok)) {
648 tmp = realloc(provides, sizeof(abstract_pkg_t *) * (count + 1));
653 provided_abpkg = ensure_abstract_pkg_by_name(item);
655 if (provided_abpkg->state_flag & SF_NEED_DETAIL) {
656 if (!(ab_pkg->state_flag & SF_NEED_DETAIL)) {
657 opkg_msg(DEBUG, "propagating provided abpkg flag to "
658 "provider abpkg %s\n", ab_pkg->name);
659 ab_pkg->state_flag |= SF_NEED_DETAIL;
663 if (!abstract_pkg_vec_contains(provided_abpkg->provided_by, ab_pkg))
664 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
667 provides[count - 1] = provided_abpkg;
670 provides[count - 1] = NULL;
672 pkg_set_ptr(pkg, PKG_PROVIDES, provides);
675 void parse_replacelist(pkg_t *pkg, char *list)
679 abstract_pkg_t *ab_pkg, *old_abpkg, **tmp, **replaces = NULL;
681 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
684 ab_pkg->pkgs = pkg_vec_alloc();
686 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
688 for (count = 1, item = strtok_r(list, ", ", &tok);
690 count++, item = strtok_r(NULL, ", ", &tok), count++) {
691 tmp = realloc(replaces, sizeof(abstract_pkg_t *) * (count + 1));
696 old_abpkg = ensure_abstract_pkg_by_name(item);
698 if (pkg->state_flag & SF_NEED_DETAIL) {
699 if (!(old_abpkg->state_flag & SF_NEED_DETAIL)) {
700 opkg_msg(DEBUG, "propagating pkg flag to replaced abpkg %s\n",
702 old_abpkg->state_flag |= SF_NEED_DETAIL;
706 if (!old_abpkg->replaced_by)
707 old_abpkg->replaced_by = abstract_pkg_vec_alloc();
709 /* if a package pkg both replaces and conflicts old_abpkg,
710 * then add it to the replaced_by vector so that old_abpkg
711 * will be upgraded to ab_pkg automatically */
712 if (pkg_conflicts_abstract(pkg, old_abpkg)) {
713 if (!abstract_pkg_vec_contains(old_abpkg->replaced_by, ab_pkg))
714 abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
718 replaces[count - 1] = old_abpkg;
724 replaces[count - 1] = NULL;
726 pkg_set_ptr(pkg, PKG_REPLACES, replaces);
729 void buildProvides(abstract_pkg_t * ab_pkg, pkg_t * pkg)
734 /* every pkg provides itself */
735 pkg->provides_count++;
736 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
737 pkg->provides = xcalloc(pkg->provides_count, sizeof(abstract_pkg_t *));
738 pkg->provides[0] = ab_pkg;
739 for (i = 1; i < pkg->provides_count; i++) {
740 abstract_pkg_t *provided_abpkg =
741 ensure_abstract_pkg_by_name(pkg->provides_str[i - 1]);
742 free(pkg->provides_str[i - 1]);
744 pkg->provides[i] = provided_abpkg;
746 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
748 if (pkg->provides_str)
749 free(pkg->provides_str);
753 void buildConflicts(pkg_t * pkg)
757 compound_depend_t *conflicts, *conflict;
759 if (!pkg->conflicts_count)
762 conflicts = pkg->conflicts =
763 xcalloc(pkg->conflicts_count, sizeof(compound_depend_t));
764 for (i = 0; i < pkg->conflicts_count; i++) {
765 conflicts->type = CONFLICTS;
766 parseDepends(conflicts, pkg->conflicts_str[i]);
767 free(pkg->conflicts_str[i]);
770 if (pkg->conflicts_str)
771 free(pkg->conflicts_str);
775 void buildReplaces(abstract_pkg_t * ab_pkg, pkg_t * pkg)
780 if (!pkg->replaces_count)
783 pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *));
785 for (i = 0; i < pkg->replaces_count; i++) {
786 abstract_pkg_t *old_abpkg =
787 ensure_abstract_pkg_by_name(pkg->replaces_str[i]);
789 pkg->replaces[i] = old_abpkg;
790 free(pkg->replaces_str[i]);
792 if (!old_abpkg->replaced_by)
793 old_abpkg->replaced_by = abstract_pkg_vec_alloc();
794 /* if a package pkg both replaces and conflicts old_abpkg,
795 * then add it to the replaced_by vector so that old_abpkg
796 * will be upgraded to ab_pkg automatically */
797 if (pkg_conflicts_abstract(pkg, old_abpkg))
798 abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
801 if (pkg->replaces_str)
802 free(pkg->replaces_str);
806 void parse_deplist(pkg_t *pkg, enum depend_type type, char *list)
810 compound_depend_t *tmp, *deps;
830 deps = pkg_get_ptr(pkg, id);
832 for (tmp = deps, count = 1; tmp && tmp->type; tmp++)
835 for (item = strtok_r(list, ",", &tok); item; item = strtok_r(NULL, ",", &tok), count++) {
836 tmp = realloc(deps, sizeof(compound_depend_t) * (count + 1));
843 memset(deps + count - 1, 0, sizeof(compound_depend_t));
844 parseDepends(deps + count - 1, item, type);
850 memset(deps + count - 1, 0, sizeof(compound_depend_t));
851 pkg_set_ptr(pkg, id, deps);
854 void buildDepends(pkg_t * pkg)
859 compound_depend_t *depends;
863 pkg->pre_depends_count + pkg->depends_count +
864 pkg->recommends_count + pkg->suggests_count))
867 depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t));
869 for (i = 0; i < pkg->pre_depends_count; i++) {
870 parseDepends(depends, pkg->pre_depends_str[i]);
871 free(pkg->pre_depends_str[i]);
872 depends->type = PREDEPEND;
875 if (pkg->pre_depends_str)
876 free(pkg->pre_depends_str);
878 for (i = 0; i < pkg->depends_count; i++) {
879 parseDepends(depends, pkg->depends_str[i]);
880 free(pkg->depends_str[i]);
883 if (pkg->depends_str)
884 free(pkg->depends_str);
886 for (i = 0; i < pkg->recommends_count; i++) {
887 parseDepends(depends, pkg->recommends_str[i]);
888 free(pkg->recommends_str[i]);
889 depends->type = RECOMMEND;
892 if (pkg->recommends_str)
893 free(pkg->recommends_str);
895 for (i = 0; i < pkg->suggests_count; i++) {
896 parseDepends(depends, pkg->suggests_str[i]);
897 free(pkg->suggests_str[i]);
898 depends->type = SUGGEST;
901 if (pkg->suggests_str)
902 free(pkg->suggests_str);
907 const char *constraint_to_str(enum version_constraint c)
928 * Returns a printable string for pkg's dependency at the specified idx. The
929 * resultant string must be passed to free() by the caller.
931 char *pkg_depend_str(pkg_t * pkg, int idx)
936 compound_depend_t *cdep = NULL, *p;
939 for (i = 0, p = pkg_get_ptr(pkg, PKG_DEPENDS); p && p->type; i++, p++)
950 /* calculate string length */
951 for (i = 0; i < cdep->possibility_count; i++) {
952 dep = cdep->possibilities[i];
955 len += 3; /* space, pipe, space */
957 len += strlen(dep->pkg->name);
960 len += 2; /* space, left parenthesis */
961 len += 3; /* constraint string (<=, >=, etc), space */
962 len += strlen(dep->version);
963 len += 1; /* right parenthesis */
967 str = xmalloc(len + 1); /* +1 for the NULL terminator */
970 for (i = 0; i < cdep->possibility_count; i++) {
971 dep = cdep->possibilities[i];
974 strncat(str, " | ", len);
976 strncat(str, dep->pkg->name, len);
979 strncat(str, " (", len);
980 strncat(str, constraint_to_str(dep->constraint), len);
981 strncat(str, dep->version, len);
982 strncat(str, ")", len);
989 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
991 compound_depend_t *depends;
994 abstract_pkg_t *ab_depend;
995 abstract_pkg_t **temp;
997 for (depends = pkg_get_ptr(pkg, PKG_DEPENDS); depends && depends->type; depends++) {
998 if (depends->type != PREDEPEND
999 && depends->type != DEPEND && depends->type != RECOMMEND)
1001 for (j = 0; j < depends->possibility_count; j++) {
1002 ab_depend = depends->possibilities[j]->pkg;
1003 if (!ab_depend->depended_upon_by) {
1004 ab_depend->depended_upon_by =
1005 xcalloc(1, sizeof(abstract_pkg_t *));
1008 temp = ab_depend->depended_upon_by;
1016 ab_depend->depended_upon_by =
1017 xrealloc(ab_depend->depended_upon_by,
1019 1) * sizeof(abstract_pkg_t *));
1021 /* the array may have been moved by realloc */
1022 temp = ab_depend->depended_upon_by + othercount;
1028 static depend_t *depend_init(void)
1030 depend_t *d = xcalloc(1, sizeof(depend_t));
1031 d->constraint = NONE;
1038 static int parseDepends(compound_depend_t * compound_depend, char *depend_str, enum depend_type type)
1041 char *depend, *name, *vstr, *rest, *tok = NULL;
1042 depend_t **possibilities = NULL, **tmp;
1044 compound_depend->type = type;
1046 for (i = 0, depend = strtok_r(depend_str, "|", &tok); depend; i++, depend = strtok_r(NULL, "|", &tok)) {
1047 name = strtok(depend, " ");
1048 rest = strtok(NULL, "\n");
1050 tmp = realloc(possibilities, sizeof(tmp) * (i + 1));
1055 possibilities = tmp;
1056 possibilities[i] = depend_init();
1057 possibilities[i]->pkg = ensure_abstract_pkg_by_name(name);
1059 if (rest && *rest == '(') {
1060 vstr = strtok(rest + 1, ")");
1062 if (!strncmp(vstr, "<<", 2)) {
1063 possibilities[i]->constraint = EARLIER;
1065 } else if (!strncmp(vstr, "<=", 2)) {
1066 possibilities[i]->constraint = EARLIER_EQUAL;
1068 } else if (!strncmp(vstr, ">=", 2)) {
1069 possibilities[i]->constraint = LATER_EQUAL;
1071 } else if (!strncmp(vstr, ">>", 2)) {
1072 possibilities[i]->constraint = LATER;
1074 } else if (!strncmp(vstr, "=", 1)) {
1075 possibilities[i]->constraint = EQUAL;
1078 /* should these be here to support deprecated designations; dpkg does */
1079 else if (!strncmp(vstr, "<", 1)) {
1080 possibilities[i]->constraint = EARLIER_EQUAL;
1082 } else if (!strncmp(vstr, ">", 1)) {
1083 possibilities[i]->constraint = LATER_EQUAL;
1087 possibilities[i]->version = trim_xstrdup(vstr);
1088 rest = strtok(NULL, " ");
1091 rest = strtok(rest, " ");
1094 if (rest && *rest == '*')
1095 compound_depend->type = GREEDY_DEPEND;
1098 compound_depend->possibility_count = i;
1099 compound_depend->possibilities = possibilities;
1104 compound_depend_t *pkg_get_depends(pkg_t *pkg, enum depend_type type)
1106 compound_depend_t *dep;
1108 for (dep = pkg_get_ptr(pkg, PKG_DEPENDS); dep && dep->type; dep++)
1109 if (type == UNSPEC || dep->type == type)