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"
23 #include "pkg_depends.h"
26 #include "parse_util.h"
27 #include "pkg_parse.h"
28 #include "opkg_utils.h"
29 #include "sprintf_alloc.h"
30 #include "file_util.h"
31 #include "libbb/libbb.h"
32 #include "libbb/gzip.h"
34 void pkg_hash_init(void)
36 hash_table_init("pkg-hash", &conf
->pkg_hash
,
37 OPKG_CONF_DEFAULT_HASH_LEN
);
40 static void 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
);
66 void pkg_hash_deinit(void)
68 hash_table_foreach(&conf
->pkg_hash
, free_pkgs
, NULL
);
69 hash_table_deinit(&conf
->pkg_hash
);
72 int dist_hash_add_from_file(const char *lists_dir
, pkg_src_t
* dist
)
74 nv_pair_list_elt_t
*l
;
75 char *list_file
, *subname
;
77 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
78 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
79 sprintf_alloc(&subname
, "%s-%s", dist
->name
, nv
->name
);
80 sprintf_alloc(&list_file
, "%s/%s", lists_dir
, subname
);
82 if (file_exists(list_file
)) {
83 if (pkg_hash_add_from_file(list_file
, dist
, NULL
, 0, 0, NULL
, NULL
)) {
87 pkg_src_list_append(&conf
->pkg_src_list
, subname
,
88 dist
->value
, "__dummy__", 0);
98 pkg_hash_add_from_file(const char *file_name
,
99 pkg_src_t
* src
, pkg_dest_t
* dest
, int is_status_file
, int state_flags
,
100 void (*cb
)(pkg_t
*, void *), void *priv
)
105 const size_t len
= 4096;
107 struct gzip_handle zh
;
109 if (src
&& src
->gzip
) {
110 fp
= gzip_fdopen(&zh
, file_name
);
112 fp
= fopen(file_name
, "r");
116 opkg_perror(ERROR
, "Failed to open %s", file_name
);
126 pkg
->state_flag
|= state_flags
;
128 ret
= parse_from_stream_nomalloc(pkg_parse_line
, pkg
, fp
, 0,
131 if (pkg
->name
== NULL
) {
132 /* probably just a blank line */
142 /* Probably a blank line, continue parsing. */
147 if (!(pkg
->state_flag
& SF_NEED_DETAIL
)) {
148 //opkg_msg(DEBUG, "Package %s is unrelated, ignoring.\n", pkg->name);
154 if (!pkg_get_architecture(pkg
) || !pkg_get_arch_priority(pkg
)) {
155 char *version_str
= pkg_version_str_alloc(pkg
);
156 opkg_msg(NOTICE
, "Package %s version %s has no "
157 "valid architecture, ignoring.\n",
158 pkg
->name
, version_str
);
166 hash_insert_pkg(pkg
, is_status_file
);
173 if (src
&& src
->gzip
)
180 * Load in feed files from the cached "src" and/or "src/gz" locations.
182 int pkg_hash_load_feeds(int state_flags
, void (*cb
)(pkg_t
*, void *), void *priv
)
184 pkg_src_list_elt_t
*iter
;
186 char *list_file
, *lists_dir
;
188 opkg_msg(INFO
, "\n");
190 lists_dir
= conf
->restrict_to_default_dest
?
191 conf
->default_dest
->lists_dir
: conf
->lists_dir
;
193 for (iter
= void_list_first(&conf
->pkg_src_list
); iter
;
194 iter
= void_list_next(&conf
->pkg_src_list
, iter
)) {
196 src
= (pkg_src_t
*) iter
->data
;
198 sprintf_alloc(&list_file
, "%s/%s", lists_dir
, src
->name
);
200 if (file_exists(list_file
)) {
201 if (pkg_hash_add_from_file(list_file
, src
, NULL
, 0, state_flags
, cb
, priv
)) {
213 * Load in status files from the configured "dest"s.
215 int pkg_hash_load_status_files(void (*cb
)(pkg_t
*, void *), void *priv
)
217 pkg_dest_list_elt_t
*iter
;
220 opkg_msg(INFO
, "\n");
222 for (iter
= void_list_first(&conf
->pkg_dest_list
); iter
;
223 iter
= void_list_next(&conf
->pkg_dest_list
, iter
)) {
225 dest
= (pkg_dest_t
*) iter
->data
;
227 if (file_exists(dest
->status_file_name
)) {
228 if (pkg_hash_add_from_file
229 (dest
->status_file_name
, NULL
, dest
, 1, SF_NEED_DETAIL
, cb
, priv
))
238 pkg_hash_load_package_details_helper(const char *pkg_name
, void *entry
, void *data
)
241 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*) entry
;
243 if (ab_pkg
->state_flag
& SF_NEED_DETAIL
) {
244 if (ab_pkg
->state_flag
& SF_MARKED
) {
245 opkg_msg(DEBUG
, "skipping already seen flagged abpkg %s\n",
250 opkg_msg(DEBUG
, "found yet incomplete flagged abpkg %s\n",
254 ab_pkg
->state_flag
|= SF_MARKED
;
258 int pkg_hash_load_package_details(void)
263 pkg_hash_load_feeds(0, NULL
, NULL
);
266 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_load_package_details_helper
, &n_need_detail
);
268 if (n_need_detail
> 0)
269 opkg_msg(DEBUG
, "Found %d packages requiring details, reloading feeds\n", n_need_detail
);
278 pkg_hash_check_unresolved(pkg_t
*maybe
)
280 char **unresolved
= NULL
;
282 pkg_vec_t
*depends
, *all
;
285 depends
= pkg_vec_alloc();
286 pkg_hash_fetch_unsatisfied_dependencies(maybe
, depends
, &unresolved
);
295 pkg_vec_free(depends
);
297 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
298 all
= pkg_vec_alloc();
299 pkg_hash_fetch_available(all
);
300 for (i
= 0; i
< all
->len
; i
++) {
301 all
->pkgs
[i
]->parent
->dependencies_checked
= 0;
308 pkg_t
*pkg_hash_fetch_best_installation_candidate(abstract_pkg_t
* apkg
,
309 int (*constraint_fcn
) (pkg_t
*
313 void *cdata
, int quiet
)
318 int wrong_arch_found
= 0;
320 pkg_vec_t
*matching_pkgs
;
321 abstract_pkg_vec_t
*matching_apkgs
;
322 abstract_pkg_vec_t
*provided_apkg_vec
;
323 abstract_pkg_t
**provided_apkgs
;
324 abstract_pkg_vec_t
*providers
;
325 pkg_t
*latest_installed_parent
= NULL
;
326 pkg_t
*latest_matching
= NULL
;
327 pkg_t
*priorized_matching
= NULL
;
328 pkg_t
*held_pkg
= NULL
;
329 pkg_t
*good_pkg_by_name
= NULL
;
331 if (apkg
== NULL
|| apkg
->provided_by
== NULL
332 || (apkg
->provided_by
->len
== 0))
335 matching_pkgs
= pkg_vec_alloc();
336 matching_apkgs
= abstract_pkg_vec_alloc();
337 providers
= abstract_pkg_vec_alloc();
339 opkg_msg(DEBUG
, "Best installation candidate for %s:\n", apkg
->name
);
341 provided_apkg_vec
= apkg
->provided_by
;
342 nprovides
= provided_apkg_vec
->len
;
343 provided_apkgs
= provided_apkg_vec
->pkgs
;
345 opkg_msg(DEBUG
, "apkg=%s nprovides=%d.\n", apkg
->name
,
348 /* accumulate all the providers */
349 for (i
= 0; i
< nprovides
; i
++) {
350 abstract_pkg_t
*provider_apkg
= provided_apkgs
[i
];
351 opkg_msg(DEBUG
, "Adding %s to providers.\n",
352 provider_apkg
->name
);
353 abstract_pkg_vec_insert(providers
, provider_apkg
);
355 nprovides
= providers
->len
;
357 for (i
= 0; i
< nprovides
; i
++) {
358 abstract_pkg_t
*provider_apkg
=
359 abstract_pkg_vec_get(providers
, i
);
360 abstract_pkg_t
*replacement_apkg
= NULL
;
363 if (provider_apkg
->replaced_by
364 && provider_apkg
->replaced_by
->len
) {
365 replacement_apkg
= provider_apkg
->replaced_by
->pkgs
[0];
366 if (provider_apkg
->replaced_by
->len
> 1) {
367 opkg_msg(NOTICE
, "Multiple replacers for %s, "
368 "using first one (%s).\n",
370 replacement_apkg
->name
);
374 if (replacement_apkg
)
376 "replacement_apkg=%s for provider_apkg=%s.\n",
377 replacement_apkg
->name
, provider_apkg
->name
);
379 if (replacement_apkg
&& (replacement_apkg
!= provider_apkg
)) {
380 if (abstract_pkg_vec_contains
381 (providers
, replacement_apkg
))
384 provider_apkg
= replacement_apkg
;
387 if (!(vec
= provider_apkg
->pkgs
)) {
388 opkg_msg(DEBUG
, "No pkgs for provider_apkg %s.\n",
389 provider_apkg
->name
);
393 /* now check for supported architecture */
397 /* count packages matching max arch priority and keep track of last one */
398 for (j
= 0; j
< vec
->len
; j
++) {
399 pkg_t
*maybe
= vec
->pkgs
[j
];
400 arch_priority
= pkg_get_arch_priority(maybe
);
403 "%s arch=%s arch_priority=%d version=%s.\n",
404 maybe
->name
, pkg_get_architecture(maybe
),
405 arch_priority
, pkg_get_string(maybe
, PKG_VERSION
));
406 /* We make sure not to add the same package twice. Need to search for the reason why
407 they show up twice sometimes. */
408 if ((arch_priority
> 0)
410 (!pkg_vec_contains(matching_pkgs
, maybe
))
412 (!pkg_hash_check_unresolved(maybe
))) {
414 abstract_pkg_vec_insert(matching_apkgs
,
416 pkg_vec_insert(matching_pkgs
, maybe
);
420 if (vec
->len
> 0 && matching_pkgs
->len
< 1)
421 wrong_arch_found
= 1;
425 if (matching_pkgs
->len
< 1) {
426 if (wrong_arch_found
)
427 opkg_msg(ERROR
, "Packages for %s found, but"
428 " incompatible with the architectures configured\n",
430 pkg_vec_free(matching_pkgs
);
431 abstract_pkg_vec_free(matching_apkgs
);
432 abstract_pkg_vec_free(providers
);
436 if (matching_pkgs
->len
> 1)
437 pkg_vec_sort(matching_pkgs
,
438 pkg_name_version_and_architecture_compare
);
439 if (matching_apkgs
->len
> 1)
440 abstract_pkg_vec_sort(matching_apkgs
, abstract_pkg_name_compare
);
442 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
443 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
444 if (constraint_fcn(matching
, cdata
)) {
445 opkg_msg(DEBUG
, "Candidate: %s %s.\n",
446 matching
->name
, pkg_get_string(matching
, PKG_VERSION
));
447 good_pkg_by_name
= matching
;
448 /* It has been provided by hand, so it is what user want */
449 if (matching
->provided_by_hand
== 1)
454 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
455 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
456 latest_matching
= matching
;
457 if (matching
->parent
->state_status
== SS_INSTALLED
458 || matching
->parent
->state_status
== SS_UNPACKED
)
459 latest_installed_parent
= matching
;
460 if (matching
->state_flag
& (SF_HOLD
| SF_PREFER
)) {
463 "Multiple packages (%s and %s) providing"
464 " same name marked HOLD or PREFER. "
465 "Using latest.\n", held_pkg
->name
,
471 if (!good_pkg_by_name
&& !held_pkg
&& !latest_installed_parent
472 && matching_apkgs
->len
> 1 && !quiet
) {
474 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
475 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
476 arch_priority
= pkg_get_arch_priority(matching
);
477 if (arch_priority
> prio
) {
478 priorized_matching
= matching
;
479 prio
= arch_priority
;
480 opkg_msg(DEBUG
, "Match %s with priority %i.\n",
481 matching
->name
, prio
);
487 if (conf
->verbosity
>= INFO
&& matching_apkgs
->len
> 1) {
488 opkg_msg(INFO
, "%d matching pkgs for apkg=%s:\n",
489 matching_pkgs
->len
, apkg
->name
);
490 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
491 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
492 opkg_msg(INFO
, "%s %s %s\n",
493 matching
->name
, pkg_get_string(matching
, PKG_VERSION
),
494 pkg_get_architecture(matching
));
498 nmatching
= matching_apkgs
->len
;
500 pkg_vec_free(matching_pkgs
);
501 abstract_pkg_vec_free(matching_apkgs
);
502 abstract_pkg_vec_free(providers
);
504 if (good_pkg_by_name
) { /* We found a good candidate, we will install it */
505 return good_pkg_by_name
;
508 opkg_msg(INFO
, "Using held package %s.\n", held_pkg
->name
);
511 if (latest_installed_parent
) {
513 "Using latest version of installed package %s.\n",
514 latest_installed_parent
->name
);
515 return latest_installed_parent
;
517 if (priorized_matching
) {
518 opkg_msg(INFO
, "Using priorized matching %s %s %s.\n",
519 priorized_matching
->name
, pkg_get_string(priorized_matching
, PKG_VERSION
),
520 pkg_get_architecture(priorized_matching
));
521 return priorized_matching
;
524 opkg_msg(INFO
, "No matching pkg out of %d matching_apkgs.\n",
528 if (latest_matching
) {
529 opkg_msg(INFO
, "Using latest matching %s %s %s.\n",
530 latest_matching
->name
, pkg_get_string(latest_matching
, PKG_VERSION
),
531 pkg_get_architecture(latest_matching
));
532 return latest_matching
;
537 static int pkg_name_constraint_fcn(pkg_t
* pkg
, void *cdata
)
539 const char *name
= (const char *)cdata
;
541 if (strcmp(pkg
->name
, name
) == 0)
547 static pkg_vec_t
*pkg_vec_fetch_by_name(const char *pkg_name
)
549 abstract_pkg_t
*ab_pkg
;
551 if (!(ab_pkg
= abstract_pkg_fetch_by_name(pkg_name
)))
557 if (ab_pkg
->provided_by
) {
558 abstract_pkg_t
*abpkg
=
559 abstract_pkg_vec_get(ab_pkg
->provided_by
, 0);
569 pkg_t
*pkg_hash_fetch_best_installation_candidate_by_name(const char *name
)
571 abstract_pkg_t
*apkg
= NULL
;
573 if (!(apkg
= abstract_pkg_fetch_by_name(name
)))
576 return pkg_hash_fetch_best_installation_candidate(apkg
,
577 pkg_name_constraint_fcn
,
581 pkg_t
*pkg_hash_fetch_by_name_version(const char *pkg_name
, const char *version
)
585 char *version_str
= NULL
;
587 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
)))
590 for (i
= 0; i
< vec
->len
; i
++) {
591 version_str
= pkg_version_str_alloc(vec
->pkgs
[i
]);
592 if (!strcmp(version_str
, version
)) {
605 pkg_t
*pkg_hash_fetch_installed_by_name_dest(const char *pkg_name
,
611 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
))) {
615 for (i
= 0; i
< vec
->len
; i
++)
616 if ((vec
->pkgs
[i
]->state_status
== SS_INSTALLED
617 || vec
->pkgs
[i
]->state_status
== SS_UNPACKED
)
618 && vec
->pkgs
[i
]->dest
== dest
) {
625 pkg_t
*pkg_hash_fetch_installed_by_name(const char *pkg_name
)
630 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
))) {
634 for (i
= 0; i
< vec
->len
; i
++) {
635 if (vec
->pkgs
[i
]->state_status
== SS_INSTALLED
636 || vec
->pkgs
[i
]->state_status
== SS_UNPACKED
) {
645 pkg_hash_fetch_available_helper(const char *pkg_name
, void *entry
, void *data
)
648 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*) entry
;
649 pkg_vec_t
*all
= (pkg_vec_t
*) data
;
650 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
655 for (j
= 0; j
< pkg_vec
->len
; j
++) {
656 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
657 pkg_vec_insert(all
, pkg
);
661 void pkg_hash_fetch_available(pkg_vec_t
* all
)
663 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_fetch_available_helper
,
668 pkg_hash_fetch_all_installed_helper(const char *pkg_name
, void *entry
,
671 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*) entry
;
672 pkg_vec_t
*all
= (pkg_vec_t
*) data
;
673 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
679 for (j
= 0; j
< pkg_vec
->len
; j
++) {
680 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
681 if (pkg
->state_status
== SS_INSTALLED
682 || pkg
->state_status
== SS_UNPACKED
)
683 pkg_vec_insert(all
, pkg
);
687 void pkg_hash_fetch_all_installed(pkg_vec_t
* all
)
689 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_fetch_all_installed_helper
,
694 * This assumes that the abstract pkg doesn't exist.
696 static abstract_pkg_t
*add_new_abstract_pkg_by_name(const char *pkg_name
)
698 abstract_pkg_t
*ab_pkg
;
700 ab_pkg
= abstract_pkg_new();
702 ab_pkg
->name
= xstrdup(pkg_name
);
703 hash_table_insert(&conf
->pkg_hash
, pkg_name
, ab_pkg
);
708 abstract_pkg_t
*ensure_abstract_pkg_by_name(const char *pkg_name
)
710 abstract_pkg_t
*ab_pkg
;
712 if (!(ab_pkg
= abstract_pkg_fetch_by_name(pkg_name
)))
713 ab_pkg
= add_new_abstract_pkg_by_name(pkg_name
);
718 void hash_insert_pkg(pkg_t
* pkg
, int set_status
)
720 abstract_pkg_t
*ab_pkg
;
722 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
724 ab_pkg
->pkgs
= pkg_vec_alloc();
726 if (pkg
->state_status
== SS_INSTALLED
) {
727 ab_pkg
->state_status
= SS_INSTALLED
;
728 } else if (pkg
->state_status
== SS_UNPACKED
) {
729 ab_pkg
->state_status
= SS_UNPACKED
;
734 buildProvides(ab_pkg
, pkg
);
736 init_providelist(pkg
, NULL
);
738 /* Need to build the conflicts graph before replaces for correct
739 * calculation of replaced_by relation.
743 buildReplaces(ab_pkg
, pkg
);
745 buildDependedUponBy(pkg
, ab_pkg
);
747 pkg_vec_insert_merge(ab_pkg
->pkgs
, pkg
, set_status
);
748 pkg
->parent
= ab_pkg
;
751 static const char *strip_offline_root(const char *file_name
)
755 if (conf
->offline_root
) {
756 len
= strlen(conf
->offline_root
);
757 if (strncmp(file_name
, conf
->offline_root
, len
) == 0)
764 void file_hash_remove(const char *file_name
)
766 file_name
= strip_offline_root(file_name
);
767 hash_table_remove(&conf
->file_hash
, file_name
);
770 pkg_t
*file_hash_get_file_owner(const char *file_name
)
772 file_name
= strip_offline_root(file_name
);
773 return hash_table_get(&conf
->file_hash
, file_name
);
776 void file_hash_set_file_owner(const char *file_name
, pkg_t
* owning_pkg
)
778 pkg_t
*old_owning_pkg
;
779 int file_name_len
= strlen(file_name
);
781 if (file_name
[file_name_len
- 1] == '/')
784 file_name
= strip_offline_root(file_name
);
786 old_owning_pkg
= hash_table_get(&conf
->file_hash
, file_name
);
787 hash_table_insert(&conf
->file_hash
, file_name
, owning_pkg
);
789 if (old_owning_pkg
) {
790 pkg_get_installed_files(old_owning_pkg
);
791 str_list_remove_elt(old_owning_pkg
->installed_files
, file_name
);
792 pkg_free_installed_files(old_owning_pkg
);
794 /* mark this package to have its filelist written */
795 old_owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;
796 owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;