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, 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
, int state_flags
)
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
);
124 pkg
->state_flag
|= state_flags
;
126 ret
= parse_from_stream_nomalloc(pkg_parse_line
, pkg
, fp
, 0,
129 if (pkg
->name
== NULL
) {
130 /* probably just a blank line */
140 /* Probably a blank line, continue parsing. */
145 if (!(pkg
->state_flag
& SF_NEED_DETAIL
)) {
146 //opkg_msg(DEBUG, "Package %s is unrelated, ignoring.\n", pkg->name);
152 if (!pkg_get_architecture(pkg
) || !pkg_get_arch_priority(pkg
)) {
153 char *version_str
= pkg_version_str_alloc(pkg
);
154 opkg_msg(NOTICE
, "Package %s version %s has no "
155 "valid architecture, ignoring.\n",
156 pkg
->name
, version_str
);
161 hash_insert_pkg(pkg
, is_status_file
);
168 if (src
&& src
->gzip
)
175 * Load in feed files from the cached "src" and/or "src/gz" locations.
177 int pkg_hash_load_feeds(int state_flags
)
179 pkg_src_list_elt_t
*iter
;
181 char *list_file
, *lists_dir
;
183 opkg_msg(INFO
, "\n");
185 lists_dir
= conf
->restrict_to_default_dest
?
186 conf
->default_dest
->lists_dir
: conf
->lists_dir
;
188 for (iter
= void_list_first(&conf
->pkg_src_list
); iter
;
189 iter
= void_list_next(&conf
->pkg_src_list
, iter
)) {
191 src
= (pkg_src_t
*) iter
->data
;
193 sprintf_alloc(&list_file
, "%s/%s", lists_dir
, src
->name
);
195 if (file_exists(list_file
)) {
196 if (pkg_hash_add_from_file(list_file
, src
, NULL
, 0, state_flags
)) {
208 * Load in status files from the configured "dest"s.
210 int pkg_hash_load_status_files(void)
212 pkg_dest_list_elt_t
*iter
;
215 opkg_msg(INFO
, "\n");
217 for (iter
= void_list_first(&conf
->pkg_dest_list
); iter
;
218 iter
= void_list_next(&conf
->pkg_dest_list
, iter
)) {
220 dest
= (pkg_dest_t
*) iter
->data
;
222 if (file_exists(dest
->status_file_name
)) {
223 if (pkg_hash_add_from_file
224 (dest
->status_file_name
, NULL
, dest
, 1, SF_NEED_DETAIL
))
233 pkg_hash_load_package_details_helper(const char *pkg_name
, void *entry
, void *data
)
236 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*) entry
;
238 if (ab_pkg
->state_flag
& SF_NEED_DETAIL
) {
239 if (ab_pkg
->state_flag
& SF_MARKED
) {
240 opkg_msg(DEBUG
, "skipping already seen flagged abpkg %s\n",
245 opkg_msg(DEBUG
, "found yet incomplete flagged abpkg %s\n",
249 ab_pkg
->state_flag
|= SF_MARKED
;
253 int pkg_hash_load_package_details(void)
258 pkg_hash_load_feeds(0);
261 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_load_package_details_helper
, &n_need_detail
);
263 if (n_need_detail
> 0)
264 opkg_msg(DEBUG
, "Found %d packages requiring details, reloading feeds\n", n_need_detail
);
272 pkg_t
*pkg_hash_fetch_best_installation_candidate(abstract_pkg_t
* apkg
,
273 int (*constraint_fcn
) (pkg_t
*
277 void *cdata
, int quiet
)
282 int wrong_arch_found
= 0;
284 pkg_vec_t
*matching_pkgs
;
285 abstract_pkg_vec_t
*matching_apkgs
;
286 abstract_pkg_vec_t
*provided_apkg_vec
;
287 abstract_pkg_t
**provided_apkgs
;
288 abstract_pkg_vec_t
*providers
;
289 pkg_t
*latest_installed_parent
= NULL
;
290 pkg_t
*latest_matching
= NULL
;
291 pkg_t
*priorized_matching
= NULL
;
292 pkg_t
*held_pkg
= NULL
;
293 pkg_t
*good_pkg_by_name
= NULL
;
295 if (apkg
== NULL
|| apkg
->provided_by
== NULL
296 || (apkg
->provided_by
->len
== 0))
299 matching_pkgs
= pkg_vec_alloc();
300 matching_apkgs
= abstract_pkg_vec_alloc();
301 providers
= abstract_pkg_vec_alloc();
303 opkg_msg(DEBUG
, "Best installation candidate for %s:\n", apkg
->name
);
305 provided_apkg_vec
= apkg
->provided_by
;
306 nprovides
= provided_apkg_vec
->len
;
307 provided_apkgs
= provided_apkg_vec
->pkgs
;
309 opkg_msg(DEBUG
, "apkg=%s nprovides=%d.\n", apkg
->name
,
312 /* accumulate all the providers */
313 for (i
= 0; i
< nprovides
; i
++) {
314 abstract_pkg_t
*provider_apkg
= provided_apkgs
[i
];
315 opkg_msg(DEBUG
, "Adding %s to providers.\n",
316 provider_apkg
->name
);
317 abstract_pkg_vec_insert(providers
, provider_apkg
);
319 nprovides
= providers
->len
;
321 for (i
= 0; i
< nprovides
; i
++) {
322 abstract_pkg_t
*provider_apkg
=
323 abstract_pkg_vec_get(providers
, i
);
324 abstract_pkg_t
*replacement_apkg
= NULL
;
327 if (provider_apkg
->replaced_by
328 && provider_apkg
->replaced_by
->len
) {
329 replacement_apkg
= provider_apkg
->replaced_by
->pkgs
[0];
330 if (provider_apkg
->replaced_by
->len
> 1) {
331 opkg_msg(NOTICE
, "Multiple replacers for %s, "
332 "using first one (%s).\n",
334 replacement_apkg
->name
);
338 if (replacement_apkg
)
340 "replacement_apkg=%s for provider_apkg=%s.\n",
341 replacement_apkg
->name
, provider_apkg
->name
);
343 if (replacement_apkg
&& (replacement_apkg
!= provider_apkg
)) {
344 if (abstract_pkg_vec_contains
345 (providers
, replacement_apkg
))
348 provider_apkg
= replacement_apkg
;
351 if (!(vec
= provider_apkg
->pkgs
)) {
352 opkg_msg(DEBUG
, "No pkgs for provider_apkg %s.\n",
353 provider_apkg
->name
);
357 /* now check for supported architecture */
362 /* count packages matching max arch priority and keep track of last one */
363 for (j
= 0; j
< vec
->len
; j
++) {
364 pkg_t
*maybe
= vec
->pkgs
[j
];
365 arch_priority
= pkg_get_arch_priority(maybe
);
367 /* check if this package actually provides the requested
370 provided_apkgs
= pkg_get_ptr(maybe
, PKG_PROVIDES
);
372 while (provided_apkgs
&& *provided_apkgs
) {
373 if (!strcmp((*provided_apkgs
)->name
, apkg
->name
)) {
380 if (found_apkg
== 0) {
382 "%s version=%s skipped since it does not provide %s\n",
383 maybe
->name
, pkg_get_string(maybe
, PKG_VERSION
), apkg
->name
);
388 "%s arch=%s arch_priority=%d version=%s.\n",
389 maybe
->name
, pkg_get_architecture(maybe
),
390 arch_priority
, pkg_get_string(maybe
, PKG_VERSION
));
391 /* We make sure not to add the same package twice. Need to search for the reason why
392 they show up twice sometimes. */
393 if ((arch_priority
> 0)
395 (!pkg_vec_contains(matching_pkgs
, maybe
))) {
397 abstract_pkg_vec_insert(matching_apkgs
,
399 pkg_vec_insert(matching_pkgs
, maybe
);
403 if (vec
->len
> 0 && matching_pkgs
->len
< 1)
404 wrong_arch_found
= 1;
408 if (matching_pkgs
->len
< 1) {
409 if (wrong_arch_found
)
410 opkg_msg(ERROR
, "Packages for %s found, but"
411 " incompatible with the architectures configured\n",
413 pkg_vec_free(matching_pkgs
);
414 abstract_pkg_vec_free(matching_apkgs
);
415 abstract_pkg_vec_free(providers
);
419 if (matching_pkgs
->len
> 1)
420 pkg_vec_sort(matching_pkgs
,
421 pkg_name_version_and_architecture_compare
);
422 if (matching_apkgs
->len
> 1)
423 abstract_pkg_vec_sort(matching_apkgs
, abstract_pkg_name_compare
);
425 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
426 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
427 if (constraint_fcn(matching
, cdata
)) {
428 opkg_msg(DEBUG
, "Candidate: %s %s.\n",
429 matching
->name
, pkg_get_string(matching
, PKG_VERSION
));
430 good_pkg_by_name
= matching
;
431 /* It has been provided by hand, so it is what user want */
432 if (matching
->provided_by_hand
== 1)
437 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
438 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
439 latest_matching
= matching
;
440 if (matching
->parent
->state_status
== SS_INSTALLED
441 || matching
->parent
->state_status
== SS_UNPACKED
)
442 latest_installed_parent
= matching
;
443 if (matching
->state_flag
& (SF_HOLD
| SF_PREFER
)) {
446 "Multiple packages (%s and %s) providing"
447 " same name marked HOLD or PREFER. "
448 "Using latest.\n", held_pkg
->name
,
454 if (!good_pkg_by_name
&& !held_pkg
&& !latest_installed_parent
455 && matching_apkgs
->len
> 1 && !quiet
) {
457 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
458 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
459 arch_priority
= pkg_get_arch_priority(matching
);
460 if (arch_priority
> prio
) {
461 priorized_matching
= matching
;
462 prio
= arch_priority
;
463 opkg_msg(DEBUG
, "Match %s with priority %i.\n",
464 matching
->name
, prio
);
470 if (conf
->verbosity
>= INFO
&& matching_apkgs
->len
> 1) {
471 opkg_msg(INFO
, "%d matching pkgs for apkg=%s:\n",
472 matching_pkgs
->len
, apkg
->name
);
473 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
474 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
475 opkg_msg(INFO
, "%s %s %s\n",
476 matching
->name
, pkg_get_string(matching
, PKG_VERSION
),
477 pkg_get_architecture(matching
));
481 nmatching
= matching_apkgs
->len
;
483 pkg_vec_free(matching_pkgs
);
484 abstract_pkg_vec_free(matching_apkgs
);
485 abstract_pkg_vec_free(providers
);
487 if (good_pkg_by_name
) { /* We found a good candidate, we will install it */
488 return good_pkg_by_name
;
491 opkg_msg(INFO
, "Using held package %s.\n", held_pkg
->name
);
494 if (latest_installed_parent
) {
496 "Using latest version of installed package %s.\n",
497 latest_installed_parent
->name
);
498 return latest_installed_parent
;
500 if (priorized_matching
) {
501 opkg_msg(INFO
, "Using priorized matching %s %s %s.\n",
502 priorized_matching
->name
, pkg_get_string(priorized_matching
, PKG_VERSION
),
503 pkg_get_architecture(priorized_matching
));
504 return priorized_matching
;
507 opkg_msg(INFO
, "No matching pkg out of %d matching_apkgs.\n",
511 if (latest_matching
) {
512 opkg_msg(INFO
, "Using latest matching %s %s %s.\n",
513 latest_matching
->name
, pkg_get_string(latest_matching
, PKG_VERSION
),
514 pkg_get_architecture(latest_matching
));
515 return latest_matching
;
520 static int pkg_name_constraint_fcn(pkg_t
* pkg
, void *cdata
)
522 const char *name
= (const char *)cdata
;
524 if (strcmp(pkg
->name
, name
) == 0)
530 static pkg_vec_t
*pkg_vec_fetch_by_name(const char *pkg_name
)
532 abstract_pkg_t
*ab_pkg
;
534 if (!(ab_pkg
= abstract_pkg_fetch_by_name(pkg_name
)))
540 if (ab_pkg
->provided_by
) {
541 abstract_pkg_t
*abpkg
=
542 abstract_pkg_vec_get(ab_pkg
->provided_by
, 0);
552 pkg_t
*pkg_hash_fetch_best_installation_candidate_by_name(const char *name
)
554 abstract_pkg_t
*apkg
= NULL
;
556 if (!(apkg
= abstract_pkg_fetch_by_name(name
)))
559 return pkg_hash_fetch_best_installation_candidate(apkg
,
560 pkg_name_constraint_fcn
,
564 pkg_t
*pkg_hash_fetch_by_name_version(const char *pkg_name
, const char *version
)
568 char *version_str
= NULL
;
570 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
)))
573 for (i
= 0; i
< vec
->len
; i
++) {
574 version_str
= pkg_version_str_alloc(vec
->pkgs
[i
]);
575 if (!strcmp(version_str
, version
)) {
588 pkg_t
*pkg_hash_fetch_installed_by_name_dest(const char *pkg_name
,
594 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
))) {
598 for (i
= 0; i
< vec
->len
; i
++)
599 if ((vec
->pkgs
[i
]->state_status
== SS_INSTALLED
600 || vec
->pkgs
[i
]->state_status
== SS_UNPACKED
)
601 && vec
->pkgs
[i
]->dest
== dest
) {
608 pkg_t
*pkg_hash_fetch_installed_by_name(const char *pkg_name
)
613 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
))) {
617 for (i
= 0; i
< vec
->len
; i
++) {
618 if (vec
->pkgs
[i
]->state_status
== SS_INSTALLED
619 || vec
->pkgs
[i
]->state_status
== SS_UNPACKED
) {
628 pkg_hash_fetch_available_helper(const char *pkg_name
, void *entry
, void *data
)
631 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*) entry
;
632 pkg_vec_t
*all
= (pkg_vec_t
*) data
;
633 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
638 for (j
= 0; j
< pkg_vec
->len
; j
++) {
639 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
640 pkg_vec_insert(all
, pkg
);
644 void pkg_hash_fetch_available(pkg_vec_t
* all
)
646 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_fetch_available_helper
,
651 pkg_hash_fetch_all_installed_helper(const char *pkg_name
, void *entry
,
654 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*) entry
;
655 pkg_vec_t
*all
= (pkg_vec_t
*) data
;
656 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
662 for (j
= 0; j
< pkg_vec
->len
; j
++) {
663 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
664 if (pkg
->state_status
== SS_INSTALLED
665 || pkg
->state_status
== SS_UNPACKED
)
666 pkg_vec_insert(all
, pkg
);
670 void pkg_hash_fetch_all_installed(pkg_vec_t
* all
)
672 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_fetch_all_installed_helper
,
677 * This assumes that the abstract pkg doesn't exist.
679 static abstract_pkg_t
*add_new_abstract_pkg_by_name(const char *pkg_name
)
681 abstract_pkg_t
*ab_pkg
;
683 ab_pkg
= abstract_pkg_new();
685 ab_pkg
->name
= xstrdup(pkg_name
);
686 hash_table_insert(&conf
->pkg_hash
, pkg_name
, ab_pkg
);
691 abstract_pkg_t
*ensure_abstract_pkg_by_name(const char *pkg_name
)
693 abstract_pkg_t
*ab_pkg
;
695 if (!(ab_pkg
= abstract_pkg_fetch_by_name(pkg_name
)))
696 ab_pkg
= add_new_abstract_pkg_by_name(pkg_name
);
701 void hash_insert_pkg(pkg_t
* pkg
, int set_status
)
703 abstract_pkg_t
*ab_pkg
;
705 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
707 ab_pkg
->pkgs
= pkg_vec_alloc();
709 if (pkg
->state_status
== SS_INSTALLED
) {
710 ab_pkg
->state_status
= SS_INSTALLED
;
711 } else if (pkg
->state_status
== SS_UNPACKED
) {
712 ab_pkg
->state_status
= SS_UNPACKED
;
717 buildProvides(ab_pkg
, pkg
);
719 init_providelist(pkg
, NULL
);
721 /* Need to build the conflicts graph before replaces for correct
722 * calculation of replaced_by relation.
726 buildReplaces(ab_pkg
, pkg
);
728 buildDependedUponBy(pkg
, ab_pkg
);
730 pkg_vec_insert_merge(ab_pkg
->pkgs
, pkg
, set_status
);
731 pkg
->parent
= ab_pkg
;
734 static const char *strip_offline_root(const char *file_name
)
738 if (conf
->offline_root
) {
739 len
= strlen(conf
->offline_root
);
740 if (strncmp(file_name
, conf
->offline_root
, len
) == 0)
747 void file_hash_remove(const char *file_name
)
749 file_name
= strip_offline_root(file_name
);
750 hash_table_remove(&conf
->file_hash
, file_name
);
753 pkg_t
*file_hash_get_file_owner(const char *file_name
)
755 file_name
= strip_offline_root(file_name
);
756 return hash_table_get(&conf
->file_hash
, file_name
);
759 void file_hash_set_file_owner(const char *file_name
, pkg_t
* owning_pkg
)
761 pkg_t
*old_owning_pkg
;
762 int file_name_len
= strlen(file_name
);
764 if (file_name
[file_name_len
- 1] == '/')
767 file_name
= strip_offline_root(file_name
);
769 old_owning_pkg
= hash_table_get(&conf
->file_hash
, file_name
);
770 hash_table_insert(&conf
->file_hash
, file_name
, owning_pkg
);
772 if (old_owning_pkg
) {
773 pkg_get_installed_files(old_owning_pkg
);
774 str_list_remove_elt(old_owning_pkg
->installed_files
, file_name
);
775 pkg_free_installed_files(old_owning_pkg
);
777 /* mark this package to have its filelist written */
778 old_owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;
779 owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;