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