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