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