treewrite: use Lindent to reformat to kernel coding style
[oweals/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         free(temp->version);
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         pkg_t **pkgs = vec->pkgs;
520
521         for (i = 0; i < vec->len; i++)
522                 if ((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
523                     && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
524                     && (strcmp(pkg->architecture, (*(pkgs + i))->architecture)
525                         == 0))
526                         return 1;
527         return 0;
528 }
529
530 /**
531  * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
532  * otherwise.
533  */
534 int pkg_replaces(pkg_t * pkg, pkg_t * replacee)
535 {
536         abstract_pkg_t **replaces = pkg->replaces;
537         int replaces_count = pkg->replaces_count;
538         int replacee_provides_count = replacee->provides_count;
539         int i, j;
540         for (i = 0; i < replaces_count; i++) {
541                 abstract_pkg_t *abstract_replacee = replaces[i];
542                 for (j = 0; j < replacee_provides_count; j++) {
543                         if (replacee->provides[j] == abstract_replacee)
544                                 return 1;
545                 }
546         }
547         return 0;
548 }
549
550 /**
551  * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
552  * otherwise.
553  */
554 int pkg_conflicts_abstract(pkg_t * pkg, abstract_pkg_t * conflictee)
555 {
556         compound_depend_t *conflicts = pkg->conflicts;
557         int conflicts_count = pkg->conflicts_count;
558         int i, j;
559         for (i = 0; i < conflicts_count; i++) {
560                 int possibility_count = conflicts[i].possibility_count;
561                 struct depend **possibilities = conflicts[i].possibilities;
562                 for (j = 0; j < possibility_count; j++) {
563                         if (possibilities[j]->pkg == conflictee) {
564                                 return 1;
565                         }
566                 }
567         }
568         return 0;
569 }
570
571 /**
572  * pkg_conflicts returns 1 if pkg->conflicts contains one of
573  * conflictee's provides and 0 otherwise.
574  */
575 int pkg_conflicts(pkg_t * pkg, pkg_t * conflictee)
576 {
577         compound_depend_t *conflicts = pkg->conflicts;
578         int conflicts_count = pkg->conflicts_count;
579         abstract_pkg_t **conflictee_provides = conflictee->provides;
580         int conflictee_provides_count = conflictee->provides_count;
581         int i, j, k;
582         int possibility_count;
583         struct depend **possibilities;
584         abstract_pkg_t *possibility;
585
586         for (i = 0; i < conflicts_count; i++) {
587                 possibility_count = conflicts[i].possibility_count;
588                 possibilities = conflicts[i].possibilities;
589                 for (j = 0; j < possibility_count; j++) {
590                         possibility = possibilities[j]->pkg;
591                         for (k = 0; k < conflictee_provides_count; k++) {
592                                 if (possibility == conflictee_provides[k]) {
593                                         return 1;
594                                 }
595                         }
596                 }
597         }
598         return 0;
599 }
600
601 static char **merge_unresolved(char **oldstuff, char **newstuff)
602 {
603         int oldlen = 0, newlen = 0;
604         char **result;
605         int i, j;
606
607         if (!newstuff)
608                 return oldstuff;
609
610         while (oldstuff && oldstuff[oldlen])
611                 oldlen++;
612         while (newstuff && newstuff[newlen])
613                 newlen++;
614
615         result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
616
617         for (i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
618                 *(result + i) = *(newstuff + j);
619
620         *(result + i) = NULL;
621
622         return result;
623 }
624
625 /*
626  * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
627  * this is null terminated, no count is carried around
628  */
629 char **add_unresolved_dep(pkg_t * pkg, char **the_lost, int ref_ndx)
630 {
631         int count;
632         char **resized;
633
634         count = 0;
635         while (the_lost && the_lost[count])
636                 count++;
637
638         count++;                /* need one to hold the null */
639         resized = xrealloc(the_lost, sizeof(char *) * (count + 1));
640         resized[count - 1] = pkg_depend_str(pkg, ref_ndx);
641         resized[count] = NULL;
642
643         return resized;
644 }
645
646 void buildProvides(abstract_pkg_t * ab_pkg, pkg_t * pkg)
647 {
648         int i;
649
650         /* every pkg provides itself */
651         pkg->provides_count++;
652         abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
653         pkg->provides = xcalloc(pkg->provides_count, sizeof(abstract_pkg_t *));
654         pkg->provides[0] = ab_pkg;
655
656         for (i = 1; i < pkg->provides_count; i++) {
657                 abstract_pkg_t *provided_abpkg =
658                     ensure_abstract_pkg_by_name(pkg->provides_str[i - 1]);
659                 free(pkg->provides_str[i - 1]);
660
661                 pkg->provides[i] = provided_abpkg;
662
663                 abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
664         }
665         if (pkg->provides_str)
666                 free(pkg->provides_str);
667 }
668
669 void buildConflicts(pkg_t * pkg)
670 {
671         int i;
672         compound_depend_t *conflicts;
673
674         if (!pkg->conflicts_count)
675                 return;
676
677         conflicts = pkg->conflicts =
678             xcalloc(pkg->conflicts_count, sizeof(compound_depend_t));
679         for (i = 0; i < pkg->conflicts_count; i++) {
680                 conflicts->type = CONFLICTS;
681                 parseDepends(conflicts, pkg->conflicts_str[i]);
682                 free(pkg->conflicts_str[i]);
683                 conflicts++;
684         }
685         if (pkg->conflicts_str)
686                 free(pkg->conflicts_str);
687 }
688
689 void buildReplaces(abstract_pkg_t * ab_pkg, pkg_t * pkg)
690 {
691         int i;
692
693         if (!pkg->replaces_count)
694                 return;
695
696         pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *));
697
698         for (i = 0; i < pkg->replaces_count; i++) {
699                 abstract_pkg_t *old_abpkg =
700                     ensure_abstract_pkg_by_name(pkg->replaces_str[i]);
701
702                 pkg->replaces[i] = old_abpkg;
703                 free(pkg->replaces_str[i]);
704
705                 if (!old_abpkg->replaced_by)
706                         old_abpkg->replaced_by = abstract_pkg_vec_alloc();
707                 /* if a package pkg both replaces and conflicts old_abpkg,
708                  * then add it to the replaced_by vector so that old_abpkg
709                  * will be upgraded to ab_pkg automatically */
710                 if (pkg_conflicts_abstract(pkg, old_abpkg))
711                         abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
712         }
713
714         if (pkg->replaces_str)
715                 free(pkg->replaces_str);
716 }
717
718 void buildDepends(pkg_t * pkg)
719 {
720         unsigned int count;
721         int i;
722         compound_depend_t *depends;
723
724         if (!
725             (count =
726              pkg->pre_depends_count + pkg->depends_count +
727              pkg->recommends_count + pkg->suggests_count))
728                 return;
729
730         depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t));
731
732         for (i = 0; i < pkg->pre_depends_count; i++) {
733                 parseDepends(depends, pkg->pre_depends_str[i]);
734                 free(pkg->pre_depends_str[i]);
735                 depends->type = PREDEPEND;
736                 depends++;
737         }
738         if (pkg->pre_depends_str)
739                 free(pkg->pre_depends_str);
740
741         for (i = 0; i < pkg->depends_count; i++) {
742                 parseDepends(depends, pkg->depends_str[i]);
743                 free(pkg->depends_str[i]);
744                 depends++;
745         }
746         if (pkg->depends_str)
747                 free(pkg->depends_str);
748
749         for (i = 0; i < pkg->recommends_count; i++) {
750                 parseDepends(depends, pkg->recommends_str[i]);
751                 free(pkg->recommends_str[i]);
752                 depends->type = RECOMMEND;
753                 depends++;
754         }
755         if (pkg->recommends_str)
756                 free(pkg->recommends_str);
757
758         for (i = 0; i < pkg->suggests_count; i++) {
759                 parseDepends(depends, pkg->suggests_str[i]);
760                 free(pkg->suggests_str[i]);
761                 depends->type = SUGGEST;
762                 depends++;
763         }
764         if (pkg->suggests_str)
765                 free(pkg->suggests_str);
766 }
767
768 const char *constraint_to_str(enum version_constraint c)
769 {
770         switch (c) {
771         case NONE:
772                 return "";
773         case EARLIER:
774                 return "< ";
775         case EARLIER_EQUAL:
776                 return "<= ";
777         case EQUAL:
778                 return "= ";
779         case LATER_EQUAL:
780                 return ">= ";
781         case LATER:
782                 return "> ";
783         }
784
785         return "";
786 }
787
788 /*
789  * Returns a printable string for pkg's dependency at the specified idx. The
790  * resultant string must be passed to free() by the caller.
791  */
792 char *pkg_depend_str(pkg_t * pkg, int idx)
793 {
794         int i;
795         unsigned int len;
796         char *str;
797         compound_depend_t *cdep;
798         depend_t *dep;
799
800         len = 0;
801         cdep = &pkg->depends[idx];
802
803         /* calculate string length */
804         for (i = 0; i < cdep->possibility_count; i++) {
805                 dep = cdep->possibilities[i];
806
807                 if (i != 0)
808                         len += 3;       /* space, pipe, space */
809
810                 len += strlen(dep->pkg->name);
811
812                 if (dep->version) {
813                         len += 2;       /* space, left parenthesis */
814                         len += 3;       /* constraint string (<=, >=, etc), space */
815                         len += strlen(dep->version);
816                         len += 1;       /* right parenthesis */
817                 }
818         }
819
820         str = xmalloc(len + 1); /* +1 for the NULL terminator */
821         str[0] = '\0';
822
823         for (i = 0; i < cdep->possibility_count; i++) {
824                 dep = cdep->possibilities[i];
825
826                 if (i != 0)
827                         strncat(str, " | ", len);
828
829                 strncat(str, dep->pkg->name, len);
830
831                 if (dep->version) {
832                         strncat(str, " (", len);
833                         strncat(str, constraint_to_str(dep->constraint), len);
834                         strncat(str, dep->version, len);
835                         strncat(str, ")", len);
836                 }
837         }
838
839         return str;
840 }
841
842 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
843 {
844         compound_depend_t *depends;
845         int count, othercount;
846         int i, j;
847         abstract_pkg_t *ab_depend;
848         abstract_pkg_t **temp;
849
850         count = pkg->pre_depends_count +
851             pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
852
853         for (i = 0; i < count; i++) {
854                 depends = &pkg->depends[i];
855                 if (depends->type != PREDEPEND
856                     && depends->type != DEPEND && depends->type != RECOMMEND)
857                         continue;
858                 for (j = 0; j < depends->possibility_count; j++) {
859                         ab_depend = depends->possibilities[j]->pkg;
860                         if (!ab_depend->depended_upon_by) {
861                                 ab_depend->depended_upon_by =
862                                     xcalloc(1, sizeof(abstract_pkg_t *));
863                         }
864
865                         temp = ab_depend->depended_upon_by;
866                         othercount = 1;
867                         while (*temp) {
868                                 temp++;
869                                 othercount++;
870                         }
871                         *temp = ab_pkg;
872
873                         ab_depend->depended_upon_by =
874                             xrealloc(ab_depend->depended_upon_by,
875                                      (othercount +
876                                       1) * sizeof(abstract_pkg_t *));
877
878                         /* the array may have been moved by realloc */
879                         temp = ab_depend->depended_upon_by + othercount;
880                         *temp = NULL;
881                 }
882         }
883 }
884
885 static depend_t *depend_init(void)
886 {
887         depend_t *d = xcalloc(1, sizeof(depend_t));
888         d->constraint = NONE;
889         d->version = NULL;
890         d->pkg = NULL;
891
892         return d;
893 }
894
895 static int parseDepends(compound_depend_t * compound_depend, char *depend_str)
896 {
897         char *pkg_name, buffer[2048];
898         unsigned int num_of_ors = 0;
899         int i;
900         char *src, *dest;
901         depend_t **possibilities;
902
903         /* first count the number of ored possibilities for satisfying dependency */
904         src = depend_str;
905         while (*src)
906                 if (*src++ == '|')
907                         num_of_ors++;
908
909         compound_depend->type = DEPEND;
910
911         compound_depend->possibility_count = num_of_ors + 1;
912         possibilities = xcalloc((num_of_ors + 1), sizeof(depend_t *));
913         compound_depend->possibilities = possibilities;
914
915         src = depend_str;
916         for (i = 0; i < num_of_ors + 1; i++) {
917                 possibilities[i] = depend_init();
918                 /* gobble up just the name first */
919                 dest = buffer;
920                 while (*src &&
921                        !isspace(*src) &&
922                        (*src != '(') && (*src != '*') && (*src != '|'))
923                         *dest++ = *src++;
924                 *dest = '\0';
925                 pkg_name = trim_xstrdup(buffer);
926
927                 /* now look at possible version info */
928
929                 /* skip to next chars */
930                 if (isspace(*src))
931                         while (*src && isspace(*src))
932                                 src++;
933
934                 /* extract constraint and version */
935                 if (*src == '(') {
936                         src++;
937                         if (!strncmp(src, "<<", 2)) {
938                                 possibilities[i]->constraint = EARLIER;
939                                 src += 2;
940                         } else if (!strncmp(src, "<=", 2)) {
941                                 possibilities[i]->constraint = EARLIER_EQUAL;
942                                 src += 2;
943                         } else if (!strncmp(src, ">=", 2)) {
944                                 possibilities[i]->constraint = LATER_EQUAL;
945                                 src += 2;
946                         } else if (!strncmp(src, ">>", 2)) {
947                                 possibilities[i]->constraint = LATER;
948                                 src += 2;
949                         } else if (!strncmp(src, "=", 1)) {
950                                 possibilities[i]->constraint = EQUAL;
951                                 src++;
952                         }
953                         /* should these be here to support deprecated designations; dpkg does */
954                         else if (!strncmp(src, "<", 1)) {
955                                 possibilities[i]->constraint = EARLIER_EQUAL;
956                                 src++;
957                         } else if (!strncmp(src, ">", 1)) {
958                                 possibilities[i]->constraint = LATER_EQUAL;
959                                 src++;
960                         }
961
962                         /* now we have any constraint, pass space to version string */
963                         while (isspace(*src))
964                                 src++;
965
966                         /* this would be the version string */
967                         dest = buffer;
968                         while (*src && *src != ')')
969                                 *dest++ = *src++;
970                         *dest = '\0';
971
972                         possibilities[i]->version = trim_xstrdup(buffer);
973                 }
974                 /* hook up the dependency to its abstract pkg */
975                 possibilities[i]->pkg = ensure_abstract_pkg_by_name(pkg_name);
976
977                 free(pkg_name);
978
979                 /* now get past the ) and any possible | chars */
980                 while (*src &&
981                        (isspace(*src) || (*src == ')') || (*src == '|')))
982                         src++;
983                 if (*src == '*') {
984                         compound_depend->type = GREEDY_DEPEND;
985                         src++;
986                 }
987         }
988
989         return 0;
990 }