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