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 "pkg_parse.h"
26 #include "opkg_utils.h"
27 #include "sprintf_alloc.h"
28 #include "file_util.h"
29 #include "libbb/libbb.h"
34 hash_table_init("pkg-hash", &conf->pkg_hash,
35 OPKG_CONF_DEFAULT_HASH_LEN);
39 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);
68 hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
69 hash_table_deinit(&conf->pkg_hash);
73 pkg_hash_add_from_file(const char *file_name,
74 pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
79 const size_t len = 4096;
82 fp = fopen(file_name, "r");
84 opkg_perror(ERROR, "Failed to open %s", file_name);
95 ret = pkg_parse_from_stream_nomalloc(pkg, fp, 0,
103 /* Probably a blank line, continue parsing. */
108 if (!pkg->architecture || !pkg->arch_priority) {
109 char *version_str = pkg_version_str_alloc(pkg);
110 opkg_msg(NOTICE, "Package %s version %s has no "
111 "valid architecture, ignoring.\n",
112 pkg->name, version_str);
117 hash_insert_pkg(pkg, is_status_file);
128 * Load in feed files from the cached "src" and/or "src/gz" locations.
131 pkg_hash_load_feeds(void)
133 pkg_src_list_elt_t *iter;
135 char *list_file, *lists_dir;
137 opkg_msg(INFO, "\n");
139 lists_dir = conf->restrict_to_default_dest ?
140 conf->default_dest->lists_dir : conf->lists_dir;
142 for (iter = void_list_first(&conf->pkg_src_list); iter;
143 iter = void_list_next(&conf->pkg_src_list, iter)) {
145 src = (pkg_src_t *)iter->data;
147 sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
149 if (file_exists(list_file)) {
150 if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
162 * Load in status files from the configured "dest"s.
165 pkg_hash_load_status_files(void)
167 pkg_dest_list_elt_t *iter;
170 opkg_msg(INFO, "\n");
172 for (iter = void_list_first(&conf->pkg_dest_list); iter;
173 iter = void_list_next(&conf->pkg_dest_list, iter)) {
175 dest = (pkg_dest_t *)iter->data;
177 if (file_exists(dest->status_file_name)) {
178 if (pkg_hash_add_from_file(dest->status_file_name, NULL, dest, 1))
186 static abstract_pkg_t *
187 abstract_pkg_fetch_by_name(const char * pkg_name)
189 return (abstract_pkg_t *)hash_table_get(&conf->pkg_hash, pkg_name);
193 pkg_hash_fetch_best_installation_candidate(abstract_pkg_t *apkg,
194 int (*constraint_fcn)(pkg_t *pkg, void *cdata),
195 void *cdata, int quiet)
200 int wrong_arch_found = 0;
201 pkg_vec_t *matching_pkgs;
202 abstract_pkg_vec_t *matching_apkgs;
203 abstract_pkg_vec_t *provided_apkg_vec;
204 abstract_pkg_t **provided_apkgs;
205 abstract_pkg_vec_t *providers;
206 pkg_t *latest_installed_parent = NULL;
207 pkg_t *latest_matching = NULL;
208 pkg_t *priorized_matching = NULL;
209 pkg_t *held_pkg = NULL;
210 pkg_t *good_pkg_by_name = NULL;
212 if (apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
215 matching_pkgs = pkg_vec_alloc();
216 matching_apkgs = abstract_pkg_vec_alloc();
217 providers = abstract_pkg_vec_alloc();
219 opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
221 provided_apkg_vec = apkg->provided_by;
222 nprovides = provided_apkg_vec->len;
223 provided_apkgs = provided_apkg_vec->pkgs;
225 opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name, nprovides);
227 /* accumulate all the providers */
228 for (i = 0; i < nprovides; i++) {
229 abstract_pkg_t *provider_apkg = provided_apkgs[i];
230 opkg_msg(DEBUG, "Adding %s to providers.\n", provider_apkg->name);
231 abstract_pkg_vec_insert(providers, provider_apkg);
233 nprovides = providers->len;
235 for (i = 0; i < nprovides; i++) {
236 abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
237 abstract_pkg_t *replacement_apkg = NULL;
240 if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
241 replacement_apkg = provider_apkg->replaced_by->pkgs[0];
242 if (provider_apkg->replaced_by->len > 1) {
243 opkg_msg(NOTICE, "Multiple replacers for %s, "
244 "using first one (%s).\n",
245 provider_apkg->name, replacement_apkg->name);
249 if (replacement_apkg)
250 opkg_msg(DEBUG, "replacement_apkg=%s for provider_apkg=%s.\n",
251 replacement_apkg->name, provider_apkg->name);
253 if (replacement_apkg && (replacement_apkg != provider_apkg)) {
254 if (abstract_pkg_vec_contains(providers, replacement_apkg))
257 provider_apkg = replacement_apkg;
260 if (!(vec = provider_apkg->pkgs)) {
261 opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
262 provider_apkg->name);
267 /* now check for supported architecture */
271 /* count packages matching max arch priority and keep track of last one */
272 for (j=0; j<vec->len; j++) {
273 pkg_t *maybe = vec->pkgs[j];
274 opkg_msg(DEBUG, "%s arch=%s arch_priority=%d version=%s.\n",
275 maybe->name, maybe->architecture,
276 maybe->arch_priority, maybe->version);
277 /* We make sure not to add the same package twice. Need to search for the reason why
278 they show up twice sometimes. */
279 if ((maybe->arch_priority > 0) && (! pkg_vec_contains(matching_pkgs, maybe))) {
281 abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
282 pkg_vec_insert(matching_pkgs, maybe);
286 if (vec->len > 0 && matching_pkgs->len < 1)
287 wrong_arch_found = 1;
291 if (matching_pkgs->len < 1) {
292 if (wrong_arch_found)
293 opkg_msg(ERROR, "Packages for %s found, but"
294 " incompatible with the architectures configured\n",
296 pkg_vec_free(matching_pkgs);
297 abstract_pkg_vec_free(matching_apkgs);
298 abstract_pkg_vec_free(providers);
303 if (matching_pkgs->len > 1)
304 pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
305 if (matching_apkgs->len > 1)
306 abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
308 for (i = 0; i < matching_pkgs->len; i++) {
309 pkg_t *matching = matching_pkgs->pkgs[i];
310 if (constraint_fcn(matching, cdata)) {
311 opkg_msg(DEBUG, "Candidate: %s %s.\n",
312 matching->name, matching->version) ;
313 good_pkg_by_name = matching;
314 /* It has been provided by hand, so it is what user want */
315 if (matching->provided_by_hand == 1)
321 for (i = 0; i < matching_pkgs->len; i++) {
322 pkg_t *matching = matching_pkgs->pkgs[i];
323 latest_matching = matching;
324 if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
325 latest_installed_parent = matching;
326 if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
328 opkg_msg(NOTICE, "Multiple packages (%s and %s) providing"
329 " same name marked HOLD or PREFER. "
331 held_pkg->name, matching->name);
336 if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
338 for (i = 0; i < matching_pkgs->len; i++) {
339 pkg_t *matching = matching_pkgs->pkgs[i];
340 if (matching->arch_priority > prio) {
341 priorized_matching = matching;
342 prio = matching->arch_priority;
343 opkg_msg(DEBUG, "Match %s with priority %i.\n",
344 matching->name, prio);
350 if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
351 opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
352 matching_pkgs->len, apkg->name);
353 for (i = 0; i < matching_pkgs->len; i++) {
354 pkg_t *matching = matching_pkgs->pkgs[i];
355 opkg_msg(INFO, "%s %s %s\n",
356 matching->name, matching->version,
357 matching->architecture);
361 nmatching = matching_apkgs->len;
363 pkg_vec_free(matching_pkgs);
364 abstract_pkg_vec_free(matching_apkgs);
365 abstract_pkg_vec_free(providers);
367 if (good_pkg_by_name) { /* We found a good candidate, we will install it */
368 return good_pkg_by_name;
371 opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
374 if (latest_installed_parent) {
375 opkg_msg(INFO, "Using latest version of installed package %s.\n",
376 latest_installed_parent->name);
377 return latest_installed_parent;
379 if (priorized_matching) {
380 opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
381 priorized_matching->name, priorized_matching->version,
382 priorized_matching->architecture);
383 return priorized_matching;
386 opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
390 if (latest_matching) {
391 opkg_msg(INFO, "Using latest matching %s %s %s.\n",
392 latest_matching->name, latest_matching->version,
393 latest_matching->architecture);
394 return latest_matching;
400 pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
402 const char *name = (const char *)cdata;
404 if (strcmp(pkg->name, name) == 0)
411 pkg_vec_fetch_by_name(const char *pkg_name)
413 abstract_pkg_t * ab_pkg;
415 if(!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
421 if (ab_pkg->provided_by) {
422 abstract_pkg_t *abpkg = abstract_pkg_vec_get(ab_pkg->provided_by, 0);
434 pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
436 abstract_pkg_t *apkg = NULL;
438 if (!(apkg = abstract_pkg_fetch_by_name(name)))
441 return pkg_hash_fetch_best_installation_candidate(apkg,
442 pkg_name_constraint_fcn, apkg->name, 0);
447 pkg_hash_fetch_by_name_version(const char *pkg_name, const char * version)
451 char *version_str = NULL;
453 if(!(vec = pkg_vec_fetch_by_name(pkg_name)))
456 for(i = 0; i < vec->len; i++) {
457 version_str = pkg_version_str_alloc(vec->pkgs[i]);
458 if(!strcmp(version_str, version)) {
472 pkg_hash_fetch_installed_by_name_dest(const char *pkg_name, pkg_dest_t *dest)
477 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
481 for (i = 0; i < vec->len; i++)
482 if((vec->pkgs[i]->state_status == SS_INSTALLED
483 || vec->pkgs[i]->state_status == SS_UNPACKED)
484 && vec->pkgs[i]->dest == dest) {
492 pkg_hash_fetch_installed_by_name(const char *pkg_name)
497 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
501 for (i = 0; i < vec->len; i++) {
502 if (vec->pkgs[i]->state_status == SS_INSTALLED
503 || vec->pkgs[i]->state_status == SS_UNPACKED) {
513 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
516 abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
517 pkg_vec_t *all = (pkg_vec_t *)data;
518 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
523 for (j = 0; j < pkg_vec->len; j++) {
524 pkg_t *pkg = pkg_vec->pkgs[j];
525 pkg_vec_insert(all, pkg);
530 pkg_hash_fetch_available(pkg_vec_t *all)
532 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
537 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
539 abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
540 pkg_vec_t *all = (pkg_vec_t *)data;
541 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
547 for (j = 0; j < pkg_vec->len; j++) {
548 pkg_t *pkg = pkg_vec->pkgs[j];
549 if (pkg->state_status == SS_INSTALLED
550 || pkg->state_status == SS_UNPACKED)
551 pkg_vec_insert(all, pkg);
556 pkg_hash_fetch_all_installed(pkg_vec_t *all)
558 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
563 * This assumes that the abstract pkg doesn't exist.
565 static abstract_pkg_t *
566 add_new_abstract_pkg_by_name(const char *pkg_name)
568 abstract_pkg_t *ab_pkg;
570 ab_pkg = abstract_pkg_new();
572 ab_pkg->name = xstrdup(pkg_name);
573 hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
580 ensure_abstract_pkg_by_name(const char *pkg_name)
582 abstract_pkg_t * ab_pkg;
584 if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
585 ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
591 hash_insert_pkg(pkg_t *pkg, int set_status)
593 abstract_pkg_t * ab_pkg;
595 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
597 ab_pkg->pkgs = pkg_vec_alloc();
599 if (pkg->state_status == SS_INSTALLED) {
600 ab_pkg->state_status = SS_INSTALLED;
601 } else if (pkg->state_status == SS_UNPACKED) {
602 ab_pkg->state_status = SS_UNPACKED;
607 buildProvides(ab_pkg, pkg);
609 /* Need to build the conflicts graph before replaces for correct
610 * calculation of replaced_by relation.
614 buildReplaces(ab_pkg, pkg);
616 buildDependedUponBy(pkg, ab_pkg);
618 pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
619 pkg->parent = ab_pkg;
623 strip_offline_root(const char *file_name)
627 if (conf->offline_root) {
628 len = strlen(conf->offline_root);
629 if (strncmp(file_name, conf->offline_root, len) == 0)
637 file_hash_remove(const char *file_name)
639 file_name = strip_offline_root(file_name);
640 hash_table_remove(&conf->file_hash, file_name);
644 file_hash_get_file_owner(const char *file_name)
646 file_name = strip_offline_root(file_name);
647 return hash_table_get(&conf->file_hash, file_name);
651 file_hash_set_file_owner(const char *file_name, pkg_t *owning_pkg)
653 pkg_t *old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
654 int file_name_len = strlen(file_name);
656 if (file_name[file_name_len -1] == '/')
659 file_name = strip_offline_root(file_name);
661 hash_table_insert(&conf->file_hash, file_name, owning_pkg);
663 if (old_owning_pkg) {
664 pkg_get_installed_files(old_owning_pkg);
665 str_list_remove_elt(old_owning_pkg->installed_files, file_name);
666 pkg_free_installed_files(old_owning_pkg);
668 /* mark this package to have its filelist written */
669 old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
670 owning_pkg->state_flag |= SF_FILELIST_CHANGED;