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