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