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