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"
36 hash_table_init("pkg-hash", &conf->pkg_hash,
37 OPKG_CONF_DEFAULT_HASH_LEN);
41 free_pkgs(const char *key, void *entry, void *data)
44 abstract_pkg_t *ab_pkg;
46 /* Each entry in the hash table is an abstract package, which contains
47 * a list of packages that provide the abstract package.
50 ab_pkg = (abstract_pkg_t*) entry;
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]);
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);
70 hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
71 hash_table_deinit(&conf->pkg_hash);
75 dist_hash_add_from_file(const char *lists_dir, pkg_src_t *dist)
77 nv_pair_list_elt_t *l;
78 char *list_file, *subname;
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);
85 if (file_exists(list_file)) {
86 if (pkg_hash_add_from_file(list_file, dist, NULL, 0)) {
90 pkg_src_list_append (&conf->pkg_src_list, subname, dist->value, "__dummy__", 0);
101 pkg_hash_add_from_file(const char *file_name,
102 pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
107 const size_t len = 4096;
109 struct gzip_handle zh;
111 if (src && src->gzip) {
112 fp = gzip_fdopen(&zh, file_name);
115 fp = fopen(file_name, "r");
119 opkg_perror(ERROR, "Failed to open %s", file_name);
130 ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
133 if (pkg->name == NULL) {
134 /* probably just a blank line */
144 /* Probably a blank line, continue parsing. */
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);
158 hash_insert_pkg(pkg, is_status_file);
165 if (src && src->gzip)
172 * Load in feed files from the cached "src" and/or "src/gz" locations.
175 pkg_hash_load_feeds(void)
177 pkg_src_list_elt_t *iter;
178 pkg_src_t *src, *subdist;
179 char *list_file, *lists_dir;
181 opkg_msg(INFO, "\n");
183 lists_dir = conf->restrict_to_default_dest ?
184 conf->default_dest->lists_dir : conf->lists_dir;
186 for (iter = void_list_first(&conf->pkg_src_list); iter;
187 iter = void_list_next(&conf->pkg_src_list, iter)) {
189 src = (pkg_src_t *)iter->data;
191 sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
193 if (file_exists(list_file)) {
194 if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
206 * Load in status files from the configured "dest"s.
209 pkg_hash_load_status_files(void)
211 pkg_dest_list_elt_t *iter;
214 opkg_msg(INFO, "\n");
216 for (iter = void_list_first(&conf->pkg_dest_list); iter;
217 iter = void_list_next(&conf->pkg_dest_list, iter)) {
219 dest = (pkg_dest_t *)iter->data;
221 if (file_exists(dest->status_file_name)) {
222 if (pkg_hash_add_from_file(dest->status_file_name, NULL, dest, 1))
230 static abstract_pkg_t *
231 abstract_pkg_fetch_by_name(const char * pkg_name)
233 return (abstract_pkg_t *)hash_table_get(&conf->pkg_hash, pkg_name);
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)
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;
256 if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
259 matching_pkgs = pkg_vec_alloc();
260 matching_apkgs = abstract_pkg_vec_alloc();
261 providers = abstract_pkg_vec_alloc();
263 opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
265 provided_apkg_vec = apkg->provided_by;
266 nprovides = provided_apkg_vec->len;
267 provided_apkgs = provided_apkg_vec->pkgs;
269 opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name, nprovides);
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);
277 nprovides = providers->len;
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;
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);
293 if (replacement_apkg)
294 opkg_msg(DEBUG, "replacement_apkg=%s for provider_apkg=%s.\n",
295 replacement_apkg->name, provider_apkg->name);
297 if (replacement_apkg && (replacement_apkg != provider_apkg)) {
298 if (abstract_pkg_vec_contains(providers, replacement_apkg))
301 provider_apkg = replacement_apkg;
304 if (!(vec = provider_apkg->pkgs)) {
305 opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
306 provider_apkg->name);
311 /* now check for supported architecture */
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))) {
325 abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
326 pkg_vec_insert(matching_pkgs, maybe);
330 if (vec->len > 0 && matching_pkgs->len < 1)
331 wrong_arch_found = 1;
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",
340 pkg_vec_free(matching_pkgs);
341 abstract_pkg_vec_free(matching_apkgs);
342 abstract_pkg_vec_free(providers);
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);
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)
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)) {
372 opkg_msg(NOTICE, "Multiple packages (%s and %s) providing"
373 " same name marked HOLD or PREFER. "
375 held_pkg->name, matching->name);
380 if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
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);
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);
405 nmatching = matching_apkgs->len;
407 pkg_vec_free(matching_pkgs);
408 abstract_pkg_vec_free(matching_apkgs);
409 abstract_pkg_vec_free(providers);
411 if (good_pkg_by_name) { /* We found a good candidate, we will install it */
412 return good_pkg_by_name;
415 opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
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;
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;
430 opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
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;
444 pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
446 const char *name = (const char *)cdata;
448 if (strcmp(pkg->name, name) == 0)
455 pkg_vec_fetch_by_name(const char *pkg_name)
457 abstract_pkg_t * ab_pkg;
459 if(!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
465 if (ab_pkg->provided_by) {
466 abstract_pkg_t *abpkg = abstract_pkg_vec_get(ab_pkg->provided_by, 0);
478 pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
480 abstract_pkg_t *apkg = NULL;
482 if (!(apkg = abstract_pkg_fetch_by_name(name)))
485 return pkg_hash_fetch_best_installation_candidate(apkg,
486 pkg_name_constraint_fcn, apkg->name, 0);
491 pkg_hash_fetch_by_name_version(const char *pkg_name, const char * version)
495 char *version_str = NULL;
497 if(!(vec = pkg_vec_fetch_by_name(pkg_name)))
500 for(i = 0; i < vec->len; i++) {
501 version_str = pkg_version_str_alloc(vec->pkgs[i]);
502 if(!strcmp(version_str, version)) {
516 pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, pkg_dest_t *dest)
521 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
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) {
536 pkg_hash_fetch_installed_by_name(const char *pkg_name)
541 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
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) {
557 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
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;
567 for (j = 0; j < pkg_vec->len; j++) {
568 pkg_t *pkg = pkg_vec->pkgs[j];
569 pkg_vec_insert(all, pkg);
574 pkg_hash_fetch_available(pkg_vec_t *all)
576 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
581 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
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;
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);
600 pkg_hash_fetch_all_installed(pkg_vec_t *all)
602 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
607 * This assumes that the abstract pkg doesn't exist.
609 static abstract_pkg_t *
610 add_new_abstract_pkg_by_name(const char *pkg_name)
612 abstract_pkg_t *ab_pkg;
614 ab_pkg = abstract_pkg_new();
616 ab_pkg->name = xstrdup(pkg_name);
617 hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
624 ensure_abstract_pkg_by_name(const char *pkg_name)
626 abstract_pkg_t * ab_pkg;
628 if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
629 ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
635 hash_insert_pkg(pkg_t *pkg, int set_status)
637 abstract_pkg_t * ab_pkg;
639 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
641 ab_pkg->pkgs = pkg_vec_alloc();
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;
651 buildProvides(ab_pkg, pkg);
653 /* Need to build the conflicts graph before replaces for correct
654 * calculation of replaced_by relation.
658 buildReplaces(ab_pkg, pkg);
660 buildDependedUponBy(pkg, ab_pkg);
662 pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
663 pkg->parent = ab_pkg;
667 strip_offline_root(const char *file_name)
671 if (conf->offline_root) {
672 len = strlen(conf->offline_root);
673 if (strncmp(file_name, conf->offline_root, len) == 0)
681 file_hash_remove(const char *file_name)
683 file_name = strip_offline_root(file_name);
684 hash_table_remove(&conf->file_hash, file_name);
688 file_hash_get_file_owner(const char *file_name)
690 file_name = strip_offline_root(file_name);
691 return hash_table_get(&conf->file_hash, file_name);
695 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;