libopkg: drop support for Release files
[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 <stdio.h>
19
20 #include "hash_table.h"
21 #include "pkg.h"
22 #include "opkg_message.h"
23 #include "pkg_vec.h"
24 #include "pkg_hash.h"
25 #include "parse_util.h"
26 #include "pkg_parse.h"
27 #include "opkg_utils.h"
28 #include "sprintf_alloc.h"
29 #include "file_util.h"
30 #include "libbb/libbb.h"
31 #include "libbb/gzip.h"
32
33 void
34 pkg_hash_init(void)
35 {
36         hash_table_init("pkg-hash", &conf->pkg_hash,
37                         OPKG_CONF_DEFAULT_HASH_LEN);
38 }
39
40 static void
41 free_pkgs(const char *key, void *entry, void *data)
42 {
43         int i;
44         abstract_pkg_t *ab_pkg;
45
46         /* Each entry in the hash table is an abstract package, which contains
47          * a list of packages that provide the abstract package.
48          */
49
50         ab_pkg = (abstract_pkg_t*) entry;
51
52         if (ab_pkg->pkgs) {
53                 for (i = 0; i < ab_pkg->pkgs->len; i++) {
54                         pkg_deinit (ab_pkg->pkgs->pkgs[i]);
55                         free (ab_pkg->pkgs->pkgs[i]);
56                 }
57         }
58
59         abstract_pkg_vec_free (ab_pkg->provided_by);
60         abstract_pkg_vec_free (ab_pkg->replaced_by);
61         pkg_vec_free (ab_pkg->pkgs);
62         free (ab_pkg->depended_upon_by);
63         free (ab_pkg->name);
64         free (ab_pkg);
65 }
66
67 void
68 pkg_hash_deinit(void)
69 {
70         hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
71         hash_table_deinit(&conf->pkg_hash);
72 }
73
74 int
75 dist_hash_add_from_file(const char *lists_dir, pkg_src_t *dist)
76 {
77         nv_pair_list_elt_t *l;
78         char *list_file, *subname;
79
80         list_for_each_entry(l , &conf->arch_list.head, node) {
81                 nv_pair_t *nv = (nv_pair_t *)l->data;
82                 sprintf_alloc(&subname, "%s-%s", dist->name, nv->name);
83                 sprintf_alloc(&list_file, "%s/%s", lists_dir, subname);
84
85                 if (file_exists(list_file)) {
86                         if (pkg_hash_add_from_file(list_file, dist, NULL, 0)) {
87                                 free(list_file);
88                                 return -1;
89                         }
90                         pkg_src_list_append (&conf->pkg_src_list, subname, dist->value, "__dummy__", 0);
91                 }
92
93                 free(list_file);
94         }
95
96         return 0;
97 }
98
99
100 int
101 pkg_hash_add_from_file(const char *file_name,
102                         pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
103 {
104         pkg_t *pkg;
105         FILE *fp;
106         char *buf;
107         const size_t len = 4096;
108         int ret = 0;
109         struct gzip_handle zh;
110
111         if (src && src->gzip) {
112                 fp = gzip_fdopen(&zh, file_name);
113         }
114         else {
115                 fp = fopen(file_name, "r");
116         }
117
118         if (fp == NULL) {
119                 opkg_perror(ERROR, "Failed to open %s", file_name);
120                 return -1;
121         }
122
123         buf = xmalloc(len);
124
125         do {
126                 pkg = pkg_new();
127                 pkg->src = src;
128                 pkg->dest = dest;
129
130                 ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
131                                 &buf, len);
132
133                 if (pkg->name == NULL) {
134                         /* probably just a blank line */
135                         ret = 1;
136                 }
137
138                 if (ret) {
139                         pkg_deinit (pkg);
140                         free(pkg);
141                         if (ret == -1)
142                                 break;
143                         if (ret == 1)
144                                 /* Probably a blank line, continue parsing. */
145                                 ret = 0;
146                         continue;
147                 }
148
149                 if (!pkg->architecture || !pkg->arch_priority) {
150                         char *version_str = pkg_version_str_alloc(pkg);
151                         opkg_msg(NOTICE, "Package %s version %s has no "
152                                         "valid architecture, ignoring.\n",
153                                         pkg->name, version_str);
154                         free(version_str);
155                         continue;
156                 }
157
158                 hash_insert_pkg(pkg, is_status_file);
159
160         } while (!feof(fp));
161
162         free(buf);
163         fclose(fp);
164
165         if (src && src->gzip)
166                 gzip_close(&zh);
167
168         return ret;
169 }
170
171 /*
172  * Load in feed files from the cached "src" and/or "src/gz" locations.
173  */
174 int
175 pkg_hash_load_feeds(void)
176 {
177         pkg_src_list_elt_t *iter;
178         pkg_src_t *src, *subdist;
179         char *list_file, *lists_dir;
180
181         opkg_msg(INFO, "\n");
182
183         lists_dir = conf->restrict_to_default_dest ?
184                 conf->default_dest->lists_dir : conf->lists_dir;
185
186         for (iter = void_list_first(&conf->pkg_src_list); iter;
187                         iter = void_list_next(&conf->pkg_src_list, iter)) {
188
189                 src = (pkg_src_t *)iter->data;
190
191                 sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
192
193                 if (file_exists(list_file)) {
194                         if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
195                                 free(list_file);
196                                 return -1;
197                         }
198                 }
199                 free(list_file);
200         }
201
202         return 0;
203 }
204
205 /*
206  * Load in status files from the configured "dest"s.
207  */
208 int
209 pkg_hash_load_status_files(void)
210 {
211         pkg_dest_list_elt_t *iter;
212         pkg_dest_t *dest;
213
214         opkg_msg(INFO, "\n");
215
216         for (iter = void_list_first(&conf->pkg_dest_list); iter;
217                         iter = void_list_next(&conf->pkg_dest_list, iter)) {
218
219                 dest = (pkg_dest_t *)iter->data;
220
221                 if (file_exists(dest->status_file_name)) {
222                         if (pkg_hash_add_from_file(dest->status_file_name, NULL, dest, 1))
223                                 return -1;
224                 }
225         }
226
227         return 0;
228 }
229
230 static abstract_pkg_t *
231 abstract_pkg_fetch_by_name(const char * pkg_name)
232 {
233         return (abstract_pkg_t *)hash_table_get(&conf->pkg_hash, pkg_name);
234 }
235
236 pkg_t *
237 pkg_hash_fetch_best_installation_candidate(abstract_pkg_t *apkg,
238                 int (*constraint_fcn)(pkg_t *pkg, void *cdata),
239                 void *cdata, int quiet)
240 {
241      int i, j;
242      int nprovides = 0;
243      int nmatching = 0;
244      int wrong_arch_found = 0;
245      pkg_vec_t *matching_pkgs;
246      abstract_pkg_vec_t *matching_apkgs;
247      abstract_pkg_vec_t *provided_apkg_vec;
248      abstract_pkg_t **provided_apkgs;
249      abstract_pkg_vec_t *providers;
250      pkg_t *latest_installed_parent = NULL;
251      pkg_t *latest_matching = NULL;
252      pkg_t *priorized_matching = NULL;
253      pkg_t *held_pkg = NULL;
254      pkg_t *good_pkg_by_name = NULL;
255
256      if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
257           return NULL;
258
259      matching_pkgs = pkg_vec_alloc();
260      matching_apkgs = abstract_pkg_vec_alloc();
261      providers = abstract_pkg_vec_alloc();
262
263      opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
264
265      provided_apkg_vec = apkg->provided_by;
266      nprovides = provided_apkg_vec->len;
267      provided_apkgs = provided_apkg_vec->pkgs;
268      if (nprovides > 1)
269           opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name, nprovides);
270
271      /* accumulate all the providers */
272      for (i = 0; i < nprovides; i++) {
273           abstract_pkg_t *provider_apkg = provided_apkgs[i];
274           opkg_msg(DEBUG, "Adding %s to providers.\n", provider_apkg->name);
275           abstract_pkg_vec_insert(providers, provider_apkg);
276      }
277      nprovides = providers->len;
278
279      for (i = 0; i < nprovides; i++) {
280           abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
281           abstract_pkg_t *replacement_apkg = NULL;
282           pkg_vec_t *vec;
283
284           if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
285                replacement_apkg = provider_apkg->replaced_by->pkgs[0];
286                if (provider_apkg->replaced_by->len > 1) {
287                     opkg_msg(NOTICE, "Multiple replacers for %s, "
288                                 "using first one (%s).\n",
289                                 provider_apkg->name, replacement_apkg->name);
290                }
291           }
292
293           if (replacement_apkg)
294                opkg_msg(DEBUG, "replacement_apkg=%s for provider_apkg=%s.\n",
295                             replacement_apkg->name, provider_apkg->name);
296
297           if (replacement_apkg && (replacement_apkg != provider_apkg)) {
298                if (abstract_pkg_vec_contains(providers, replacement_apkg))
299                     continue;
300                else
301                     provider_apkg = replacement_apkg;
302           }
303
304           if (!(vec = provider_apkg->pkgs)) {
305                opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
306                                provider_apkg->name);
307                continue;
308           }
309
310
311           /* now check for supported architecture */
312           {
313                int max_count = 0;
314
315                /* count packages matching max arch priority and keep track of last one */
316                for (j=0; j<vec->len; j++) {
317                     pkg_t *maybe = vec->pkgs[j];
318                     opkg_msg(DEBUG, "%s arch=%s arch_priority=%d version=%s.\n",
319                                  maybe->name, maybe->architecture,
320                                  maybe->arch_priority, maybe->version);
321                     /* We make sure not to add the same package twice. Need to search for the reason why
322                        they show up twice sometimes. */
323                     if ((maybe->arch_priority > 0) && (! pkg_vec_contains(matching_pkgs, maybe))) {
324                          max_count++;
325                          abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
326                          pkg_vec_insert(matching_pkgs, maybe);
327                     }
328                }
329
330                 if (vec->len > 0 && matching_pkgs->len < 1)
331                         wrong_arch_found = 1;
332           }
333      }
334
335      if (matching_pkgs->len < 1) {
336           if (wrong_arch_found)
337                 opkg_msg(ERROR, "Packages for %s found, but"
338                         " incompatible with the architectures configured\n",
339                         apkg->name);
340           pkg_vec_free(matching_pkgs);
341           abstract_pkg_vec_free(matching_apkgs);
342           abstract_pkg_vec_free(providers);
343           return NULL;
344      }
345
346
347      if (matching_pkgs->len > 1)
348           pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
349      if (matching_apkgs->len > 1)
350           abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
351
352      for (i = 0; i < matching_pkgs->len; i++) {
353           pkg_t *matching = matching_pkgs->pkgs[i];
354           if (constraint_fcn(matching, cdata)) {
355              opkg_msg(DEBUG, "Candidate: %s %s.\n",
356                              matching->name, matching->version) ;
357              good_pkg_by_name = matching;
358              /* It has been provided by hand, so it is what user want */
359              if (matching->provided_by_hand == 1)
360                 break;
361           }
362      }
363
364
365      for (i = 0; i < matching_pkgs->len; i++) {
366           pkg_t *matching = matching_pkgs->pkgs[i];
367           latest_matching = matching;
368           if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
369                latest_installed_parent = matching;
370           if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
371                if (held_pkg)
372                     opkg_msg(NOTICE, "Multiple packages (%s and %s) providing"
373                                 " same name marked HOLD or PREFER. "
374                                 "Using latest.\n",
375                                 held_pkg->name, matching->name);
376                held_pkg = matching;
377           }
378      }
379
380      if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
381           int prio = 0;
382           for (i = 0; i < matching_pkgs->len; i++) {
383               pkg_t *matching = matching_pkgs->pkgs[i];
384                   if (matching->arch_priority > prio) {
385                       priorized_matching = matching;
386                       prio = matching->arch_priority;
387                       opkg_msg(DEBUG, "Match %s with priority %i.\n",
388                                 matching->name, prio);
389                   }
390               }
391
392           }
393
394      if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
395           opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
396                                 matching_pkgs->len, apkg->name);
397           for (i = 0; i < matching_pkgs->len; i++) {
398                pkg_t *matching = matching_pkgs->pkgs[i];
399                opkg_msg(INFO, "%s %s %s\n",
400                         matching->name, matching->version,
401                         matching->architecture);
402           }
403      }
404
405      nmatching = matching_apkgs->len;
406
407      pkg_vec_free(matching_pkgs);
408      abstract_pkg_vec_free(matching_apkgs);
409      abstract_pkg_vec_free(providers);
410
411      if (good_pkg_by_name) {   /* We found a good candidate, we will install it */
412           return good_pkg_by_name;
413      }
414      if (held_pkg) {
415           opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
416           return held_pkg;
417      }
418      if (latest_installed_parent) {
419           opkg_msg(INFO, "Using latest version of installed package %s.\n",
420                         latest_installed_parent->name);
421           return latest_installed_parent;
422      }
423      if (priorized_matching) {
424           opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
425                         priorized_matching->name, priorized_matching->version,
426                         priorized_matching->architecture);
427           return priorized_matching;
428      }
429      if (nmatching > 1) {
430           opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
431                         nmatching);
432           return NULL;
433      }
434      if (latest_matching) {
435           opkg_msg(INFO, "Using latest matching %s %s %s.\n",
436                         latest_matching->name, latest_matching->version,
437                         latest_matching->architecture);
438           return latest_matching;
439      }
440      return NULL;
441 }
442
443 static int
444 pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
445 {
446         const char *name = (const char *)cdata;
447
448         if (strcmp(pkg->name, name) == 0)
449                 return 1;
450         else
451                 return 0;
452 }
453
454 static pkg_vec_t *
455 pkg_vec_fetch_by_name(const char *pkg_name)
456 {
457         abstract_pkg_t * ab_pkg;
458
459         if(!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
460                 return NULL;
461
462         if (ab_pkg->pkgs)
463                 return ab_pkg->pkgs;
464
465         if (ab_pkg->provided_by) {
466                 abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
467                 if (abpkg != NULL)
468                         return abpkg->pkgs;
469                 else
470                         return ab_pkg->pkgs;
471         }
472
473         return NULL;
474 }
475
476
477 pkg_t *
478 pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
479 {
480         abstract_pkg_t *apkg = NULL;
481
482         if (!(apkg = abstract_pkg_fetch_by_name(name)))
483                 return NULL;
484
485         return pkg_hash_fetch_best_installation_candidate(apkg,
486                                 pkg_name_constraint_fcn, apkg->name, 0);
487 }
488
489
490 pkg_t *
491 pkg_hash_fetch_by_name_version(const char *pkg_name, const char * version)
492 {
493         pkg_vec_t * vec;
494         int i;
495         char *version_str = NULL;
496
497         if(!(vec = pkg_vec_fetch_by_name(pkg_name)))
498                 return NULL;
499
500         for(i = 0; i < vec->len; i++) {
501                 version_str = pkg_version_str_alloc(vec->pkgs[i]);
502                 if(!strcmp(version_str, version)) {
503                         free(version_str);
504                         break;
505                 }
506                 free(version_str);
507         }
508
509         if(i == vec->len)
510                 return NULL;
511
512         return vec->pkgs[i];
513 }
514
515 pkg_t *
516 pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, pkg_dest_t *dest)
517 {
518         pkg_vec_t * vec;
519         int i;
520
521         if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
522                 return NULL;
523         }
524
525         for (i = 0; i < vec->len; i++)
526                 if((vec->pkgs[i]->state_status == SS_INSTALLED
527                                 || vec->pkgs[i]->state_status == SS_UNPACKED)
528                                 && vec->pkgs[i]->dest == dest) {
529                         return vec->pkgs[i];
530         }
531
532         return NULL;
533 }
534
535 pkg_t *
536 pkg_hash_fetch_installed_by_name(const char *pkg_name)
537 {
538         pkg_vec_t * vec;
539         int i;
540
541         if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
542                 return NULL;
543         }
544
545         for (i = 0; i < vec->len; i++) {
546                 if (vec->pkgs[i]->state_status == SS_INSTALLED
547                                 || vec->pkgs[i]->state_status == SS_UNPACKED) {
548                         return vec->pkgs[i];
549                 }
550         }
551
552         return NULL;
553 }
554
555
556 static void
557 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
558 {
559         int j;
560         abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
561         pkg_vec_t *all = (pkg_vec_t *)data;
562         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
563
564         if (!pkg_vec)
565                 return;
566
567         for (j = 0; j < pkg_vec->len; j++) {
568                 pkg_t *pkg = pkg_vec->pkgs[j];
569                 pkg_vec_insert(all, pkg);
570         }
571 }
572
573 void
574 pkg_hash_fetch_available(pkg_vec_t *all)
575 {
576         hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
577                         all);
578 }
579
580 static void
581 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
582 {
583         abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
584         pkg_vec_t *all = (pkg_vec_t *)data;
585         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
586         int j;
587
588         if (!pkg_vec)
589                 return;
590
591         for (j = 0; j < pkg_vec->len; j++) {
592                 pkg_t *pkg = pkg_vec->pkgs[j];
593                 if (pkg->state_status == SS_INSTALLED
594                                 || pkg->state_status == SS_UNPACKED)
595                         pkg_vec_insert(all, pkg);
596         }
597 }
598
599 void
600 pkg_hash_fetch_all_installed(pkg_vec_t *all)
601 {
602         hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
603                         all);
604 }
605
606 /*
607  * This assumes that the abstract pkg doesn't exist.
608  */
609 static abstract_pkg_t *
610 add_new_abstract_pkg_by_name(const char *pkg_name)
611 {
612         abstract_pkg_t *ab_pkg;
613
614         ab_pkg = abstract_pkg_new();
615
616         ab_pkg->name = xstrdup(pkg_name);
617         hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
618
619         return ab_pkg;
620 }
621
622
623 abstract_pkg_t *
624 ensure_abstract_pkg_by_name(const char *pkg_name)
625 {
626         abstract_pkg_t * ab_pkg;
627
628         if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
629                 ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
630
631         return ab_pkg;
632 }
633
634 void
635 hash_insert_pkg(pkg_t *pkg, int set_status)
636 {
637         abstract_pkg_t * ab_pkg;
638
639         ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
640         if (!ab_pkg->pkgs)
641                 ab_pkg->pkgs = pkg_vec_alloc();
642
643         if (pkg->state_status == SS_INSTALLED) {
644                 ab_pkg->state_status = SS_INSTALLED;
645         } else if (pkg->state_status == SS_UNPACKED) {
646                 ab_pkg->state_status = SS_UNPACKED;
647         }
648
649         buildDepends(pkg);
650
651         buildProvides(ab_pkg, pkg);
652
653         /* Need to build the conflicts graph before replaces for correct
654          * calculation of replaced_by relation.
655          */
656         buildConflicts(pkg);
657
658         buildReplaces(ab_pkg, pkg);
659
660         buildDependedUponBy(pkg, ab_pkg);
661
662         pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
663         pkg->parent = ab_pkg;
664 }
665
666 static const char *
667 strip_offline_root(const char *file_name)
668 {
669         unsigned int len;
670
671         if (conf->offline_root) {
672                 len = strlen(conf->offline_root);
673                 if (strncmp(file_name, conf->offline_root, len) == 0)
674                         file_name += len;
675         }
676
677         return file_name;
678 }
679
680 void
681 file_hash_remove(const char *file_name)
682 {
683         file_name = strip_offline_root(file_name);
684         hash_table_remove(&conf->file_hash, file_name);
685 }
686
687 pkg_t *
688 file_hash_get_file_owner(const char *file_name)
689 {
690         file_name = strip_offline_root(file_name);
691         return hash_table_get(&conf->file_hash, file_name);
692 }
693
694 void
695 file_hash_set_file_owner(const char *file_name, pkg_t *owning_pkg)
696 {
697         pkg_t *old_owning_pkg;
698         int file_name_len = strlen(file_name);
699
700         if (file_name[file_name_len -1] == '/')
701                 return;
702
703         file_name = strip_offline_root(file_name);
704
705         old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
706         hash_table_insert(&conf->file_hash, file_name, owning_pkg);
707
708         if (old_owning_pkg) {
709                 pkg_get_installed_files(old_owning_pkg);
710                 str_list_remove_elt(old_owning_pkg->installed_files, file_name);
711                 pkg_free_installed_files(old_owning_pkg);
712
713                 /* mark this package to have its filelist written */
714                 old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
715                 owning_pkg->state_flag |= SF_FILELIST_CHANGED;
716         }
717 }