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.
24 #include "hash_table.h"
26 #include "opkg_message.h"
29 #include "pkg_parse.h"
30 #include "opkg_utils.h"
32 static abstract_pkg_t
* add_new_abstract_pkg_by_name(hash_table_t
* hash
, const char * pkg_name
);
35 * this will talk to both feeds-lists files and installed status files
39 * pkg_hash_init(name, &hash, 1000);
40 * pkg_hash_add_from_file(<feed filename>);
42 * the query function is just there as a shell to prove to me that this
43 * sort of works, but isn't far from doing something useful
46 * modified: CDW 3 Jan. 2002
50 int pkg_hash_init(const char *name
, hash_table_t
*hash
, int len
)
52 return hash_table_init(name
, hash
, len
);
55 void free_pkgs (const char *key
, void *entry
, void *data
)
58 abstract_pkg_t
*ab_pkg
;
60 /* each entry in the hash table is an abstract package, which contains a list
61 * of packages that provide the abstract package */
63 ab_pkg
= (abstract_pkg_t
*) entry
;
67 for (i
= 0; i
< ab_pkg
->pkgs
->len
; i
++)
69 pkg_deinit (ab_pkg
->pkgs
->pkgs
[i
]);
70 free (ab_pkg
->pkgs
->pkgs
[i
]);
74 abstract_pkg_vec_free (ab_pkg
->provided_by
);
75 abstract_pkg_vec_free (ab_pkg
->replaced_by
);
76 pkg_vec_free (ab_pkg
->pkgs
);
77 free (ab_pkg
->depended_upon_by
);
82 void pkg_hash_deinit(hash_table_t
*hash
)
84 hash_table_foreach (hash
, free_pkgs
, NULL
);
85 hash_table_deinit(hash
);
89 /* Find the default arch for a given package status file if none is given. */
90 static char *pkg_get_default_arch(opkg_conf_t
*conf
)
92 nv_pair_list_elt_t
*l
;
93 char *def_arch
= HOST_CPU_STR
; /* Default arch */
94 int def_prio
= 0; /* Other archs override this */
96 l
= conf
->arch_list
.head
;
99 nv_pair_t
*nv
= l
->data
;
100 int priority
= strtol(nv
->value
, NULL
, 0);
102 /* Check if this arch has higher priority, and is valid */
103 if ((priority
> def_prio
) &&
104 (strcmp(nv
->name
, "all")) && (strcmp(nv
->name
, "noarch"))) {
105 /* Our new default */
112 return strdup(def_arch
);
115 int pkg_hash_add_from_file(opkg_conf_t
*conf
, const char *file_name
,
116 pkg_src_t
*src
, pkg_dest_t
*dest
, int is_status_file
)
118 hash_table_t
*hash
= &conf
->pkg_hash
;
123 raw
= raw_start
= read_raw_pkgs_from_file(file_name
);
127 while(*raw
){ /* don't worry, we'll increment raw in the parsing function */
132 if (pkg_parse_raw(pkg
, &raw
, src
, dest
) == 0) {
133 if (!pkg
->architecture
) {
134 char *version_str
= pkg_version_str_alloc(pkg
);
135 pkg
->architecture
= pkg_get_default_arch(conf
);
136 opkg_message(conf
, OPKG_ERROR
, "Package %s version %s has no architecture specified, defaulting to %s.\n",
137 pkg
->name
, version_str
, pkg
->architecture
);
140 hash_insert_pkg(hash
, pkg
, is_status_file
,conf
);
147 /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
148 memory after read_raw_pkgs_from_file */
157 abstract_pkg_t
* abstract_pkg_fetch_by_name(hash_table_t
* hash
, const char * pkg_name
)
159 return (abstract_pkg_t
*)hash_table_get(hash
, pkg_name
);
162 abstract_pkg_vec_t
*pkg_hash_fetch_all_installation_candidates(hash_table_t
*hash
, const char *name
)
164 abstract_pkg_t
*apkg
= abstract_pkg_fetch_by_name(hash
, name
);
167 return apkg
->provided_by
;
171 pkg_t
*pkg_hash_fetch_best_installation_candidate(opkg_conf_t
*conf
, abstract_pkg_t
*apkg
,
172 int (*constraint_fcn
)(pkg_t
*pkg
, void *cdata
), void *cdata
, int quiet
, int *err
)
177 pkg_vec_t
*matching_pkgs
= pkg_vec_alloc();
178 abstract_pkg_vec_t
*matching_apkgs
= abstract_pkg_vec_alloc();
179 abstract_pkg_vec_t
*provided_apkg_vec
;
180 abstract_pkg_t
**provided_apkgs
;
181 abstract_pkg_vec_t
*providers
= abstract_pkg_vec_alloc();
182 pkg_t
*latest_installed_parent
= NULL
;
183 pkg_t
*latest_matching
= NULL
;
184 pkg_t
*held_pkg
= NULL
;
185 pkg_t
*good_pkg_by_name
= NULL
;
190 if (matching_apkgs
== NULL
|| providers
== NULL
||
191 apkg
== NULL
|| apkg
->provided_by
== NULL
|| (apkg
->provided_by
->len
== 0))
194 opkg_message(conf
, OPKG_DEBUG
, "best installation candidate for %s\n", apkg
->name
);
196 provided_apkg_vec
= apkg
->provided_by
;
197 nprovides
= provided_apkg_vec
->len
;
198 provided_apkgs
= provided_apkg_vec
->pkgs
;
200 opkg_message(conf
, OPKG_DEBUG
, " apkg=%s nprovides=%d\n", apkg
->name
, nprovides
);
202 /* accumulate all the providers */
203 for (i
= 0; i
< nprovides
; i
++) {
204 abstract_pkg_t
*provider_apkg
= provided_apkgs
[i
];
205 opkg_message(conf
, OPKG_DEBUG
, " adding %s to providers\n", provider_apkg
->name
);
206 abstract_pkg_vec_insert(providers
, provider_apkg
);
208 nprovides
= providers
->len
;
210 for (i
= 0; i
< nprovides
; i
++) {
211 abstract_pkg_t
*provider_apkg
= abstract_pkg_vec_get(providers
, i
);
212 abstract_pkg_t
*replacement_apkg
= NULL
;
215 if (provider_apkg
->replaced_by
&& provider_apkg
->replaced_by
->len
) {
216 replacement_apkg
= provider_apkg
->replaced_by
->pkgs
[0];
217 if (provider_apkg
->replaced_by
->len
> 1) {
218 opkg_message(conf
, OPKG_NOTICE
, "Multiple replacers for %s, using first one (%s)\n",
219 provider_apkg
->name
, replacement_apkg
->name
);
223 if (replacement_apkg
)
224 opkg_message(conf
, OPKG_DEBUG
, " replacement_apkg=%s for provider_apkg=%s\n",
225 replacement_apkg
->name
, provider_apkg
->name
);
227 if (replacement_apkg
&& (replacement_apkg
!= provider_apkg
)) {
228 if (abstract_pkg_vec_contains(providers
, replacement_apkg
))
231 provider_apkg
= replacement_apkg
;
234 if (!(vec
= provider_apkg
->pkgs
)) {
235 opkg_message(conf
, OPKG_DEBUG
, " no pkgs for provider_apkg %s\n", provider_apkg
->name
);
240 /* now check for supported architecture */
245 /* count packages matching max arch priority and keep track of last one */
246 for (i
= 0; i
< vec
->len
; i
++) {
247 pkg_t
*maybe
= vec
->pkgs
[i
];
248 opkg_message(conf
, OPKG_DEBUG
, " %s arch=%s arch_priority=%d version=%s \n",
249 maybe
->name
, maybe
->architecture
, maybe
->arch_priority
, maybe
->version
);
250 if (maybe
->arch_priority
> 0) {
252 abstract_pkg_vec_insert(matching_apkgs
, maybe
->parent
);
253 pkg_vec_insert(matching_pkgs
, maybe
);
257 if (vec
->len
> 0 && matching_pkgs
->len
< 1)
259 opkg_message (conf
, OPKG_ERROR
, "Packages were found, but none compatible with the architectures configured\n");
261 *err
= OPKG_PKG_HAS_NO_AVAILABLE_ARCH
;
266 if (matching_pkgs
->len
> 1)
267 pkg_vec_sort(matching_pkgs
, pkg_name_version_and_architecture_compare
);
268 if (matching_apkgs
->len
> 1)
269 abstract_pkg_vec_sort(matching_pkgs
, abstract_pkg_name_compare
);
271 /* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
272 needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
273 /* The problem is what to do when there are more than a mathing package, with the same name and several version ?
274 Until now I always got the latest, but that breaks the downgrade option.
275 If I stop at the first one, I would probably miss the new ones
276 Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
277 or from a Packages feed.
278 It it is from a file it always need to be checked whatever version I have in feeds or everywhere, according to force-down or whatever options*/
281 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
282 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
283 if (constraint_fcn(matching
, cdata
)) { /* We found it */
284 opkg_message(conf
, OPKG_DEBUG
, " Found a valid candidate for the install: %s %s \n", matching
->name
, matching
->version
) ;
285 good_pkg_by_name
= matching
;
286 if ( matching
->provided_by_hand
== 1 ) /* It has been provided by hand, so it is what user want */
292 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
293 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
294 latest_matching
= matching
;
295 if (matching
->parent
->state_status
== SS_INSTALLED
|| matching
->parent
->state_status
== SS_UNPACKED
)
296 latest_installed_parent
= matching
;
297 if (matching
->state_flag
& (SF_HOLD
|SF_PREFER
)) {
299 opkg_message(conf
, OPKG_NOTICE
, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER. Using latest.\n",
300 held_pkg
->name
, matching
->name
);
305 if (!good_pkg_by_name
&& !held_pkg
&& !latest_installed_parent
&& matching_apkgs
->len
> 1 && !quiet
) {
306 opkg_message(conf
, OPKG_ERROR
, "Package=%s, %d matching providers\n",
307 apkg
->name
, matching_apkgs
->len
);
308 for (i
= 0; i
< matching_apkgs
->len
; i
++) {
309 abstract_pkg_t
*matching
= matching_apkgs
->pkgs
[i
];
310 opkg_message(conf
, OPKG_ERROR
, " %s\n", matching
->name
);
312 opkg_message(conf
, OPKG_ERROR
, "Please select one with opkg install or opkg flag prefer\n");
315 if (matching_apkgs
->len
> 1 && conf
->verbosity
> 1) {
316 opkg_message(conf
, OPKG_NOTICE
, "%s: for apkg=%s, %d matching pkgs\n",
317 __FUNCTION__
, apkg
->name
, matching_pkgs
->len
);
318 for (i
= 0; i
< matching_pkgs
->len
; i
++) {
319 pkg_t
*matching
= matching_pkgs
->pkgs
[i
];
320 opkg_message(conf
, OPKG_INFO
, " %s %s %s\n",
321 matching
->name
, matching
->version
, matching
->architecture
);
325 nmatching
= matching_apkgs
->len
;
327 pkg_vec_free(matching_pkgs
);
328 abstract_pkg_vec_free(matching_apkgs
);
329 abstract_pkg_vec_free(providers
);
331 if (good_pkg_by_name
) { /* We found a good candidate, we will install it */
332 return good_pkg_by_name
;
335 opkg_message(conf
, OPKG_INFO
, " using held package %s\n", held_pkg
->name
);
338 if (latest_installed_parent
) {
339 opkg_message(conf
, OPKG_INFO
, " using latest version of installed package %s\n", latest_installed_parent
->name
);
340 return latest_installed_parent
;
343 opkg_message(conf
, OPKG_INFO
, " no matching pkg out of matching_apkgs=%d\n", nmatching
);
346 if (latest_matching
) {
347 opkg_message(conf
, OPKG_INFO
, " using latest matching %s %s %s\n",
348 latest_matching
->name
, latest_matching
->version
, latest_matching
->architecture
);
349 return latest_matching
;
354 static int pkg_name_constraint_fcn(pkg_t
*pkg
, void *cdata
)
356 const char *name
= (const char *)cdata
;
357 if (strcmp(pkg
->name
, name
) == 0)
363 pkg_t
*pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t
*conf
, const char *name
, int *err
)
365 hash_table_t
*hash
= &conf
->pkg_hash
;
366 abstract_pkg_t
*apkg
= NULL
;
369 if (!(apkg
= abstract_pkg_fetch_by_name(hash
, name
)))
372 ret
= pkg_hash_fetch_best_installation_candidate(conf
, apkg
, pkg_name_constraint_fcn
, apkg
->name
, 0, err
);
378 pkg_t
* pkg_hash_fetch_by_name_version(hash_table_t
*hash
,
379 const char *pkg_name
,
380 const char * version
)
384 char *version_str
= NULL
;
386 if(!(vec
= pkg_vec_fetch_by_name(hash
, pkg_name
)))
389 for(i
= 0; i
< vec
->len
; i
++) {
390 version_str
= pkg_version_str_alloc(vec
->pkgs
[i
]);
391 if(!strcmp(version_str
, version
)) {
404 pkg_t
*pkg_hash_fetch_installed_by_name_dest(hash_table_t
*hash
,
405 const char *pkg_name
,
411 if(!(vec
= pkg_vec_fetch_by_name(hash
, pkg_name
))) {
415 for(i
= 0; i
< vec
->len
; i
++)
416 if((vec
->pkgs
[i
]->state_status
== SS_INSTALLED
|| vec
->pkgs
[i
]->state_status
== SS_UNPACKED
) && vec
->pkgs
[i
]->dest
== dest
) {
422 pkg_t
*pkg_hash_fetch_installed_by_name(hash_table_t
*hash
,
423 const char *pkg_name
)
428 if(!(vec
= pkg_vec_fetch_by_name(hash
, pkg_name
))){
432 for(i
= 0; i
< vec
->len
; i
++)
433 if (vec
->pkgs
[i
]->state_status
== SS_INSTALLED
|| vec
->pkgs
[i
]->state_status
== SS_UNPACKED
){
440 pkg_vec_t
*pkg_vec_fetch_by_name(hash_table_t
*hash
, const char *pkg_name
)
442 abstract_pkg_t
* ab_pkg
;
444 if(!(ab_pkg
= abstract_pkg_fetch_by_name(hash
, pkg_name
))){
450 } else if (ab_pkg
->provided_by
) {
451 abstract_pkg_t
*abpkg
= abstract_pkg_vec_get(ab_pkg
->provided_by
, 0);
463 static void pkg_hash_fetch_available_helper(const char *pkg_name
, void *entry
, void *data
)
466 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*)entry
;
467 pkg_vec_t
*all
= (pkg_vec_t
*)data
;
468 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
470 for (j
= 0; j
< pkg_vec
->len
; j
++) {
471 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
472 pkg_vec_insert(all
, pkg
);
477 void pkg_hash_fetch_available(hash_table_t
*hash
, pkg_vec_t
*all
)
479 hash_table_foreach(hash
, pkg_hash_fetch_available_helper
, all
);
480 //qsort(all->pkgs, all->len, sizeof(pkg_t *), pkg_compare_names);
483 static void pkg_hash_fetch_all_installed_helper(const char *pkg_name
, void *entry
, void *data
)
485 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*)entry
;
486 pkg_vec_t
*all
= (pkg_vec_t
*)data
;
487 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
490 for (j
= 0; j
< pkg_vec
->len
; j
++) {
491 pkg_t
*pkg
= pkg_vec
->pkgs
[j
];
492 if (pkg
->state_status
== SS_INSTALLED
|| pkg
->state_status
== SS_UNPACKED
) {
493 pkg_vec_insert(all
, pkg
);
498 void pkg_hash_fetch_all_installed(hash_table_t
*hash
, pkg_vec_t
*all
)
500 hash_table_foreach(hash
, pkg_hash_fetch_all_installed_helper
, all
);
501 //qsort(all->pkgs, all->len, sizeof(void*), pkg_compare_names);
504 static void pkg_hash_dump_helper(const char *pkg_name
, void *entry
, void *data
)
508 abstract_pkg_t
*ab_pkg
= (abstract_pkg_t
*)entry
;
509 opkg_conf_t
*conf
= (opkg_conf_t
*)data
;
510 abstract_pkg_t
** dependents
= ab_pkg
->depended_upon_by
;
511 fprintf(stdout
, "%s\n", ab_pkg
->name
);
513 if (dependents
!= NULL
)
514 while (dependents
[i
] != NULL
)
515 printf ("\tdepended upon by - %s\n", dependents
[i
++]->name
);
516 dependents
= ab_pkg
->provided_by
->pkgs
;
518 if (dependents
!= NULL
)
519 while (dependents
[i
] != NULL
&& i
< ab_pkg
->provided_by
->len
)
520 printf ("\tprovided by - %s\n", dependents
[i
++]->name
);
521 pkg
= pkg_hash_fetch_best_installation_candidate_by_name (conf
, ab_pkg
->name
, NULL
);
524 while (i
< pkg
->depends_count
)
525 printf ("\tdepends on - %s\n", pkg
->depends_str
[i
++]);
528 void pkg_hash_dump(hash_table_t
*hash
, void *data
)
531 printf ("\n\n+=+%s+=+\n\n", __FUNCTION__
);
532 hash_table_foreach(hash
, pkg_hash_dump_helper
, data
);
533 printf ("\n+=+%s+=+\n\n", __FUNCTION__
);
536 abstract_pkg_t
* ensure_abstract_pkg_by_name(hash_table_t
* hash
, const char * pkg_name
)
538 abstract_pkg_t
* ab_pkg
;
540 if(!(ab_pkg
= abstract_pkg_fetch_by_name(hash
, pkg_name
)))
541 ab_pkg
= add_new_abstract_pkg_by_name(hash
, pkg_name
);
546 pkg_t
*hash_insert_pkg(hash_table_t
*hash
, pkg_t
*pkg
, int set_status
,opkg_conf_t
*conf
)
548 abstract_pkg_t
* ab_pkg
;
554 arch_priority
= pkg
->arch_priority
;
556 if (buildDepends(hash
, pkg
)<0){
557 fprintf(stderr
, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__
);
560 ab_pkg
= ensure_abstract_pkg_by_name(hash
, pkg
->name
);
563 if (pkg
->state_status
== SS_INSTALLED
) {
564 ab_pkg
->state_status
= SS_INSTALLED
;
565 } else if (pkg
->state_status
== SS_UNPACKED
) {
566 ab_pkg
->state_status
= SS_UNPACKED
;
571 ab_pkg
->pkgs
= pkg_vec_alloc();
573 /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
574 pkg
= pkg_vec_insert_merge(ab_pkg
->pkgs
, pkg
, set_status
,conf
);
575 pkg
->parent
= ab_pkg
;
577 if (buildProvides(hash
, ab_pkg
, pkg
)<0){
578 fprintf(stderr
, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__
);
581 /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
582 if (buildConflicts(hash
, ab_pkg
, pkg
)<0){
583 fprintf(stderr
, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__
);
586 if (buildReplaces(hash
, ab_pkg
, pkg
)<0) {
587 fprintf(stderr
, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__
);
591 buildDependedUponBy(pkg
, ab_pkg
);
596 * this will assume that we've already determined that
597 * the abstract pkg doesn't exist, 'cause we should know these things...
599 static abstract_pkg_t
* add_new_abstract_pkg_by_name(hash_table_t
* hash
, const char * pkg_name
)
601 abstract_pkg_t
* ab_pkg
;
603 ab_pkg
= abstract_pkg_new();
604 if (ab_pkg
== NULL
) { return NULL
; }
606 ab_pkg
->name
= strdup(pkg_name
);
607 hash_table_insert(hash
, pkg_name
, ab_pkg
);
613 pkg_t
*file_hash_get_file_owner(opkg_conf_t
*conf
, const char *file_name
)
615 hash_table_t
*file_hash
= &conf
->file_hash
;
617 return hash_table_get(file_hash
, file_name
);
620 int file_hash_set_file_owner(opkg_conf_t
*conf
, const char *file_name
, pkg_t
*owning_pkg
)
622 hash_table_t
*file_hash
= &conf
->file_hash
;
623 pkg_t
*old_owning_pkg
= hash_table_get(file_hash
, file_name
);
624 int file_name_len
= strlen(file_name
);
626 if (file_name
[file_name_len
-1] == '/')
629 if (conf
->offline_root
) {
630 int len
= strlen(conf
->offline_root
);
631 if (strncmp(file_name
, conf
->offline_root
, len
) == 0) {
636 // opkg_message(conf, OPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
637 hash_table_insert(file_hash
, file_name
, owning_pkg
);
638 if (old_owning_pkg
) {
639 pkg_get_installed_files(old_owning_pkg
);
640 str_list_remove_elt(old_owning_pkg
->installed_files
, file_name
);
641 pkg_free_installed_files(old_owning_pkg
);
642 /* mark this package to have its filelist written */
643 old_owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;
644 owning_pkg
->state_flag
|= SF_FILELIST_CHANGED
;