treewrite: use Lindent to reformat to kernel coding style
[project/opkg-lede.git] / libopkg / pkg_hash.c
1 /* opkg_hash.c - the opkg package management system
2
3 Steven M. Ayer
4
5 Copyright (C) 2002 Compaq Computer Corporation
6
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.
11
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.
16 */
17
18 #include <stdio.h>
19
20 #include "hash_table.h"
21 #include "pkg.h"
22 #include "opkg_message.h"
23 #include "pkg_vec.h"
24 #include "pkg_hash.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"
32
33 void pkg_hash_init(void)
34 {
35 hash_table_init("pkg-hash", &conf->pkg_hash,
36 OPKG_CONF_DEFAULT_HASH_LEN);
37 }
38
39 static void free_pkgs(const char *key, void *entry, void *data)
40 {
41 int i;
42 abstract_pkg_t *ab_pkg;
43
44 /* Each entry in the hash table is an abstract package, which contains
45 * a list of packages that provide the abstract package.
46 */
47
48 ab_pkg = (abstract_pkg_t *) entry;
49
50 if (ab_pkg->pkgs) {
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]);
54 }
55 }
56
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);
61 free(ab_pkg->name);
62 free(ab_pkg);
63 }
64
65 void pkg_hash_deinit(void)
66 {
67 hash_table_foreach(&conf->pkg_hash, free_pkgs, NULL);
68 hash_table_deinit(&conf->pkg_hash);
69 }
70
71 int dist_hash_add_from_file(const char *lists_dir, pkg_src_t * dist)
72 {
73 nv_pair_list_elt_t *l;
74 char *list_file, *subname;
75
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);
80
81 if (file_exists(list_file)) {
82 if (pkg_hash_add_from_file(list_file, dist, NULL, 0)) {
83 free(list_file);
84 return -1;
85 }
86 pkg_src_list_append(&conf->pkg_src_list, subname,
87 dist->value, "__dummy__", 0);
88 }
89
90 free(list_file);
91 }
92
93 return 0;
94 }
95
96 int
97 pkg_hash_add_from_file(const char *file_name,
98 pkg_src_t * src, pkg_dest_t * dest, int is_status_file)
99 {
100 pkg_t *pkg;
101 FILE *fp;
102 char *buf;
103 const size_t len = 4096;
104 int ret = 0;
105 struct gzip_handle zh;
106
107 if (src && src->gzip) {
108 fp = gzip_fdopen(&zh, file_name);
109 } else {
110 fp = fopen(file_name, "r");
111 }
112
113 if (fp == NULL) {
114 opkg_perror(ERROR, "Failed to open %s", file_name);
115 return -1;
116 }
117
118 buf = xmalloc(len);
119
120 do {
121 pkg = pkg_new();
122 pkg->src = src;
123 pkg->dest = dest;
124
125 ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
126 &buf, len);
127
128 if (pkg->name == NULL) {
129 /* probably just a blank line */
130 ret = 1;
131 }
132
133 if (ret) {
134 pkg_deinit(pkg);
135 free(pkg);
136 if (ret == -1)
137 break;
138 if (ret == 1)
139 /* Probably a blank line, continue parsing. */
140 ret = 0;
141 continue;
142 }
143
144 if (!pkg->architecture || !pkg->arch_priority) {
145 char *version_str = pkg_version_str_alloc(pkg);
146 opkg_msg(NOTICE, "Package %s version %s has no "
147 "valid architecture, ignoring.\n",
148 pkg->name, version_str);
149 free(version_str);
150 continue;
151 }
152
153 hash_insert_pkg(pkg, is_status_file);
154
155 } while (!feof(fp));
156
157 free(buf);
158 fclose(fp);
159
160 if (src && src->gzip)
161 gzip_close(&zh);
162
163 return ret;
164 }
165
166 /*
167 * Load in feed files from the cached "src" and/or "src/gz" locations.
168 */
169 int pkg_hash_load_feeds(void)
170 {
171 pkg_src_list_elt_t *iter;
172 pkg_src_t *src, *subdist;
173 char *list_file, *lists_dir;
174
175 opkg_msg(INFO, "\n");
176
177 lists_dir = conf->restrict_to_default_dest ?
178 conf->default_dest->lists_dir : conf->lists_dir;
179
180 for (iter = void_list_first(&conf->pkg_src_list); iter;
181 iter = void_list_next(&conf->pkg_src_list, iter)) {
182
183 src = (pkg_src_t *) iter->data;
184
185 sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
186
187 if (file_exists(list_file)) {
188 if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
189 free(list_file);
190 return -1;
191 }
192 }
193 free(list_file);
194 }
195
196 return 0;
197 }
198
199 /*
200 * Load in status files from the configured "dest"s.
201 */
202 int pkg_hash_load_status_files(void)
203 {
204 pkg_dest_list_elt_t *iter;
205 pkg_dest_t *dest;
206
207 opkg_msg(INFO, "\n");
208
209 for (iter = void_list_first(&conf->pkg_dest_list); iter;
210 iter = void_list_next(&conf->pkg_dest_list, iter)) {
211
212 dest = (pkg_dest_t *) iter->data;
213
214 if (file_exists(dest->status_file_name)) {
215 if (pkg_hash_add_from_file
216 (dest->status_file_name, NULL, dest, 1))
217 return -1;
218 }
219 }
220
221 return 0;
222 }
223
224 static abstract_pkg_t *abstract_pkg_fetch_by_name(const char *pkg_name)
225 {
226 return (abstract_pkg_t *) hash_table_get(&conf->pkg_hash, pkg_name);
227 }
228
229 pkg_t *pkg_hash_fetch_best_installation_candidate(abstract_pkg_t * apkg,
230 int (*constraint_fcn) (pkg_t *
231 pkg,
232 void
233 *cdata),
234 void *cdata, int quiet)
235 {
236 int i, j;
237 int nprovides = 0;
238 int nmatching = 0;
239 int wrong_arch_found = 0;
240 pkg_vec_t *matching_pkgs;
241 abstract_pkg_vec_t *matching_apkgs;
242 abstract_pkg_vec_t *provided_apkg_vec;
243 abstract_pkg_t **provided_apkgs;
244 abstract_pkg_vec_t *providers;
245 pkg_t *latest_installed_parent = NULL;
246 pkg_t *latest_matching = NULL;
247 pkg_t *priorized_matching = NULL;
248 pkg_t *held_pkg = NULL;
249 pkg_t *good_pkg_by_name = NULL;
250
251 if (apkg == NULL || apkg->provided_by == NULL
252 || (apkg->provided_by->len == 0))
253 return NULL;
254
255 matching_pkgs = pkg_vec_alloc();
256 matching_apkgs = abstract_pkg_vec_alloc();
257 providers = abstract_pkg_vec_alloc();
258
259 opkg_msg(DEBUG, "Best installation candidate for %s:\n", apkg->name);
260
261 provided_apkg_vec = apkg->provided_by;
262 nprovides = provided_apkg_vec->len;
263 provided_apkgs = provided_apkg_vec->pkgs;
264 if (nprovides > 1)
265 opkg_msg(DEBUG, "apkg=%s nprovides=%d.\n", apkg->name,
266 nprovides);
267
268 /* accumulate all the providers */
269 for (i = 0; i < nprovides; i++) {
270 abstract_pkg_t *provider_apkg = provided_apkgs[i];
271 opkg_msg(DEBUG, "Adding %s to providers.\n",
272 provider_apkg->name);
273 abstract_pkg_vec_insert(providers, provider_apkg);
274 }
275 nprovides = providers->len;
276
277 for (i = 0; i < nprovides; i++) {
278 abstract_pkg_t *provider_apkg =
279 abstract_pkg_vec_get(providers, i);
280 abstract_pkg_t *replacement_apkg = NULL;
281 pkg_vec_t *vec;
282
283 if (provider_apkg->replaced_by
284 && provider_apkg->replaced_by->len) {
285 replacement_apkg = provider_apkg->replaced_by->pkgs[0];
286 if (provider_apkg->replaced_by->len > 1) {
287 opkg_msg(NOTICE, "Multiple replacers for %s, "
288 "using first one (%s).\n",
289 provider_apkg->name,
290 replacement_apkg->name);
291 }
292 }
293
294 if (replacement_apkg)
295 opkg_msg(DEBUG,
296 "replacement_apkg=%s for provider_apkg=%s.\n",
297 replacement_apkg->name, provider_apkg->name);
298
299 if (replacement_apkg && (replacement_apkg != provider_apkg)) {
300 if (abstract_pkg_vec_contains
301 (providers, replacement_apkg))
302 continue;
303 else
304 provider_apkg = replacement_apkg;
305 }
306
307 if (!(vec = provider_apkg->pkgs)) {
308 opkg_msg(DEBUG, "No pkgs for provider_apkg %s.\n",
309 provider_apkg->name);
310 continue;
311 }
312
313 /* now check for supported architecture */
314 {
315 int max_count = 0;
316
317 /* count packages matching max arch priority and keep track of last one */
318 for (j = 0; j < vec->len; j++) {
319 pkg_t *maybe = vec->pkgs[j];
320 opkg_msg(DEBUG,
321 "%s arch=%s arch_priority=%d version=%s.\n",
322 maybe->name, maybe->architecture,
323 maybe->arch_priority, maybe->version);
324 /* We make sure not to add the same package twice. Need to search for the reason why
325 they show up twice sometimes. */
326 if ((maybe->arch_priority > 0)
327 &&
328 (!pkg_vec_contains(matching_pkgs, maybe))) {
329 max_count++;
330 abstract_pkg_vec_insert(matching_apkgs,
331 maybe->parent);
332 pkg_vec_insert(matching_pkgs, maybe);
333 }
334 }
335
336 if (vec->len > 0 && matching_pkgs->len < 1)
337 wrong_arch_found = 1;
338 }
339 }
340
341 if (matching_pkgs->len < 1) {
342 if (wrong_arch_found)
343 opkg_msg(ERROR, "Packages for %s found, but"
344 " incompatible with the architectures configured\n",
345 apkg->name);
346 pkg_vec_free(matching_pkgs);
347 abstract_pkg_vec_free(matching_apkgs);
348 abstract_pkg_vec_free(providers);
349 return NULL;
350 }
351
352 if (matching_pkgs->len > 1)
353 pkg_vec_sort(matching_pkgs,
354 pkg_name_version_and_architecture_compare);
355 if (matching_apkgs->len > 1)
356 abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
357
358 for (i = 0; i < matching_pkgs->len; i++) {
359 pkg_t *matching = matching_pkgs->pkgs[i];
360 if (constraint_fcn(matching, cdata)) {
361 opkg_msg(DEBUG, "Candidate: %s %s.\n",
362 matching->name, matching->version);
363 good_pkg_by_name = matching;
364 /* It has been provided by hand, so it is what user want */
365 if (matching->provided_by_hand == 1)
366 break;
367 }
368 }
369
370 for (i = 0; i < matching_pkgs->len; i++) {
371 pkg_t *matching = matching_pkgs->pkgs[i];
372 latest_matching = matching;
373 if (matching->parent->state_status == SS_INSTALLED
374 || matching->parent->state_status == SS_UNPACKED)
375 latest_installed_parent = matching;
376 if (matching->state_flag & (SF_HOLD | SF_PREFER)) {
377 if (held_pkg)
378 opkg_msg(NOTICE,
379 "Multiple packages (%s and %s) providing"
380 " same name marked HOLD or PREFER. "
381 "Using latest.\n", held_pkg->name,
382 matching->name);
383 held_pkg = matching;
384 }
385 }
386
387 if (!good_pkg_by_name && !held_pkg && !latest_installed_parent
388 && matching_apkgs->len > 1 && !quiet) {
389 int prio = 0;
390 for (i = 0; i < matching_pkgs->len; i++) {
391 pkg_t *matching = matching_pkgs->pkgs[i];
392 if (matching->arch_priority > prio) {
393 priorized_matching = matching;
394 prio = matching->arch_priority;
395 opkg_msg(DEBUG, "Match %s with priority %i.\n",
396 matching->name, prio);
397 }
398 }
399
400 }
401
402 if (conf->verbosity >= INFO && matching_apkgs->len > 1) {
403 opkg_msg(INFO, "%d matching pkgs for apkg=%s:\n",
404 matching_pkgs->len, apkg->name);
405 for (i = 0; i < matching_pkgs->len; i++) {
406 pkg_t *matching = matching_pkgs->pkgs[i];
407 opkg_msg(INFO, "%s %s %s\n",
408 matching->name, matching->version,
409 matching->architecture);
410 }
411 }
412
413 nmatching = matching_apkgs->len;
414
415 pkg_vec_free(matching_pkgs);
416 abstract_pkg_vec_free(matching_apkgs);
417 abstract_pkg_vec_free(providers);
418
419 if (good_pkg_by_name) { /* We found a good candidate, we will install it */
420 return good_pkg_by_name;
421 }
422 if (held_pkg) {
423 opkg_msg(INFO, "Using held package %s.\n", held_pkg->name);
424 return held_pkg;
425 }
426 if (latest_installed_parent) {
427 opkg_msg(INFO,
428 "Using latest version of installed package %s.\n",
429 latest_installed_parent->name);
430 return latest_installed_parent;
431 }
432 if (priorized_matching) {
433 opkg_msg(INFO, "Using priorized matching %s %s %s.\n",
434 priorized_matching->name, priorized_matching->version,
435 priorized_matching->architecture);
436 return priorized_matching;
437 }
438 if (nmatching > 1) {
439 opkg_msg(INFO, "No matching pkg out of %d matching_apkgs.\n",
440 nmatching);
441 return NULL;
442 }
443 if (latest_matching) {
444 opkg_msg(INFO, "Using latest matching %s %s %s.\n",
445 latest_matching->name, latest_matching->version,
446 latest_matching->architecture);
447 return latest_matching;
448 }
449 return NULL;
450 }
451
452 static int pkg_name_constraint_fcn(pkg_t * pkg, void *cdata)
453 {
454 const char *name = (const char *)cdata;
455
456 if (strcmp(pkg->name, name) == 0)
457 return 1;
458 else
459 return 0;
460 }
461
462 static pkg_vec_t *pkg_vec_fetch_by_name(const char *pkg_name)
463 {
464 abstract_pkg_t *ab_pkg;
465
466 if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
467 return NULL;
468
469 if (ab_pkg->pkgs)
470 return ab_pkg->pkgs;
471
472 if (ab_pkg->provided_by) {
473 abstract_pkg_t *abpkg =
474 abstract_pkg_vec_get(ab_pkg->provided_by, 0);
475 if (abpkg != NULL)
476 return abpkg->pkgs;
477 else
478 return ab_pkg->pkgs;
479 }
480
481 return NULL;
482 }
483
484 pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(const char *name)
485 {
486 abstract_pkg_t *apkg = NULL;
487
488 if (!(apkg = abstract_pkg_fetch_by_name(name)))
489 return NULL;
490
491 return pkg_hash_fetch_best_installation_candidate(apkg,
492 pkg_name_constraint_fcn,
493 apkg->name, 0);
494 }
495
496 pkg_t *pkg_hash_fetch_by_name_version(const char *pkg_name, const char *version)
497 {
498 pkg_vec_t *vec;
499 int i;
500 char *version_str = NULL;
501
502 if (!(vec = pkg_vec_fetch_by_name(pkg_name)))
503 return NULL;
504
505 for (i = 0; i < vec->len; i++) {
506 version_str = pkg_version_str_alloc(vec->pkgs[i]);
507 if (!strcmp(version_str, version)) {
508 free(version_str);
509 break;
510 }
511 free(version_str);
512 }
513
514 if (i == vec->len)
515 return NULL;
516
517 return vec->pkgs[i];
518 }
519
520 pkg_t *pkg_hash_fetch_installed_by_name_dest(const char *pkg_name,
521 pkg_dest_t * dest)
522 {
523 pkg_vec_t *vec;
524 int i;
525
526 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
527 return NULL;
528 }
529
530 for (i = 0; i < vec->len; i++)
531 if ((vec->pkgs[i]->state_status == SS_INSTALLED
532 || vec->pkgs[i]->state_status == SS_UNPACKED)
533 && vec->pkgs[i]->dest == dest) {
534 return vec->pkgs[i];
535 }
536
537 return NULL;
538 }
539
540 pkg_t *pkg_hash_fetch_installed_by_name(const char *pkg_name)
541 {
542 pkg_vec_t *vec;
543 int i;
544
545 if (!(vec = pkg_vec_fetch_by_name(pkg_name))) {
546 return NULL;
547 }
548
549 for (i = 0; i < vec->len; i++) {
550 if (vec->pkgs[i]->state_status == SS_INSTALLED
551 || vec->pkgs[i]->state_status == SS_UNPACKED) {
552 return vec->pkgs[i];
553 }
554 }
555
556 return NULL;
557 }
558
559 static void
560 pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
561 {
562 int j;
563 abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
564 pkg_vec_t *all = (pkg_vec_t *) data;
565 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
566
567 if (!pkg_vec)
568 return;
569
570 for (j = 0; j < pkg_vec->len; j++) {
571 pkg_t *pkg = pkg_vec->pkgs[j];
572 pkg_vec_insert(all, pkg);
573 }
574 }
575
576 void pkg_hash_fetch_available(pkg_vec_t * all)
577 {
578 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_available_helper,
579 all);
580 }
581
582 static void
583 pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry,
584 void *data)
585 {
586 abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
587 pkg_vec_t *all = (pkg_vec_t *) data;
588 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
589 int j;
590
591 if (!pkg_vec)
592 return;
593
594 for (j = 0; j < pkg_vec->len; j++) {
595 pkg_t *pkg = pkg_vec->pkgs[j];
596 if (pkg->state_status == SS_INSTALLED
597 || pkg->state_status == SS_UNPACKED)
598 pkg_vec_insert(all, pkg);
599 }
600 }
601
602 void pkg_hash_fetch_all_installed(pkg_vec_t * all)
603 {
604 hash_table_foreach(&conf->pkg_hash, pkg_hash_fetch_all_installed_helper,
605 all);
606 }
607
608 /*
609 * This assumes that the abstract pkg doesn't exist.
610 */
611 static abstract_pkg_t *add_new_abstract_pkg_by_name(const char *pkg_name)
612 {
613 abstract_pkg_t *ab_pkg;
614
615 ab_pkg = abstract_pkg_new();
616
617 ab_pkg->name = xstrdup(pkg_name);
618 hash_table_insert(&conf->pkg_hash, pkg_name, ab_pkg);
619
620 return ab_pkg;
621 }
622
623 abstract_pkg_t *ensure_abstract_pkg_by_name(const char *pkg_name)
624 {
625 abstract_pkg_t *ab_pkg;
626
627 if (!(ab_pkg = abstract_pkg_fetch_by_name(pkg_name)))
628 ab_pkg = add_new_abstract_pkg_by_name(pkg_name);
629
630 return ab_pkg;
631 }
632
633 void hash_insert_pkg(pkg_t * pkg, int set_status)
634 {
635 abstract_pkg_t *ab_pkg;
636
637 ab_pkg = ensure_abstract_pkg_by_name(pkg->name);
638 if (!ab_pkg->pkgs)
639 ab_pkg->pkgs = pkg_vec_alloc();
640
641 if (pkg->state_status == SS_INSTALLED) {
642 ab_pkg->state_status = SS_INSTALLED;
643 } else if (pkg->state_status == SS_UNPACKED) {
644 ab_pkg->state_status = SS_UNPACKED;
645 }
646
647 buildDepends(pkg);
648
649 buildProvides(ab_pkg, pkg);
650
651 /* Need to build the conflicts graph before replaces for correct
652 * calculation of replaced_by relation.
653 */
654 buildConflicts(pkg);
655
656 buildReplaces(ab_pkg, pkg);
657
658 buildDependedUponBy(pkg, ab_pkg);
659
660 pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status);
661 pkg->parent = ab_pkg;
662 }
663
664 static const char *strip_offline_root(const char *file_name)
665 {
666 unsigned int len;
667
668 if (conf->offline_root) {
669 len = strlen(conf->offline_root);
670 if (strncmp(file_name, conf->offline_root, len) == 0)
671 file_name += len;
672 }
673
674 return file_name;
675 }
676
677 void file_hash_remove(const char *file_name)
678 {
679 file_name = strip_offline_root(file_name);
680 hash_table_remove(&conf->file_hash, file_name);
681 }
682
683 pkg_t *file_hash_get_file_owner(const char *file_name)
684 {
685 file_name = strip_offline_root(file_name);
686 return hash_table_get(&conf->file_hash, file_name);
687 }
688
689 void file_hash_set_file_owner(const char *file_name, pkg_t * owning_pkg)
690 {
691 pkg_t *old_owning_pkg;
692 int file_name_len = strlen(file_name);
693
694 if (file_name[file_name_len - 1] == '/')
695 return;
696
697 file_name = strip_offline_root(file_name);
698
699 old_owning_pkg = hash_table_get(&conf->file_hash, file_name);
700 hash_table_insert(&conf->file_hash, file_name, owning_pkg);
701
702 if (old_owning_pkg) {
703 pkg_get_installed_files(old_owning_pkg);
704 str_list_remove_elt(old_owning_pkg->installed_files, file_name);
705 pkg_free_installed_files(old_owning_pkg);
706
707 /* mark this package to have its filelist written */
708 old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
709 owning_pkg->state_flag |= SF_FILELIST_CHANGED;
710 }
711 }