ccd3197f974e2a15dfc54bb93712eee8681bb89a
[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, int *err)
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 (err)
182        *err = 0;
183
184      if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
185           return NULL;
186
187      matching_pkgs = pkg_vec_alloc();
188      matching_apkgs = abstract_pkg_vec_alloc();
189      providers = abstract_pkg_vec_alloc();
190
191      opkg_message(conf, OPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
192
193      provided_apkg_vec = apkg->provided_by;
194      nprovides = provided_apkg_vec->len;
195      provided_apkgs = provided_apkg_vec->pkgs;
196      if (nprovides > 1)
197           opkg_message(conf, OPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
198
199      /* accumulate all the providers */
200      for (i = 0; i < nprovides; i++) {
201           abstract_pkg_t *provider_apkg = provided_apkgs[i];
202           opkg_message(conf, OPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
203           abstract_pkg_vec_insert(providers, provider_apkg);
204      }
205      nprovides = providers->len;
206
207      for (i = 0; i < nprovides; i++) {
208           abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
209           abstract_pkg_t *replacement_apkg = NULL;
210           pkg_vec_t *vec;
211
212           if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
213                replacement_apkg = provider_apkg->replaced_by->pkgs[0];
214                if (provider_apkg->replaced_by->len > 1) {
215                     opkg_message(conf, OPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
216                                  provider_apkg->name, replacement_apkg->name);
217                }
218           }
219
220           if (replacement_apkg)
221                opkg_message(conf, OPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
222                             replacement_apkg->name, provider_apkg->name);
223
224           if (replacement_apkg && (replacement_apkg != provider_apkg)) {
225                if (abstract_pkg_vec_contains(providers, replacement_apkg))
226                     continue;
227                else
228                     provider_apkg = replacement_apkg;
229           }
230
231           if (!(vec = provider_apkg->pkgs)) {
232                opkg_message(conf, OPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
233                continue;
234           }
235     
236
237           /* now check for supported architecture */
238           {
239                int max_count = 0;
240                int i;
241
242                /* count packages matching max arch priority and keep track of last one */
243                for (i = 0; i < vec->len; i++) {
244                     pkg_t *maybe = vec->pkgs[i];
245                     opkg_message(conf, OPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
246                                  maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
247                     /* We make sure not to add the same package twice. Need to search for the reason why 
248                        they show up twice sometimes. */
249                     if ((maybe->arch_priority > 0) && (! pkg_vec_contains(matching_pkgs, maybe))) {
250                          max_count++;
251                          abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
252                          pkg_vec_insert(matching_pkgs, maybe);
253                     }
254                }
255
256                 if (vec->len > 0 && matching_pkgs->len < 1)
257                         wrong_arch_found = 1;
258           }
259      }
260
261      if (matching_pkgs->len < 1) {
262           if (wrong_arch_found)
263                 opkg_message (conf, OPKG_ERROR, "Packages for %s found, but"
264                         " incompatible with the architectures configured\n",
265                         apkg->name);
266           pkg_vec_free(matching_pkgs);
267           abstract_pkg_vec_free(matching_apkgs);
268           abstract_pkg_vec_free(providers);
269           return NULL;
270      }
271
272
273      if (matching_pkgs->len > 1)
274           pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
275      if (matching_apkgs->len > 1)
276           abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
277
278 /* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
279    needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
280 /* The problem is what to do when there are more than a mathing package, with the same name and several version ?
281    Until now I always got the latest, but that breaks the downgrade option.
282    If I stop at the first one, I would probably miss the new ones 
283    Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
284    or from a Packages feed.
285    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*/
286 /*Pigi*/
287
288      for (i = 0; i < matching_pkgs->len; i++) {
289           pkg_t *matching = matching_pkgs->pkgs[i];
290           if (constraint_fcn(matching, cdata)) {  /* We found it */
291              opkg_message(conf, OPKG_DEBUG, " Found a valid candidate for the install: %s %s  \n", matching->name, matching->version) ;
292              good_pkg_by_name = matching;
293              if ( matching->provided_by_hand == 1 )    /* It has been provided by hand, so it is what user want */
294                 break;                                 
295           }
296      }
297
298
299      for (i = 0; i < matching_pkgs->len; i++) {
300           pkg_t *matching = matching_pkgs->pkgs[i];
301           latest_matching = matching;
302           if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
303                latest_installed_parent = matching;
304           if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
305                if (held_pkg)
306                     opkg_message(conf, OPKG_NOTICE, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
307                                  held_pkg->name, matching->name);
308                held_pkg = matching;
309           }
310      }
311
312      if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
313           int prio = 0;
314           for (i = 0; i < matching_pkgs->len; i++) {
315               pkg_t *matching = matching_pkgs->pkgs[i];
316                   if (matching->arch_priority > prio) {
317                       priorized_matching = matching;
318                       prio = matching->arch_priority;
319                       opkg_message(conf, OPKG_DEBUG, "Match with priority %i    %s\n", prio, matching->name);
320                   }
321               }
322           
323           }
324
325      if (conf->verbosity >= OPKG_INFO && matching_apkgs->len > 1) {
326           opkg_message(conf, OPKG_INFO, "%s: for apkg=%s, %d matching pkgs\n",
327                        __FUNCTION__, apkg->name, matching_pkgs->len);
328           for (i = 0; i < matching_pkgs->len; i++) {
329                pkg_t *matching = matching_pkgs->pkgs[i];
330                opkg_message(conf, OPKG_INFO, "    %s %s %s\n",
331                             matching->name, matching->version, matching->architecture);
332           }
333      }
334
335      nmatching = matching_apkgs->len;
336
337      pkg_vec_free(matching_pkgs);
338      abstract_pkg_vec_free(matching_apkgs);
339      abstract_pkg_vec_free(providers);
340
341      if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
342           return good_pkg_by_name;
343      }
344      if (held_pkg) {
345           opkg_message(conf, OPKG_INFO, "  using held package %s\n", held_pkg->name);
346           return held_pkg;
347      }
348      if (latest_installed_parent) {
349           opkg_message(conf, OPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
350           return latest_installed_parent;
351      }
352      if (priorized_matching) {
353           opkg_message(conf, OPKG_INFO, "  using priorized matching %s %s %s\n",
354                        priorized_matching->name, priorized_matching->version, priorized_matching->architecture);
355           return priorized_matching;
356      }
357      if (nmatching > 1) {
358           opkg_message(conf, OPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
359           return NULL;
360      }
361      if (latest_matching) {
362           opkg_message(conf, OPKG_INFO, "  using latest matching %s %s %s\n",
363                        latest_matching->name, latest_matching->version, latest_matching->architecture);
364           return latest_matching;
365      }
366      return NULL;
367 }
368
369 static int
370 pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
371 {
372         const char *name = (const char *)cdata;
373
374         if (strcmp(pkg->name, name) == 0)
375                 return 1;
376         else
377                 return 0;   
378 }
379
380 static pkg_vec_t *
381 pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
382 {
383         abstract_pkg_t * ab_pkg;
384
385         if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
386                 return NULL;
387
388         if (ab_pkg->pkgs)
389                 return ab_pkg->pkgs;
390
391         if (ab_pkg->provided_by) {
392                 abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
393                 if (abpkg != NULL)
394                         return abpkg->pkgs;
395                 else
396                         return ab_pkg->pkgs;
397         }
398
399         return NULL;
400 }
401
402
403 pkg_t *
404 pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t *conf,
405                 const char *name, int *err)
406 {
407         hash_table_t *hash = &conf->pkg_hash;
408         abstract_pkg_t *apkg = NULL;
409
410         if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
411                 return NULL;
412
413         return pkg_hash_fetch_best_installation_candidate(conf, apkg,
414                                 pkg_name_constraint_fcn, apkg->name, 0, err);
415 }
416
417
418 pkg_t *
419 pkg_hash_fetch_by_name_version(hash_table_t *hash, 
420                         const char *pkg_name, const char * version)
421 {
422         pkg_vec_t * vec;
423         int i;
424         char *version_str = NULL;
425     
426         if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
427                 return NULL;
428     
429         for(i = 0; i < vec->len; i++) {
430                 version_str = pkg_version_str_alloc(vec->pkgs[i]);
431                 if(!strcmp(version_str, version)) {
432                         free(version_str);
433                         break;
434                 }
435                 free(version_str);
436         }
437
438         if(i == vec->len)
439                 return NULL;
440     
441         return vec->pkgs[i];
442 }
443
444 pkg_t *
445 pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
446                         const char *pkg_name, pkg_dest_t *dest)
447 {
448         pkg_vec_t * vec;
449         int i;
450
451         if (!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
452                 return NULL;
453         }
454
455         for (i = 0; i < vec->len; i++)
456                 if((vec->pkgs[i]->state_status == SS_INSTALLED
457                                 || vec->pkgs[i]->state_status == SS_UNPACKED)
458                                 && vec->pkgs[i]->dest == dest) {
459                         return vec->pkgs[i];
460         }
461
462         return NULL;
463 }
464
465 pkg_t *
466 pkg_hash_fetch_installed_by_name(hash_table_t *hash,
467                                         const char *pkg_name)
468 {
469         pkg_vec_t * vec;
470         int i;
471
472         if (!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
473                 return NULL;
474         }
475
476         for (i = 0; i < vec->len; i++) {
477                 if (vec->pkgs[i]->state_status == SS_INSTALLED
478                                 || vec->pkgs[i]->state_status == SS_UNPACKED) {
479                         return vec->pkgs[i];
480                 }
481         }
482
483         return NULL;
484 }
485
486
487 static void
488 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
489 {
490         int j;
491         abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
492         pkg_vec_t *all = (pkg_vec_t *)data;
493         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
494
495         if (!pkg_vec)
496                 return;
497
498         for (j = 0; j < pkg_vec->len; j++) {
499                 pkg_t *pkg = pkg_vec->pkgs[j];
500                 pkg_vec_insert(all, pkg);
501         }
502 }
503
504 void
505 pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
506 {
507         hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
508 }
509
510 static void
511 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
512 {
513         abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
514         pkg_vec_t *all = (pkg_vec_t *)data;
515         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
516         int j;
517
518         if (!pkg_vec)
519                 return;
520
521         for (j = 0; j < pkg_vec->len; j++) {
522                 pkg_t *pkg = pkg_vec->pkgs[j];
523                 if (pkg->state_status == SS_INSTALLED
524                                 || pkg->state_status == SS_UNPACKED)
525                         pkg_vec_insert(all, pkg);
526         }
527 }
528
529 void
530 pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
531 {
532         hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
533 }
534
535 /*
536  * This assumes that the abstract pkg doesn't exist.
537  */
538 static abstract_pkg_t *
539 add_new_abstract_pkg_by_name(hash_table_t *hash, const char *pkg_name)
540 {
541         abstract_pkg_t *ab_pkg;
542
543         ab_pkg = abstract_pkg_new();
544
545         ab_pkg->name = xstrdup(pkg_name);
546         hash_table_insert(hash, pkg_name, ab_pkg);
547
548         return ab_pkg;
549 }
550
551
552 abstract_pkg_t *
553 ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
554 {
555         abstract_pkg_t * ab_pkg;
556
557         if (!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
558                 ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
559
560         return ab_pkg;
561 }
562
563 pkg_t *
564 hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,
565                 opkg_conf_t *conf)
566 {
567         abstract_pkg_t * ab_pkg;
568
569         if(!pkg)
570                 return pkg;
571
572         buildDepends(hash, pkg);
573
574         ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
575
576         if (set_status) {
577                 if (pkg->state_status == SS_INSTALLED) {
578                         ab_pkg->state_status = SS_INSTALLED;
579                 } else if (pkg->state_status == SS_UNPACKED) {
580                         ab_pkg->state_status = SS_UNPACKED;
581                 }
582         }
583
584         if(!ab_pkg->pkgs)
585                 ab_pkg->pkgs = pkg_vec_alloc();
586
587         buildProvides(hash, ab_pkg, pkg);
588
589         /* Need to build the conflicts graph before replaces for correct
590          * calculation of replaced_by relation.
591          */
592         buildConflicts(hash, ab_pkg, pkg);
593
594         buildReplaces(hash, ab_pkg, pkg);
595
596         buildDependedUponBy(pkg, ab_pkg);
597
598         /* pkg_vec_insert_merge might munge package, but it returns an
599          * unmunged pkg.
600          */
601         pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
602         pkg->parent = ab_pkg;
603
604         return pkg;
605 }
606
607
608 pkg_t *
609 file_hash_get_file_owner(opkg_conf_t *conf, const char *file_name)
610 {
611         return hash_table_get(&conf->file_hash, file_name); 
612 }
613
614 void
615 file_hash_set_file_owner(opkg_conf_t *conf, const char *file_name,
616                 pkg_t *owning_pkg)
617 {
618         pkg_t *old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
619         int file_name_len = strlen(file_name);
620
621         if (file_name[file_name_len -1] == '/')
622                 return;
623
624         if (conf->offline_root) {
625                 int len = strlen(conf->offline_root);
626                 if (strncmp(file_name, conf->offline_root, len) == 0) {
627                         file_name += len;
628                 }
629         }
630
631         hash_table_insert(&conf->file_hash, file_name, owning_pkg); 
632
633         if (old_owning_pkg) {
634                 pkg_get_installed_files(conf, old_owning_pkg);
635                 str_list_remove_elt(old_owning_pkg->installed_files, file_name);
636                 pkg_free_installed_files(old_owning_pkg);
637
638                 /* mark this package to have its filelist written */
639                 old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
640                 owning_pkg->state_flag |= SF_FILELIST_CHANGED;
641         }
642 }