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.
23 #include "opkg_utils.h"
25 #include "opkg_message.h"
26 #include "pkg_parse.h"
27 #include "hash_table.h"
29 static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str);
30 static depend_t * depend_init(void);
31 static void depend_deinit(depend_t *d);
32 static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx);
33 static char ** merge_unresolved(char ** oldstuff, char ** newstuff);
34 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
36 static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
38 depend_t *depend = (depend_t *)cdata;
39 if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
45 static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
47 depend_t *depend = (depend_t *)cdata;
49 pkg_t * temp = pkg_new();
51 parseVersion(temp, depend->version);
52 comparison = pkg_compare_versions(pkg, temp);
55 fprintf(stderr, "%s: pkg=%s pkg->version=%s constraint=%p type=%d version=%s comparison=%d satisfied=%d\n",
56 __FUNCTION__, pkg->name, pkg->version,
57 depend, depend->constraint, depend->version,
58 comparison, version_constraints_satisfied(depend, pkg));
60 if (version_constraints_satisfied(depend, pkg))
66 /* returns ndependences or negative error value */
67 int pkg_hash_fetch_unsatisfied_dependencies(opkg_conf_t *conf, pkg_t * pkg,
68 pkg_vec_t *unsatisfied, char *** unresolved)
70 pkg_t * satisfier_entry_pkg;
74 abstract_pkg_t * ab_pkg;
77 * this is a setup to check for redundant/cyclic dependency checks,
78 * which are marked at the abstract_pkg level
80 if (!(ab_pkg = pkg->parent)) {
81 fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
85 if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */
89 ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */
93 count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
101 /* foreach dependency */
102 for (i = 0; i < count; i++) {
103 compound_depend_t * compound_depend = &pkg->depends[i];
104 depend_t ** possible_satisfiers = compound_depend->possibilities;;
106 satisfier_entry_pkg = NULL;
108 if (compound_depend->type == GREEDY_DEPEND) {
109 /* foreach possible satisfier */
110 for (j = 0; j < compound_depend->possibility_count; j++) {
111 /* foreach provided_by, which includes the abstract_pkg itself */
112 abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
113 abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
114 int nposs = ab_provider_vec->len;
115 abstract_pkg_t **ab_providers = ab_provider_vec->pkgs;
117 for (l = 0; l < nposs; l++) {
118 pkg_vec_t *test_vec = ab_providers[l]->pkgs;
119 /* if no depends on this one, try the first package that Provides this one */
120 if (!test_vec){ /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */
124 /* cruise this possiblity's pkg_vec looking for an installed version */
125 for (k = 0; k < test_vec->len; k++) {
126 pkg_t *pkg_scout = test_vec->pkgs[k];
127 /* not installed, and not already known about? */
128 if ((pkg_scout->state_want != SW_INSTALL)
129 && !pkg_scout->parent->dependencies_checked
130 && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
131 char ** newstuff = NULL;
133 pkg_vec_t *tmp_vec = pkg_vec_alloc ();
134 /* check for not-already-installed dependencies */
135 rc = pkg_hash_fetch_unsatisfied_dependencies(conf,
139 if (newstuff == NULL) {
142 for (i = 0; i < rc; i++) {
143 pkg_t *p = tmp_vec->pkgs[i];
144 if (p->state_want == SW_INSTALL)
146 opkg_message(conf, OPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
150 pkg_vec_free (tmp_vec);
152 /* mark this one for installation */
153 opkg_message(conf, OPKG_NOTICE, "Adding satisfier for greedy dependence: %s\n", pkg_scout->name);
154 pkg_vec_insert(unsatisfied, pkg_scout);
157 opkg_message(conf, OPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
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(conf, 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 ( satisfying_pkg,dependence_to_satisfy)) {
180 satisfying_pkg = NULL;
183 opkg_message(conf, OPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
184 if (satisfying_pkg != NULL) {
190 /* if nothing installed matches, then look for uninstalled satisfier */
192 /* foreach possible satisfier, look for installed package */
193 for (j = 0; j < compound_depend->possibility_count; j++) {
194 /* foreach provided_by, which includes the abstract_pkg itself */
195 depend_t *dependence_to_satisfy = possible_satisfiers[j];
196 abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
197 pkg_t *satisfying_pkg =
198 pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg,
199 pkg_constraint_satisfied,
200 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,dependence_to_satisfy)) {
204 satisfying_pkg = NULL;
208 /* user request overrides package recommendation */
209 if (satisfying_pkg != NULL
210 && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
211 && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
212 opkg_message (conf, OPKG_NOTICE, "%s: ignoring recommendation for %s at user request\n",
213 pkg->name, satisfying_pkg->name);
217 opkg_message(conf, OPKG_DEBUG, "%s:%d: satisfying_pkg=%p\n", __FILE__, __LINE__, satisfying_pkg);
218 if (satisfying_pkg != NULL) {
219 satisfier_entry_pkg = satisfying_pkg;
225 /* we didn't find one, add something to the unsatisfied vector */
227 if (!satisfier_entry_pkg) {
228 /* failure to meet recommendations is not an error */
229 if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
230 the_lost = add_unresolved_dep(pkg, the_lost, i);
232 opkg_message (conf, OPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
233 pkg->name, compound_depend->possibilities[0]->pkg->name);
236 if (compound_depend->type == SUGGEST) {
237 /* just mention it politely */
238 opkg_message (conf, OPKG_NOTICE, "package %s suggests installing %s\n",
239 pkg->name, satisfier_entry_pkg->name);
241 char ** newstuff = NULL;
243 if (satisfier_entry_pkg != pkg &&
244 !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) {
245 pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
246 pkg_hash_fetch_unsatisfied_dependencies(conf,
250 the_lost = merge_unresolved(the_lost, newstuff);
256 *unresolved = the_lost;
258 return unsatisfied->len;
261 /*checking for conflicts !in replaces
262 If a packages conflicts with another but is also replacing it, I should not consider it a
264 returns 0 if conflicts <> replaces or 1 if conflicts == replaces
266 int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
269 int replaces_count = pkg->replaces_count;
270 abstract_pkg_t **replaces;
272 if (pkg->replaces_count==0) // No replaces, it's surely a conflict
275 replaces = pkg->replaces;
277 for (i = 0; i < replaces_count; i++) {
278 if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) { // Found
279 opkg_message(NULL, OPKG_DEBUG2, "Seems I've found a replace %s %s \n",pkg_scout->name,pkg->replaces[i]->name);
288 /* Abhaya: added support for conflicts */
289 pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
291 pkg_vec_t * installed_conflicts, * test_vec;
292 compound_depend_t * conflicts;
293 depend_t ** possible_satisfiers;
294 depend_t * possible_satisfier;
295 register int i, j, k;
297 abstract_pkg_t * ab_pkg;
302 * this is a setup to check for redundant/cyclic dependency checks,
303 * which are marked at the abstract_pkg level
305 if(!(ab_pkg = pkg->parent)){
306 fprintf(stderr, "dependency check error. pkg %s isn't in hash table\n", pkg->name);
307 return (pkg_vec_t *)NULL;
310 conflicts = pkg->conflicts;
312 return (pkg_vec_t *)NULL;
314 installed_conflicts = pkg_vec_alloc();
316 count = pkg->conflicts_count;
320 /* foreach conflict */
321 for(i = 0; i < pkg->conflicts_count; i++){
323 possible_satisfiers = conflicts->possibilities;
325 /* foreach possible satisfier */
326 for(j = 0; j < conflicts->possibility_count; j++){
327 possible_satisfier = possible_satisfiers[j];
328 if (!possible_satisfier)
329 fprintf(stderr, "%s:%d: possible_satisfier is null\n", __FUNCTION__, __LINE__);
330 if (!possible_satisfier->pkg)
331 fprintf(stderr, "%s:%d: possible_satisfier->pkg is null\n", __FUNCTION__, __LINE__);
332 test_vec = possible_satisfier->pkg->pkgs;
334 /* pkg_vec found, it is an actual package conflict
335 * cruise this possiblity's pkg_vec looking for an installed version */
336 pkg_scouts = test_vec->pkgs;
337 for(k = 0; k < test_vec->len; k++){
338 pkg_scout = pkg_scouts[k];
340 fprintf(stderr, "%s: null pkg scout\n", __FUNCTION__);
343 if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
344 version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
345 if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
346 pkg_vec_insert(installed_conflicts, pkg_scout);
355 if (installed_conflicts->len)
356 return installed_conflicts;
357 pkg_vec_free(installed_conflicts);
358 return (pkg_vec_t *)NULL;
361 int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
366 if(depends->constraint == NONE)
371 parseVersion(temp, depends->version);
373 comparison = pkg_compare_versions(pkg, temp);
375 free (temp->version);
378 if((depends->constraint == EARLIER) &&
381 else if((depends->constraint == LATER) &&
384 else if(comparison == 0)
386 else if((depends->constraint == LATER_EQUAL) &&
389 else if((depends->constraint == EARLIER_EQUAL) &&
396 int pkg_dependence_satisfiable(opkg_conf_t *conf, depend_t *depend)
398 abstract_pkg_t *apkg = depend->pkg;
399 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
400 int n_providers = provider_apkgs->len;
401 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
407 for (i = 0; i < n_providers; i++) {
408 abstract_pkg_t *papkg = apkgs[i];
409 pkg_vec = papkg->pkgs;
411 n_pkgs = pkg_vec->len;
412 for (j = 0; j < n_pkgs; j++) {
413 pkg_t *pkg = pkg_vec->pkgs[j];
414 if (version_constraints_satisfied(depend, pkg)) {
423 int pkg_dependence_satisfied(opkg_conf_t *conf, depend_t *depend)
425 abstract_pkg_t *apkg = depend->pkg;
426 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
427 int n_providers = provider_apkgs->len;
428 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
433 for (i = 0; i < n_providers; i++) {
434 abstract_pkg_t *papkg = apkgs[i];
435 pkg_vec_t *pkg_vec = papkg->pkgs;
437 n_pkgs = pkg_vec->len;
438 for (j = 0; j < n_pkgs; j++) {
439 pkg_t *pkg = pkg_vec->pkgs[j];
440 if (version_constraints_satisfied(depend, pkg)) {
441 if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
450 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
453 pkg_t ** pkgs = vec->pkgs;
455 for(i = 0; i < vec->len; i++)
456 if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
457 && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
458 && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
466 * pkg_has_common_provides returns 1 if pkg and replacee both provide
467 * the same abstract package and 0 otherwise.
469 int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
471 abstract_pkg_t **provides = pkg->provides;
472 int provides_count = pkg->provides_count;
473 abstract_pkg_t **replacee_provides = replacee->provides;
474 int replacee_provides_count = replacee->provides_count;
476 for (i = 0; i < provides_count; i++) {
477 abstract_pkg_t *apkg = provides[i];
478 for (j = 0; j < replacee_provides_count; j++) {
479 abstract_pkg_t *replacee_apkg = replacee_provides[i];
480 if (apkg == replacee_apkg)
489 * pkg_provides_abstract returns 1 if pkg->provides contains providee
492 int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
494 abstract_pkg_t **provides = pkg->provides;
495 int provides_count = pkg->provides_count;
497 for (i = 0; i < provides_count; i++) {
498 if (provides[i] == providee)
505 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
508 int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
510 abstract_pkg_t **replaces = pkg->replaces;
511 int replaces_count = pkg->replaces_count;
512 /* abstract_pkg_t **replacee_provides = pkg->provides;
513 int replacee_provides_count = pkg->provides_count; */
515 for (i = 0; i < replaces_count; i++) {
516 abstract_pkg_t *abstract_replacee = replaces[i];
517 for (j = 0; j < replaces_count; j++) {
518 /* opkg_message(NULL, OPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
519 pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
520 if (replacee->provides[j] == abstract_replacee)
529 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
532 int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
534 compound_depend_t *conflicts = pkg->conflicts;
535 int conflicts_count = pkg->conflicts_count;
537 for (i = 0; i < conflicts_count; i++) {
538 int possibility_count = conflicts[i].possibility_count;
539 struct depend **possibilities = conflicts[i].possibilities;
540 for (j = 0; j < possibility_count; j++) {
541 if (possibilities[j]->pkg == conflictee) {
550 * pkg_conflicts returns 1 if pkg->conflicts contains one of
551 * conflictee's provides and 0 otherwise.
553 int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
555 compound_depend_t *conflicts = pkg->conflicts;
556 int conflicts_count = pkg->conflicts_count;
557 abstract_pkg_t **conflictee_provides = conflictee->provides;
558 int conflictee_provides_count = conflictee->provides_count;
560 int possibility_count;
561 struct depend **possibilities;
562 abstract_pkg_t *possibility ;
564 for (i = 0; i < conflicts_count; i++) {
565 possibility_count = conflicts[i].possibility_count;
566 possibilities = conflicts[i].possibilities;
567 for (j = 0; j < possibility_count; j++) {
568 possibility = possibilities[j]->pkg;
569 for (k = 0; k < conflictee_provides_count; k++) {
570 if (possibility == conflictee_provides[k]) {
579 static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
581 int oldlen = 0, newlen = 0;
588 while(oldstuff && oldstuff[oldlen]) oldlen++;
589 while(newstuff && newstuff[newlen]) newlen++;
591 result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
592 if (result == NULL) {
593 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
597 for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
598 *(result + i) = *(newstuff + j);
600 *(result + i) = NULL;
606 * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
607 * this is null terminated, no count is carried around
609 char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
613 char *depend_str = pkg_depend_str(pkg, ref_ndx);
616 while(the_lost && the_lost[count]) count++;
618 count++; /* need one to hold the null */
619 resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1));
620 if (resized == NULL) {
621 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
624 resized[count - 1] = strdup(depend_str);
625 resized[count] = NULL;
630 void printDepends(pkg_t * pkg)
633 compound_depend_t * depend;
636 count = pkg->pre_depends_count + pkg->depends_count;
638 depend = pkg->depends;
640 fprintf(stderr, "Depends pointer is NULL\n");
643 for(i = 0; i < count; i++){
644 fprintf(stderr, "%s has %d possibilities:\n",
645 (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"),
646 depend->possibility_count);
647 for(j = 0; j < depend->possibility_count; j++)
648 fprintf(stderr, "\t%s version %s (%d)\n",
649 depend->possibilities[j]->pkg->name,
650 depend->possibilities[j]->version,
651 depend->possibilities[j]->constraint);
656 int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
660 /* every pkg provides itself */
661 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
663 if (!pkg->provides_count)
668 pkg->provides = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * (pkg->provides_count + 1));
669 if (pkg->provides == NULL) {
670 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
673 pkg->provides[0] = ab_pkg;
675 // if (strcmp(ab_pkg->name, pkg->name))
676 // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
678 for(i = 0; i < pkg->provides_count; i++){
679 abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
681 pkg->provides[i+1] = provided_abpkg;
684 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
689 /* Abhaya: added conflicts support */
690 int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
693 compound_depend_t * conflicts;
695 if (!pkg->conflicts_count)
698 conflicts = pkg->conflicts = malloc(sizeof(compound_depend_t) *
699 pkg->conflicts_count);
700 if (conflicts == NULL) {
701 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
704 for (i = 0; i < pkg->conflicts_count; i++) {
705 conflicts->type = CONFLICTS;
706 parseDepends(conflicts, hash,
707 pkg->conflicts_str[i]);
709 for (j = 0; j < conflicts->possibility_count; j++) {
710 depend_t *possibility = conflicts->possibilities[j];
711 abstract_pkg_t *conflicting_apkg = possibility->pkg;
712 pkg_add_conflict_pair(ab_pkg, conflicting_apkg);
720 int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
724 if (!pkg->replaces_count)
727 pkg->replaces = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * pkg->replaces_count);
728 if (pkg->replaces == NULL) {
729 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
733 // if (strcmp(ab_pkg->name, pkg->name))
734 // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
736 for(i = 0; i < pkg->replaces_count; i++){
737 abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
739 pkg->replaces[i] = old_abpkg;
742 if (!old_abpkg->replaced_by)
743 old_abpkg->replaced_by = abstract_pkg_vec_alloc();
744 if ( old_abpkg->replaced_by == NULL ){
747 /* if a package pkg both replaces and conflicts old_abpkg,
748 * then add it to the replaced_by vector so that old_abpkg
749 * will be upgraded to ab_pkg automatically */
750 if (pkg_conflicts_abstract(pkg, old_abpkg))
751 abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
756 int buildDepends(hash_table_t * hash, pkg_t * pkg)
760 compound_depend_t * depends;
762 if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
765 if (0 && pkg->pre_depends_count)
766 fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
767 pkg->name, pkg->pre_depends_count, pkg->depends_count);
768 depends = pkg->depends = malloc(sizeof(compound_depend_t) * count);
769 if (depends == NULL) {
770 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
775 for(i = 0; i < pkg->pre_depends_count; i++){
776 parseDepends(depends, hash, pkg->pre_depends_str[i]);
777 if (0 && pkg->pre_depends_count)
778 fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n",
779 pkg->pre_depends_str[i], depends, depends->possibility_count);
780 depends->type = PREDEPEND;
784 for(i = 0; i < pkg->recommends_count; i++){
785 parseDepends(depends, hash, pkg->recommends_str[i]);
786 if (0 && pkg->recommends_count)
787 fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n",
788 pkg->recommends_str[i], depends, depends->possibility_count);
789 depends->type = RECOMMEND;
793 for(i = 0; i < pkg->suggests_count; i++){
794 parseDepends(depends, hash, pkg->suggests_str[i]);
795 if (0 && pkg->suggests_count)
796 fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n",
797 pkg->suggests_str[i], depends, depends->possibility_count);
798 depends->type = SUGGEST;
802 for(i = 0; i < pkg->depends_count; i++){
803 parseDepends(depends, hash, pkg->depends_str[i]);
804 if (0 && pkg->depends_count)
805 fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n",
806 pkg->depends_str[i], depends, depends->possibility_count);
813 * pkg_depend_string: returns the depends string specified by index.
814 * All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
815 * [0,npredepends) -> returns pre_depends_str[index]
816 * [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
817 * [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
818 * [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
820 char *pkg_depend_str(pkg_t *pkg, int index)
822 if (index < pkg->pre_depends_count) {
823 return pkg->pre_depends_str[index];
825 index -= pkg->pre_depends_count;
827 if (index < pkg->recommends_count) {
828 return pkg->recommends_str[index];
830 index -= pkg->recommends_count;
832 if (index < pkg->suggests_count) {
833 return pkg->suggests_str[index];
835 index -= pkg->suggests_count;
837 if (index < pkg->depends_count) {
838 return pkg->depends_str[index];
840 fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
844 void freeDepends(pkg_t *pkg)
848 if (pkg == NULL || pkg->depends == NULL) {
852 fprintf(stderr, "Freeing depends=%p\n", pkg->depends);
853 for (i=0; i < pkg->depends->possibility_count; i++) {
854 depend_deinit(pkg->depends->possibilities[i]);
856 free(pkg->depends->possibilities);
861 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
863 compound_depend_t * depends;
864 int count, othercount;
866 abstract_pkg_t * ab_depend;
867 abstract_pkg_t ** temp;
869 count = pkg->pre_depends_count + pkg->depends_count;
870 depends = pkg->depends;
872 if (0 && pkg->pre_depends_count)
873 fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
874 pkg->name, pkg->pre_depends_count, pkg->depends_count);
875 for (i = 0; i < count; i++) {
876 if (0 && pkg->pre_depends_count)
877 fprintf(stderr, " i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends);
878 for (j = 0; j < depends->possibility_count; j++){
879 ab_depend = depends->possibilities[j]->pkg;
880 if(!ab_depend->depended_upon_by)
881 ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *));
883 temp = ab_depend->depended_upon_by;
891 ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by,
892 (othercount + 1) * sizeof(abstract_pkg_t *));
893 /* the array may have moved */
894 temp = ab_depend->depended_upon_by + othercount;
901 static depend_t * depend_init(void)
903 depend_t * d = (depend_t *)malloc(sizeof(depend_t));
905 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
908 d->constraint = NONE;
915 static void depend_deinit(depend_t *d)
920 static int parseDepends(compound_depend_t *compound_depend,
921 hash_table_t * hash, char * depend_str)
923 char * pkg_name, buffer[2048];
926 register char * src, * dest;
927 depend_t ** possibilities;
929 /* first count the number of ored possibilities for satisfying dependency */
935 compound_depend->type = DEPEND;
937 compound_depend->possibility_count = num_of_ors + 1;
938 possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1));
941 compound_depend->possibilities = possibilities;
944 for(i = 0; i < num_of_ors + 1; i++){
945 possibilities[i] = depend_init();
946 if (!possibilities[i])
948 /* gobble up just the name first */
957 pkg_name = trim_alloc(buffer);
958 if (pkg_name == NULL )
961 /* now look at possible version info */
963 /* skip to next chars */
965 while(*src && isspace(*src)) src++;
967 /* extract constraint and version */
970 if(!strncmp(src, "<<", 2)){
971 possibilities[i]->constraint = EARLIER;
974 else if(!strncmp(src, "<=", 2)){
975 possibilities[i]->constraint = EARLIER_EQUAL;
978 else if(!strncmp(src, ">=", 2)){
979 possibilities[i]->constraint = LATER_EQUAL;
982 else if(!strncmp(src, ">>", 2)){
983 possibilities[i]->constraint = LATER;
986 else if(!strncmp(src, "=", 1)){
987 possibilities[i]->constraint = EQUAL;
990 /* should these be here to support deprecated designations; dpkg does */
991 else if(!strncmp(src, "<", 1)){
992 possibilities[i]->constraint = EARLIER_EQUAL;
995 else if(!strncmp(src, ">", 1)){
996 possibilities[i]->constraint = LATER_EQUAL;
1000 /* now we have any constraint, pass space to version string */
1001 while(isspace(*src)) src++;
1003 /* this would be the version string */
1005 while(*src && *src != ')')
1009 possibilities[i]->version = trim_alloc(buffer);
1010 /* fprintf(stderr, "let's print the depends version string:");
1011 fprintf(stderr, "version %s\n", possibilities[i]->version);*/
1012 if (possibilities[i]->version == NULL )
1017 /* hook up the dependency to its abstract pkg */
1018 possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
1022 /* now get past the ) and any possible | chars */
1030 compound_depend->type = GREEDY_DEPEND;