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;
48 if (version_constraints_satisfied(depend, pkg))
54 /* returns ndependences or negative error value */
55 int pkg_hash_fetch_unsatisfied_dependencies(opkg_conf_t *conf, pkg_t * pkg,
56 pkg_vec_t *unsatisfied, char *** unresolved)
58 pkg_t * satisfier_entry_pkg;
62 abstract_pkg_t * ab_pkg;
65 * this is a setup to check for redundant/cyclic dependency checks,
66 * which are marked at the abstract_pkg level
68 if (!(ab_pkg = pkg->parent)) {
69 fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
73 if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */
77 ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */
81 count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
89 /* foreach dependency */
90 for (i = 0; i < count; i++) {
91 compound_depend_t * compound_depend = &pkg->depends[i];
92 depend_t ** possible_satisfiers = compound_depend->possibilities;;
94 satisfier_entry_pkg = NULL;
96 if (compound_depend->type == GREEDY_DEPEND) {
97 /* foreach possible satisfier */
98 for (j = 0; j < compound_depend->possibility_count; j++) {
99 /* foreach provided_by, which includes the abstract_pkg itself */
100 abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
101 abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
102 int nposs = ab_provider_vec->len;
103 abstract_pkg_t **ab_providers = ab_provider_vec->pkgs;
105 for (l = 0; l < nposs; l++) {
106 pkg_vec_t *test_vec = ab_providers[l]->pkgs;
107 /* if no depends on this one, try the first package that Provides this one */
108 if (!test_vec){ /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */
112 /* cruise this possiblity's pkg_vec looking for an installed version */
113 for (k = 0; k < test_vec->len; k++) {
114 pkg_t *pkg_scout = test_vec->pkgs[k];
115 /* not installed, and not already known about? */
116 if ((pkg_scout->state_want != SW_INSTALL)
117 && !pkg_scout->parent->dependencies_checked
118 && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
119 char ** newstuff = NULL;
121 pkg_vec_t *tmp_vec = pkg_vec_alloc ();
122 /* check for not-already-installed dependencies */
123 rc = pkg_hash_fetch_unsatisfied_dependencies(conf,
127 if (newstuff == NULL) {
130 for (i = 0; i < rc; i++) {
131 pkg_t *p = tmp_vec->pkgs[i];
132 if (p->state_want == SW_INSTALL)
134 opkg_message(conf, OPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
138 pkg_vec_free (tmp_vec);
140 /* mark this one for installation */
141 opkg_message(conf, OPKG_NOTICE, "Adding satisfier for greedy dependence: %s\n", pkg_scout->name);
142 pkg_vec_insert(unsatisfied, pkg_scout);
145 opkg_message(conf, OPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
156 /* foreach possible satisfier, look for installed package */
157 for (j = 0; j < compound_depend->possibility_count; j++) {
158 /* foreach provided_by, which includes the abstract_pkg itself */
159 depend_t *dependence_to_satisfy = possible_satisfiers[j];
160 abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
161 pkg_t *satisfying_pkg =
162 pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg,
163 pkg_installed_and_constraint_satisfied,
164 dependence_to_satisfy, 1, NULL);
165 /* Being that I can't test constraing in pkg_hash, I will test it here */
166 if (satisfying_pkg != NULL) {
167 if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
168 satisfying_pkg = NULL;
171 opkg_message(conf, OPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
172 if (satisfying_pkg != NULL) {
178 /* if nothing installed matches, then look for uninstalled satisfier */
180 /* foreach possible satisfier, look for installed package */
181 for (j = 0; j < compound_depend->possibility_count; j++) {
182 /* foreach provided_by, which includes the abstract_pkg itself */
183 depend_t *dependence_to_satisfy = possible_satisfiers[j];
184 abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
185 pkg_t *satisfying_pkg =
186 pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg,
187 pkg_constraint_satisfied,
188 dependence_to_satisfy, 1, NULL);
189 /* Being that I can't test constraing in pkg_hash, I will test it here too */
190 if (satisfying_pkg != NULL) {
191 if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
192 satisfying_pkg = NULL;
196 /* user request overrides package recommendation */
197 if (satisfying_pkg != NULL
198 && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
199 && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
200 opkg_message (conf, OPKG_NOTICE, "%s: ignoring recommendation for %s at user request\n",
201 pkg->name, satisfying_pkg->name);
205 opkg_message(conf, OPKG_DEBUG, "%s:%d: satisfying_pkg=%p\n", __FILE__, __LINE__, satisfying_pkg);
206 if (satisfying_pkg != NULL) {
207 satisfier_entry_pkg = satisfying_pkg;
213 /* we didn't find one, add something to the unsatisfied vector */
215 if (!satisfier_entry_pkg) {
216 /* failure to meet recommendations is not an error */
217 if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
218 the_lost = add_unresolved_dep(pkg, the_lost, i);
220 opkg_message (conf, OPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
221 pkg->name, compound_depend->possibilities[0]->pkg->name);
224 if (compound_depend->type == SUGGEST) {
225 /* just mention it politely */
226 opkg_message (conf, OPKG_NOTICE, "package %s suggests installing %s\n",
227 pkg->name, satisfier_entry_pkg->name);
229 char ** newstuff = NULL;
231 if (satisfier_entry_pkg != pkg &&
232 !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) {
233 pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
234 pkg_hash_fetch_unsatisfied_dependencies(conf,
238 the_lost = merge_unresolved(the_lost, newstuff);
244 *unresolved = the_lost;
246 return unsatisfied->len;
249 /*checking for conflicts !in replaces
250 If a packages conflicts with another but is also replacing it, I should not consider it a
252 returns 0 if conflicts <> replaces or 1 if conflicts == replaces
254 int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
257 int replaces_count = pkg->replaces_count;
258 abstract_pkg_t **replaces;
260 if (pkg->replaces_count==0) // No replaces, it's surely a conflict
263 replaces = pkg->replaces;
265 for (i = 0; i < replaces_count; i++) {
266 if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) { // Found
267 opkg_message(NULL, OPKG_DEBUG2, "Seems I've found a replace %s %s \n",pkg_scout->name,pkg->replaces[i]->name);
276 /* Abhaya: added support for conflicts */
277 pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
279 pkg_vec_t * installed_conflicts, * test_vec;
280 compound_depend_t * conflicts;
281 depend_t ** possible_satisfiers;
282 depend_t * possible_satisfier;
283 register int i, j, k;
285 abstract_pkg_t * ab_pkg;
290 * this is a setup to check for redundant/cyclic dependency checks,
291 * which are marked at the abstract_pkg level
293 if(!(ab_pkg = pkg->parent)){
294 fprintf(stderr, "dependency check error. pkg %s isn't in hash table\n", pkg->name);
295 return (pkg_vec_t *)NULL;
298 conflicts = pkg->conflicts;
300 return (pkg_vec_t *)NULL;
302 installed_conflicts = pkg_vec_alloc();
304 count = pkg->conflicts_count;
308 /* foreach conflict */
309 for(i = 0; i < pkg->conflicts_count; i++){
311 possible_satisfiers = conflicts->possibilities;
313 /* foreach possible satisfier */
314 for(j = 0; j < conflicts->possibility_count; j++){
315 possible_satisfier = possible_satisfiers[j];
316 if (!possible_satisfier)
317 fprintf(stderr, "%s:%d: possible_satisfier is null\n", __FUNCTION__, __LINE__);
318 if (!possible_satisfier->pkg)
319 fprintf(stderr, "%s:%d: possible_satisfier->pkg is null\n", __FUNCTION__, __LINE__);
320 test_vec = possible_satisfier->pkg->pkgs;
322 /* pkg_vec found, it is an actual package conflict
323 * cruise this possiblity's pkg_vec looking for an installed version */
324 pkg_scouts = test_vec->pkgs;
325 for(k = 0; k < test_vec->len; k++){
326 pkg_scout = pkg_scouts[k];
328 fprintf(stderr, "%s: null pkg scout\n", __FUNCTION__);
331 if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
332 version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
333 if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
334 pkg_vec_insert(installed_conflicts, pkg_scout);
343 if (installed_conflicts->len)
344 return installed_conflicts;
345 pkg_vec_free(installed_conflicts);
346 return (pkg_vec_t *)NULL;
349 int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
354 if(depends->constraint == NONE)
359 parseVersion(temp, depends->version);
361 comparison = pkg_compare_versions(pkg, temp);
363 free (temp->version);
366 if((depends->constraint == EARLIER) &&
369 else if((depends->constraint == LATER) &&
372 else if(comparison == 0)
374 else if((depends->constraint == LATER_EQUAL) &&
377 else if((depends->constraint == EARLIER_EQUAL) &&
384 int pkg_dependence_satisfiable(opkg_conf_t *conf, depend_t *depend)
386 abstract_pkg_t *apkg = depend->pkg;
387 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
388 int n_providers = provider_apkgs->len;
389 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
395 for (i = 0; i < n_providers; i++) {
396 abstract_pkg_t *papkg = apkgs[i];
397 pkg_vec = papkg->pkgs;
399 n_pkgs = pkg_vec->len;
400 for (j = 0; j < n_pkgs; j++) {
401 pkg_t *pkg = pkg_vec->pkgs[j];
402 if (version_constraints_satisfied(depend, pkg)) {
411 int pkg_dependence_satisfied(opkg_conf_t *conf, depend_t *depend)
413 abstract_pkg_t *apkg = depend->pkg;
414 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
415 int n_providers = provider_apkgs->len;
416 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
421 for (i = 0; i < n_providers; i++) {
422 abstract_pkg_t *papkg = apkgs[i];
423 pkg_vec_t *pkg_vec = papkg->pkgs;
425 n_pkgs = pkg_vec->len;
426 for (j = 0; j < n_pkgs; j++) {
427 pkg_t *pkg = pkg_vec->pkgs[j];
428 if (version_constraints_satisfied(depend, pkg)) {
429 if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
438 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
441 pkg_t ** pkgs = vec->pkgs;
443 for(i = 0; i < vec->len; i++)
444 if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
445 && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
446 && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
454 * pkg_has_common_provides returns 1 if pkg and replacee both provide
455 * the same abstract package and 0 otherwise.
457 int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
459 abstract_pkg_t **provides = pkg->provides;
460 int provides_count = pkg->provides_count;
461 abstract_pkg_t **replacee_provides = replacee->provides;
462 int replacee_provides_count = replacee->provides_count;
464 for (i = 0; i < provides_count; i++) {
465 abstract_pkg_t *apkg = provides[i];
466 for (j = 0; j < replacee_provides_count; j++) {
467 abstract_pkg_t *replacee_apkg = replacee_provides[i];
468 if (apkg == replacee_apkg)
477 * pkg_provides_abstract returns 1 if pkg->provides contains providee
480 int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
482 abstract_pkg_t **provides = pkg->provides;
483 int provides_count = pkg->provides_count;
485 for (i = 0; i < provides_count; i++) {
486 if (provides[i] == providee)
493 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
496 int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
498 abstract_pkg_t **replaces = pkg->replaces;
499 int replaces_count = pkg->replaces_count;
500 /* abstract_pkg_t **replacee_provides = pkg->provides;
501 int replacee_provides_count = pkg->provides_count; */
503 for (i = 0; i < replaces_count; i++) {
504 abstract_pkg_t *abstract_replacee = replaces[i];
505 for (j = 0; j < replaces_count; j++) {
506 /* opkg_message(NULL, OPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
507 pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
508 if (replacee->provides[j] == abstract_replacee)
517 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
520 int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
522 compound_depend_t *conflicts = pkg->conflicts;
523 int conflicts_count = pkg->conflicts_count;
525 for (i = 0; i < conflicts_count; i++) {
526 int possibility_count = conflicts[i].possibility_count;
527 struct depend **possibilities = conflicts[i].possibilities;
528 for (j = 0; j < possibility_count; j++) {
529 if (possibilities[j]->pkg == conflictee) {
538 * pkg_conflicts returns 1 if pkg->conflicts contains one of
539 * conflictee's provides and 0 otherwise.
541 int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
543 compound_depend_t *conflicts = pkg->conflicts;
544 int conflicts_count = pkg->conflicts_count;
545 abstract_pkg_t **conflictee_provides = conflictee->provides;
546 int conflictee_provides_count = conflictee->provides_count;
548 int possibility_count;
549 struct depend **possibilities;
550 abstract_pkg_t *possibility ;
552 for (i = 0; i < conflicts_count; i++) {
553 possibility_count = conflicts[i].possibility_count;
554 possibilities = conflicts[i].possibilities;
555 for (j = 0; j < possibility_count; j++) {
556 possibility = possibilities[j]->pkg;
557 for (k = 0; k < conflictee_provides_count; k++) {
558 if (possibility == conflictee_provides[k]) {
567 static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
569 int oldlen = 0, newlen = 0;
576 while(oldstuff && oldstuff[oldlen]) oldlen++;
577 while(newstuff && newstuff[newlen]) newlen++;
579 result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
580 if (result == NULL) {
581 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
585 for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
586 *(result + i) = *(newstuff + j);
588 *(result + i) = NULL;
594 * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
595 * this is null terminated, no count is carried around
597 char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
601 char *depend_str = pkg_depend_str(pkg, ref_ndx);
604 while(the_lost && the_lost[count]) count++;
606 count++; /* need one to hold the null */
607 resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1));
608 if (resized == NULL) {
609 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
612 resized[count - 1] = strdup(depend_str);
613 resized[count] = NULL;
618 void printDepends(pkg_t * pkg)
621 compound_depend_t * depend;
624 count = pkg->pre_depends_count + pkg->depends_count;
626 depend = pkg->depends;
628 fprintf(stderr, "Depends pointer is NULL\n");
631 for(i = 0; i < count; i++){
632 fprintf(stderr, "%s has %d possibilities:\n",
633 (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"),
634 depend->possibility_count);
635 for(j = 0; j < depend->possibility_count; j++)
636 fprintf(stderr, "\t%s version %s (%d)\n",
637 depend->possibilities[j]->pkg->name,
638 depend->possibilities[j]->version,
639 depend->possibilities[j]->constraint);
644 int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
648 /* every pkg provides itself */
649 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
651 if (!pkg->provides_count)
656 pkg->provides = (abstract_pkg_t **)calloc((pkg->provides_count + 1), sizeof(abstract_pkg_t *));
657 if (pkg->provides == NULL) {
658 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
661 pkg->provides[0] = ab_pkg;
663 // if (strcmp(ab_pkg->name, pkg->name))
664 // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
666 for(i = 0; i < pkg->provides_count; i++){
667 abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
669 pkg->provides[i+1] = provided_abpkg;
672 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
677 /* Abhaya: added conflicts support */
678 int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
681 compound_depend_t * conflicts;
683 if (!pkg->conflicts_count)
686 conflicts = pkg->conflicts = calloc(pkg->conflicts_count, sizeof(compound_depend_t));
687 if (conflicts == NULL) {
688 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
691 for (i = 0; i < pkg->conflicts_count; i++) {
692 conflicts->type = CONFLICTS;
693 parseDepends(conflicts, hash,
694 pkg->conflicts_str[i]);
700 int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
704 if (!pkg->replaces_count)
707 pkg->replaces = (abstract_pkg_t **)calloc(pkg->replaces_count, sizeof(abstract_pkg_t *));
708 if (pkg->replaces == NULL) {
709 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
713 for(i = 0; i < pkg->replaces_count; i++){
714 abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
716 pkg->replaces[i] = old_abpkg;
719 if (!old_abpkg->replaced_by)
720 old_abpkg->replaced_by = abstract_pkg_vec_alloc();
721 if ( old_abpkg->replaced_by == NULL ){
724 /* if a package pkg both replaces and conflicts old_abpkg,
725 * then add it to the replaced_by vector so that old_abpkg
726 * will be upgraded to ab_pkg automatically */
727 if (pkg_conflicts_abstract(pkg, old_abpkg))
728 abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
733 int buildDepends(hash_table_t * hash, pkg_t * pkg)
737 compound_depend_t * depends;
739 if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
742 if (0 && pkg->pre_depends_count)
743 fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
744 pkg->name, pkg->pre_depends_count, pkg->depends_count);
745 depends = pkg->depends = calloc(count, sizeof(compound_depend_t));
746 if (depends == NULL) {
747 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
752 for(i = 0; i < pkg->pre_depends_count; i++){
753 parseDepends(depends, hash, pkg->pre_depends_str[i]);
754 if (0 && pkg->pre_depends_count)
755 fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n",
756 pkg->pre_depends_str[i], depends, depends->possibility_count);
757 depends->type = PREDEPEND;
761 for(i = 0; i < pkg->recommends_count; i++){
762 parseDepends(depends, hash, pkg->recommends_str[i]);
763 if (0 && pkg->recommends_count)
764 fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n",
765 pkg->recommends_str[i], depends, depends->possibility_count);
766 depends->type = RECOMMEND;
770 for(i = 0; i < pkg->suggests_count; i++){
771 parseDepends(depends, hash, pkg->suggests_str[i]);
772 if (0 && pkg->suggests_count)
773 fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n",
774 pkg->suggests_str[i], depends, depends->possibility_count);
775 depends->type = SUGGEST;
779 for(i = 0; i < pkg->depends_count; i++){
780 parseDepends(depends, hash, pkg->depends_str[i]);
781 if (0 && pkg->depends_count)
782 fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n",
783 pkg->depends_str[i], depends, depends->possibility_count);
790 * pkg_depend_string: returns the depends string specified by index.
791 * All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
792 * [0,npredepends) -> returns pre_depends_str[index]
793 * [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
794 * [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
795 * [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
797 char *pkg_depend_str(pkg_t *pkg, int index)
799 if (index < pkg->pre_depends_count) {
800 return pkg->pre_depends_str[index];
802 index -= pkg->pre_depends_count;
804 if (index < pkg->recommends_count) {
805 return pkg->recommends_str[index];
807 index -= pkg->recommends_count;
809 if (index < pkg->suggests_count) {
810 return pkg->suggests_str[index];
812 index -= pkg->suggests_count;
814 if (index < pkg->depends_count) {
815 return pkg->depends_str[index];
817 fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
821 void freeDepends(pkg_t *pkg)
825 if (pkg == NULL || pkg->depends == NULL) {
829 fprintf(stderr, "Freeing depends=%p\n", pkg->depends);
830 for (i=0; i < pkg->depends->possibility_count; i++) {
831 depend_deinit(pkg->depends->possibilities[i]);
833 free(pkg->depends->possibilities);
838 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
840 compound_depend_t * depends;
841 int count, othercount;
843 abstract_pkg_t * ab_depend;
844 abstract_pkg_t ** temp;
846 count = pkg->pre_depends_count + pkg->depends_count;
847 depends = pkg->depends;
849 if (0 && pkg->pre_depends_count)
850 fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
851 pkg->name, pkg->pre_depends_count, pkg->depends_count);
852 for (i = 0; i < count; i++) {
853 if (0 && pkg->pre_depends_count)
854 fprintf(stderr, " i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends);
855 for (j = 0; j < depends->possibility_count; j++){
856 ab_depend = depends->possibilities[j]->pkg;
857 if(!ab_depend->depended_upon_by)
858 ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *));
860 temp = ab_depend->depended_upon_by;
868 ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by,
869 (othercount + 1) * sizeof(abstract_pkg_t *));
870 /* the array may have moved */
871 temp = ab_depend->depended_upon_by + othercount;
878 static depend_t * depend_init(void)
880 depend_t * d = (depend_t *)calloc(1, sizeof(depend_t));
882 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
885 d->constraint = NONE;
892 static void depend_deinit(depend_t *d)
897 static int parseDepends(compound_depend_t *compound_depend,
898 hash_table_t * hash, char * depend_str)
900 char * pkg_name, buffer[2048];
903 register char * src, * dest;
904 depend_t ** possibilities;
906 /* first count the number of ored possibilities for satisfying dependency */
912 compound_depend->type = DEPEND;
914 compound_depend->possibility_count = num_of_ors + 1;
915 possibilities = (depend_t **)calloc((num_of_ors + 1), sizeof(depend_t *) );
918 compound_depend->possibilities = possibilities;
921 for(i = 0; i < num_of_ors + 1; i++){
922 possibilities[i] = depend_init();
923 if (!possibilities[i])
925 /* gobble up just the name first */
934 pkg_name = trim_alloc(buffer);
935 if (pkg_name == NULL )
938 /* now look at possible version info */
940 /* skip to next chars */
942 while(*src && isspace(*src)) src++;
944 /* extract constraint and version */
947 if(!strncmp(src, "<<", 2)){
948 possibilities[i]->constraint = EARLIER;
951 else if(!strncmp(src, "<=", 2)){
952 possibilities[i]->constraint = EARLIER_EQUAL;
955 else if(!strncmp(src, ">=", 2)){
956 possibilities[i]->constraint = LATER_EQUAL;
959 else if(!strncmp(src, ">>", 2)){
960 possibilities[i]->constraint = LATER;
963 else if(!strncmp(src, "=", 1)){
964 possibilities[i]->constraint = EQUAL;
967 /* should these be here to support deprecated designations; dpkg does */
968 else if(!strncmp(src, "<", 1)){
969 possibilities[i]->constraint = EARLIER_EQUAL;
972 else if(!strncmp(src, ">", 1)){
973 possibilities[i]->constraint = LATER_EQUAL;
977 /* now we have any constraint, pass space to version string */
978 while(isspace(*src)) src++;
980 /* this would be the version string */
982 while(*src && *src != ')')
986 possibilities[i]->version = trim_alloc(buffer);
987 /* fprintf(stderr, "let's print the depends version string:");
988 fprintf(stderr, "version %s\n", possibilities[i]->version);*/
989 if (possibilities[i]->version == NULL )
994 /* hook up the dependency to its abstract pkg */
995 possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
999 /* now get past the ) and any possible | chars */
1007 compound_depend->type = GREEDY_DEPEND;