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