fix a buffer overflow bug that cause
[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 *held_pkg = NULL;
182      pkg_t *good_pkg_by_name = NULL;
183
184      if (err)
185        *err = 0;
186
187      if (matching_apkgs == NULL || providers == NULL || 
188          apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
189           return NULL;
190
191      opkg_message(conf, OPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
192
193      provided_apkg_vec = apkg->provided_by;
194      nprovides = provided_apkg_vec->len;
195      provided_apkgs = provided_apkg_vec->pkgs;
196      if (nprovides > 1)
197           opkg_message(conf, OPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
198
199      /* accumulate all the providers */
200      for (i = 0; i < nprovides; i++) {
201           abstract_pkg_t *provider_apkg = provided_apkgs[i];
202           opkg_message(conf, OPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
203           abstract_pkg_vec_insert(providers, provider_apkg);
204      }
205      nprovides = providers->len;
206
207      for (i = 0; i < nprovides; i++) {
208           abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
209           abstract_pkg_t *replacement_apkg = NULL;
210           pkg_vec_t *vec;
211
212           if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
213                replacement_apkg = provider_apkg->replaced_by->pkgs[0];
214                if (provider_apkg->replaced_by->len > 1) {
215                     opkg_message(conf, OPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
216                                  provider_apkg->name, replacement_apkg->name);
217                }
218           }
219
220           if (replacement_apkg)
221                opkg_message(conf, OPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
222                             replacement_apkg->name, provider_apkg->name);
223
224           if (replacement_apkg && (replacement_apkg != provider_apkg)) {
225                if (abstract_pkg_vec_contains(providers, replacement_apkg))
226                     continue;
227                else
228                     provider_apkg = replacement_apkg;
229           }
230
231           if (!(vec = provider_apkg->pkgs)) {
232                opkg_message(conf, OPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
233                continue;
234           }
235     
236
237           /* now check for supported architecture */
238           {
239                int max_count = 0;
240                int i;
241
242                /* count packages matching max arch priority and keep track of last one */
243                for (i = 0; i < vec->len; i++) {
244                     pkg_t *maybe = vec->pkgs[i];
245                     opkg_message(conf, OPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
246                                  maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
247                     if (maybe->arch_priority > 0)  {
248                          max_count++;
249                          abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
250                          pkg_vec_insert(matching_pkgs, maybe);
251                     }
252                }
253
254                 if (vec->len > 0 && matching_pkgs->len < 1)
255                 {
256                   opkg_message (conf, OPKG_ERROR, "Packages were found, but none compatible with the architectures configured\n");
257                   if (err)
258                     *err = OPKG_PKG_HAS_NO_AVAILABLE_ARCH;
259                 }
260           }
261      }
262
263      if (matching_pkgs->len > 1)
264           pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
265      if (matching_apkgs->len > 1)
266           abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
267
268 /* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
269    needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
270 /* The problem is what to do when there are more than a mathing package, with the same name and several version ?
271    Until now I always got the latest, but that breaks the downgrade option.
272    If I stop at the first one, I would probably miss the new ones 
273    Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
274    or from a Packages feed.
275    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*/
276 /*Pigi*/
277
278      for (i = 0; i < matching_pkgs->len; i++) {
279           pkg_t *matching = matching_pkgs->pkgs[i];
280           if (constraint_fcn(matching, cdata)) {  /* We found it */
281              opkg_message(conf, OPKG_DEBUG, " Found a valid candidate for the install: %s %s  \n", matching->name, matching->version) ;
282              good_pkg_by_name = matching;
283              if ( matching->provided_by_hand == 1 )    /* It has been provided by hand, so it is what user want */
284                 break;                                 
285           }
286      }
287
288
289      for (i = 0; i < matching_pkgs->len; i++) {
290           pkg_t *matching = matching_pkgs->pkgs[i];
291           latest_matching = matching;
292           if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
293                latest_installed_parent = matching;
294           if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
295                if (held_pkg)
296                     opkg_message(conf, OPKG_NOTICE, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
297                                  held_pkg->name, matching->name);
298                held_pkg = matching;
299           }
300      }
301
302      if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
303           opkg_message(conf, OPKG_ERROR, "Package=%s, %d matching providers\n",
304                        apkg->name, matching_apkgs->len);
305           for (i = 0; i < matching_apkgs->len; i++) {
306               abstract_pkg_t *matching = matching_apkgs->pkgs[i];
307               opkg_message(conf, OPKG_ERROR, "    %s\n", matching->name);
308           }
309           opkg_message(conf, OPKG_ERROR, "Please select one with opkg install or opkg flag prefer\n");
310      }
311
312      if (matching_apkgs->len > 1 && conf->verbosity > 1) {
313           opkg_message(conf, OPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
314                        __FUNCTION__, apkg->name, matching_pkgs->len);
315           for (i = 0; i < matching_pkgs->len; i++) {
316                pkg_t *matching = matching_pkgs->pkgs[i];
317                opkg_message(conf, OPKG_INFO, "    %s %s %s\n",
318                             matching->name, matching->version, matching->architecture);
319           }
320      }
321
322      nmatching = matching_apkgs->len;
323
324      pkg_vec_free(matching_pkgs);
325      abstract_pkg_vec_free(matching_apkgs);
326      abstract_pkg_vec_free(providers);
327
328      if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
329           return good_pkg_by_name;
330      }
331      if (held_pkg) {
332           opkg_message(conf, OPKG_INFO, "  using held package %s\n", held_pkg->name);
333           return held_pkg;
334      }
335      if (latest_installed_parent) {
336           opkg_message(conf, OPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
337           return latest_installed_parent;
338      }
339      if (nmatching > 1) {
340           opkg_message(conf, OPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
341           return NULL;
342      }
343      if (latest_matching) {
344           opkg_message(conf, OPKG_INFO, "  using latest matching %s %s %s\n",
345                        latest_matching->name, latest_matching->version, latest_matching->architecture);
346           return latest_matching;
347      }
348      return NULL;
349 }
350
351 static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
352 {
353      const char *name = (const char *)cdata;
354      if (strcmp(pkg->name, name) == 0)
355           return 1;
356      else
357           return 0;   
358 }
359
360 pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(opkg_conf_t *conf, const char *name, int *err)
361 {
362      hash_table_t *hash = &conf->pkg_hash;
363      abstract_pkg_t *apkg = NULL;
364      pkg_t *ret;
365
366      if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
367           return NULL;
368
369      ret = pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0, err);
370
371      return ret;
372 }
373
374
375 pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
376                                        const char *pkg_name,
377                                        const char * version)
378 {
379     pkg_vec_t * vec;
380     register int i;
381     char *version_str = NULL;
382     
383     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
384         return NULL;
385     
386     for(i = 0; i < vec->len; i++) {
387         version_str = pkg_version_str_alloc(vec->pkgs[i]);
388         if(!strcmp(version_str, version)) {
389             free(version_str);
390             break;
391         }
392         free(version_str);
393     }
394         
395     if(i == vec->len)
396         return NULL;
397     
398     return vec->pkgs[i];
399 }
400
401 pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
402                                              const char *pkg_name,
403                                              pkg_dest_t *dest)
404 {
405     pkg_vec_t * vec;
406     register int i;
407
408     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
409         return NULL;
410     }
411     
412     for(i = 0; i < vec->len; i++)
413         if((vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED) && vec->pkgs[i]->dest == dest) {
414             return vec->pkgs[i];
415         }
416     return NULL;
417 }
418
419 pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
420                                         const char *pkg_name)
421 {
422     pkg_vec_t * vec;
423     register int i;
424
425     if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
426         return NULL;
427     } 
428
429     for(i = 0; i < vec->len; i++)
430         if (vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED){
431             return vec->pkgs[i];
432         } 
433     
434     return NULL;
435 }
436
437 pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
438 {
439     abstract_pkg_t * ab_pkg;
440
441     if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name))){
442        return NULL;
443     }
444     
445     if (ab_pkg->pkgs) {
446       return ab_pkg->pkgs;
447     } else if (ab_pkg->provided_by) {
448       abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
449       if (abpkg != NULL){
450           return abpkg->pkgs;
451       } else {
452           return ab_pkg->pkgs;
453       }
454     } else {
455       return NULL;
456     }
457 }
458
459
460 static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
461 {
462   int j;
463   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
464   pkg_vec_t *all = (pkg_vec_t *)data;
465   pkg_vec_t *pkg_vec = ab_pkg->pkgs;
466   if (pkg_vec) {
467     for (j = 0; j < pkg_vec->len; j++) {
468       pkg_t *pkg = pkg_vec->pkgs[j];
469       pkg_vec_insert(all, pkg);
470     }
471   }
472 }
473
474 void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
475 {
476     hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
477 }
478
479 static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
480 {
481   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
482   pkg_vec_t *all = (pkg_vec_t *)data;
483   pkg_vec_t *pkg_vec = ab_pkg->pkgs;
484   int j;
485   if (pkg_vec) {
486     for (j = 0; j < pkg_vec->len; j++) {
487       pkg_t *pkg = pkg_vec->pkgs[j];
488       if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
489         pkg_vec_insert(all, pkg);
490       }
491     }
492   }
493 }
494 void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
495 {
496     hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
497 }
498
499 static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
500 {
501   int i;
502   pkg_t *pkg;
503   abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
504   opkg_conf_t *conf = (opkg_conf_t *)data;
505   abstract_pkg_t ** dependents = ab_pkg->depended_upon_by;
506   fprintf(stdout, "%s\n", ab_pkg->name);
507   i = 0;
508   if (dependents != NULL)
509     while (dependents [i] != NULL)
510       printf ("\tdepended upon by - %s\n", dependents [i ++]->name);
511   dependents = ab_pkg->provided_by->pkgs;
512   i = 0;
513   if (dependents != NULL)
514     while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
515       printf ("\tprovided by - %s\n", dependents [i ++]->name);
516   pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name, NULL);
517   if (pkg) {
518     i = 0;
519     while (i < pkg->depends_count)
520       printf ("\tdepends on - %s\n", pkg->depends_str [i ++]);  
521   }
522 }
523 void pkg_hash_dump(hash_table_t *hash, void *data)
524 {
525
526   printf ("\n\n+=+%s+=+\n\n", __FUNCTION__);
527   hash_table_foreach(hash, pkg_hash_dump_helper, data);
528   printf ("\n+=+%s+=+\n\n", __FUNCTION__);    
529 }
530
531 abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
532 {
533   abstract_pkg_t * ab_pkg;
534
535   if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
536     ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
537
538   return ab_pkg;
539 }
540
541 pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,opkg_conf_t *conf)
542 {
543      abstract_pkg_t * ab_pkg;
544      int arch_priority;
545
546      if(!pkg)
547           return pkg;
548
549      arch_priority = pkg->arch_priority;
550
551      if (buildDepends(hash, pkg)<0){
552         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
553         return NULL;
554      }
555      ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
556
557      if (set_status) {
558           if (pkg->state_status == SS_INSTALLED) {
559                ab_pkg->state_status = SS_INSTALLED;
560           } else if (pkg->state_status == SS_UNPACKED) {
561                ab_pkg->state_status = SS_UNPACKED;
562           }
563      }
564
565      if(!ab_pkg->pkgs)
566           ab_pkg->pkgs = pkg_vec_alloc();
567     
568      /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
569      pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
570      pkg->parent = ab_pkg;
571
572      if (buildProvides(hash, ab_pkg, pkg)<0){
573         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
574         return NULL;
575      }
576      /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
577      if (buildConflicts(hash, ab_pkg, pkg)<0){
578         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
579         return NULL;
580      }
581      if (buildReplaces(hash, ab_pkg, pkg)<0) {
582         fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
583         return NULL;
584      }
585     
586      buildDependedUponBy(pkg, ab_pkg);
587      return pkg;
588 }
589
590 /*
591  * this will assume that we've already determined that
592  * the abstract pkg doesn't exist, 'cause we should know these things...
593  */
594 static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
595 {
596   abstract_pkg_t * ab_pkg;
597
598   ab_pkg = abstract_pkg_new();
599   if (ab_pkg == NULL) { return NULL; }
600
601   ab_pkg->name = strdup(pkg_name);
602   hash_table_insert(hash, pkg_name, ab_pkg);
603
604   return ab_pkg;
605 }
606
607
608 pkg_t *file_hash_get_file_owner(opkg_conf_t *conf, const char *file_name)
609 {
610      hash_table_t *file_hash = &conf->file_hash;
611
612      return hash_table_get(file_hash, file_name); 
613 }
614
615 int file_hash_set_file_owner(opkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg)
616 {
617      hash_table_t *file_hash = &conf->file_hash;
618      pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name);
619      int file_name_len = strlen(file_name);
620
621      if (file_name[file_name_len -1] == '/')
622           return 0;
623
624      if (conf->offline_root) {
625           int len = strlen(conf->offline_root);
626           if (strncmp(file_name, conf->offline_root, len) == 0) {
627                file_name += len;
628           }
629      }
630
631      // opkg_message(conf, OPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
632      hash_table_insert(file_hash, file_name, owning_pkg); 
633      if (old_owning_pkg) {
634           pkg_get_installed_files(old_owning_pkg);
635           str_list_remove_elt(old_owning_pkg->installed_files, file_name);
636           pkg_free_installed_files(old_owning_pkg);
637           /* mark this package to have its filelist written */
638           old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
639           owning_pkg->state_flag |= SF_FILELIST_CHANGED;
640           
641      }
642      return 0;
643 }
644
645