opkg: improve download error reporting
[oweals/opkg-lede.git] / pkg_hash.c
1 /* opkg_hash.c - the itsy 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 "opkg.h"
19 #include <errno.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "hash_table.h"
25 #include "pkg.h"
26 #include "opkg_message.h"
27 #include "pkg_vec.h"
28 #include "pkg_hash.h"
29 #include "pkg_parse.h"
30 #include "opkg_utils.h"
31
32 static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
33
34 /*
35  * this will talk to both feeds-lists files and installed status files
36  * example api:
37  *
38  * hash_table_t hash;
39  * pkg_hash_init(name, &hash, 1000);
40  * pkg_hash_add_from_file(<feed filename>);
41  *
42  * the query function is just there as a shell to prove to me that this
43  * sort of works, but isn't far from doing something useful
44  * 
45  * -sma, 12/21/01
46  * modified: CDW 3 Jan. 2002
47  */
48
49
50 \f
51 int pkg_hash_init(const char *name, hash_table_t *hash, int len)
52 {
53   return hash_table_init(name, hash, len);
54 }
55
56 void pkg_hash_deinit(hash_table_t *hash)
57 {
58   hash_table_deinit(hash);
59 }
60
61
62 /* Find the default arch for a given package status file if none is given. */
63 static char *pkg_get_default_arch(opkg_conf_t *conf)
64 {
65      nv_pair_list_elt_t *l;
66      char *def_arch = HOST_CPU_STR;             /* Default arch */
67      int def_prio = 0;                          /* Other archs override this */
68
69      l = conf->arch_list.head;
70
71      while (l) {
72           nv_pair_t *nv = l->data;
73           int priority = strtol(nv->value, NULL, 0);
74
75           /* Check if this arch has higher priority, and is valid */
76           if ((priority > def_prio) &&
77               (strcmp(nv->name, "all")) && (strcmp(nv->name, "noarch"))) {
78                /* Our new default */
79                def_prio = priority;
80                def_arch = nv->name;
81           }
82           l = l->next;
83      }
84
85      return strdup(def_arch);
86 }
87
88 int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
89                            pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
90 {
91      hash_table_t *hash = &conf->pkg_hash;
92      char **raw;
93      char **raw_start;
94      pkg_t *pkg;
95     
96      raw = raw_start = read_raw_pkgs_from_file(file_name);
97      if (!raw)
98         return -ENOMEM;
99
100      while(*raw){         /* don't worry, we'll increment raw in the parsing function */
101           pkg = pkg_new();
102           if (!pkg)
103                return -ENOMEM;
104
105           if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
106                if (!pkg->architecture) {
107                     char *version_str = pkg_version_str_alloc(pkg);
108                     pkg->architecture = pkg_get_default_arch(conf);
109                     opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
110                                  pkg->name, version_str, pkg->architecture);
111                     free(version_str);
112                }
113                hash_insert_pkg(hash, pkg, is_status_file,conf);
114           } else {
115                free(pkg);
116           }
117      }
118
119      /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
120         memory after read_raw_pkgs_from_file */
121      raw = raw_start;
122      while (*raw) {
123           free(*raw++);
124      }
125      free(raw_start);
126      return 0;
127 }
128
129 abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
130 {
131   return (abstract_pkg_t *)hash_table_get(hash, pkg_name);
132 }
133
134 abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name)
135 {
136     abstract_pkg_t *apkg = abstract_pkg_fetch_by_name(hash, name);
137     if (apkg)
138        return NULL;
139     return apkg->provided_by;
140 }
141
142
143 pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pkg_t *apkg, 
144                                                   int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet)
145 {
146      int i; 
147      int nprovides = 0;
148      int nmatching = 0;
149      pkg_vec_t *matching_pkgs = pkg_vec_alloc();
150      abstract_pkg_vec_t *matching_apkgs = abstract_pkg_vec_alloc();
151      abstract_pkg_vec_t *provided_apkg_vec;
152      abstract_pkg_t **provided_apkgs;
153      abstract_pkg_vec_t *providers = abstract_pkg_vec_alloc();
154      pkg_t *latest_installed_parent = NULL;
155      pkg_t *latest_matching = NULL;
156      pkg_t *held_pkg = NULL;
157      pkg_t *good_pkg_by_name = NULL;
158
159      if (matching_apkgs == NULL || providers == NULL || 
160          apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
161           return NULL;
162
163      opkg_message(conf, OPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
164
165      provided_apkg_vec = apkg->provided_by;
166      nprovides = provided_apkg_vec->len;
167      provided_apkgs = provided_apkg_vec->pkgs;
168      if (nprovides > 1)
169           opkg_message(conf, OPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
170
171      /* accumulate all the providers */
172      for (i = 0; i < nprovides; i++) {
173           abstract_pkg_t *provider_apkg = provided_apkgs[i];
174           opkg_message(conf, OPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
175           abstract_pkg_vec_insert(providers, provider_apkg);
176      }
177      nprovides = providers->len;
178
179      for (i = 0; i < nprovides; i++) {
180           abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
181           abstract_pkg_t *replacement_apkg = NULL;
182           pkg_vec_t *vec;
183
184           if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
185                replacement_apkg = provider_apkg->replaced_by->pkgs[0];
186                if (provider_apkg->replaced_by->len > 1) {
187                     opkg_message(conf, OPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
188                                  provider_apkg->name, replacement_apkg->name);
189                }
190           }
191
192           if (replacement_apkg)
193                opkg_message(conf, OPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
194                             replacement_apkg->name, provider_apkg->name);
195
196           if (replacement_apkg && (replacement_apkg != provider_apkg)) {
197                if (abstract_pkg_vec_contains(providers, replacement_apkg))
198                     continue;
199                else
200                     provider_apkg = replacement_apkg;
201           }
202
203           if (!(vec = provider_apkg->pkgs)) {
204                opkg_message(conf, OPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
205                continue;
206           }
207     
208
209           /* now check for supported architecture */
210           {
211                int max_count = 0;
212                int i;
213
214                /* count packages matching max arch priority and keep track of last one */
215                for (i = 0; i < vec->len; i++) {
216                     pkg_t *maybe = vec->pkgs[i];
217                     opkg_message(conf, OPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
218                                  maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
219                     if (maybe->arch_priority > 0)  {
220                          max_count++;
221                          abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
222                          pkg_vec_insert(matching_pkgs, maybe);
223                     }
224                }
225           }
226      }
227
228      if (matching_pkgs->len > 1)
229           pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
230      if (matching_apkgs->len > 1)
231           abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
232
233 /* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
234    needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
235 /* The problem is what to do when there are more than a mathing package, with the same name and several version ?
236    Until now I always got the latest, but that breaks the downgrade option.
237    If I stop at the first one, I would probably miss the new ones 
238    Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
239    or from a Packages feed.
240    It it is from a file it always need to be checked whatever version I have in feeds or everywhere, according to force-down or whatever options*/
241 /*Pigi*/
242
243      for (i = 0; i < matching_pkgs->len; i++) {
244           pkg_t *matching = matching_pkgs->pkgs[i];
245           if (constraint_fcn(matching, cdata)) {  /* We found it */
246              opkg_message(conf, OPKG_DEBUG, " Found a valid candidate for the install: %s %s  \n", matching->name, matching->version) ;
247              good_pkg_by_name = matching;
248              if ( matching->provided_by_hand == 1 )    /* It has been provided by hand, so it is what user want */
249                 break;                                 
250           }
251      }
252
253
254      for (i = 0; i < matching_pkgs->len; i++) {
255           pkg_t *matching = matching_pkgs->pkgs[i];
256           latest_matching = matching;
257           if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
258                latest_installed_parent = matching;
259           if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
260                if (held_pkg)
261                     opkg_message(conf, OPKG_ERROR, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
262                                  held_pkg->name, matching->name);
263                held_pkg = matching;
264           }
265      }
266
267      if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
268           opkg_message(conf, OPKG_ERROR, "Package=%s, %d matching providers\n",
269                        apkg->name, matching_apkgs->len);
270           for (i = 0; i < matching_apkgs->len; i++) {
271               abstract_pkg_t *matching = matching_apkgs->pkgs[i];
272               opkg_message(conf, OPKG_ERROR, "    %s\n", matching->name);
273           }
274           opkg_message(conf, OPKG_ERROR, "Please select one with opkg install or opkg flag prefer\n");
275      }
276
277      if (matching_apkgs->len > 1 && conf->verbosity > 1) {
278           opkg_message(conf, OPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
279                        __FUNCTION__, apkg->name, matching_pkgs->len);
280           for (i = 0; i < matching_pkgs->len; i++) {
281                pkg_t *matching = matching_pkgs->pkgs[i];
282                opkg_message(conf, OPKG_INFO, "    %s %s %s\n",
283                             matching->name, matching->version, matching->architecture);
284           }
285      }
286
287      nmatching = matching_apkgs->len;
288
289      pkg_vec_free(matching_pkgs);
290      abstract_pkg_vec_free(matching_apkgs);
291      abstract_pkg_vec_free(providers);
292
293      if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
294           return good_pkg_by_name;
295      }
296      if (held_pkg) {
297           opkg_message(conf, OPKG_INFO, "  using held package %s\n", held_pkg->name);
298           return held_pkg;
299      }
300      if (latest_installed_parent) {
301           opkg_message(conf, OPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
302           return latest_installed_parent;
303      }
304      if (nmatching > 1) {
305           opkg_message(conf, OPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
306           return NULL;
307      }
308      if (latest_matching) {
309           opkg_message(conf, OPKG_INFO, "  using latest matching %s %s %s\n",
310                        latest_matching->name, latest_matching->version, latest_matching->architecture);
311           return latest_matching;
312      }
313      return NULL;
314 }
315
316 static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
317 {
318      const char *name = (const char *)cdata;
319      if (strcmp(pkg->name, name) == 0)
320           return 1;
321      else
322           return 0;   
323 }
324
325 pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t *conf, const char *name)
326 {
327      hash_table_t *hash = &conf->pkg_hash;
328      abstract_pkg_t *apkg = NULL;
329
330      if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
331           return NULL;
332      
333      return pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0);
334 }
335
336
337 pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
338                                        const char *pkg_name,
339                                        const char * version)
340 {
341     pkg_vec_t * vec;
342     register int i;
343     char *version_str = NULL;
344     
345     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
346         return NULL;
347     
348     for(i = 0; i < vec->len; i++) {
349         version_str = pkg_version_str_alloc(vec->pkgs[i]);
350         if(!strcmp(version_str, version)) {
351             free(version_str);
352             break;
353         }
354         free(version_str);
355     }
356         
357     if(i == vec->len)
358         return NULL;
359     
360     return vec->pkgs[i];
361 }
362
363 pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
364                                              const char *pkg_name,
365                                              pkg_dest_t *dest)
366 {
367     pkg_vec_t * vec;
368     register int i;
369
370     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
371         return NULL;
372     }
373     
374     for(i = 0; i < vec->len; i++)
375         if((vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED) && vec->pkgs[i]->dest == dest) {
376             return vec->pkgs[i];
377         }
378     return NULL;
379 }
380
381 pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
382                                         const char *pkg_name)
383 {
384     pkg_vec_t * vec;
385     register int i;
386
387     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
388         return NULL;
389     } 
390
391     for(i = 0; i < vec->len; i++)
392         if (vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED){
393             return vec->pkgs[i];
394         } 
395     
396     return NULL;
397 }
398
399 pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
400 {
401     abstract_pkg_t * ab_pkg;
402
403     if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name))){
404        return NULL;
405     }
406     
407     if (ab_pkg->pkgs) {
408       return ab_pkg->pkgs;
409     } else if (ab_pkg->provided_by) {
410       abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
411       if (abpkg != NULL){
412           return abpkg->pkgs;
413       } else {
414           return ab_pkg->pkgs;
415       }
416     } else {
417       return NULL;
418     }
419 }
420
421 static int pkg_compare_names(const void *p1, const void *p2)
422 {
423   const pkg_t *pkg1 = *(const pkg_t **)p1;
424   const pkg_t *pkg2 = *(const pkg_t **)p2;
425   if (pkg1->name == NULL)
426     return 1;
427   if (pkg2->name == NULL)
428     return -1;
429   return(strcmp(pkg1->name, pkg2->name));
430 }
431
432
433 static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
434 {
435   int j;
436   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
437   pkg_vec_t *all = (pkg_vec_t *)data;
438   pkg_vec_t *pkg_vec = ab_pkg->pkgs;
439   if (pkg_vec) {
440     for (j = 0; j < pkg_vec->len; j++) {
441       pkg_t *pkg = pkg_vec->pkgs[j];
442       pkg_vec_insert(all, pkg);
443     }
444   }
445 }
446
447 void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
448 {
449     hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
450     qsort(all->pkgs, all->len, sizeof(pkg_t *), pkg_compare_names);
451 }
452
453 static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
454 {
455   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
456   pkg_vec_t *all = (pkg_vec_t *)data;
457   pkg_vec_t *pkg_vec = ab_pkg->pkgs;
458   int j;
459   if (pkg_vec) {
460     for (j = 0; j < pkg_vec->len; j++) {
461       pkg_t *pkg = pkg_vec->pkgs[j];
462       if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
463         pkg_vec_insert(all, pkg);
464       }
465     }
466   }
467 }
468 void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
469 {
470     hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
471     qsort(all->pkgs, all->len, sizeof(void*), pkg_compare_names);
472 }
473
474 static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
475 {
476   int i;
477   pkg_t *pkg;
478   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
479   opkg_conf_t *conf = (opkg_conf_t *)data;
480   abstract_pkg_t ** dependents = ab_pkg->depended_upon_by;
481   fprintf(stdout, "%s\n", ab_pkg->name);
482   i = 0;
483   if (dependents != NULL)
484     while (dependents [i] != NULL)
485       printf ("\tdepended upon by - %s\n", dependents [i ++]->name);
486   dependents = ab_pkg->provided_by->pkgs;
487   i = 0;
488   if (dependents != NULL)
489     while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
490       printf ("\tprovided by - %s\n", dependents [i ++]->name);
491   pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name);
492   if (pkg) {
493     i = 0;
494     while (i < pkg->depends_count)
495       printf ("\tdepends on - %s\n", pkg->depends_str [i ++]);  
496   }
497 }
498 void pkg_hash_dump(hash_table_t *hash, void *data)
499 {
500
501   printf ("\n\n+=+%s+=+\n\n", __FUNCTION__);
502   hash_table_foreach(hash, pkg_hash_dump_helper, data);
503   printf ("\n+=+%s+=+\n\n", __FUNCTION__);    
504 }
505
506 abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
507 {
508   abstract_pkg_t * ab_pkg;
509
510   if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
511     ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
512
513   return ab_pkg;
514 }
515
516 pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,opkg_conf_t *conf)
517 {
518      abstract_pkg_t * ab_pkg;
519      int arch_priority;
520
521      if(!pkg)
522           return pkg;
523
524      arch_priority = pkg->arch_priority;
525
526      if (buildDepends(hash, pkg)<0){
527         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
528         return NULL;
529      }
530      ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
531
532      if (set_status) {
533           if (pkg->state_status == SS_INSTALLED) {
534                ab_pkg->state_status = SS_INSTALLED;
535           } else if (pkg->state_status == SS_UNPACKED) {
536                ab_pkg->state_status = SS_UNPACKED;
537           }
538      }
539
540      if(!ab_pkg->pkgs)
541           ab_pkg->pkgs = pkg_vec_alloc();
542     
543      /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
544      pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
545      pkg->parent = ab_pkg;
546
547      if (buildProvides(hash, ab_pkg, pkg)<0){
548         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
549         return NULL;
550      }
551      /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
552      if (buildConflicts(hash, ab_pkg, pkg)<0){
553         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
554         return NULL;
555      }
556      if (buildReplaces(hash, ab_pkg, pkg)<0) {
557         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
558         return NULL;
559      }
560     
561      buildDependedUponBy(pkg, ab_pkg);
562      return pkg;
563 }
564
565 /*
566  * this will assume that we've already determined that
567  * the abstract pkg doesn't exist, 'cause we should know these things...
568  */
569 static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
570 {
571   abstract_pkg_t * ab_pkg;
572
573   ab_pkg = abstract_pkg_new();
574   if (ab_pkg == NULL) { return NULL; }
575
576   ab_pkg->name = strdup(pkg_name);
577   hash_table_insert(hash, pkg_name, ab_pkg);
578
579   return ab_pkg;
580 }
581
582
583 pkg_t *file_hash_get_file_owner(opkg_conf_t *conf, const char *file_name)
584 {
585      hash_table_t *file_hash = &conf->file_hash;
586
587      return hash_table_get(file_hash, file_name); 
588 }
589
590 int file_hash_set_file_owner(opkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg)
591 {
592      hash_table_t *file_hash = &conf->file_hash;
593      pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name);
594      int file_name_len = strlen(file_name);
595
596      if (file_name[file_name_len -1] == '/')
597           return 0;
598
599      if (conf->offline_root) {
600           int len = strlen(conf->offline_root);
601           if (strncmp(file_name, conf->offline_root, len) == 0) {
602                file_name += len;
603           }
604      }
605
606      // opkg_message(conf, OPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
607      hash_table_insert(file_hash, file_name, owning_pkg); 
608      if (old_owning_pkg) {
609           str_list_remove_elt(old_owning_pkg->installed_files, file_name);
610           /* mark this package to have its filelist written */
611           old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
612           owning_pkg->state_flag |= SF_FILELIST_CHANGED;
613      }
614      return 0;
615 }
616
617