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 "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"
37 hash_table_init("pkg-hash", &conf
->pkg_hash
,
38 OPKG_CONF_DEFAULT_HASH_LEN
);
42 free_pkgs(const char *key
, void *entry
, void *data
)
45 abstract_pkg_t
*ab_pkg
;
47 /* Each entry in the hash table is an abstract package, which contains
48 * a list of packages that provide the abstract package.
51 ab_pkg
= (abstract_pkg_t
*) entry
;
54 for (i
= 0; i
< ab_pkg
->pkgs
->len
; i
++) {
55 pkg_deinit (ab_pkg
->pkgs
->pkgs
[i
]);
56 free (ab_pkg
->pkgs
->pkgs
[i
]);
60 abstract_pkg_vec_free (ab_pkg
->provided_by
);
61 abstract_pkg_vec_free (ab_pkg
->replaced_by
);
62 pkg_vec_free (ab_pkg
->pkgs
);
63 free (ab_pkg
->depended_upon_by
);
71 hash_table_foreach(&conf
->pkg_hash
, free_pkgs
, NULL
);
72 hash_table_deinit(&conf
->pkg_hash
);
76 dist_hash_add_from_file(const char *lists_dir
, pkg_src_t
*dist
)
78 nv_pair_list_elt_t
*l
;
79 char *list_file
, *subname
;
81 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
82 nv_pair_t
*nv
= (nv_pair_t
*)l
->data
;
83 sprintf_alloc(&subname
, "%s-%s", dist
->name
, nv
->name
);
84 sprintf_alloc(&list_file
, "%s/%s", lists_dir
, subname
);
86 if (file_exists(list_file
)) {
87 if (pkg_hash_add_from_file(list_file
, dist
, NULL
, 0)) {
91 pkg_src_list_append (&conf
->pkg_src_list
, subname
, dist
->value
, "__dummy__", 0);
102 pkg_hash_add_from_file(const char *file_name
,
103 pkg_src_t
*src
, pkg_dest_t
*dest
, int is_status_file
)
108 const size_t len
= 4096;
110 struct gzip_handle zh
;
112 if (src
&& src
->gzip
) {
113 fp
= gzip_fdopen(&zh
, file_name
);
116 fp
= fopen(file_name
, "r");
120 opkg_perror(ERROR
, "Failed to open %s", file_name
);
131 ret
= parse_from_stream_nomalloc(pkg_parse_line
, pkg
, fp
, 0,
134 if (pkg
->name
== NULL
) {
135 /* probably just a blank line */
145 /* Probably a blank line, continue parsing. */
150 if (!pkg
->architecture
|| !pkg
->arch_priority
) {
151 char *version_str
= pkg_version_str_alloc(pkg
);
152 opkg_msg(NOTICE
, "Package %s version %s has no "
153 "valid architecture, ignoring.\n",
154 pkg
->name
, version_str
);
159 hash_insert_pkg(pkg
, is_status_file
);
166 if (src
&& src
->gzip
)
173 * Load in feed files from the cached "src" and/or "src/gz" locations.
176 pkg_hash_load_feeds(void)
178 pkg_src_list_elt_t
*iter
;
179 pkg_src_t
*src
, *subdist
;
180 char *list_file
, *lists_dir
;
182 opkg_msg(INFO
, "\n");
184 lists_dir
= conf
->restrict_to_default_dest
?
185 conf
->default_dest
->lists_dir
: conf
->lists_dir
;
187 for (iter
= void_list_first(&conf
->dist_src_list
); iter
;
188 iter
= void_list_next(&conf
->dist_src_list
, iter
)) {
190 src
= (pkg_src_t
*)iter
->data
;
192 sprintf_alloc(&list_file
, "%s/%s", lists_dir
, src
->name
);
194 if (file_exists(list_file
)) {
196 release_t
*release
= release_new();
197 if(release_init_from_file(release
, list_file
)) {
203 const char **comps
= release_comps(release
, &ncomp
);
204 subdist
= (pkg_src_t
*) xmalloc(sizeof(pkg_src_t
));
205 memcpy(subdist
, src
, sizeof(pkg_src_t
));
207 for(i
= 0; i
< ncomp
; i
++){
208 subdist
->name
= NULL
;
209 sprintf_alloc(&subdist
->name
, "%s-%s", src
->name
, comps
[i
]);
210 if (dist_hash_add_from_file(lists_dir
, subdist
)) {
211 free(subdist
->name
); free(subdist
);
216 free(subdist
->name
); free(subdist
);
221 for (iter
= void_list_first(&conf
->pkg_src_list
); iter
;
222 iter
= void_list_next(&conf
->pkg_src_list
, iter
)) {
224 src
= (pkg_src_t
*)iter
->data
;
226 sprintf_alloc(&list_file
, "%s/%s", lists_dir
, src
->name
);
228 if (file_exists(list_file
)) {
229 if (pkg_hash_add_from_file(list_file
, src
, NULL
, 0)) {
241 * Load in status files from the configured "dest"s.
244 pkg_hash_load_status_files(void)
246 pkg_dest_list_elt_t
*iter
;
249 opkg_msg(INFO
, "\n");
251 for (iter
= void_list_first(&conf
->pkg_dest_list
); iter
;
252 iter
= void_list_next(&conf
->pkg_dest_list
, iter
)) {
254 dest
= (pkg_dest_t
*)iter
->data
;
256 if (file_exists(dest
->status_file_name
)) {
257 if (pkg_hash_add_from_file(dest
->status_file_name
, NULL
, dest
, 1))
265 static abstract_pkg_t
*
266 abstract_pkg_fetch_by_name(const char * pkg_name
)
268 return (abstract_pkg_t
*)hash_table_get(&conf
->pkg_hash
, pkg_name
);
272 pkg_hash_fetch_best_installation_candidate(abstract_pkg_t
*apkg
,
273 int (*constraint_fcn
)(pkg_t
*pkg
, void *cdata
),
274 void *cdata
, int quiet
)
279 int wrong_arch_found
= 0;
280 pkg_vec_t
*matching_pkgs
;
281 abstract_pkg_vec_t
*matching_apkgs
;
282 abstract_pkg_vec_t
*provided_apkg_vec
;
283 abstract_pkg_t
**provided_apkgs
;
284 abstract_pkg_vec_t
*providers
;
285 pkg_t
*latest_installed_parent
= NULL
;
286 pkg_t
*latest_matching
= NULL
;
287 pkg_t
*priorized_matching
= NULL
;
288 pkg_t
*held_pkg
= NULL
;
289 pkg_t
*good_pkg_by_name
= NULL
;
291 if (apkg
== NULL
|| apkg
->provided_by
== NULL
|| (apkg
->provided_by
->len
== 0))
294 matching_pkgs
= pkg_vec_alloc();
295 matching_apkgs
= abstract_pkg_vec_alloc();
296 providers
= abstract_pkg_vec_alloc();
298 opkg_msg(DEBUG
, "Best installation candidate for %s:\n", apkg
->name
);
300 provided_apkg_vec
= apkg
->provided_by
;
301 nprovides
= provided_apkg_vec
->len
;
302 provided_apkgs
= provided_apkg_vec
->pkgs
;
304 opkg_msg(DEBUG
, "apkg=%s nprovides=%d.\n", apkg
->name
, nprovides
);
306 /* accumulate all the providers */
307 for (i
= 0; i
< nprovides
; i
++) {
308 abstract_pkg_t
*provider_apkg
= provided_apkgs
[i
];
309 opkg_msg(DEBUG
, "Adding %s to providers.\n", provider_apkg
->name
);
310 abstract_pkg_vec_insert(providers
, provider_apkg
);
312 nprovides
= providers
->len
;
314 for (i
= 0; i
< nprovides
; i
++) {
315 abstract_pkg_t
*provider_apkg
= abstract_pkg_vec_get(providers
, i
);
316 abstract_pkg_t
*replacement_apkg
= NULL
;
319 if (provider_apkg
->replaced_by
&& provider_apkg
->replaced_by
->len
) {
320 replacement_apkg
= provider_apkg
->replaced_by
->pkgs
[0];
321 if (provider_apkg
->replaced_by
->len
> 1) {
322 opkg_msg(NOTICE
, "Multiple replacers for %s, "
323 "using first one (%s).\n",
324 provider_apkg
->name
, replacement_apkg
->name
);
328 if (replacement_apkg
)
329 opkg_msg(DEBUG
, "replacement_apkg=%s for provider_apkg=%s.\n",
330 replacement_apkg
->name
, provider_apkg
->name
);
332 if (replacement_apkg
&& (replacement_apkg
!= provider_apkg
)) {
333 if (abstract_pkg_vec_contains(providers
, replacement_apkg
))
336 provider_apkg
= replacement_apkg
;
339 if (!(vec
= provider_apkg
->pkgs
)) {
340 opkg_msg(DEBUG
, "No pkgs for provider_apkg %s.\n",
341 provider_apkg
->name
);
346 /* now check for supported architecture */
350 /* count packages matching max arch priority and keep track of last one */
351 for (j
=0; j
<vec
->len
; j
++) {
352 pkg_t
*maybe
= vec
->pkgs
[j
];
353 opkg_msg(DEBUG
, "%s arch=%s arch_priority=%d version=%s.\n",
354 maybe
->name
, maybe
->architecture
,
355 maybe
->arch_priority
, maybe
->version
);
356 /* We make sure not to add the same package twice. Need to search for the reason why
357 they show up twice sometimes. */
358 if ((maybe
->arch_priority
> 0) && (! pkg_vec_contains(matching_pkgs
, maybe
))) {
360 abstract_pkg_vec_insert(matching_apkgs
, maybe
->parent
);
361 pkg_vec_insert(matching_pkgs
, maybe
);
365 if (vec
->len
> 0 && matching_pkgs
->len
< 1)
366 wrong_arch_found
= 1;
370 if (matching_pkgs
->len
< 1) {
371 if (wrong_arch_found
)
372 opkg_msg(ERROR
, "Packages for %s found, but"
373 " incompatible with the architectures configured\n",
375 pkg_vec_free(matching_pkgs
);
376 abstract_pkg_vec_free(matching_apkgs
);
377 abstract_pkg_vec_free(providers
);
382 if (matching_pkgs
->len
> 1)
383 pkg_vec_sort(matching_pkgs
, pkg_name_version_and_architecture_compare
);
384 if (matching_apkgs
->len
> 1)
385 abstract_pkg_vec_sort(matching_pkgs
, abstract_pkg_name_compare
);
387 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
388 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
389 if (constraint_fcn(matching
, cdata
)) {
390 opkg_msg(DEBUG
, "Candidate: %s %s.\n",
391 matching
->name
, matching
->version
) ;
392 good_pkg_by_name
= matching
;
393 /* It has been provided by hand, so it is what user want */
394 if (matching
->provided_by_hand
== 1)
400 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
401 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
402 latest_matching
= matching
;
403 if (matching
->parent
->state_status
== SS_INSTALLED
|| matching
->parent
->state_status
== SS_UNPACKED
)
404 latest_installed_parent
= matching
;
405 if (matching
->state_flag
& (SF_HOLD
|SF_PREFER
)) {
407 opkg_msg(NOTICE
, "Multiple packages (%s and %s) providing"
408 " same name marked HOLD or PREFER. "
410 held_pkg
->name
, matching
->name
);
415 if (!good_pkg_by_name
&& !held_pkg
&& !latest_installed_parent
&& matching_apkgs
->len
> 1 && !quiet
) {
417 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
418 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
419 if (matching
->arch_priority
> prio
) {
420 priorized_matching
= matching
;
421 prio
= matching
->arch_priority
;
422 opkg_msg(DEBUG
, "Match %s with priority %i.\n",
423 matching
->name
, prio
);
429 if (conf
->verbosity
>= INFO
&& matching_apkgs
->len
> 1) {
430 opkg_msg(INFO
, "%d matching pkgs for apkg=%s:\n",
431 matching_pkgs
->len
, apkg
->name
);
432 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
433 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
434 opkg_msg(INFO
, "%s %s %s\n",
435 matching
->name
, matching
->version
,
436 matching
->architecture
);
440 nmatching
= matching_apkgs
->len
;
442 pkg_vec_free(matching_pkgs
);
443 abstract_pkg_vec_free(matching_apkgs
);
444 abstract_pkg_vec_free(providers
);
446 if (good_pkg_by_name
) { /* We found a good candidate, we will install it */
447 return good_pkg_by_name
;
450 opkg_msg(INFO
, "Using held package %s.\n", held_pkg
->name
);
453 if (latest_installed_parent
) {
454 opkg_msg(INFO
, "Using latest version of installed package %s.\n",
455 latest_installed_parent
->name
);
456 return latest_installed_parent
;
458 if (priorized_matching
) {
459 opkg_msg(INFO
, "Using priorized matching %s %s %s.\n",
460 priorized_matching
->name
, priorized_matching
->version
,
461 priorized_matching
->architecture
);
462 return priorized_matching
;
465 opkg_msg(INFO
, "No matching pkg out of %d matching_apkgs.\n",
469 if (latest_matching
) {
470 opkg_msg(INFO
, "Using latest matching %s %s %s.\n",
471 latest_matching
->name
, latest_matching
->version
,
472 latest_matching
->architecture
);
473 return latest_matching
;
479 pkg_name_constraint_fcn(pkg_t
*pkg
, void *cdata
)
481 const char *name
= (const char *)cdata
;
483 if (strcmp(pkg
->name
, name
) == 0)
490 pkg_vec_fetch_by_name(const char *pkg_name
)
492 abstract_pkg_t
* ab_pkg
;
494 if(!(ab_pkg
= abstract_pkg_fetch_by_name(pkg_name
)))
500 if (ab_pkg
->provided_by
) {
501 abstract_pkg_t
*abpkg
= abstract_pkg_vec_get(ab_pkg
->provided_by
, 0);
513 pkg_hash_fetch_best_installation_candidate_by_name(const char *name
)
515 abstract_pkg_t
*apkg
= NULL
;
517 if (!(apkg
= abstract_pkg_fetch_by_name(name
)))
520 return pkg_hash_fetch_best_installation_candidate(apkg
,
521 pkg_name_constraint_fcn
, apkg
->name
, 0);
526 pkg_hash_fetch_by_name_version(const char *pkg_name
, const char * version
)
530 char *version_str
= NULL
;
532 if(!(vec
= pkg_vec_fetch_by_name(pkg_name
)))
535 for(i
= 0; i
< vec
->len
; i
++) {
536 version_str
= pkg_version_str_alloc(vec
->pkgs
[i
]);
537 if(!strcmp(version_str
, version
)) {
551 pkg_hash_fetch_installed_by_name_dest(const char *pkg_name
, pkg_dest_t
*dest
)
556 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
))) {
560 for (i
= 0; i
< vec
->len
; i
++)
561 if((vec
->pkgs
[i
]->state_status
== SS_INSTALLED
562 || vec
->pkgs
[i
]->state_status
== SS_UNPACKED
)
563 && vec
->pkgs
[i
]->dest
== dest
) {
571 pkg_hash_fetch_installed_by_name(const char *pkg_name
)
576 if (!(vec
= pkg_vec_fetch_by_name(pkg_name
))) {
580 for (i
= 0; i
< vec
->len
; i
++) {
581 if (vec
->pkgs
[i
]->state_status
== SS_INSTALLED
582 || vec
->pkgs
[i
]->state_status
== SS_UNPACKED
) {
592 pkg_hash_fetch_available_helper(const char *pkg_name
, void *entry
, void *data
)
595 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*)entry
;
596 pkg_vec_t
*all
= (pkg_vec_t
*)data
;
597 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
602 for (j
= 0; j
< pkg_vec
->len
; j
++) {
603 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
604 pkg_vec_insert(all
, pkg
);
609 pkg_hash_fetch_available(pkg_vec_t
*all
)
611 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_fetch_available_helper
,
616 pkg_hash_fetch_all_installed_helper(const char *pkg_name
, void *entry
, void *data
)
618 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*)entry
;
619 pkg_vec_t
*all
= (pkg_vec_t
*)data
;
620 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
626 for (j
= 0; j
< pkg_vec
->len
; j
++) {
627 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
628 if (pkg
->state_status
== SS_INSTALLED
629 || pkg
->state_status
== SS_UNPACKED
)
630 pkg_vec_insert(all
, pkg
);
635 pkg_hash_fetch_all_installed(pkg_vec_t
*all
)
637 hash_table_foreach(&conf
->pkg_hash
, pkg_hash_fetch_all_installed_helper
,
642 * This assumes that the abstract pkg doesn't exist.
644 static abstract_pkg_t
*
645 add_new_abstract_pkg_by_name(const char *pkg_name
)
647 abstract_pkg_t
*ab_pkg
;
649 ab_pkg
= abstract_pkg_new();
651 ab_pkg
->name
= xstrdup(pkg_name
);
652 hash_table_insert(&conf
->pkg_hash
, pkg_name
, ab_pkg
);
659 ensure_abstract_pkg_by_name(const char *pkg_name
)
661 abstract_pkg_t
* ab_pkg
;
663 if (!(ab_pkg
= abstract_pkg_fetch_by_name(pkg_name
)))
664 ab_pkg
= add_new_abstract_pkg_by_name(pkg_name
);
670 hash_insert_pkg(pkg_t
*pkg
, int set_status
)
672 abstract_pkg_t
* ab_pkg
;
674 ab_pkg
= ensure_abstract_pkg_by_name(pkg
->name
);
676 ab_pkg
->pkgs
= pkg_vec_alloc();
678 if (pkg
->state_status
== SS_INSTALLED
) {
679 ab_pkg
->state_status
= SS_INSTALLED
;
680 } else if (pkg
->state_status
== SS_UNPACKED
) {
681 ab_pkg
->state_status
= SS_UNPACKED
;
686 buildProvides(ab_pkg
, pkg
);
688 /* Need to build the conflicts graph before replaces for correct
689 * calculation of replaced_by relation.
693 buildReplaces(ab_pkg
, pkg
);
695 buildDependedUponBy(pkg
, ab_pkg
);
697 pkg_vec_insert_merge(ab_pkg
->pkgs
, pkg
, set_status
);
698 pkg
->parent
= ab_pkg
;
702 strip_offline_root(const char *file_name
)
706 if (conf
->offline_root
) {
707 len
= strlen(conf
->offline_root
);
708 if (strncmp(file_name
, conf
->offline_root
, len
) == 0)
716 file_hash_remove(const char *file_name
)
718 file_name
= strip_offline_root(file_name
);
719 hash_table_remove(&conf
->file_hash
, file_name
);
723 file_hash_get_file_owner(const char *file_name
)
725 file_name
= strip_offline_root(file_name
);
726 return hash_table_get(&conf
->file_hash
, file_name
);
730 file_hash_set_file_owner(const char *file_name
, pkg_t
*owning_pkg
)
732 pkg_t
*old_owning_pkg
;
733 int file_name_len
= strlen(file_name
);
735 if (file_name
[file_name_len
-1] == '/')
738 file_name
= strip_offline_root(file_name
);
740 old_owning_pkg
= hash_table_get(&conf
->file_hash
, file_name
);
741 hash_table_insert(&conf
->file_hash
, file_name
, owning_pkg
);
743 if (old_owning_pkg
) {
744 pkg_get_installed_files(old_owning_pkg
);
745 str_list_remove_elt(old_owning_pkg
->installed_files
, file_name
);
746 pkg_free_installed_files(old_owning_pkg
);
748 /* mark this package to have its filelist written */
749 old_owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;
750 owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;