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