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