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