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