Fix problems in error list
[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 "includes.h"
19 #include <errno.h>
20 #include <ctype.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "hash_table.h"
25 #include "pkg.h"
26 #include "opkg_message.h"
27 #include "pkg_vec.h"
28 #include "pkg_hash.h"
29 #include "pkg_parse.h"
30 #include "opkg_utils.h"
31
32 static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
33
34 /*
35  * this will talk to both feeds-lists files and installed status files
36  * example api:
37  *
38  * hash_table_t hash;
39  * pkg_hash_init(name, &hash, 1000);
40  * pkg_hash_add_from_file(<feed filename>);
41  *
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
44  * 
45  * -sma, 12/21/01
46  * modified: CDW 3 Jan. 2002
47  */
48
49
50 int pkg_hash_init(const char *name, hash_table_t *hash, int len)
51 {
52   return hash_table_init(name, hash, len);
53 }
54
55 void free_pkgs (const char *key, void *entry, void *data)
56 {
57   int i;
58   abstract_pkg_t *ab_pkg;
59
60   /* each entry in the hash table is an abstract package, which contains a list
61    * of packages that provide the abstract package */
62   
63   ab_pkg = (abstract_pkg_t*) entry;
64
65   if (ab_pkg->pkgs)
66   {
67     for (i = 0; i < ab_pkg->pkgs->len; i++)
68     {
69       pkg_deinit (ab_pkg->pkgs->pkgs[i]);
70       free (ab_pkg->pkgs->pkgs[i]);
71     }
72   }
73
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);
78   free (ab_pkg->name);
79   free (ab_pkg);
80 }
81
82 void pkg_hash_deinit(hash_table_t *hash)
83 {
84   hash_table_foreach (hash, free_pkgs, NULL);
85   hash_table_deinit(hash);
86 }
87
88
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)
91 {
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 */
95
96      list_for_each_entry(l , &conf->arch_list.head, node) {
97           nv_pair_t *nv = (nv_pair_t *)l->data;
98           int priority = strtol(nv->value, NULL, 0);
99
100           /* Check if this arch has higher priority, and is valid */
101           if ((priority > def_prio) &&
102               (strcmp(nv->name, "all")) && (strcmp(nv->name, "noarch"))) {
103                /* Our new default */
104                def_prio = priority;
105                def_arch = nv->name;
106           }
107      }
108
109      return strdup(def_arch);
110 }
111
112 int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
113                            pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
114 {
115      hash_table_t *hash = &conf->pkg_hash;
116      char **raw;
117      char **raw_start;
118      pkg_t *pkg;
119     
120      raw = raw_start = read_raw_pkgs_from_file(file_name);
121      if (!raw)
122         return -ENOMEM;
123
124      while(*raw){         /* don't worry, we'll increment raw in the parsing function */
125           pkg = pkg_new();
126           if (!pkg)
127                return -ENOMEM;
128
129           if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
130                if (!pkg->architecture) {
131                     char *version_str = pkg_version_str_alloc(pkg);
132                     pkg->architecture = pkg_get_default_arch(conf);
133                     opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
134                                  pkg->name, version_str, pkg->architecture);
135                     free(version_str);
136                }
137                hash_insert_pkg(hash, pkg, is_status_file,conf);
138           } else {
139                pkg_deinit (pkg);
140                free(pkg);
141           }
142      }
143
144      /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
145         memory after read_raw_pkgs_from_file */
146      raw = raw_start;
147      while (*raw) {
148           free(*raw++);
149      }
150      free(raw_start);
151      return 0;
152 }
153
154 abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
155 {
156   return (abstract_pkg_t *)hash_table_get(hash, pkg_name);
157 }
158
159 abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name)
160 {
161     abstract_pkg_t *apkg = abstract_pkg_fetch_by_name(hash, name);
162     if (apkg)
163        return NULL;
164     return apkg->provided_by;
165 }
166
167
168 pkg_t *pkg_hash_fetch_best_installation_candidate(opkg_conf_t *conf, abstract_pkg_t *apkg, 
169                                                   int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet, int *err)
170 {
171      int i; 
172      int nprovides = 0;
173      int nmatching = 0;
174      pkg_vec_t *matching_pkgs = pkg_vec_alloc();
175      abstract_pkg_vec_t *matching_apkgs = abstract_pkg_vec_alloc();
176      abstract_pkg_vec_t *provided_apkg_vec;
177      abstract_pkg_t **provided_apkgs;
178      abstract_pkg_vec_t *providers = abstract_pkg_vec_alloc();
179      pkg_t *latest_installed_parent = NULL;
180      pkg_t *latest_matching = NULL;
181      pkg_t *priorized_matching = NULL;
182      pkg_t *held_pkg = NULL;
183      pkg_t *good_pkg_by_name = NULL;
184
185      if (err)
186        *err = 0;
187
188      if (matching_apkgs == NULL || providers == NULL || 
189          apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
190           return NULL;
191
192      opkg_message(conf, OPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
193
194      provided_apkg_vec = apkg->provided_by;
195      nprovides = provided_apkg_vec->len;
196      provided_apkgs = provided_apkg_vec->pkgs;
197      if (nprovides > 1)
198           opkg_message(conf, OPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
199
200      /* accumulate all the providers */
201      for (i = 0; i < nprovides; i++) {
202           abstract_pkg_t *provider_apkg = provided_apkgs[i];
203           opkg_message(conf, OPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
204           abstract_pkg_vec_insert(providers, provider_apkg);
205      }
206      nprovides = providers->len;
207
208      for (i = 0; i < nprovides; i++) {
209           abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
210           abstract_pkg_t *replacement_apkg = NULL;
211           pkg_vec_t *vec;
212
213           if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
214                replacement_apkg = provider_apkg->replaced_by->pkgs[0];
215                if (provider_apkg->replaced_by->len > 1) {
216                     opkg_message(conf, OPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
217                                  provider_apkg->name, replacement_apkg->name);
218                }
219           }
220
221           if (replacement_apkg)
222                opkg_message(conf, OPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
223                             replacement_apkg->name, provider_apkg->name);
224
225           if (replacement_apkg && (replacement_apkg != provider_apkg)) {
226                if (abstract_pkg_vec_contains(providers, replacement_apkg))
227                     continue;
228                else
229                     provider_apkg = replacement_apkg;
230           }
231
232           if (!(vec = provider_apkg->pkgs)) {
233                opkg_message(conf, OPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
234                continue;
235           }
236     
237
238           /* now check for supported architecture */
239           {
240                int max_count = 0;
241                int i;
242
243                /* count packages matching max arch priority and keep track of last one */
244                for (i = 0; i < vec->len; i++) {
245                     pkg_t *maybe = vec->pkgs[i];
246                     opkg_message(conf, OPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
247                                  maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
248                     /* We make sure not to add the same package twice. Need to search for the reason why 
249                        they show up twice sometimes. */
250                     if ((maybe->arch_priority > 0) && (! pkg_vec_contains(matching_pkgs, maybe))) {
251                          max_count++;
252                          abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
253                          pkg_vec_insert(matching_pkgs, maybe);
254                     }
255                }
256
257                 if (vec->len > 0 && matching_pkgs->len < 1)
258                 {
259                   opkg_message (conf, OPKG_ERROR, "Packages were found, but none compatible with the architectures configured\n");
260                   if (err)
261                     *err = OPKG_PKG_HAS_NO_AVAILABLE_ARCH;
262                 }
263           }
264      }
265
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);
270
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*/
279 /*Pigi*/
280
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 */
287                 break;                                 
288           }
289      }
290
291
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)) {
298                if (held_pkg)
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);
301                held_pkg = matching;
302           }
303      }
304
305      if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
306           int prio = 0;
307           for (i = 0; i < matching_pkgs->len; i++) {
308               pkg_t *matching = matching_pkgs->pkgs[i];
309                   if (matching->arch_priority > prio) {
310                       priorized_matching = matching;
311                       prio = matching->arch_priority;
312                       opkg_message(conf, OPKG_DEBUG, "Match with priority %i    %s\n", prio, matching->name);
313                   }
314               }
315           
316           }
317
318      if (matching_apkgs->len > 1 && conf->verbosity > 1) {
319           opkg_message(conf, OPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
320                        __FUNCTION__, apkg->name, matching_pkgs->len);
321           for (i = 0; i < matching_pkgs->len; i++) {
322                pkg_t *matching = matching_pkgs->pkgs[i];
323                opkg_message(conf, OPKG_INFO, "    %s %s %s\n",
324                             matching->name, matching->version, matching->architecture);
325           }
326      }
327
328      nmatching = matching_apkgs->len;
329
330      pkg_vec_free(matching_pkgs);
331      abstract_pkg_vec_free(matching_apkgs);
332      abstract_pkg_vec_free(providers);
333
334      if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
335           return good_pkg_by_name;
336      }
337      if (held_pkg) {
338           opkg_message(conf, OPKG_INFO, "  using held package %s\n", held_pkg->name);
339           return held_pkg;
340      }
341      if (latest_installed_parent) {
342           opkg_message(conf, OPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
343           return latest_installed_parent;
344      }
345      if (priorized_matching) {
346           opkg_message(conf, OPKG_INFO, "  using priorized matching %s %s %s\n",
347                        priorized_matching->name, priorized_matching->version, priorized_matching->architecture);
348           return priorized_matching;
349      }
350      if (nmatching > 1) {
351           opkg_message(conf, OPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
352           return NULL;
353      }
354      if (latest_matching) {
355           opkg_message(conf, OPKG_INFO, "  using latest matching %s %s %s\n",
356                        latest_matching->name, latest_matching->version, latest_matching->architecture);
357           return latest_matching;
358      }
359      return NULL;
360 }
361
362 static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
363 {
364      const char *name = (const char *)cdata;
365      if (strcmp(pkg->name, name) == 0)
366           return 1;
367      else
368           return 0;   
369 }
370
371 pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t *conf, const char *name, int *err)
372 {
373      hash_table_t *hash = &conf->pkg_hash;
374      abstract_pkg_t *apkg = NULL;
375      pkg_t *ret;
376
377      if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
378           return NULL;
379
380      ret = pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0, err);
381
382      return ret;
383 }
384
385
386 pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
387                                        const char *pkg_name,
388                                        const char * version)
389 {
390     pkg_vec_t * vec;
391     register int i;
392     char *version_str = NULL;
393     
394     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
395         return NULL;
396     
397     for(i = 0; i < vec->len; i++) {
398         version_str = pkg_version_str_alloc(vec->pkgs[i]);
399         if(!strcmp(version_str, version)) {
400             free(version_str);
401             break;
402         }
403         free(version_str);
404     }
405         
406     if(i == vec->len)
407         return NULL;
408     
409     return vec->pkgs[i];
410 }
411
412 pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
413                                              const char *pkg_name,
414                                              pkg_dest_t *dest)
415 {
416     pkg_vec_t * vec;
417     register int i;
418
419     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
420         return NULL;
421     }
422     
423     for(i = 0; i < vec->len; i++)
424         if((vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED) && vec->pkgs[i]->dest == dest) {
425             return vec->pkgs[i];
426         }
427     return NULL;
428 }
429
430 pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
431                                         const char *pkg_name)
432 {
433     pkg_vec_t * vec;
434     register int i;
435
436     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
437         return NULL;
438     } 
439
440     for(i = 0; i < vec->len; i++)
441         if (vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED){
442             return vec->pkgs[i];
443         } 
444     
445     return NULL;
446 }
447
448 pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
449 {
450     abstract_pkg_t * ab_pkg;
451
452     if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name))){
453        return NULL;
454     }
455     
456     if (ab_pkg->pkgs) {
457       return ab_pkg->pkgs;
458     } else if (ab_pkg->provided_by) {
459       abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
460       if (abpkg != NULL){
461           return abpkg->pkgs;
462       } else {
463           return ab_pkg->pkgs;
464       }
465     } else {
466       return NULL;
467     }
468 }
469
470
471 static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
472 {
473   int j;
474   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
475   pkg_vec_t *all = (pkg_vec_t *)data;
476   pkg_vec_t *pkg_vec = ab_pkg->pkgs;
477   if (pkg_vec) {
478     for (j = 0; j < pkg_vec->len; j++) {
479       pkg_t *pkg = pkg_vec->pkgs[j];
480       pkg_vec_insert(all, pkg);
481     }
482   }
483 }
484
485 void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
486 {
487     hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
488 }
489
490 static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
491 {
492   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
493   pkg_vec_t *all = (pkg_vec_t *)data;
494   pkg_vec_t *pkg_vec = ab_pkg->pkgs;
495   int j;
496   if (pkg_vec) {
497     for (j = 0; j < pkg_vec->len; j++) {
498       pkg_t *pkg = pkg_vec->pkgs[j];
499       if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
500         pkg_vec_insert(all, pkg);
501       }
502     }
503   }
504 }
505 void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
506 {
507     hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
508 }
509
510 static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
511 {
512   int i;
513   pkg_t *pkg;
514   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
515   opkg_conf_t *conf = (opkg_conf_t *)data;
516   abstract_pkg_t ** dependents = ab_pkg->depended_upon_by;
517   fprintf(stdout, "%s\n", ab_pkg->name);
518   i = 0;
519   if (dependents != NULL)
520     while (dependents [i] != NULL)
521       printf ("\tdepended upon by - %s\n", dependents [i ++]->name);
522   dependents = ab_pkg->provided_by->pkgs;
523   i = 0;
524   if (dependents != NULL)
525     while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
526       printf ("\tprovided by - %s\n", dependents [i ++]->name);
527   pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name, NULL);
528   if (pkg) {
529     i = 0;
530     while (i < pkg->depends_count)
531       printf ("\tdepends on - %s\n", pkg->depends_str [i ++]);  
532   }
533 }
534 void pkg_hash_dump(hash_table_t *hash, void *data)
535 {
536
537   printf ("\n\n+=+%s+=+\n\n", __FUNCTION__);
538   hash_table_foreach(hash, pkg_hash_dump_helper, data);
539   printf ("\n+=+%s+=+\n\n", __FUNCTION__);    
540 }
541
542 abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
543 {
544   abstract_pkg_t * ab_pkg;
545
546   if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
547     ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
548
549   return ab_pkg;
550 }
551
552 pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,opkg_conf_t *conf)
553 {
554      abstract_pkg_t * ab_pkg;
555      int arch_priority;
556
557      if(!pkg)
558           return pkg;
559
560      arch_priority = pkg->arch_priority;
561
562      if (buildDepends(hash, pkg)<0){
563         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
564         return NULL;
565      }
566      ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
567
568      if (set_status) {
569           if (pkg->state_status == SS_INSTALLED) {
570                ab_pkg->state_status = SS_INSTALLED;
571           } else if (pkg->state_status == SS_UNPACKED) {
572                ab_pkg->state_status = SS_UNPACKED;
573           }
574      }
575
576      if(!ab_pkg->pkgs)
577           ab_pkg->pkgs = pkg_vec_alloc();
578     
579      /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
580      pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
581      pkg->parent = ab_pkg;
582
583      if (buildProvides(hash, ab_pkg, pkg)<0){
584         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
585         return NULL;
586      }
587      /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
588      if (buildConflicts(hash, ab_pkg, pkg)<0){
589         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
590         return NULL;
591      }
592      if (buildReplaces(hash, ab_pkg, pkg)<0) {
593         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
594         return NULL;
595      }
596     
597      buildDependedUponBy(pkg, ab_pkg);
598      return pkg;
599 }
600
601 /*
602  * this will assume that we've already determined that
603  * the abstract pkg doesn't exist, 'cause we should know these things...
604  */
605 static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
606 {
607   abstract_pkg_t * ab_pkg;
608
609   ab_pkg = abstract_pkg_new();
610   if (ab_pkg == NULL) { return NULL; }
611
612   ab_pkg->name = strdup(pkg_name);
613   hash_table_insert(hash, pkg_name, ab_pkg);
614
615   return ab_pkg;
616 }
617
618
619 pkg_t *file_hash_get_file_owner(opkg_conf_t *conf, const char *file_name)
620 {
621      hash_table_t *file_hash = &conf->file_hash;
622
623      return hash_table_get(file_hash, file_name); 
624 }
625
626 int file_hash_set_file_owner(opkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg)
627 {
628      hash_table_t *file_hash = &conf->file_hash;
629      pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name);
630      int file_name_len = strlen(file_name);
631
632      if (file_name[file_name_len -1] == '/')
633           return 0;
634
635      if (conf->offline_root) {
636           int len = strlen(conf->offline_root);
637           if (strncmp(file_name, conf->offline_root, len) == 0) {
638                file_name += len;
639           }
640      }
641
642      // opkg_message(conf, OPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
643      hash_table_insert(file_hash, file_name, owning_pkg); 
644      if (old_owning_pkg) {
645           pkg_get_installed_files(old_owning_pkg);
646           str_list_remove_elt(old_owning_pkg->installed_files, file_name);
647           pkg_free_installed_files(old_owning_pkg);
648           /* mark this package to have its filelist written */
649           old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
650           owning_pkg->state_flag |= SF_FILELIST_CHANGED;
651           
652      }
653      return 0;
654 }
655
656