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