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