Remove if (0 && ...) ... statements.
[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 "includes.h"
19 #include <errno.h>
20 #include <ctype.h>
21    
22 #include "pkg.h"
23 #include "opkg_utils.h"
24 #include "pkg_hash.h"
25 #include "opkg_message.h"
26 #include "pkg_parse.h"
27 #include "hash_table.h"
28 #include "libbb/libbb.h"
29
30 static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str);
31 static depend_t * depend_init(void);
32 static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx);
33 static char ** merge_unresolved(char ** oldstuff, char ** newstuff);
34 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
35
36 static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
37 {
38      depend_t *depend = (depend_t *)cdata;
39      if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
40           return 1;
41      else
42           return 0;
43 }
44
45 static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
46 {
47      depend_t *depend = (depend_t *)cdata;
48      if (version_constraints_satisfied(depend, pkg))
49           return 1;
50      else
51           return 0;
52 }
53
54 /* returns ndependences or negative error value */ 
55 int pkg_hash_fetch_unsatisfied_dependencies(opkg_conf_t *conf, pkg_t * pkg, 
56                                             pkg_vec_t *unsatisfied, char *** unresolved)
57 {
58      pkg_t * satisfier_entry_pkg;
59      int i, j, k;
60      int count, found;
61      char ** the_lost;
62      abstract_pkg_t * ab_pkg;
63
64      /* 
65       * this is a setup to check for redundant/cyclic dependency checks, 
66       * which are marked at the abstract_pkg level
67       */
68      if (!(ab_pkg = pkg->parent)) {
69           fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
70           *unresolved = NULL;
71           return 0;
72      }
73      if (ab_pkg->dependencies_checked) {    /* avoid duplicate or cyclic checks */
74           *unresolved = NULL;
75           return 0;
76      } else { 
77           ab_pkg->dependencies_checked = 1;  /* mark it for subsequent visits */
78      }
79      /**/
80
81      count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
82      if (!count){
83           *unresolved = NULL;
84           return 0;
85      }
86
87      the_lost = NULL;
88         
89      /* foreach dependency */
90      for (i = 0; i < count; i++) {
91           compound_depend_t * compound_depend = &pkg->depends[i];
92           depend_t ** possible_satisfiers = compound_depend->possibilities;;
93           found = 0;
94           satisfier_entry_pkg = NULL;
95
96           if (compound_depend->type == GREEDY_DEPEND) {
97                /* foreach possible satisfier */
98                for (j = 0; j < compound_depend->possibility_count; j++) {
99                     /* foreach provided_by, which includes the abstract_pkg itself */
100                     abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
101                     abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
102                     int nposs = ab_provider_vec->len;
103                     abstract_pkg_t **ab_providers = ab_provider_vec->pkgs; 
104                     int l;
105                     for (l = 0; l < nposs; l++) {
106                          pkg_vec_t *test_vec = ab_providers[l]->pkgs;
107                          /* if no depends on this one, try the first package that Provides this one */
108                          if (!test_vec){   /* no pkg_vec hooked up to the abstract_pkg!  (need another feed?) */
109                               continue;
110                          }
111               
112                          /* cruise this possiblity's pkg_vec looking for an installed version */
113                          for (k = 0; k < test_vec->len; k++) {
114                               pkg_t *pkg_scout = test_vec->pkgs[k];
115                               /* not installed, and not already known about? */
116                               if ((pkg_scout->state_want != SW_INSTALL)
117                                   && !pkg_scout->parent->dependencies_checked
118                                   && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
119                                    char ** newstuff = NULL;
120                                    int rc;
121                                    pkg_vec_t *tmp_vec = pkg_vec_alloc ();
122                                    /* check for not-already-installed dependencies */
123                                    rc = pkg_hash_fetch_unsatisfied_dependencies(conf, 
124                                                                                 pkg_scout, 
125                                                                                 tmp_vec,
126                                                                                 &newstuff);
127                                    if (newstuff == NULL) {
128                                         int i;
129                                         int ok = 1;
130                                         for (i = 0; i < rc; i++) {
131                                             pkg_t *p = tmp_vec->pkgs[i];
132                                             if (p->state_want == SW_INSTALL)
133                                                 continue;
134                                             opkg_message(conf, OPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
135                                             ok = 0;
136                                             break;
137                                         }
138                                         pkg_vec_free (tmp_vec);
139                                         if (ok) {
140                                             /* mark this one for installation */
141                                             opkg_message(conf, OPKG_NOTICE, "Adding satisfier for greedy dependence: %s\n", pkg_scout->name);
142                                             pkg_vec_insert(unsatisfied, pkg_scout);
143                                         }
144                                    } else  {
145                                         opkg_message(conf, OPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
146                                         free (newstuff);
147                                    }
148                               }
149                          }
150                     }
151                }
152
153                continue;
154           }
155
156           /* foreach possible satisfier, look for installed package  */
157           for (j = 0; j < compound_depend->possibility_count; j++) {
158                /* foreach provided_by, which includes the abstract_pkg itself */
159                depend_t *dependence_to_satisfy = possible_satisfiers[j];
160                abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
161                pkg_t *satisfying_pkg = 
162                     pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
163                                                                pkg_installed_and_constraint_satisfied, 
164                                                                dependence_to_satisfy, 1, NULL);
165                /* Being that I can't test constraing in pkg_hash, I will test it here */
166                if (satisfying_pkg != NULL) {
167                   if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
168                       satisfying_pkg = NULL;
169                   }
170                }
171                opkg_message(conf, OPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
172                if (satisfying_pkg != NULL) {
173                     found = 1;
174                     break;
175                }
176
177           }
178           /* if nothing installed matches, then look for uninstalled satisfier */
179           if (!found) {
180                /* foreach possible satisfier, look for installed package  */
181                for (j = 0; j < compound_depend->possibility_count; j++) {
182                     /* foreach provided_by, which includes the abstract_pkg itself */
183                     depend_t *dependence_to_satisfy = possible_satisfiers[j];
184                     abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
185                     pkg_t *satisfying_pkg = 
186                          pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
187                                                                     pkg_constraint_satisfied, 
188                                                                     dependence_to_satisfy, 1, NULL);
189                     /* Being that I can't test constraing in pkg_hash, I will test it here too */
190                     if (satisfying_pkg != NULL) {
191                          if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
192                             satisfying_pkg = NULL;
193                          }
194                     }
195
196                     /* user request overrides package recommendation */
197                     if (satisfying_pkg != NULL
198                         && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
199                         && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
200                          opkg_message (conf, OPKG_NOTICE, "%s: ignoring recommendation for %s at user request\n",
201                                        pkg->name, satisfying_pkg->name);
202                          continue;
203                     }
204
205                     opkg_message(conf, OPKG_DEBUG, "%s:%d: satisfying_pkg=%p\n", __FILE__, __LINE__, satisfying_pkg);
206                     if (satisfying_pkg != NULL) {
207                          satisfier_entry_pkg = satisfying_pkg;
208                          break;
209                     }
210                }
211           }
212
213           /* we didn't find one, add something to the unsatisfied vector */
214           if (!found) {
215                if (!satisfier_entry_pkg) {
216                     /* failure to meet recommendations is not an error */
217                     if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
218                          the_lost = add_unresolved_dep(pkg, the_lost, i);
219                     else
220                          opkg_message (conf, OPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
221                                        pkg->name, compound_depend->possibilities[0]->pkg->name);
222                }
223                else {
224                     if (compound_depend->type == SUGGEST) {
225                          /* just mention it politely */
226                          opkg_message (conf, OPKG_NOTICE, "package %s suggests installing %s\n",
227                                        pkg->name, satisfier_entry_pkg->name);
228                     } else {
229                          char ** newstuff = NULL;
230                          
231                          if (satisfier_entry_pkg != pkg &&
232                              !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) {
233                               pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
234                               pkg_hash_fetch_unsatisfied_dependencies(conf, 
235                                                                       satisfier_entry_pkg, 
236                                                                       unsatisfied,
237                                                                       &newstuff);
238                               the_lost = merge_unresolved(the_lost, newstuff);
239                          }
240                     }
241                }
242           }
243      }
244      *unresolved = the_lost;
245
246      return unsatisfied->len;
247 }
248
249 /*checking for conflicts !in replaces 
250   If a packages conflicts with another but is also replacing it, I should not consider it a 
251   really conflicts 
252   returns 0 if conflicts <> replaces or 1 if conflicts == replaces 
253 */
254 int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
255 {
256     int i ;
257     int replaces_count = pkg->replaces_count;
258     abstract_pkg_t **replaces;
259
260     if (pkg->replaces_count==0)    // No replaces, it's surely a conflict
261         return 0;
262
263     replaces = pkg->replaces;
264
265     for (i = 0; i < replaces_count; i++) {
266         if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) {      // Found
267             opkg_message(NULL, OPKG_DEBUG2, "Seems I've found a replace %s %s \n",pkg_scout->name,pkg->replaces[i]->name);
268             return 1;
269         }
270     }
271     return 0;
272
273 }
274
275
276 /* Abhaya: added support for conflicts */
277 pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
278 {
279     pkg_vec_t * installed_conflicts, * test_vec;
280     compound_depend_t * conflicts;
281     depend_t ** possible_satisfiers;
282     depend_t * possible_satisfier;
283     int i, j, k;
284     int count;
285     abstract_pkg_t * ab_pkg;
286     pkg_t **pkg_scouts; 
287     pkg_t *pkg_scout; 
288
289     /* 
290      * this is a setup to check for redundant/cyclic dependency checks, 
291      * which are marked at the abstract_pkg level
292      */
293     if(!(ab_pkg = pkg->parent)){
294         fprintf(stderr, "dependency check error.  pkg %s isn't in hash table\n", pkg->name);
295         return (pkg_vec_t *)NULL;
296     }
297
298     conflicts = pkg->conflicts;
299     if(!conflicts){
300         return (pkg_vec_t *)NULL;
301     }
302     installed_conflicts = pkg_vec_alloc();
303
304     count = pkg->conflicts_count;
305
306
307
308     /* foreach conflict */
309     for(i = 0; i < pkg->conflicts_count; i++){
310
311         possible_satisfiers = conflicts->possibilities;
312
313         /* foreach possible satisfier */
314         for(j = 0; j < conflicts->possibility_count; j++){
315             possible_satisfier = possible_satisfiers[j];
316             if (!possible_satisfier)
317                 fprintf(stderr, "%s:%d: possible_satisfier is null\n", __FUNCTION__, __LINE__);
318             if (!possible_satisfier->pkg)
319                 fprintf(stderr, "%s:%d: possible_satisfier->pkg is null\n", __FUNCTION__, __LINE__);
320             test_vec = possible_satisfier->pkg->pkgs;
321             if (test_vec) {
322                 /* pkg_vec found, it is an actual package conflict
323                  * cruise this possiblity's pkg_vec looking for an installed version */
324                 pkg_scouts = test_vec->pkgs;
325                 for(k = 0; k < test_vec->len; k++){
326                     pkg_scout = pkg_scouts[k];
327                     if (!pkg_scout) {
328                         fprintf(stderr,  "%s: null pkg scout\n", __FUNCTION__);
329                         continue; 
330                     }
331                     if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
332                        version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
333                         if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
334                             pkg_vec_insert(installed_conflicts, pkg_scout);
335                         }
336                     }
337                 }
338             }
339         }
340         conflicts++;
341     }
342
343     if (installed_conflicts->len)
344             return installed_conflicts;
345     pkg_vec_free(installed_conflicts);
346         return (pkg_vec_t *)NULL;
347 }
348
349 int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
350 {
351     pkg_t * temp;
352     int comparison;
353
354     if(depends->constraint == NONE)
355         return 1;
356
357     temp = pkg_new();
358
359     parseVersion(temp, depends->version);
360
361     comparison = pkg_compare_versions(pkg, temp);
362
363     free (temp->version);
364     free(temp);
365
366     if((depends->constraint == EARLIER) && 
367        (comparison < 0))
368         return 1;
369     else if((depends->constraint == LATER) && 
370             (comparison > 0))
371         return 1;
372     else if(comparison == 0)
373         return 1;
374     else if((depends->constraint == LATER_EQUAL) && 
375             (comparison >= 0))
376         return 1;
377     else if((depends->constraint == EARLIER_EQUAL) && 
378             (comparison <= 0))
379         return 1;
380     
381     return 0;
382 }
383
384 int pkg_dependence_satisfiable(opkg_conf_t *conf, depend_t *depend)
385 {
386      abstract_pkg_t *apkg = depend->pkg;
387      abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
388      int n_providers = provider_apkgs->len;
389      abstract_pkg_t **apkgs = provider_apkgs->pkgs;
390      pkg_vec_t *pkg_vec;
391      int n_pkgs ; 
392      int i;
393      int j;
394
395      for (i = 0; i < n_providers; i++) {
396           abstract_pkg_t *papkg = apkgs[i];
397           pkg_vec = papkg->pkgs;
398           if (pkg_vec) {
399                n_pkgs = pkg_vec->len;
400                for (j = 0; j < n_pkgs; j++) {
401                     pkg_t *pkg = pkg_vec->pkgs[j];
402                     if (version_constraints_satisfied(depend, pkg)) {
403                          return 1;
404                     }
405                }
406           }
407      }
408      return 0;
409 }
410
411 int pkg_dependence_satisfied(opkg_conf_t *conf, depend_t *depend)
412 {
413      abstract_pkg_t *apkg = depend->pkg;
414      abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
415      int n_providers = provider_apkgs->len;
416      abstract_pkg_t **apkgs = provider_apkgs->pkgs;
417      int i;
418      int n_pkgs;
419      int j;
420
421      for (i = 0; i < n_providers; i++) {
422           abstract_pkg_t *papkg = apkgs[i];
423           pkg_vec_t *pkg_vec = papkg->pkgs;
424           if (pkg_vec) {
425                n_pkgs = pkg_vec->len;
426                for (j = 0; j < n_pkgs; j++) {
427                     pkg_t *pkg = pkg_vec->pkgs[j];
428                     if (version_constraints_satisfied(depend, pkg)) {
429                          if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
430                               return 1;
431                     }
432                }
433           }
434      }
435      return 0;
436 }
437
438 static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
439 {
440     int i;
441     pkg_t ** pkgs = vec->pkgs;
442
443     for(i = 0; i < vec->len; i++)
444         if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
445            && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
446            && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
447             return 1;
448     return 0;
449 }
450
451
452 #ifdef DeadCode
453 /**
454  * pkg_has_common_provides returns 1 if pkg and replacee both provide
455  * the same abstract package and 0 otherwise.
456  */
457 int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
458 {
459      abstract_pkg_t **provides = pkg->provides;
460      int provides_count = pkg->provides_count;
461      abstract_pkg_t **replacee_provides = replacee->provides;
462      int replacee_provides_count = replacee->provides_count;
463      int i, j;
464      for (i = 0; i < provides_count; i++) {
465           abstract_pkg_t *apkg = provides[i];
466           for (j = 0; j < replacee_provides_count; j++) {
467                abstract_pkg_t *replacee_apkg = replacee_provides[i];
468                if (apkg == replacee_apkg)
469                     return 1;
470           }
471      }
472      return 0;
473 }
474 #endif
475
476 /**
477  * pkg_provides_abstract returns 1 if pkg->provides contains providee
478  * and 0 otherwise.
479  */
480 int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
481 {
482      abstract_pkg_t **provides = pkg->provides;
483      int provides_count = pkg->provides_count;
484      int i;
485      for (i = 0; i < provides_count; i++) {
486           if (provides[i] == providee)
487                return 1;
488      }
489      return 0;
490 }
491
492 /**
493  * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
494  * otherwise.
495  */
496 int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
497 {
498      abstract_pkg_t **replaces = pkg->replaces;
499      int replaces_count = pkg->replaces_count;
500      /* abstract_pkg_t **replacee_provides = pkg->provides;
501      int replacee_provides_count = pkg->provides_count; */
502      int i, j;
503      for (i = 0; i < replaces_count; i++) {
504           abstract_pkg_t *abstract_replacee = replaces[i];
505           for (j = 0; j < replaces_count; j++) {
506    /*            opkg_message(NULL, OPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
507                  pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
508                if (replacee->provides[j] == abstract_replacee)
509                     return 1;
510           }
511      }
512      return 0;
513 }
514
515
516 /**
517  * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
518  * otherwise.
519  */
520 int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
521 {
522      compound_depend_t *conflicts = pkg->conflicts;
523      int conflicts_count = pkg->conflicts_count;
524      int i, j;
525      for (i = 0; i < conflicts_count; i++) {
526           int possibility_count = conflicts[i].possibility_count;
527           struct depend **possibilities = conflicts[i].possibilities;
528           for (j = 0; j < possibility_count; j++) {
529                if (possibilities[j]->pkg == conflictee) {
530                     return 1;
531                }
532           }
533      }
534      return 0;
535 }
536
537 /**
538  * pkg_conflicts returns 1 if pkg->conflicts contains one of
539  * conflictee's provides and 0 otherwise.
540  */
541 int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
542 {
543      compound_depend_t *conflicts = pkg->conflicts;
544      int conflicts_count = pkg->conflicts_count;
545      abstract_pkg_t **conflictee_provides = conflictee->provides;
546      int conflictee_provides_count = conflictee->provides_count;
547      int i, j, k;
548      int possibility_count;
549      struct depend **possibilities;
550      abstract_pkg_t *possibility ;
551
552      for (i = 0; i < conflicts_count; i++) {
553           possibility_count = conflicts[i].possibility_count;
554           possibilities = conflicts[i].possibilities;
555           for (j = 0; j < possibility_count; j++) {
556                possibility = possibilities[j]->pkg;
557                for (k = 0; k < conflictee_provides_count; k++) {
558                     if (possibility == conflictee_provides[k]) {
559                          return 1;
560                     }
561                }
562           }
563      }
564      return 0;
565 }
566
567 static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
568 {
569     int oldlen = 0, newlen = 0;
570     char ** result;
571     int i, j;
572
573     if(!newstuff)
574         return oldstuff;
575
576     while(oldstuff && oldstuff[oldlen]) oldlen++;
577     while(newstuff && newstuff[newlen]) newlen++;
578
579     result = xrealloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
580
581     for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
582         *(result + i) = *(newstuff + j);
583
584     *(result + i) = NULL;
585
586     return result;
587 }
588
589 /* 
590  * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
591  * this is null terminated, no count is carried around 
592  */
593 char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
594 {
595     int count;
596     char ** resized;
597     char *depend_str = pkg_depend_str(pkg, ref_ndx);
598
599     count = 0;
600     while(the_lost && the_lost[count]) count++;
601
602     count++;  /* need one to hold the null */
603     resized = xrealloc(the_lost, sizeof(char *) * (count + 1));
604     resized[count - 1] = xstrdup(depend_str);
605     resized[count] = NULL;
606     
607     return resized;
608 }
609
610 void buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
611 {
612     int i;
613
614     /* every pkg provides itself */
615     abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
616
617     if (!pkg->provides_count)
618       return;
619
620     pkg->provides = xcalloc((pkg->provides_count + 1), sizeof(abstract_pkg_t *));
621     pkg->provides[0] = ab_pkg;
622
623     for(i = 0; i < pkg->provides_count; i++){
624         abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
625
626         pkg->provides[i+1] = provided_abpkg;
627
628         abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
629     }
630 }
631
632 /* Abhaya: added conflicts support */
633 void buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
634 {
635     int i;
636     compound_depend_t * conflicts;
637
638     if (!pkg->conflicts_count)
639         return;
640
641     conflicts = pkg->conflicts = xcalloc(pkg->conflicts_count, sizeof(compound_depend_t));
642     for (i = 0; i < pkg->conflicts_count; i++) {
643          conflicts->type = CONFLICTS;
644          parseDepends(conflicts, hash,
645                       pkg->conflicts_str[i]);
646          conflicts++;
647     }
648 }
649
650 void buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
651 {
652      int i;
653
654      if (!pkg->replaces_count)
655           return;
656
657      pkg->replaces = xcalloc(pkg->replaces_count, sizeof(abstract_pkg_t *));
658
659      for(i = 0; i < pkg->replaces_count; i++){
660           abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
661
662           pkg->replaces[i] = old_abpkg;
663
664           if (!old_abpkg->replaced_by)
665                old_abpkg->replaced_by = abstract_pkg_vec_alloc();
666           /* if a package pkg both replaces and conflicts old_abpkg,
667            * then add it to the replaced_by vector so that old_abpkg
668            * will be upgraded to ab_pkg automatically */
669           if (pkg_conflicts_abstract(pkg, old_abpkg))
670                abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
671      }
672 }
673
674 void buildDepends(hash_table_t * hash, pkg_t * pkg)
675 {
676      int count;
677      int i;
678      compound_depend_t * depends;
679
680      if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
681           return;
682
683      depends = pkg->depends = xcalloc(count, sizeof(compound_depend_t));
684
685      for(i = 0; i < pkg->pre_depends_count; i++){
686           parseDepends(depends, hash, pkg->pre_depends_str[i]);
687           depends->type = PREDEPEND;
688           depends++;
689      }
690
691      for(i = 0; i < pkg->recommends_count; i++){
692           parseDepends(depends, hash, pkg->recommends_str[i]);
693           depends->type = RECOMMEND;
694           depends++;
695      }
696
697      for(i = 0; i < pkg->suggests_count; i++){
698           parseDepends(depends, hash, pkg->suggests_str[i]);
699           depends->type = SUGGEST;
700           depends++;
701      }
702
703      for(i = 0; i < pkg->depends_count; i++){
704           parseDepends(depends, hash, pkg->depends_str[i]);
705           depends++;
706      }
707 }
708
709 /*
710  * pkg_depend_string: returns the depends string specified by index.
711  *   All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
712  *   [0,npredepends) -> returns pre_depends_str[index]
713  *   [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
714  *   [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
715  *   [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
716  */
717 char *pkg_depend_str(pkg_t *pkg, int index)
718 {
719      if (index < pkg->pre_depends_count) {
720           return pkg->pre_depends_str[index];
721      }
722      index -= pkg->pre_depends_count;
723
724      if (index < pkg->recommends_count) {
725           return pkg->recommends_str[index];
726      }
727      index -= pkg->recommends_count;
728
729      if (index < pkg->suggests_count) {
730           return pkg->suggests_str[index];
731      }
732      index -= pkg->suggests_count;
733
734      if (index < pkg->depends_count) {
735           return pkg->depends_str[index];
736      }
737      fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
738      return NULL;
739 }
740
741 void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
742 {
743      compound_depend_t * depends;
744      int count, othercount;
745      int i, j;
746      abstract_pkg_t * ab_depend;
747      abstract_pkg_t ** temp;
748
749      count = pkg->pre_depends_count + pkg->depends_count;
750      depends = pkg->depends;
751
752      for (i = 0; i < count; i++) {
753           for (j = 0; j < depends->possibility_count; j++){
754                ab_depend = depends->possibilities[j]->pkg;
755                if(!ab_depend->depended_upon_by)
756                     ab_depend->depended_upon_by = xcalloc(1, sizeof(abstract_pkg_t *));
757
758                temp = ab_depend->depended_upon_by;
759                othercount = 1;
760                while(*temp){
761                     temp++;
762                     othercount++;
763                }
764                *temp = ab_pkg;
765
766                ab_depend->depended_upon_by = xrealloc(ab_depend->depended_upon_by, 
767                                                                         (othercount + 1) * sizeof(abstract_pkg_t *));
768                /* the array may have moved */
769                temp = ab_depend->depended_upon_by + othercount;
770                *temp = NULL;
771           }
772           depends++;
773      }
774 }
775
776 static depend_t * depend_init(void)
777 {
778     depend_t * d = xcalloc(1, sizeof(depend_t));    
779     d->constraint = NONE;
780     d->version = NULL;
781     d->pkg = NULL;
782     
783     return d;
784 }
785
786 static int parseDepends(compound_depend_t *compound_depend, 
787                         hash_table_t * hash, char * depend_str)
788 {
789      char * pkg_name, buffer[2048];
790      int num_of_ors = 0;
791      int i;
792      char * src, * dest;
793      depend_t ** possibilities;
794
795      /* first count the number of ored possibilities for satisfying dependency */
796      src = depend_str;
797      while(*src)
798           if(*src++ == '|')
799                num_of_ors++;
800
801      compound_depend->type = DEPEND;
802
803      compound_depend->possibility_count = num_of_ors + 1;
804      possibilities = xcalloc((num_of_ors + 1), sizeof(depend_t *) );
805      compound_depend->possibilities = possibilities;
806
807      src = depend_str;
808      for(i = 0; i < num_of_ors + 1; i++){
809           possibilities[i] = depend_init();
810           /* gobble up just the name first */
811           dest = buffer;
812           while(*src &&
813                 !isspace(*src) &&
814                 (*src != '(') &&
815                 (*src != '*') &&
816                 (*src != '|'))
817                *dest++ = *src++;
818           *dest = '\0';
819           pkg_name = trim_alloc(buffer);
820           if (pkg_name == NULL )
821                return -ENOMEM;
822         
823           /* now look at possible version info */
824         
825           /* skip to next chars */
826           if(isspace(*src))
827                while(*src && isspace(*src)) src++;
828
829           /* extract constraint and version */
830           if(*src == '('){
831                src++;
832                if(!strncmp(src, "<<", 2)){
833                     possibilities[i]->constraint = EARLIER;
834                     src += 2;
835                }
836                else if(!strncmp(src, "<=", 2)){
837                     possibilities[i]->constraint = EARLIER_EQUAL;
838                     src += 2;
839                }
840                else if(!strncmp(src, ">=", 2)){
841                     possibilities[i]->constraint = LATER_EQUAL;
842                     src += 2;
843                }
844                else if(!strncmp(src, ">>", 2)){
845                     possibilities[i]->constraint = LATER;
846                     src += 2;
847                }
848                else if(!strncmp(src, "=", 1)){
849                     possibilities[i]->constraint = EQUAL;
850                     src++;
851                }
852                /* should these be here to support deprecated designations; dpkg does */
853                else if(!strncmp(src, "<", 1)){
854                     possibilities[i]->constraint = EARLIER_EQUAL;
855                     src++;
856                }
857                else if(!strncmp(src, ">", 1)){
858                     possibilities[i]->constraint = LATER_EQUAL;
859                     src++; 
860                }
861
862                /* now we have any constraint, pass space to version string */
863                while(isspace(*src)) src++;
864
865                /* this would be the version string */
866                dest = buffer;
867                while(*src && *src != ')')
868                     *dest++ = *src++;
869                *dest = '\0';
870             
871                possibilities[i]->version = trim_alloc(buffer);
872                /*                   fprintf(stderr, "let's print the depends version string:");
873                                     fprintf(stderr, "version %s\n", possibilities[i]->version);*/
874                if (possibilities[i]->version == NULL )
875                     return -ENOMEM;
876
877          
878           }
879           /* hook up the dependency to its abstract pkg */
880           possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
881
882           free(pkg_name);
883         
884           /* now get past the ) and any possible | chars */
885           while(*src &&
886                 (isspace(*src) ||
887                  (*src == ')') ||
888                  (*src == '|')))
889                src++;
890           if (*src == '*')
891           {
892                compound_depend->type = GREEDY_DEPEND;
893                src++;
894           }
895      }
896
897      return 0;
898 }