1 /* opkg_hash.c - the opkg package management system
5 Copyright (C) 2002 Compaq Computer Corporation
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.
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.
20 #include "hash_table.h"
22 #include "opkg_message.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"
33 void pkg_hash_init(void)
35 hash_table_init("pkg-hash", &conf->pkg_hash,
36 OPKG_CONF_DEFAULT_HASH_LEN);
39 static void free_pkgs(const char *key, void *entry, void *data)
42 abstract_pkg_t *ab_pkg;
44 /* Each entry in the hash table is an abstract package, which contains
45 * a list of packages that provide the abstract package.
48 ab_pkg = (abstract_pkg_t *) entry;
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]);
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);
65 void pkg_hash_deinit(void)
67 hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
68 hash_table_deinit(&conf->pkg_hash);
71 int dist_hash_add_from_file(const char *lists_dir, pkg_src_t * dist)
73 nv_pair_list_elt_t *l;
74 char *list_file, *subname;
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);
81 if (file_exists(list_file)) {
82 if (pkg_hash_add_from_file(list_file, dist, NULL, 0)) {
86 pkg_src_list_append(&conf->pkg_src_list, subname,
87 dist->value, "__dummy__", 0);
97 pkg_hash_add_from_file(const char *file_name,
98 pkg_src_t * src, pkg_dest_t * dest, int is_status_file)
103 const size_t len = 4096;
105 struct gzip_handle zh;
107 if (src && src->gzip) {
108 fp = gzip_fdopen(&zh, file_name);
110 fp = fopen(file_name, "r");
114 opkg_perror(ERROR, "Failed to open %s", file_name);
125 ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
128 if (pkg->name == NULL) {
129 /* probably just a blank line */
139 /* Probably a blank line, continue parsing. */
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);
153 hash_insert_pkg(pkg, is_status_file);
160 if (src && src->gzip)
167 * Load in feed files from the cached "src" and/or "src/gz" locations.
169 int pkg_hash_load_feeds(void)
171 pkg_src_list_elt_t *iter;
172 pkg_src_t *src, *subdist;
173 char *list_file, *lists_dir;
175 opkg_msg(INFO, "\n");
177 lists_dir = conf->restrict_to_default_dest ?
178 conf->default_dest->lists_dir : conf->lists_dir;
180 for (iter = void_list_first(&conf->pkg_src_list); iter;
181 iter = void_list_next(&conf->pkg_src_list, iter)) {
183 src = (pkg_src_t *) iter->data;
185 sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
187 if (file_exists(list_file)) {
188 if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
200 * Load in status files from the configured "dest"s.
202 int pkg_hash_load_status_files(void)
204 pkg_dest_list_elt_t *iter;
207 opkg_msg(INFO, "\n");
209 for (iter = void_list_first(&conf->pkg_dest_list); iter;
210 iter = void_list_next(&conf->pkg_dest_list, iter)) {
212 dest = (pkg_dest_t *) iter->data;
214 if (file_exists(dest->status_file_name)) {
215 if (pkg_hash_add_from_file
216 (dest->status_file_name, NULL, dest, 1))
224 static abstract_pkg_t *abstract_pkg_fetch_by_name(const char *pkg_name)
226 return (abstract_pkg_t *) hash_table_get(&conf->pkg_hash, pkg_name);
229 pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,
230 int (*constraint_fcn) (pkg_t *
234 void *cdata, int quiet)
239 int wrong_arch_found = 0;
241 pkg_vec_t *matching_pkgs;
242 abstract_pkg_vec_t *matching_apkgs;
243 abstract_pkg_vec_t *provided_apkg_vec;
244 abstract_pkg_t **provided_apkgs;
245 abstract_pkg_vec_t *providers;
246 pkg_t *latest_installed_parent = NULL;
247 pkg_t *latest_matching = NULL;
248 pkg_t *priorized_matching = NULL;
249 pkg_t *held_pkg = NULL;
250 pkg_t *good_pkg_by_name = NULL;
252 if (apkg == NULL || apkg->provided_by == NULL
253 || (apkg->provided_by->len == 0))
256 matching_pkgs = pkg_vec_alloc();
257 matching_apkgs = abstract_pkg_vec_alloc();
258 providers = abstract_pkg_vec_alloc();
260 opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
262 provided_apkg_vec = apkg->provided_by;
263 nprovides = provided_apkg_vec->len;
264 provided_apkgs = provided_apkg_vec->pkgs;
266 opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name,
269 /* accumulate all the providers */
270 for (i = 0; i < nprovides; i++) {
271 abstract_pkg_t *provider_apkg = provided_apkgs[i];
272 opkg_msg(DEBUG, "Adding %s to providers.\n",
273 provider_apkg->name);
274 abstract_pkg_vec_insert(providers, provider_apkg);
276 nprovides = providers->len;
278 for (i = 0; i < nprovides; i++) {
279 abstract_pkg_t *provider_apkg =
280 abstract_pkg_vec_get(providers, i);
281 abstract_pkg_t *replacement_apkg = NULL;
284 if (provider_apkg->replaced_by
285 && provider_apkg->replaced_by->len) {
286 replacement_apkg = provider_apkg->replaced_by->pkgs[0];
287 if (provider_apkg->replaced_by->len > 1) {
288 opkg_msg(NOTICE, "Multiple replacers for %s, "
289 "using first one (%s).\n",
291 replacement_apkg->name);
295 if (replacement_apkg)
297 "replacement_apkg=%s for provider_apkg=%s.\n",
298 replacement_apkg->name, provider_apkg->name);
300 if (replacement_apkg && (replacement_apkg != provider_apkg)) {
301 if (abstract_pkg_vec_contains
302 (providers, replacement_apkg))
305 provider_apkg = replacement_apkg;
308 if (!(vec = provider_apkg->pkgs)) {
309 opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
310 provider_apkg->name);
314 /* now check for supported architecture */
318 /* count packages matching max arch priority and keep track of last one */
319 for (j = 0; j < vec->len; j++) {
320 pkg_t *maybe = vec->pkgs[j];
321 arch_priority = pkg_get_arch_priority(maybe);
324 "%s arch=%s arch_priority=%d version=%s.\n",
325 maybe->name, pkg_get_architecture(maybe),
326 arch_priority, pkg_get_string(maybe, PKG_VERSION));
327 /* We make sure not to add the same package twice. Need to search for the reason why
328 they show up twice sometimes. */
329 if ((arch_priority > 0)
331 (!pkg_vec_contains(matching_pkgs, maybe))) {
333 abstract_pkg_vec_insert(matching_apkgs,
335 pkg_vec_insert(matching_pkgs, maybe);
339 if (vec->len > 0 && matching_pkgs->len < 1)
340 wrong_arch_found = 1;
344 if (matching_pkgs->len < 1) {
345 if (wrong_arch_found)
346 opkg_msg(ERROR, "Packages for %s found, but"
347 " incompatible with the architectures configured\n",
349 pkg_vec_free(matching_pkgs);
350 abstract_pkg_vec_free(matching_apkgs);
351 abstract_pkg_vec_free(providers);
355 if (matching_pkgs->len > 1)
356 pkg_vec_sort(matching_pkgs,
357 pkg_name_version_and_architecture_compare);
358 if (matching_apkgs->len > 1)
359 abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
361 for (i = 0; i < matching_pkgs->len; i++) {
362 pkg_t *matching = matching_pkgs->pkgs[i];
363 if (constraint_fcn(matching, cdata)) {
364 opkg_msg(DEBUG, "Candidate: %s %s.\n",
365 matching->name, pkg_get_string(matching, PKG_VERSION));
366 good_pkg_by_name = matching;
367 /* It has been provided by hand, so it is what user want */
368 if (matching->provided_by_hand == 1)
373 for (i = 0; i < matching_pkgs->len; i++) {
374 pkg_t *matching = matching_pkgs->pkgs[i];
375 latest_matching = matching;
376 if (matching->parent->state_status == SS_INSTALLED
377 || matching->parent->state_status == SS_UNPACKED)
378 latest_installed_parent = matching;
379 if (matching->state_flag & (SF_HOLD | SF_PREFER)) {
382 "Multiple packages (%s and %s) providing"
383 " same name marked HOLD or PREFER. "
384 "Using latest.\n", held_pkg->name,
390 if (!good_pkg_by_name && !held_pkg && !latest_installed_parent
391 && matching_apkgs->len > 1 && !quiet) {
393 for (i = 0; i < matching_pkgs->len; i++) {
394 pkg_t *matching = matching_pkgs->pkgs[i];
395 arch_priority = pkg_get_arch_priority(matching);
396 if (arch_priority > prio) {
397 priorized_matching = matching;
398 prio = arch_priority;
399 opkg_msg(DEBUG, "Match %s with priority %i.\n",
400 matching->name, prio);
406 if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
407 opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
408 matching_pkgs->len, apkg->name);
409 for (i = 0; i < matching_pkgs->len; i++) {
410 pkg_t *matching = matching_pkgs->pkgs[i];
411 opkg_msg(INFO, "%s %s %s\n",
412 matching->name, pkg_get_string(matching, PKG_VERSION),
413 pkg_get_architecture(matching));
417 nmatching = matching_apkgs->len;
419 pkg_vec_free(matching_pkgs);
420 abstract_pkg_vec_free(matching_apkgs);
421 abstract_pkg_vec_free(providers);
423 if (good_pkg_by_name) { /* We found a good candidate, we will install it */
424 return good_pkg_by_name;
427 opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
430 if (latest_installed_parent) {
432 "Using latest version of installed package %s.\n",
433 latest_installed_parent->name);
434 return latest_installed_parent;
436 if (priorized_matching) {
437 opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
438 priorized_matching->name, pkg_get_string(priorized_matching, PKG_VERSION),
439 pkg_get_architecture(priorized_matching));
440 return priorized_matching;
443 opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
447 if (latest_matching) {
448 opkg_msg(INFO, "Using latest matching %s %s %s.\n",
449 latest_matching->name, pkg_get_string(latest_matching, PKG_VERSION),
450 pkg_get_architecture(latest_matching));
451 return latest_matching;
456 static int pkg_name_constraint_fcn(pkg_t * pkg, void *cdata)
458 const char *name = (const char *)cdata;
460 if (strcmp(pkg->name, name) == 0)
466 static pkg_vec_t *pkg_vec_fetch_by_name(const char *pkg_name)
468 abstract_pkg_t *ab_pkg;
470 if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
476 if (ab_pkg->provided_by) {
477 abstract_pkg_t *abpkg =
478 abstract_pkg_vec_get(ab_pkg->provided_by, 0);
488 pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
490 abstract_pkg_t *apkg = NULL;
492 if (!(apkg = abstract_pkg_fetch_by_name(name)))
495 return pkg_hash_fetch_best_installation_candidate(apkg,
496 pkg_name_constraint_fcn,
500 pkg_t *pkg_hash_fetch_by_name_version(const char *pkg_name, const char *version)
504 char *version_str = NULL;
506 if (!(vec = pkg_vec_fetch_by_name(pkg_name)))
509 for (i = 0; i < vec->len; i++) {
510 version_str = pkg_version_str_alloc(vec->pkgs[i]);
511 if (!strcmp(version_str, version)) {
524 pkg_t *pkg_hash_fetch_installed_by_name_dest(const char *pkg_name,
530 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
534 for (i = 0; i < vec->len; i++)
535 if ((vec->pkgs[i]->state_status == SS_INSTALLED
536 || vec->pkgs[i]->state_status == SS_UNPACKED)
537 && vec->pkgs[i]->dest == dest) {
544 pkg_t *pkg_hash_fetch_installed_by_name(const char *pkg_name)
549 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
553 for (i = 0; i < vec->len; i++) {
554 if (vec->pkgs[i]->state_status == SS_INSTALLED
555 || vec->pkgs[i]->state_status == SS_UNPACKED) {
564 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
567 abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
568 pkg_vec_t *all = (pkg_vec_t *) data;
569 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
574 for (j = 0; j < pkg_vec->len; j++) {
575 pkg_t *pkg = pkg_vec->pkgs[j];
576 pkg_vec_insert(all, pkg);
580 void pkg_hash_fetch_available(pkg_vec_t * all)
582 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
587 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry,
590 abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
591 pkg_vec_t *all = (pkg_vec_t *) data;
592 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
598 for (j = 0; j < pkg_vec->len; j++) {
599 pkg_t *pkg = pkg_vec->pkgs[j];
600 if (pkg->state_status == SS_INSTALLED
601 || pkg->state_status == SS_UNPACKED)
602 pkg_vec_insert(all, pkg);
606 void pkg_hash_fetch_all_installed(pkg_vec_t * all)
608 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
613 * This assumes that the abstract pkg doesn't exist.
615 static abstract_pkg_t *add_new_abstract_pkg_by_name(const char *pkg_name)
617 abstract_pkg_t *ab_pkg;
619 ab_pkg = abstract_pkg_new();
621 ab_pkg->name = xstrdup(pkg_name);
622 hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
627 abstract_pkg_t *ensure_abstract_pkg_by_name(const char *pkg_name)
629 abstract_pkg_t *ab_pkg;
631 if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
632 ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
637 void hash_insert_pkg(pkg_t * pkg, int set_status)
639 abstract_pkg_t *ab_pkg;
641 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
643 ab_pkg->pkgs = pkg_vec_alloc();
645 if (pkg->state_status == SS_INSTALLED) {
646 ab_pkg->state_status = SS_INSTALLED;
647 } else if (pkg->state_status == SS_UNPACKED) {
648 ab_pkg->state_status = SS_UNPACKED;
653 buildProvides(ab_pkg, pkg);
655 init_providelist(pkg, NULL);
657 /* Need to build the conflicts graph before replaces for correct
658 * calculation of replaced_by relation.
662 buildReplaces(ab_pkg, pkg);
664 buildDependedUponBy(pkg, ab_pkg);
666 pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
667 pkg->parent = ab_pkg;
670 static const char *strip_offline_root(const char *file_name)
674 if (conf->offline_root) {
675 len = strlen(conf->offline_root);
676 if (strncmp(file_name, conf->offline_root, len) == 0)
683 void file_hash_remove(const char *file_name)
685 file_name = strip_offline_root(file_name);
686 hash_table_remove(&conf->file_hash, file_name);
689 pkg_t *file_hash_get_file_owner(const char *file_name)
691 file_name = strip_offline_root(file_name);
692 return hash_table_get(&conf->file_hash, file_name);
695 void file_hash_set_file_owner(const char *file_name, pkg_t * owning_pkg)
697 pkg_t *old_owning_pkg;
698 int file_name_len = strlen(file_name);
700 if (file_name[file_name_len - 1] == '/')
703 file_name = strip_offline_root(file_name);
705 old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
706 hash_table_insert(&conf->file_hash, file_name, owning_pkg);
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);
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;