1 /* pkg_depends.c - the itsy 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);
377 if((depends->constraint == EARLIER) &&
380 else if((depends->constraint == LATER) &&
383 else if(comparison == 0)
385 else if((depends->constraint == LATER_EQUAL) &&
388 else if((depends->constraint == EARLIER_EQUAL) &&
395 int pkg_dependence_satisfiable(opkg_conf_t *conf, depend_t *depend)
397 abstract_pkg_t *apkg = depend->pkg;
398 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
399 int n_providers = provider_apkgs->len;
400 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
406 for (i = 0; i < n_providers; i++) {
407 abstract_pkg_t *papkg = apkgs[i];
408 pkg_vec = papkg->pkgs;
410 n_pkgs = pkg_vec->len;
411 for (j = 0; j < n_pkgs; j++) {
412 pkg_t *pkg = pkg_vec->pkgs[j];
413 if (version_constraints_satisfied(depend, pkg)) {
422 int pkg_dependence_satisfied(opkg_conf_t *conf, depend_t *depend)
424 abstract_pkg_t *apkg = depend->pkg;
425 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
426 int n_providers = provider_apkgs->len;
427 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
432 for (i = 0; i < n_providers; i++) {
433 abstract_pkg_t *papkg = apkgs[i];
434 pkg_vec_t *pkg_vec = papkg->pkgs;
436 n_pkgs = pkg_vec->len;
437 for (j = 0; j < n_pkgs; j++) {
438 pkg_t *pkg = pkg_vec->pkgs[j];
439 if (version_constraints_satisfied(depend, pkg)) {
440 if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
449 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
452 pkg_t ** pkgs = vec->pkgs;
454 for(i = 0; i < vec->len; i++)
455 if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
456 && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
457 && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
465 * pkg_has_common_provides returns 1 if pkg and replacee both provide
466 * the same abstract package and 0 otherwise.
468 int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
470 abstract_pkg_t **provides = pkg->provides;
471 int provides_count = pkg->provides_count;
472 abstract_pkg_t **replacee_provides = replacee->provides;
473 int replacee_provides_count = replacee->provides_count;
475 for (i = 0; i < provides_count; i++) {
476 abstract_pkg_t *apkg = provides[i];
477 for (j = 0; j < replacee_provides_count; j++) {
478 abstract_pkg_t *replacee_apkg = replacee_provides[i];
479 if (apkg == replacee_apkg)
488 * pkg_provides_abstract returns 1 if pkg->provides contains providee
491 int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
493 abstract_pkg_t **provides = pkg->provides;
494 int provides_count = pkg->provides_count;
496 for (i = 0; i < provides_count; i++) {
497 if (provides[i] == providee)
504 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
507 int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
509 abstract_pkg_t **replaces = pkg->replaces;
510 int replaces_count = pkg->replaces_count;
511 /* abstract_pkg_t **replacee_provides = pkg->provides;
512 int replacee_provides_count = pkg->provides_count; */
514 for (i = 0; i < replaces_count; i++) {
515 abstract_pkg_t *abstract_replacee = replaces[i];
516 for (j = 0; j < replaces_count; j++) {
517 /* opkg_message(NULL, OPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
518 pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
519 if (replacee->provides[j] == abstract_replacee)
528 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
531 int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
533 compound_depend_t *conflicts = pkg->conflicts;
534 int conflicts_count = pkg->conflicts_count;
536 for (i = 0; i < conflicts_count; i++) {
537 int possibility_count = conflicts[i].possibility_count;
538 struct depend **possibilities = conflicts[i].possibilities;
539 for (j = 0; j < possibility_count; j++) {
540 if (possibilities[j]->pkg == conflictee) {
549 * pkg_conflicts returns 1 if pkg->conflicts contains one of
550 * conflictee's provides and 0 otherwise.
552 int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
554 compound_depend_t *conflicts = pkg->conflicts;
555 int conflicts_count = pkg->conflicts_count;
556 abstract_pkg_t **conflictee_provides = conflictee->provides;
557 int conflictee_provides_count = conflictee->provides_count;
559 int possibility_count;
560 struct depend **possibilities;
561 abstract_pkg_t *possibility ;
563 for (i = 0; i < conflicts_count; i++) {
564 possibility_count = conflicts[i].possibility_count;
565 possibilities = conflicts[i].possibilities;
566 for (j = 0; j < possibility_count; j++) {
567 possibility = possibilities[j]->pkg;
568 for (k = 0; k < conflictee_provides_count; k++) {
569 if (possibility == conflictee_provides[k]) {
578 static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
580 int oldlen = 0, newlen = 0;
587 while(oldstuff && oldstuff[oldlen]) oldlen++;
588 while(newstuff && newstuff[newlen]) newlen++;
590 result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
591 if (result == NULL) {
592 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
596 for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
597 *(result + i) = *(newstuff + j);
599 *(result + i) = NULL;
605 * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
606 * this is null terminated, no count is carried around
608 char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
612 char *depend_str = pkg_depend_str(pkg, ref_ndx);
615 while(the_lost && the_lost[count]) count++;
617 count++; /* need one to hold the null */
618 resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1));
619 if (resized == NULL) {
620 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
623 resized[count - 1] = strdup(depend_str);
624 resized[count] = NULL;
629 void printDepends(pkg_t * pkg)
632 compound_depend_t * depend;
635 count = pkg->pre_depends_count + pkg->depends_count;
637 depend = pkg->depends;
639 fprintf(stderr, "Depends pointer is NULL\n");
642 for(i = 0; i < count; i++){
643 fprintf(stderr, "%s has %d possibilities:\n",
644 (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"),
645 depend->possibility_count);
646 for(j = 0; j < depend->possibility_count; j++)
647 fprintf(stderr, "\t%s version %s (%d)\n",
648 depend->possibilities[j]->pkg->name,
649 depend->possibilities[j]->version,
650 depend->possibilities[j]->constraint);
655 int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
659 /* every pkg provides itself */
660 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
662 if (!pkg->provides_count)
665 pkg->provides = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * (pkg->provides_count + 1));
666 if (pkg->provides == NULL) {
667 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
670 pkg->provides[0] = ab_pkg;
672 // if (strcmp(ab_pkg->name, pkg->name))
673 // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
675 for(i = 0; i < pkg->provides_count; i++){
676 abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
678 pkg->provides[i+1] = provided_abpkg;
681 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
686 /* Abhaya: added conflicts support */
687 int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
690 compound_depend_t * conflicts;
692 if (!pkg->conflicts_count)
695 conflicts = pkg->conflicts = malloc(sizeof(compound_depend_t) *
696 pkg->conflicts_count);
697 if (conflicts == NULL) {
698 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
701 for (i = 0; i < pkg->conflicts_count; i++) {
702 conflicts->type = CONFLICTS;
703 parseDepends(conflicts, hash,
704 pkg->conflicts_str[i]);
706 for (j = 0; j < conflicts->possibility_count; j++) {
707 depend_t *possibility = conflicts->possibilities[j];
708 abstract_pkg_t *conflicting_apkg = possibility->pkg;
709 pkg_add_conflict_pair(ab_pkg, conflicting_apkg);
717 int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
721 if (!pkg->replaces_count)
724 pkg->replaces = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * pkg->replaces_count);
725 if (pkg->replaces == NULL) {
726 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
730 // if (strcmp(ab_pkg->name, pkg->name))
731 // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
733 for(i = 0; i < pkg->replaces_count; i++){
734 abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
736 pkg->replaces[i] = old_abpkg;
739 if (!old_abpkg->replaced_by)
740 old_abpkg->replaced_by = abstract_pkg_vec_alloc();
741 if ( old_abpkg->replaced_by == NULL ){
744 /* if a package pkg both replaces and conflicts old_abpkg,
745 * then add it to the replaced_by vector so that old_abpkg
746 * will be upgraded to ab_pkg automatically */
747 if (pkg_conflicts_abstract(pkg, old_abpkg))
748 abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
753 int buildDepends(hash_table_t * hash, pkg_t * pkg)
757 compound_depend_t * depends;
759 if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
762 if (0 && pkg->pre_depends_count)
763 fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
764 pkg->name, pkg->pre_depends_count, pkg->depends_count);
765 depends = pkg->depends = malloc(sizeof(compound_depend_t) * count);
766 if (depends == NULL) {
767 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
772 for(i = 0; i < pkg->pre_depends_count; i++){
773 parseDepends(depends, hash, pkg->pre_depends_str[i]);
774 if (0 && pkg->pre_depends_count)
775 fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n",
776 pkg->pre_depends_str[i], depends, depends->possibility_count);
777 depends->type = PREDEPEND;
781 for(i = 0; i < pkg->recommends_count; i++){
782 parseDepends(depends, hash, pkg->recommends_str[i]);
783 if (0 && pkg->recommends_count)
784 fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n",
785 pkg->recommends_str[i], depends, depends->possibility_count);
786 depends->type = RECOMMEND;
790 for(i = 0; i < pkg->suggests_count; i++){
791 parseDepends(depends, hash, pkg->suggests_str[i]);
792 if (0 && pkg->suggests_count)
793 fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n",
794 pkg->suggests_str[i], depends, depends->possibility_count);
795 depends->type = SUGGEST;
799 for(i = 0; i < pkg->depends_count; i++){
800 parseDepends(depends, hash, pkg->depends_str[i]);
801 if (0 && pkg->depends_count)
802 fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n",
803 pkg->depends_str[i], depends, depends->possibility_count);
810 * pkg_depend_string: returns the depends string specified by index.
811 * All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
812 * [0,npredepends) -> returns pre_depends_str[index]
813 * [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
814 * [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
815 * [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
817 char *pkg_depend_str(pkg_t *pkg, int index)
819 if (index < pkg->pre_depends_count) {
820 return pkg->pre_depends_str[index];
822 index -= pkg->pre_depends_count;
824 if (index < pkg->recommends_count) {
825 return pkg->recommends_str[index];
827 index -= pkg->recommends_count;
829 if (index < pkg->suggests_count) {
830 return pkg->suggests_str[index];
832 index -= pkg->suggests_count;
834 if (index < pkg->depends_count) {
835 return pkg->depends_str[index];
837 fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
841 void freeDepends(pkg_t *pkg)
845 if (pkg == NULL || pkg->depends == NULL) {
849 fprintf(stderr, "Freeing depends=%p\n", pkg->depends);
850 for (i=0; i < pkg->depends->possibility_count; i++) {
851 depend_deinit(pkg->depends->possibilities[i]);
853 free(pkg->depends->possibilities);
858 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
860 compound_depend_t * depends;
861 int count, othercount;
863 abstract_pkg_t * ab_depend;
864 abstract_pkg_t ** temp;
866 count = pkg->pre_depends_count + pkg->depends_count;
867 depends = pkg->depends;
869 if (0 && pkg->pre_depends_count)
870 fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
871 pkg->name, pkg->pre_depends_count, pkg->depends_count);
872 for (i = 0; i < count; i++) {
873 if (0 && pkg->pre_depends_count)
874 fprintf(stderr, " i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends);
875 for (j = 0; j < depends->possibility_count; j++){
876 ab_depend = depends->possibilities[j]->pkg;
877 if(!ab_depend->depended_upon_by)
878 ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *));
880 temp = ab_depend->depended_upon_by;
888 ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by,
889 (othercount + 1) * sizeof(abstract_pkg_t *));
890 /* the array may have moved */
891 temp = ab_depend->depended_upon_by + othercount;
898 static depend_t * depend_init(void)
900 depend_t * d = (depend_t *)malloc(sizeof(depend_t));
902 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
905 d->constraint = NONE;
912 static void depend_deinit(depend_t *d)
917 static int parseDepends(compound_depend_t *compound_depend,
918 hash_table_t * hash, char * depend_str)
920 char * pkg_name, buffer[2048];
923 register char * src, * dest;
924 depend_t ** possibilities;
926 /* first count the number of ored possibilities for satisfying dependency */
932 compound_depend->type = DEPEND;
934 compound_depend->possibility_count = num_of_ors + 1;
935 possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1));
938 compound_depend->possibilities = possibilities;
941 for(i = 0; i < num_of_ors + 1; i++){
942 possibilities[i] = depend_init();
943 if (!possibilities[i])
945 /* gobble up just the name first */
954 pkg_name = trim_alloc(buffer);
955 if (pkg_name == NULL )
958 /* now look at possible version info */
960 /* skip to next chars */
962 while(*src && isspace(*src)) src++;
964 /* extract constraint and version */
967 if(!strncmp(src, "<<", 2)){
968 possibilities[i]->constraint = EARLIER;
971 else if(!strncmp(src, "<=", 2)){
972 possibilities[i]->constraint = EARLIER_EQUAL;
975 else if(!strncmp(src, ">=", 2)){
976 possibilities[i]->constraint = LATER_EQUAL;
979 else if(!strncmp(src, ">>", 2)){
980 possibilities[i]->constraint = LATER;
983 else if(!strncmp(src, "=", 1)){
984 possibilities[i]->constraint = EQUAL;
987 /* should these be here to support deprecated designations; dpkg does */
988 else if(!strncmp(src, "<", 1)){
989 possibilities[i]->constraint = EARLIER_EQUAL;
992 else if(!strncmp(src, ">", 1)){
993 possibilities[i]->constraint = LATER_EQUAL;
997 /* now we have any constraint, pass space to version string */
998 while(isspace(*src)) src++;
1000 /* this would be the version string */
1002 while(*src && *src != ')')
1006 possibilities[i]->version = trim_alloc(buffer);
1007 /* fprintf(stderr, "let's print the depends version string:");
1008 fprintf(stderr, "version %s\n", possibilities[i]->version);*/
1009 if (possibilities[i]->version == NULL )
1014 /* hook up the dependency to its abstract pkg */
1015 possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
1019 /* now get past the ) and any possible | chars */
1027 compound_depend->type = GREEDY_DEPEND;