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