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"
23 #include "opkg_message.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"
35 hash_table_init("pkg-hash", &conf->pkg_hash,
36 OPKG_CONF_DEFAULT_HASH_LEN);
40 free_pkgs(const char *key, void *entry, void *data)
43 abstract_pkg_t *ab_pkg;
45 /* Each entry in the hash table is an abstract package, which contains
46 * a list of packages that provide the abstract package.
49 ab_pkg = (abstract_pkg_t*) entry;
52 for (i = 0; i < ab_pkg->pkgs->len; i++) {
53 pkg_deinit (ab_pkg->pkgs->pkgs[i]);
54 free (ab_pkg->pkgs->pkgs[i]);
58 abstract_pkg_vec_free (ab_pkg->provided_by);
59 abstract_pkg_vec_free (ab_pkg->replaced_by);
60 pkg_vec_free (ab_pkg->pkgs);
61 free (ab_pkg->depended_upon_by);
69 hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
70 hash_table_deinit(&conf->pkg_hash);
74 dist_hash_add_from_file(const char *lists_dir, pkg_src_t *dist)
76 nv_pair_list_elt_t *l;
77 char *list_file, *subname;
79 list_for_each_entry(l , &conf->arch_list.head, node) {
80 nv_pair_t *nv = (nv_pair_t *)l->data;
81 sprintf_alloc(&subname, "%s-%s", dist->name, nv->name);
82 sprintf_alloc(&list_file, "%s/%s", lists_dir, subname);
84 if (file_exists(list_file)) {
85 if (pkg_hash_add_from_file(list_file, dist, NULL, 0)) {
89 pkg_src_list_append (&conf->pkg_src_list, subname, dist->value, "__dummy__", 0);
100 pkg_hash_add_from_file(const char *file_name,
101 pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
106 const size_t len = 4096;
109 fp = fopen(file_name, "r");
111 opkg_perror(ERROR, "Failed to open %s", file_name);
122 ret = pkg_parse_from_stream_nomalloc(pkg, fp, 0,
130 /* Probably a blank line, continue parsing. */
135 if (!pkg->architecture || !pkg->arch_priority) {
136 char *version_str = pkg_version_str_alloc(pkg);
137 opkg_msg(NOTICE, "Package %s version %s has no "
138 "valid architecture, ignoring.\n",
139 pkg->name, version_str);
144 hash_insert_pkg(pkg, is_status_file);
155 * Load in feed files from the cached "src" and/or "src/gz" locations.
158 pkg_hash_load_feeds(void)
160 pkg_src_list_elt_t *iter;
161 pkg_src_t *src, *subdist;
162 char *list_file, *lists_dir;
164 opkg_msg(INFO, "\n");
166 lists_dir = conf->restrict_to_default_dest ?
167 conf->default_dest->lists_dir : conf->lists_dir;
169 for (iter = void_list_first(&conf->dist_src_list); iter;
170 iter = void_list_next(&conf->dist_src_list, iter)) {
172 src = (pkg_src_t *)iter->data;
174 sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
176 if (file_exists(list_file)) {
178 release_t *release = release_new();
179 if(release_init_from_file(release, list_file)) {
185 const char **comps = release_comps(release, &ncomp);
186 subdist = (pkg_src_t *) xmalloc(sizeof(pkg_src_t));
187 memcpy(subdist, src, sizeof(pkg_src_t));
189 for(i = 0; i < ncomp; i++){
190 subdist->name = NULL;
191 sprintf_alloc(&subdist->name, "%s-%s", src->name, comps[i]);
192 if (dist_hash_add_from_file(lists_dir, subdist)) {
193 free(subdist->name); free(subdist);
198 free(subdist->name); free(subdist);
203 for (iter = void_list_first(&conf->pkg_src_list); iter;
204 iter = void_list_next(&conf->pkg_src_list, iter)) {
206 src = (pkg_src_t *)iter->data;
208 sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
210 if (file_exists(list_file)) {
211 if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
223 * Load in status files from the configured "dest"s.
226 pkg_hash_load_status_files(void)
228 pkg_dest_list_elt_t *iter;
231 opkg_msg(INFO, "\n");
233 for (iter = void_list_first(&conf->pkg_dest_list); iter;
234 iter = void_list_next(&conf->pkg_dest_list, iter)) {
236 dest = (pkg_dest_t *)iter->data;
238 if (file_exists(dest->status_file_name)) {
239 if (pkg_hash_add_from_file(dest->status_file_name, NULL, dest, 1))
247 static abstract_pkg_t *
248 abstract_pkg_fetch_by_name(const char * pkg_name)
250 return (abstract_pkg_t *)hash_table_get(&conf->pkg_hash, pkg_name);
254 pkg_hash_fetch_best_installation_candidate(abstract_pkg_t *apkg,
255 int (*constraint_fcn)(pkg_t *pkg, void *cdata),
256 void *cdata, int quiet)
261 int wrong_arch_found = 0;
262 pkg_vec_t *matching_pkgs;
263 abstract_pkg_vec_t *matching_apkgs;
264 abstract_pkg_vec_t *provided_apkg_vec;
265 abstract_pkg_t **provided_apkgs;
266 abstract_pkg_vec_t *providers;
267 pkg_t *latest_installed_parent = NULL;
268 pkg_t *latest_matching = NULL;
269 pkg_t *priorized_matching = NULL;
270 pkg_t *held_pkg = NULL;
271 pkg_t *good_pkg_by_name = NULL;
273 if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
276 matching_pkgs = pkg_vec_alloc();
277 matching_apkgs = abstract_pkg_vec_alloc();
278 providers = abstract_pkg_vec_alloc();
280 opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
282 provided_apkg_vec = apkg->provided_by;
283 nprovides = provided_apkg_vec->len;
284 provided_apkgs = provided_apkg_vec->pkgs;
286 opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name, nprovides);
288 /* accumulate all the providers */
289 for (i = 0; i < nprovides; i++) {
290 abstract_pkg_t *provider_apkg = provided_apkgs[i];
291 opkg_msg(DEBUG, "Adding %s to providers.\n", provider_apkg->name);
292 abstract_pkg_vec_insert(providers, provider_apkg);
294 nprovides = providers->len;
296 for (i = 0; i < nprovides; i++) {
297 abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
298 abstract_pkg_t *replacement_apkg = NULL;
301 if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
302 replacement_apkg = provider_apkg->replaced_by->pkgs[0];
303 if (provider_apkg->replaced_by->len > 1) {
304 opkg_msg(NOTICE, "Multiple replacers for %s, "
305 "using first one (%s).\n",
306 provider_apkg->name, replacement_apkg->name);
310 if (replacement_apkg)
311 opkg_msg(DEBUG, "replacement_apkg=%s for provider_apkg=%s.\n",
312 replacement_apkg->name, provider_apkg->name);
314 if (replacement_apkg && (replacement_apkg != provider_apkg)) {
315 if (abstract_pkg_vec_contains(providers, replacement_apkg))
318 provider_apkg = replacement_apkg;
321 if (!(vec = provider_apkg->pkgs)) {
322 opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
323 provider_apkg->name);
328 /* now check for supported architecture */
332 /* count packages matching max arch priority and keep track of last one */
333 for (j=0; j<vec->len; j++) {
334 pkg_t *maybe = vec->pkgs[j];
335 opkg_msg(DEBUG, "%s arch=%s arch_priority=%d version=%s.\n",
336 maybe->name, maybe->architecture,
337 maybe->arch_priority, maybe->version);
338 /* We make sure not to add the same package twice. Need to search for the reason why
339 they show up twice sometimes. */
340 if ((maybe->arch_priority > 0) && (! pkg_vec_contains(matching_pkgs, maybe))) {
342 abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
343 pkg_vec_insert(matching_pkgs, maybe);
347 if (vec->len > 0 && matching_pkgs->len < 1)
348 wrong_arch_found = 1;
352 if (matching_pkgs->len < 1) {
353 if (wrong_arch_found)
354 opkg_msg(ERROR, "Packages for %s found, but"
355 " incompatible with the architectures configured\n",
357 pkg_vec_free(matching_pkgs);
358 abstract_pkg_vec_free(matching_apkgs);
359 abstract_pkg_vec_free(providers);
364 if (matching_pkgs->len > 1)
365 pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
366 if (matching_apkgs->len > 1)
367 abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
369 for (i = 0; i < matching_pkgs->len; i++) {
370 pkg_t *matching = matching_pkgs->pkgs[i];
371 if (constraint_fcn(matching, cdata)) {
372 opkg_msg(DEBUG, "Candidate: %s %s.\n",
373 matching->name, matching->version) ;
374 good_pkg_by_name = matching;
375 /* It has been provided by hand, so it is what user want */
376 if (matching->provided_by_hand == 1)
382 for (i = 0; i < matching_pkgs->len; i++) {
383 pkg_t *matching = matching_pkgs->pkgs[i];
384 latest_matching = matching;
385 if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
386 latest_installed_parent = matching;
387 if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
389 opkg_msg(NOTICE, "Multiple packages (%s and %s) providing"
390 " same name marked HOLD or PREFER. "
392 held_pkg->name, matching->name);
397 if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
399 for (i = 0; i < matching_pkgs->len; i++) {
400 pkg_t *matching = matching_pkgs->pkgs[i];
401 if (matching->arch_priority > prio) {
402 priorized_matching = matching;
403 prio = matching->arch_priority;
404 opkg_msg(DEBUG, "Match %s with priority %i.\n",
405 matching->name, prio);
411 if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
412 opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
413 matching_pkgs->len, apkg->name);
414 for (i = 0; i < matching_pkgs->len; i++) {
415 pkg_t *matching = matching_pkgs->pkgs[i];
416 opkg_msg(INFO, "%s %s %s\n",
417 matching->name, matching->version,
418 matching->architecture);
422 nmatching = matching_apkgs->len;
424 pkg_vec_free(matching_pkgs);
425 abstract_pkg_vec_free(matching_apkgs);
426 abstract_pkg_vec_free(providers);
428 if (good_pkg_by_name) { /* We found a good candidate, we will install it */
429 return good_pkg_by_name;
432 opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
435 if (latest_installed_parent) {
436 opkg_msg(INFO, "Using latest version of installed package %s.\n",
437 latest_installed_parent->name);
438 return latest_installed_parent;
440 if (priorized_matching) {
441 opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
442 priorized_matching->name, priorized_matching->version,
443 priorized_matching->architecture);
444 return priorized_matching;
447 opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
451 if (latest_matching) {
452 opkg_msg(INFO, "Using latest matching %s %s %s.\n",
453 latest_matching->name, latest_matching->version,
454 latest_matching->architecture);
455 return latest_matching;
461 pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
463 const char *name = (const char *)cdata;
465 if (strcmp(pkg->name, name) == 0)
472 pkg_vec_fetch_by_name(const char *pkg_name)
474 abstract_pkg_t * ab_pkg;
476 if(!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
482 if (ab_pkg->provided_by) {
483 abstract_pkg_t *abpkg = abstract_pkg_vec_get(ab_pkg->provided_by, 0);
495 pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
497 abstract_pkg_t *apkg = NULL;
499 if (!(apkg = abstract_pkg_fetch_by_name(name)))
502 return pkg_hash_fetch_best_installation_candidate(apkg,
503 pkg_name_constraint_fcn, apkg->name, 0);
508 pkg_hash_fetch_by_name_version(const char *pkg_name, const char * version)
512 char *version_str = NULL;
514 if(!(vec = pkg_vec_fetch_by_name(pkg_name)))
517 for(i = 0; i < vec->len; i++) {
518 version_str = pkg_version_str_alloc(vec->pkgs[i]);
519 if(!strcmp(version_str, version)) {
533 pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, pkg_dest_t *dest)
538 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
542 for (i = 0; i < vec->len; i++)
543 if((vec->pkgs[i]->state_status == SS_INSTALLED
544 || vec->pkgs[i]->state_status == SS_UNPACKED)
545 && vec->pkgs[i]->dest == dest) {
553 pkg_hash_fetch_installed_by_name(const char *pkg_name)
558 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
562 for (i = 0; i < vec->len; i++) {
563 if (vec->pkgs[i]->state_status == SS_INSTALLED
564 || vec->pkgs[i]->state_status == SS_UNPACKED) {
574 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
577 abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
578 pkg_vec_t *all = (pkg_vec_t *)data;
579 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
584 for (j = 0; j < pkg_vec->len; j++) {
585 pkg_t *pkg = pkg_vec->pkgs[j];
586 pkg_vec_insert(all, pkg);
591 pkg_hash_fetch_available(pkg_vec_t *all)
593 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
598 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
600 abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
601 pkg_vec_t *all = (pkg_vec_t *)data;
602 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
608 for (j = 0; j < pkg_vec->len; j++) {
609 pkg_t *pkg = pkg_vec->pkgs[j];
610 if (pkg->state_status == SS_INSTALLED
611 || pkg->state_status == SS_UNPACKED)
612 pkg_vec_insert(all, pkg);
617 pkg_hash_fetch_all_installed(pkg_vec_t *all)
619 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
624 * This assumes that the abstract pkg doesn't exist.
626 static abstract_pkg_t *
627 add_new_abstract_pkg_by_name(const char *pkg_name)
629 abstract_pkg_t *ab_pkg;
631 ab_pkg = abstract_pkg_new();
633 ab_pkg->name = xstrdup(pkg_name);
634 hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
641 ensure_abstract_pkg_by_name(const char *pkg_name)
643 abstract_pkg_t * ab_pkg;
645 if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
646 ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
652 hash_insert_pkg(pkg_t *pkg, int set_status)
654 abstract_pkg_t * ab_pkg;
656 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
658 ab_pkg->pkgs = pkg_vec_alloc();
660 if (pkg->state_status == SS_INSTALLED) {
661 ab_pkg->state_status = SS_INSTALLED;
662 } else if (pkg->state_status == SS_UNPACKED) {
663 ab_pkg->state_status = SS_UNPACKED;
668 buildProvides(ab_pkg, pkg);
670 /* Need to build the conflicts graph before replaces for correct
671 * calculation of replaced_by relation.
675 buildReplaces(ab_pkg, pkg);
677 buildDependedUponBy(pkg, ab_pkg);
679 pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
680 pkg->parent = ab_pkg;
684 strip_offline_root(const char *file_name)
688 if (conf->offline_root) {
689 len = strlen(conf->offline_root);
690 if (strncmp(file_name, conf->offline_root, len) == 0)
698 file_hash_remove(const char *file_name)
700 file_name = strip_offline_root(file_name);
701 hash_table_remove(&conf->file_hash, file_name);
705 file_hash_get_file_owner(const char *file_name)
707 file_name = strip_offline_root(file_name);
708 return hash_table_get(&conf->file_hash, file_name);
712 file_hash_set_file_owner(const char *file_name, pkg_t *owning_pkg)
714 pkg_t *old_owning_pkg;
715 int file_name_len = strlen(file_name);
717 if (file_name[file_name_len -1] == '/')
720 file_name = strip_offline_root(file_name);
722 old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
723 hash_table_insert(&conf->file_hash, file_name, owning_pkg);
725 if (old_owning_pkg) {
726 pkg_get_installed_files(old_owning_pkg);
727 str_list_remove_elt(old_owning_pkg->installed_files, file_name);
728 pkg_free_installed_files(old_owning_pkg);
730 /* mark this package to have its filelist written */
731 old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
732 owning_pkg->state_flag |= SF_FILELIST_CHANGED;