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