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