Use uppercase M for printing maintainer field, to be consistent.
[project/opkg-lede.git] / libopkg / pkg_depends.c
1 /* pkg_depends.c - the opkg package management system
2
3 Steven M. Ayer
4
5 Copyright (C) 2002 Compaq Computer Corporation
6
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.
11
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.
16 */
17
18 #include <stdio.h>
19 #include <ctype.h>
20
21 #include "pkg.h"
22 #include "opkg_utils.h"
23 #include "pkg_hash.h"
24 #include "opkg_message.h"
25 #include "pkg_parse.h"
26 #include "hash_table.h"
27 #include "libbb/libbb.h"
28
29 static int parseDepends(compound_depend_t *compound_depend, char * depend_str);
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);
34
35 static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
36 {
37 depend_t *depend = (depend_t *)cdata;
38 if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
39 return 1;
40 else
41 return 0;
42 }
43
44 static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
45 {
46 depend_t *depend = (depend_t *)cdata;
47 if (version_constraints_satisfied(depend, pkg))
48 return 1;
49 else
50 return 0;
51 }
52
53 /* returns ndependencies or negative error value */
54 int
55 pkg_hash_fetch_unsatisfied_dependencies(pkg_t * pkg, pkg_vec_t *unsatisfied,
56 char *** unresolved)
57 {
58 pkg_t * satisfier_entry_pkg;
59 int i, j, k;
60 int count, found;
61 char ** the_lost;
62 abstract_pkg_t * ab_pkg;
63
64 /*
65 * this is a setup to check for redundant/cyclic dependency checks,
66 * which are marked at the abstract_pkg level
67 */
68 if (!(ab_pkg = pkg->parent)) {
69 opkg_msg(ERROR, "Internal error, with pkg %s.\n", pkg->name);
70 *unresolved = NULL;
71 return 0;
72 }
73 if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */
74 *unresolved = NULL;
75 return 0;
76 } else {
77 ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */
78 }
79 /**/
80
81 count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
82 if (!count){
83 *unresolved = NULL;
84 return 0;
85 }
86
87 the_lost = NULL;
88
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;;
93 found = 0;
94 satisfier_entry_pkg = NULL;
95
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;
104 int l;
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?) */
109 continue;
110 }
111
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;
120 int rc;
121 pkg_vec_t *tmp_vec = pkg_vec_alloc ();
122 /* check for not-already-installed dependencies */
123 rc = pkg_hash_fetch_unsatisfied_dependencies(pkg_scout,
124 tmp_vec,
125 &newstuff);
126 if (newstuff == NULL) {
127 int m;
128 int ok = 1;
129 for (m = 0; m < rc; m++) {
130 pkg_t *p = tmp_vec->pkgs[m];
131 if (p->state_want == SW_INSTALL)
132 continue;
133 opkg_msg(DEBUG,
134 "Not installing %s due"
135 " to requirement for %s.\n",
136 pkg_scout->name,
137 p->name);
138 ok = 0;
139 break;
140 }
141 pkg_vec_free (tmp_vec);
142 if (ok) {
143 /* mark this one for installation */
144 opkg_msg(NOTICE,
145 "Adding satisfier for greedy"
146 " dependence %s.\n",
147 pkg_scout->name);
148 pkg_vec_insert(unsatisfied, pkg_scout);
149 }
150 } else {
151 opkg_msg(DEBUG,
152 "Not installing %s due to "
153 "broken depends.\n",
154 pkg_scout->name);
155 free (newstuff);
156 }
157 }
158 }
159 }
160 }
161
162 continue;
163 }
164
165 /* foreach possible satisfier, look for installed package */
166 for (j = 0; j < compound_depend->possibility_count; j++) {
167 /* foreach provided_by, which includes the abstract_pkg itself */
168 depend_t *dependence_to_satisfy = possible_satisfiers[j];
169 abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
170 pkg_t *satisfying_pkg =
171 pkg_hash_fetch_best_installation_candidate(satisfying_apkg,
172 pkg_installed_and_constraint_satisfied,
173 dependence_to_satisfy, 1);
174 /* Being that I can't test constraing in pkg_hash, I will test it here */
175 if (satisfying_pkg != NULL) {
176 if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
177 satisfying_pkg = NULL;
178 }
179 }
180 opkg_msg(DEBUG, "satisfying_pkg=%p\n", satisfying_pkg);
181 if (satisfying_pkg != NULL) {
182 found = 1;
183 break;
184 }
185
186 }
187 /* if nothing installed matches, then look for uninstalled satisfier */
188 if (!found) {
189 /* foreach possible satisfier, look for installed package */
190 for (j = 0; j < compound_depend->possibility_count; j++) {
191 /* foreach provided_by, which includes the abstract_pkg itself */
192 depend_t *dependence_to_satisfy = possible_satisfiers[j];
193 abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
194 pkg_t *satisfying_pkg =
195 pkg_hash_fetch_best_installation_candidate(satisfying_apkg,
196 pkg_constraint_satisfied,
197 dependence_to_satisfy, 1);
198 /* Being that I can't test constraing in pkg_hash, I will test it here too */
199 if (satisfying_pkg != NULL) {
200 if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
201 satisfying_pkg = NULL;
202 }
203 }
204
205 /* user request overrides package recommendation */
206 if (satisfying_pkg != NULL
207 && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
208 && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
209 opkg_msg(NOTICE, "%s: ignoring recommendation for "
210 "%s at user request\n",
211 pkg->name, satisfying_pkg->name);
212 continue;
213 }
214
215 opkg_msg(DEBUG, "satisfying_pkg=%p\n", satisfying_pkg);
216 if (satisfying_pkg != NULL) {
217 satisfier_entry_pkg = satisfying_pkg;
218 break;
219 }
220 }
221 }
222
223 /* we didn't find one, add something to the unsatisfied vector */
224 if (!found) {
225 if (!satisfier_entry_pkg) {
226 /* failure to meet recommendations is not an error */
227 if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
228 the_lost = add_unresolved_dep(pkg, the_lost, i);
229 else
230 opkg_msg(NOTICE,
231 "%s: unsatisfied recommendation for %s\n",
232 pkg->name,
233 compound_depend->possibilities[0]->pkg->name);
234 }
235 else {
236 if (compound_depend->type == SUGGEST) {
237 /* just mention it politely */
238 opkg_msg(NOTICE, "package %s suggests installing %s\n",
239 pkg->name, satisfier_entry_pkg->name);
240 } else {
241 char ** newstuff = NULL;
242
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(satisfier_entry_pkg,
247 unsatisfied,
248 &newstuff);
249 the_lost = merge_unresolved(the_lost, newstuff);
250 if (newstuff)
251 free(newstuff);
252 }
253 }
254 }
255 }
256 }
257 *unresolved = the_lost;
258
259 return unsatisfied->len;
260 }
261
262 /*checking for conflicts !in replaces
263 If a packages conflicts with another but is also replacing it, I should not consider it a
264 really conflicts
265 returns 0 if conflicts <> replaces or 1 if conflicts == replaces
266 */
267 static int
268 is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
269 {
270 int i ;
271 int replaces_count = pkg->replaces_count;
272 abstract_pkg_t **replaces;
273
274 if (pkg->replaces_count==0) // No replaces, it's surely a conflict
275 return 0;
276
277 replaces = pkg->replaces;
278
279 for (i = 0; i < replaces_count; i++) {
280 if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) { // Found
281 opkg_msg(DEBUG2, "Seems I've found a replace %s %s\n",
282 pkg_scout->name, pkg->replaces[i]->name);
283 return 1;
284 }
285 }
286 return 0;
287
288 }
289
290
291 pkg_vec_t * pkg_hash_fetch_conflicts(pkg_t * pkg)
292 {
293 pkg_vec_t * installed_conflicts, * test_vec;
294 compound_depend_t * conflicts;
295 depend_t ** possible_satisfiers;
296 depend_t * possible_satisfier;
297 int i, j, k;
298 int count;
299 abstract_pkg_t * ab_pkg;
300 pkg_t **pkg_scouts;
301 pkg_t *pkg_scout;
302
303 /*
304 * this is a setup to check for redundant/cyclic dependency checks,
305 * which are marked at the abstract_pkg level
306 */
307 if(!(ab_pkg = pkg->parent)){
308 opkg_msg(ERROR, "Internal error: %s not in hash table\n", pkg->name);
309 return (pkg_vec_t *)NULL;
310 }
311
312 conflicts = pkg->conflicts;
313 if(!conflicts){
314 return (pkg_vec_t *)NULL;
315 }
316 installed_conflicts = pkg_vec_alloc();
317
318 count = pkg->conflicts_count;
319
320
321
322 /* foreach conflict */
323 for(i = 0; i < pkg->conflicts_count; i++){
324
325 possible_satisfiers = conflicts->possibilities;
326
327 /* foreach possible satisfier */
328 for(j = 0; j < conflicts->possibility_count; j++){
329 possible_satisfier = possible_satisfiers[j];
330 if (!possible_satisfier)
331 opkg_msg(ERROR, "Internal error: possible_satisfier=NULL\n");
332 if (!possible_satisfier->pkg)
333 opkg_msg(ERROR, "Internal error: possible_satisfier->pkg=NULL\n");
334 test_vec = possible_satisfier->pkg->pkgs;
335 if (test_vec) {
336 /* pkg_vec found, it is an actual package conflict
337 * cruise this possiblity's pkg_vec looking for an installed version */
338 pkg_scouts = test_vec->pkgs;
339 for(k = 0; k < test_vec->len; k++){
340 pkg_scout = pkg_scouts[k];
341 if (!pkg_scout) {
342 opkg_msg(ERROR, "Internal error: pkg_scout=NULL\n");
343 continue;
344 }
345 if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
346 version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
347 if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
348 pkg_vec_insert(installed_conflicts, pkg_scout);
349 }
350 }
351 }
352 }
353 }
354 conflicts++;
355 }
356
357 if (installed_conflicts->len)
358 return installed_conflicts;
359 pkg_vec_free(installed_conflicts);
360 return (pkg_vec_t *)NULL;
361 }
362
363 int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
364 {
365 pkg_t * temp;
366 int comparison;
367
368 if(depends->constraint == NONE)
369 return 1;
370
371 temp = pkg_new();
372
373 parse_version(temp, depends->version);
374
375 comparison = pkg_compare_versions(pkg, temp);
376
377 free (temp->version);
378 free(temp);
379
380 if((depends->constraint == EARLIER) &&
381 (comparison < 0))
382 return 1;
383 else if((depends->constraint == LATER) &&
384 (comparison > 0))
385 return 1;
386 else if(comparison == 0)
387 return 1;
388 else if((depends->constraint == LATER_EQUAL) &&
389 (comparison >= 0))
390 return 1;
391 else if((depends->constraint == EARLIER_EQUAL) &&
392 (comparison <= 0))
393 return 1;
394
395 return 0;
396 }
397
398 int pkg_dependence_satisfiable(depend_t *depend)
399 {
400 abstract_pkg_t *apkg = depend->pkg;
401 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
402 int n_providers = provider_apkgs->len;
403 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
404 pkg_vec_t *pkg_vec;
405 int n_pkgs ;
406 int i;
407 int j;
408
409 for (i = 0; i < n_providers; i++) {
410 abstract_pkg_t *papkg = apkgs[i];
411 pkg_vec = papkg->pkgs;
412 if (pkg_vec) {
413 n_pkgs = pkg_vec->len;
414 for (j = 0; j < n_pkgs; j++) {
415 pkg_t *pkg = pkg_vec->pkgs[j];
416 if (version_constraints_satisfied(depend, pkg)) {
417 return 1;
418 }
419 }
420 }
421 }
422 return 0;
423 }
424
425 int pkg_dependence_satisfied(depend_t *depend)
426 {
427 abstract_pkg_t *apkg = depend->pkg;
428 abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
429 int n_providers = provider_apkgs->len;
430 abstract_pkg_t **apkgs = provider_apkgs->pkgs;
431 int i;
432 int n_pkgs;
433 int j;
434
435 for (i = 0; i < n_providers; i++) {
436 abstract_pkg_t *papkg = apkgs[i];
437 pkg_vec_t *pkg_vec = papkg->pkgs;
438 if (pkg_vec) {
439 n_pkgs = pkg_vec->len;
440 for (j = 0; j < n_pkgs; j++) {
441 pkg_t *pkg = pkg_vec->pkgs[j];
442 if (version_constraints_satisfied(depend, pkg)) {
443 if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
444 return 1;
445 }
446 }
447 }
448 }
449 return 0;
450 }
451
452 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
453 {
454 int i;
455 pkg_t ** pkgs = vec->pkgs;
456
457 for(i = 0; i < vec->len; i++)
458 if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
459 && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
460 && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
461 return 1;
462 return 0;
463 }
464
465 /**
466 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
467 * otherwise.
468 */
469 int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
470 {
471 abstract_pkg_t **replaces = pkg->replaces;
472 int replaces_count = pkg->replaces_count;
473 int replacee_provides_count = replacee->provides_count;
474 int i, j;
475 for (i = 0; i < replaces_count; i++) {
476 abstract_pkg_t *abstract_replacee = replaces[i];
477 for (j = 0; j < replacee_provides_count; j++) {
478 if (replacee->provides[j] == abstract_replacee)
479 return 1;
480 }
481 }
482 return 0;
483 }
484
485
486 /**
487 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
488 * otherwise.
489 */
490 int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
491 {
492 compound_depend_t *conflicts = pkg->conflicts;
493 int conflicts_count = pkg->conflicts_count;
494 int i, j;
495 for (i = 0; i < conflicts_count; i++) {
496 int possibility_count = conflicts[i].possibility_count;
497 struct depend **possibilities = conflicts[i].possibilities;
498 for (j = 0; j < possibility_count; j++) {
499 if (possibilities[j]->pkg == conflictee) {
500 return 1;
501 }
502 }
503 }
504 return 0;
505 }
506
507 /**
508 * pkg_conflicts returns 1 if pkg->conflicts contains one of
509 * conflictee's provides and 0 otherwise.
510 */
511 int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
512 {
513 compound_depend_t *conflicts = pkg->conflicts;
514 int conflicts_count = pkg->conflicts_count;
515 abstract_pkg_t **conflictee_provides = conflictee->provides;
516 int conflictee_provides_count = conflictee->provides_count;
517 int i, j, k;
518 int possibility_count;
519 struct depend **possibilities;
520 abstract_pkg_t *possibility ;
521
522 for (i = 0; i < conflicts_count; i++) {
523 possibility_count = conflicts[i].possibility_count;
524 possibilities = conflicts[i].possibilities;
525 for (j = 0; j < possibility_count; j++) {
526 possibility = possibilities[j]->pkg;
527 for (k = 0; k < conflictee_provides_count; k++) {
528 if (possibility == conflictee_provides[k]) {
529 return 1;
530 }
531 }
532 }
533 }
534 return 0;
535 }
536
537 static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
538 {
539 int oldlen = 0, newlen = 0;
540 char ** result;
541 int i, j;
542
543 if(!newstuff)
544 return oldstuff;
545
546 while(oldstuff && oldstuff[oldlen]) oldlen++;
547 while(newstuff && newstuff[newlen]) newlen++;
548
549 result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
550
551 for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
552 *(result + i) = *(newstuff + j);
553
554 *(result + i) = NULL;
555
556 return result;
557 }
558
559 /*
560 * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
561 * this is null terminated, no count is carried around
562 */
563 char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
564 {
565 int count;
566 char ** resized;
567
568 count = 0;
569 while(the_lost && the_lost[count]) count++;
570
571 count++; /* need one to hold the null */
572 resized = xrealloc(the_lost, sizeof(char *) * (count + 1));
573 resized[count - 1] = pkg_depend_str(pkg, ref_ndx);
574 resized[count] = NULL;
575
576 return resized;
577 }
578
579 void buildProvides(abstract_pkg_t * ab_pkg, pkg_t * pkg)
580 {
581 int i;
582
583 /* every pkg provides itself */
584 pkg->provides_count++;
585 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
586 pkg->provides = xcalloc(pkg->provides_count, sizeof(abstract_pkg_t *));
587 pkg->provides[0] = ab_pkg;
588
589 for (i=1; i<pkg->provides_count; i++) {
590 abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(
591 pkg->provides_str[i-1]);
592 free(pkg->provides_str[i-1]);
593
594 pkg->provides[i] = provided_abpkg;
595
596 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
597 }
598 if (pkg->provides_str)
599 free(pkg->provides_str);
600 }
601
602 void buildConflicts(pkg_t * pkg)
603 {
604 int i;
605 compound_depend_t * conflicts;
606
607 if (!pkg->conflicts_count)
608 return;
609
610 conflicts = pkg->conflicts = xcalloc(pkg->conflicts_count, sizeof(compound_depend_t));
611 for (i = 0; i < pkg->conflicts_count; i++) {
612 conflicts->type = CONFLICTS;
613 parseDepends(conflicts, pkg->conflicts_str[i]);
614 free(pkg->conflicts_str[i]);
615 conflicts++;
616 }
617 if (pkg->conflicts_str)
618 free(pkg->conflicts_str);
619 }
620
621 void buildReplaces(abstract_pkg_t * ab_pkg, pkg_t * pkg)
622 {
623 int i;
624
625 if (!pkg->replaces_count)
626 return;
627
628 pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *));
629
630 for(i = 0; i < pkg->replaces_count; i++){
631 abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(pkg->replaces_str[i]);
632
633 pkg->replaces[i] = old_abpkg;
634 free(pkg->replaces_str[i]);
635
636 if (!old_abpkg->replaced_by)
637 old_abpkg->replaced_by = abstract_pkg_vec_alloc();
638 /* if a package pkg both replaces and conflicts old_abpkg,
639 * then add it to the replaced_by vector so that old_abpkg
640 * will be upgraded to ab_pkg automatically */
641 if (pkg_conflicts_abstract(pkg, old_abpkg))
642 abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
643 }
644
645 if (pkg->replaces_str)
646 free(pkg->replaces_str);
647 }
648
649 void buildDepends(pkg_t * pkg)
650 {
651 unsigned int count;
652 int i;
653 compound_depend_t * depends;
654
655 if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
656 return;
657
658 depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t));
659
660 for(i = 0; i < pkg->pre_depends_count; i++){
661 parseDepends(depends, pkg->pre_depends_str[i]);
662 free(pkg->pre_depends_str[i]);
663 depends->type = PREDEPEND;
664 depends++;
665 }
666 if (pkg->pre_depends_str)
667 free(pkg->pre_depends_str);
668
669 for(i = 0; i < pkg->depends_count; i++){
670 parseDepends(depends, pkg->depends_str[i]);
671 free(pkg->depends_str[i]);
672 depends++;
673 }
674 if (pkg->depends_str)
675 free(pkg->depends_str);
676
677 for(i = 0; i < pkg->recommends_count; i++){
678 parseDepends(depends, pkg->recommends_str[i]);
679 free(pkg->recommends_str[i]);
680 depends->type = RECOMMEND;
681 depends++;
682 }
683 if(pkg->recommends_str)
684 free(pkg->recommends_str);
685
686 for(i = 0; i < pkg->suggests_count; i++){
687 parseDepends(depends, pkg->suggests_str[i]);
688 free(pkg->suggests_str[i]);
689 depends->type = SUGGEST;
690 depends++;
691 }
692 if(pkg->suggests_str)
693 free(pkg->suggests_str);
694 }
695
696 const char*
697 constraint_to_str(enum version_constraint c)
698 {
699 switch (c) {
700 case NONE:
701 return "";
702 case EARLIER:
703 return "< ";
704 case EARLIER_EQUAL:
705 return "<= ";
706 case EQUAL:
707 return "= ";
708 case LATER_EQUAL:
709 return ">= ";
710 case LATER:
711 return "> ";
712 }
713
714 return "";
715 }
716
717 /*
718 * Returns a printable string for pkg's dependency at the specified idx. The
719 * resultant string must be passed to free() by the caller.
720 */
721 char *
722 pkg_depend_str(pkg_t *pkg, int idx)
723 {
724 int i;
725 unsigned int len;
726 char *str;
727 compound_depend_t *cdep;
728 depend_t *dep;
729
730 len = 0;
731 cdep = &pkg->depends[idx];
732
733 /* calculate string length */
734 for (i=0; i<cdep->possibility_count; i++) {
735 dep = cdep->possibilities[i];
736
737 if (i != 0)
738 len += 3; /* space, pipe, space */
739
740 len += strlen(dep->pkg->name);
741
742 if (dep->version) {
743 len += 2; /* space, left parenthesis */
744 len += 3; /* constraint string (<=, >=, etc), space */
745 len += strlen(dep->version);
746 len += 1; /* right parenthesis */
747 }
748 }
749
750 str = xmalloc(len + 1); /* +1 for the NULL terminator */
751 str[0] = '\0';
752
753 for (i=0; i<cdep->possibility_count; i++) {
754 dep = cdep->possibilities[i];
755
756 if (i != 0)
757 strncat(str, " | ", len);
758
759 strncat(str, dep->pkg->name, len);
760
761 if (dep->version) {
762 strncat(str, " (", len);
763 strncat(str, constraint_to_str(dep->constraint), len);
764 strncat(str, dep->version, len);
765 strncat(str, ")", len);
766 }
767 }
768
769 return str;
770 }
771
772 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
773 {
774 compound_depend_t * depends;
775 int count, othercount;
776 int i, j;
777 abstract_pkg_t * ab_depend;
778 abstract_pkg_t ** temp;
779
780 count = pkg->pre_depends_count +
781 pkg->depends_count +
782 pkg->recommends_count +
783 pkg->suggests_count;
784
785 for (i = 0; i < count; i++) {
786 depends = &pkg->depends[i];
787 if (depends->type != PREDEPEND
788 && depends->type != DEPEND
789 && depends->type != RECOMMEND)
790 continue;
791 for (j = 0; j < depends->possibility_count; j++) {
792 ab_depend = depends->possibilities[j]->pkg;
793 if (!ab_depend->depended_upon_by) {
794 ab_depend->depended_upon_by =
795 xcalloc(1, sizeof(abstract_pkg_t *));
796 }
797
798 temp = ab_depend->depended_upon_by;
799 othercount = 1;
800 while (*temp) {
801 temp++;
802 othercount++;
803 }
804 *temp = ab_pkg;
805
806 ab_depend->depended_upon_by =
807 xrealloc(ab_depend->depended_upon_by,
808 (othercount + 1) * sizeof(abstract_pkg_t *));
809
810 /* the array may have been moved by realloc */
811 temp = ab_depend->depended_upon_by + othercount;
812 *temp = NULL;
813 }
814 }
815 }
816
817 static depend_t * depend_init(void)
818 {
819 depend_t * d = xcalloc(1, sizeof(depend_t));
820 d->constraint = NONE;
821 d->version = NULL;
822 d->pkg = NULL;
823
824 return d;
825 }
826
827 static int parseDepends(compound_depend_t *compound_depend,
828 char * depend_str)
829 {
830 char * pkg_name, buffer[2048];
831 unsigned int num_of_ors = 0;
832 int i;
833 char * src, * dest;
834 depend_t ** possibilities;
835
836 /* first count the number of ored possibilities for satisfying dependency */
837 src = depend_str;
838 while(*src)
839 if(*src++ == '|')
840 num_of_ors++;
841
842 compound_depend->type = DEPEND;
843
844 compound_depend->possibility_count = num_of_ors + 1;
845 possibilities = xcalloc((num_of_ors + 1), sizeof(depend_t *) );
846 compound_depend->possibilities = possibilities;
847
848 src = depend_str;
849 for(i = 0; i < num_of_ors + 1; i++){
850 possibilities[i] = depend_init();
851 /* gobble up just the name first */
852 dest = buffer;
853 while(*src &&
854 !isspace(*src) &&
855 (*src != '(') &&
856 (*src != '*') &&
857 (*src != '|'))
858 *dest++ = *src++;
859 *dest = '\0';
860 pkg_name = trim_xstrdup(buffer);
861
862 /* now look at possible version info */
863
864 /* skip to next chars */
865 if(isspace(*src))
866 while(*src && isspace(*src)) src++;
867
868 /* extract constraint and version */
869 if(*src == '('){
870 src++;
871 if(!strncmp(src, "<<", 2)){
872 possibilities[i]->constraint = EARLIER;
873 src += 2;
874 }
875 else if(!strncmp(src, "<=", 2)){
876 possibilities[i]->constraint = EARLIER_EQUAL;
877 src += 2;
878 }
879 else if(!strncmp(src, ">=", 2)){
880 possibilities[i]->constraint = LATER_EQUAL;
881 src += 2;
882 }
883 else if(!strncmp(src, ">>", 2)){
884 possibilities[i]->constraint = LATER;
885 src += 2;
886 }
887 else if(!strncmp(src, "=", 1)){
888 possibilities[i]->constraint = EQUAL;
889 src++;
890 }
891 /* should these be here to support deprecated designations; dpkg does */
892 else if(!strncmp(src, "<", 1)){
893 possibilities[i]->constraint = EARLIER_EQUAL;
894 src++;
895 }
896 else if(!strncmp(src, ">", 1)){
897 possibilities[i]->constraint = LATER_EQUAL;
898 src++;
899 }
900
901 /* now we have any constraint, pass space to version string */
902 while(isspace(*src)) src++;
903
904 /* this would be the version string */
905 dest = buffer;
906 while(*src && *src != ')')
907 *dest++ = *src++;
908 *dest = '\0';
909
910 possibilities[i]->version = trim_xstrdup(buffer);
911 }
912 /* hook up the dependency to its abstract pkg */
913 possibilities[i]->pkg = ensure_abstract_pkg_by_name(pkg_name);
914
915 free(pkg_name);
916
917 /* now get past the ) and any possible | chars */
918 while(*src &&
919 (isspace(*src) ||
920 (*src == ')') ||
921 (*src == '|')))
922 src++;
923 if (*src == '*')
924 {
925 compound_depend->type = GREEDY_DEPEND;
926 src++;
927 }
928 }
929
930 return 0;
931 }