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