f6e3827e2429b97cdadf01c9fd2f4ddde89433fc
[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 "includes.h"
19 #include <errno.h>
20 #include <ctype.h>
21
22 #include "pkg.h"
23 #include "opkg_utils.h"
24 #include "pkg_hash.h"
25 #include "opkg_message.h"
26 #include "pkg_parse.h"
27 #include "hash_table.h"
28
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);
35
36 static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
37 {
38 depend_t *depend = (depend_t *)cdata;
39 if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
40 return 1;
41 else
42 return 0;
43 }
44
45 static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
46 {
47 depend_t *depend = (depend_t *)cdata;
48 #if 0
49 pkg_t * temp = pkg_new();
50 int comparison;
51 parseVersion(temp, depend->version);
52 comparison = pkg_compare_versions(pkg, temp);
53 free(temp);
54
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));
59 #endif
60 if (version_constraints_satisfied(depend, pkg))
61 return 1;
62 else
63 return 0;
64 }
65
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)
69 {
70 pkg_t * satisfier_entry_pkg;
71 register int i, j, k;
72 int count, found;
73 char ** the_lost;
74 abstract_pkg_t * ab_pkg;
75
76 /*
77 * this is a setup to check for redundant/cyclic dependency checks,
78 * which are marked at the abstract_pkg level
79 */
80 if (!(ab_pkg = pkg->parent)) {
81 fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
82 *unresolved = NULL;
83 return 0;
84 }
85 if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */
86 *unresolved = NULL;
87 return 0;
88 } else {
89 ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */
90 }
91 /**/
92
93 count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
94 if (!count){
95 *unresolved = NULL;
96 return 0;
97 }
98
99 the_lost = NULL;
100
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;;
105 found = 0;
106 satisfier_entry_pkg = NULL;
107
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;
116 int l;
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?) */
121 continue;
122 }
123
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;
132 int rc;
133 pkg_vec_t *tmp_vec = pkg_vec_alloc ();
134 /* check for not-already-installed dependencies */
135 rc = pkg_hash_fetch_unsatisfied_dependencies(conf,
136 pkg_scout,
137 tmp_vec,
138 &newstuff);
139 if (newstuff == NULL) {
140 int i;
141 int ok = 1;
142 for (i = 0; i < rc; i++) {
143 pkg_t *p = tmp_vec->pkgs[i];
144 if (p->state_want == SW_INSTALL)
145 continue;
146 opkg_message(conf, OPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
147 ok = 0;
148 break;
149 }
150 pkg_vec_free (tmp_vec);
151 if (ok) {
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);
155 }
156 } else {
157 opkg_message(conf, OPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
158 free (newstuff);
159 }
160 }
161 }
162 }
163 }
164
165 continue;
166 }
167
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;
181 }
182 }
183 opkg_message(conf, OPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
184 if (satisfying_pkg != NULL) {
185 found = 1;
186 break;
187 }
188
189 }
190 /* if nothing installed matches, then look for uninstalled satisfier */
191 if (!found) {
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;
205 }
206 }
207
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);
214 continue;
215 }
216
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;
220 break;
221 }
222 }
223 }
224
225 /* we didn't find one, add something to the unsatisfied vector */
226 if (!found) {
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);
231 else
232 opkg_message (conf, OPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
233 pkg->name, compound_depend->possibilities[0]->pkg->name);
234 }
235 else {
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);
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(conf,
247 satisfier_entry_pkg,
248 unsatisfied,
249 &newstuff);
250 the_lost = merge_unresolved(the_lost, newstuff);
251 }
252 }
253 }
254 }
255 }
256 *unresolved = the_lost;
257
258 return unsatisfied->len;
259 }
260
261 /*checking for conflicts !in replaces
262 If a packages conflicts with another but is also replacing it, I should not consider it a
263 really conflicts
264 returns 0 if conflicts <> replaces or 1 if conflicts == replaces
265 */
266 int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
267 {
268 int i ;
269 int replaces_count = pkg->replaces_count;
270 abstract_pkg_t **replaces;
271
272 if (pkg->replaces_count==0) // No replaces, it's surely a conflict
273 return 0;
274
275 replaces = pkg->replaces;
276
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);
280 return 1;
281 }
282 }
283 return 0;
284
285 }
286
287
288 /* Abhaya: added support for conflicts */
289 pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
290 {
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;
296 int count;
297 abstract_pkg_t * ab_pkg;
298 pkg_t **pkg_scouts;
299 pkg_t *pkg_scout;
300
301 /*
302 * this is a setup to check for redundant/cyclic dependency checks,
303 * which are marked at the abstract_pkg level
304 */
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;
308 }
309
310 conflicts = pkg->conflicts;
311 if(!conflicts){
312 return (pkg_vec_t *)NULL;
313 }
314 installed_conflicts = pkg_vec_alloc();
315
316 count = pkg->conflicts_count;
317
318
319
320 /* foreach conflict */
321 for(i = 0; i < pkg->conflicts_count; i++){
322
323 possible_satisfiers = conflicts->possibilities;
324
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;
333 if (test_vec) {
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];
339 if (!pkg_scout) {
340 fprintf(stderr, "%s: null pkg scout\n", __FUNCTION__);
341 continue;
342 }
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);
347 }
348 }
349 }
350 }
351 }
352 conflicts++;
353 }
354
355 if (installed_conflicts->len)
356 return installed_conflicts;
357 pkg_vec_free(installed_conflicts);
358 return (pkg_vec_t *)NULL;
359 }
360
361 int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
362 {
363 pkg_t * temp;
364 int comparison;
365
366 if(depends->constraint == NONE)
367 return 1;
368
369 temp = pkg_new();
370
371 parseVersion(temp, depends->version);
372
373 comparison = pkg_compare_versions(pkg, temp);
374
375 free(temp);
376
377 if((depends->constraint == EARLIER) &&
378 (comparison < 0))
379 return 1;
380 else if((depends->constraint == LATER) &&
381 (comparison > 0))
382 return 1;
383 else if(comparison == 0)
384 return 1;
385 else if((depends->constraint == LATER_EQUAL) &&
386 (comparison >= 0))
387 return 1;
388 else if((depends->constraint == EARLIER_EQUAL) &&
389 (comparison <= 0))
390 return 1;
391
392 return 0;
393 }
394
395 int pkg_dependence_satisfiable(opkg_conf_t *conf, depend_t *depend)
396 {
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;
401 pkg_vec_t *pkg_vec;
402 int n_pkgs ;
403 int i;
404 int j;
405
406 for (i = 0; i < n_providers; i++) {
407 abstract_pkg_t *papkg = apkgs[i];
408 pkg_vec = papkg->pkgs;
409 if (pkg_vec) {
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)) {
414 return 1;
415 }
416 }
417 }
418 }
419 return 0;
420 }
421
422 int pkg_dependence_satisfied(opkg_conf_t *conf, depend_t *depend)
423 {
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;
428 int i;
429 int n_pkgs;
430 int j;
431
432 for (i = 0; i < n_providers; i++) {
433 abstract_pkg_t *papkg = apkgs[i];
434 pkg_vec_t *pkg_vec = papkg->pkgs;
435 if (pkg_vec) {
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)
441 return 1;
442 }
443 }
444 }
445 }
446 return 0;
447 }
448
449 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
450 {
451 register int i;
452 pkg_t ** pkgs = vec->pkgs;
453
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))
458 return 1;
459 return 0;
460 }
461
462
463 #ifdef DeadCode
464 /**
465 * pkg_has_common_provides returns 1 if pkg and replacee both provide
466 * the same abstract package and 0 otherwise.
467 */
468 int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
469 {
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;
474 int i, j;
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)
480 return 1;
481 }
482 }
483 return 0;
484 }
485 #endif
486
487 /**
488 * pkg_provides_abstract returns 1 if pkg->provides contains providee
489 * and 0 otherwise.
490 */
491 int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
492 {
493 abstract_pkg_t **provides = pkg->provides;
494 int provides_count = pkg->provides_count;
495 int i;
496 for (i = 0; i < provides_count; i++) {
497 if (provides[i] == providee)
498 return 1;
499 }
500 return 0;
501 }
502
503 /**
504 * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
505 * otherwise.
506 */
507 int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
508 {
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; */
513 int i, j;
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)
520 return 1;
521 }
522 }
523 return 0;
524 }
525
526
527 /**
528 * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
529 * otherwise.
530 */
531 int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
532 {
533 compound_depend_t *conflicts = pkg->conflicts;
534 int conflicts_count = pkg->conflicts_count;
535 int i, j;
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) {
541 return 1;
542 }
543 }
544 }
545 return 0;
546 }
547
548 /**
549 * pkg_conflicts returns 1 if pkg->conflicts contains one of
550 * conflictee's provides and 0 otherwise.
551 */
552 int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
553 {
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;
558 int i, j, k;
559 int possibility_count;
560 struct depend **possibilities;
561 abstract_pkg_t *possibility ;
562
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]) {
570 return 1;
571 }
572 }
573 }
574 }
575 return 0;
576 }
577
578 static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
579 {
580 int oldlen = 0, newlen = 0;
581 char ** result;
582 register int i, j;
583
584 if(!newstuff)
585 return oldstuff;
586
587 while(oldstuff && oldstuff[oldlen]) oldlen++;
588 while(newstuff && newstuff[newlen]) newlen++;
589
590 result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
591 if (result == NULL) {
592 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
593 return NULL;
594 }
595
596 for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
597 *(result + i) = *(newstuff + j);
598
599 *(result + i) = NULL;
600
601 return result;
602 }
603
604 /*
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
607 */
608 char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
609 {
610 int count;
611 char ** resized;
612 char *depend_str = pkg_depend_str(pkg, ref_ndx);
613
614 count = 0;
615 while(the_lost && the_lost[count]) count++;
616
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__);
621 return NULL;
622 }
623 resized[count - 1] = strdup(depend_str);
624 resized[count] = NULL;
625
626 return resized;
627 }
628
629 void printDepends(pkg_t * pkg)
630 {
631 register int i, j;
632 compound_depend_t * depend;
633 int count;
634
635 count = pkg->pre_depends_count + pkg->depends_count;
636
637 depend = pkg->depends;
638 if(!depend){
639 fprintf(stderr, "Depends pointer is NULL\n");
640 return;
641 }
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);
651 depend++;
652 }
653 }
654
655 int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
656 {
657 register int i, j;
658
659 /* every pkg provides itself */
660 abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
661
662 if (!pkg->provides_count)
663 return 0;
664
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__);
668 return -1 ;
669 }
670 pkg->provides[0] = ab_pkg;
671
672 // if (strcmp(ab_pkg->name, pkg->name))
673 // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
674
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]);
677
678 pkg->provides[i+1] = provided_abpkg;
679
680 j = 0;
681 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
682 }
683 return 0;
684 }
685
686 /* Abhaya: added conflicts support */
687 int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
688 {
689 register int i;
690 compound_depend_t * conflicts;
691
692 if (!pkg->conflicts_count)
693 return 0;
694
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__);
699 return -1;
700 }
701 for (i = 0; i < pkg->conflicts_count; i++) {
702 conflicts->type = CONFLICTS;
703 parseDepends(conflicts, hash,
704 pkg->conflicts_str[i]);
705 #if 0
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);
710 }
711 #endif
712 conflicts++;
713 }
714 return 0;
715 }
716
717 int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
718 {
719 register int i, j;
720
721 if (!pkg->replaces_count)
722 return 0;
723
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__);
727 return -1;
728 }
729
730 // if (strcmp(ab_pkg->name, pkg->name))
731 // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
732
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]);
735
736 pkg->replaces[i] = old_abpkg;
737
738 j = 0;
739 if (!old_abpkg->replaced_by)
740 old_abpkg->replaced_by = abstract_pkg_vec_alloc();
741 if ( old_abpkg->replaced_by == NULL ){
742 return -1;
743 }
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);
749 }
750 return 0;
751 }
752
753 int buildDepends(hash_table_t * hash, pkg_t * pkg)
754 {
755 int count;
756 register int i;
757 compound_depend_t * depends;
758
759 if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
760 return 0;
761
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__);
768 return -1;
769 }
770
771
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;
778 depends++;
779 }
780
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;
787 depends++;
788 }
789
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;
796 depends++;
797 }
798
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);
804 depends++;
805 }
806 return 0;
807 }
808
809 /*
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]
816 */
817 char *pkg_depend_str(pkg_t *pkg, int index)
818 {
819 if (index < pkg->pre_depends_count) {
820 return pkg->pre_depends_str[index];
821 }
822 index -= pkg->pre_depends_count;
823
824 if (index < pkg->recommends_count) {
825 return pkg->recommends_str[index];
826 }
827 index -= pkg->recommends_count;
828
829 if (index < pkg->suggests_count) {
830 return pkg->suggests_str[index];
831 }
832 index -= pkg->suggests_count;
833
834 if (index < pkg->depends_count) {
835 return pkg->depends_str[index];
836 }
837 fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
838 return NULL;
839 }
840
841 void freeDepends(pkg_t *pkg)
842 {
843 int i;
844
845 if (pkg == NULL || pkg->depends == NULL) {
846 return;
847 }
848
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]);
852 }
853 free(pkg->depends->possibilities);
854 free(pkg->depends);
855 pkg->depends = NULL;
856 }
857
858 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
859 {
860 compound_depend_t * depends;
861 int count, othercount;
862 register int i, j;
863 abstract_pkg_t * ab_depend;
864 abstract_pkg_t ** temp;
865
866 count = pkg->pre_depends_count + pkg->depends_count;
867 depends = pkg->depends;
868
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 *));
879
880 temp = ab_depend->depended_upon_by;
881 othercount = 1;
882 while(*temp){
883 temp++;
884 othercount++;
885 }
886 *temp = ab_pkg;
887
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;
892 *temp = NULL;
893 }
894 depends++;
895 }
896 }
897
898 static depend_t * depend_init(void)
899 {
900 depend_t * d = (depend_t *)malloc(sizeof(depend_t));
901 if ( d==NULL ){
902 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
903 return NULL;
904 }
905 d->constraint = NONE;
906 d->version = NULL;
907 d->pkg = NULL;
908
909 return d;
910 }
911
912 static void depend_deinit(depend_t *d)
913 {
914 free(d);
915 }
916
917 static int parseDepends(compound_depend_t *compound_depend,
918 hash_table_t * hash, char * depend_str)
919 {
920 char * pkg_name, buffer[2048];
921 int num_of_ors = 0;
922 register int i;
923 register char * src, * dest;
924 depend_t ** possibilities;
925
926 /* first count the number of ored possibilities for satisfying dependency */
927 src = depend_str;
928 while(*src)
929 if(*src++ == '|')
930 num_of_ors++;
931
932 compound_depend->type = DEPEND;
933
934 compound_depend->possibility_count = num_of_ors + 1;
935 possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1));
936 if (!possibilities)
937 return -ENOMEM;
938 compound_depend->possibilities = possibilities;
939
940 src = depend_str;
941 for(i = 0; i < num_of_ors + 1; i++){
942 possibilities[i] = depend_init();
943 if (!possibilities[i])
944 return -ENOMEM;
945 /* gobble up just the name first */
946 dest = buffer;
947 while(*src &&
948 !isspace(*src) &&
949 (*src != '(') &&
950 (*src != '*') &&
951 (*src != '|'))
952 *dest++ = *src++;
953 *dest = '\0';
954 pkg_name = trim_alloc(buffer);
955 if (pkg_name == NULL )
956 return -ENOMEM;
957
958 /* now look at possible version info */
959
960 /* skip to next chars */
961 if(isspace(*src))
962 while(*src && isspace(*src)) src++;
963
964 /* extract constraint and version */
965 if(*src == '('){
966 src++;
967 if(!strncmp(src, "<<", 2)){
968 possibilities[i]->constraint = EARLIER;
969 src += 2;
970 }
971 else if(!strncmp(src, "<=", 2)){
972 possibilities[i]->constraint = EARLIER_EQUAL;
973 src += 2;
974 }
975 else if(!strncmp(src, ">=", 2)){
976 possibilities[i]->constraint = LATER_EQUAL;
977 src += 2;
978 }
979 else if(!strncmp(src, ">>", 2)){
980 possibilities[i]->constraint = LATER;
981 src += 2;
982 }
983 else if(!strncmp(src, "=", 1)){
984 possibilities[i]->constraint = EQUAL;
985 src++;
986 }
987 /* should these be here to support deprecated designations; dpkg does */
988 else if(!strncmp(src, "<", 1)){
989 possibilities[i]->constraint = EARLIER_EQUAL;
990 src++;
991 }
992 else if(!strncmp(src, ">", 1)){
993 possibilities[i]->constraint = LATER_EQUAL;
994 src++;
995 }
996
997 /* now we have any constraint, pass space to version string */
998 while(isspace(*src)) src++;
999
1000 /* this would be the version string */
1001 dest = buffer;
1002 while(*src && *src != ')')
1003 *dest++ = *src++;
1004 *dest = '\0';
1005
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 )
1010 return -ENOMEM;
1011
1012
1013 }
1014 /* hook up the dependency to its abstract pkg */
1015 possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
1016
1017 free(pkg_name);
1018
1019 /* now get past the ) and any possible | chars */
1020 while(*src &&
1021 (isspace(*src) ||
1022 (*src == ')') ||
1023 (*src == '|')))
1024 src++;
1025 if (*src == '*')
1026 {
1027 compound_depend->type = GREEDY_DEPEND;
1028 src++;
1029 }
1030 }
1031
1032 return 0;
1033 }