c75a8d41d3542a99ee86f21a6ec70d2dce2ce2ff
[oweals/opkg-lede.git] / libopkg / opkg_install.c
1 /* opkg_install.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
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 <dirent.h>
21 #include <glob.h>
22 #include <time.h>
23 #include <signal.h>
24
25 #include "pkg.h"
26 #include "pkg_hash.h"
27 #include "pkg_extract.h"
28
29 #include "opkg_install.h"
30 #include "opkg_configure.h"
31 #include "opkg_download.h"
32 #include "opkg_remove.h"
33
34 #include "opkg_utils.h"
35 #include "opkg_message.h"
36 #include "opkg_cmd.h"
37 #include "opkg_defines.h"
38
39 #include "sprintf_alloc.h"
40 #include "file_util.h"
41 #include "str_util.h"
42 #include "xsystem.h"
43 #include "user.h"
44 #include "libbb/libbb.h"
45
46 static int
47 satisfy_dependencies_for(opkg_conf_t *conf, pkg_t *pkg)
48 {
49      int i, err;
50      pkg_vec_t *depends = pkg_vec_alloc();
51      pkg_t *dep;
52      char **unresolved = NULL;
53      int ndepends;
54
55      ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf, 
56                                                         pkg, depends, 
57                                                         &unresolved);
58
59      if (unresolved) {
60           opkg_message(conf, OPKG_ERROR,
61                        "%s: Cannot satisfy the following dependencies for %s:\n\t",
62                        conf->force_depends ? "Warning" : "ERROR", pkg->name);
63           while (*unresolved) {
64                opkg_message(conf, OPKG_ERROR, " %s", *unresolved);
65                unresolved++;
66           }
67           opkg_message(conf, OPKG_ERROR, "\n");
68           if (! conf->force_depends) {
69                opkg_message(conf, OPKG_INFO,
70                             "This could mean that your package list is out of date or that the packages\n"
71                             "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
72                             "of this problem try again with the '-force-depends' option.\n");
73                pkg_vec_free(depends);
74                return OPKG_PKG_DEPS_UNSATISFIED;
75           }
76      }
77
78      if (ndepends <= 0) {
79           pkg_vec_free(depends);
80           return 0;
81      }
82
83      /* Mark packages as to-be-installed */
84      for (i=0; i < depends->len; i++) {
85           /* Dependencies should be installed the same place as pkg */
86           if (depends->pkgs[i]->dest == NULL) {
87                depends->pkgs[i]->dest = pkg->dest;
88           }
89           depends->pkgs[i]->state_want = SW_INSTALL;
90      }
91
92      for (i = 0; i < depends->len; i++) {
93           dep = depends->pkgs[i];
94           /* The package was uninstalled when we started, but another
95              dep earlier in this loop may have depended on it and pulled
96              it in, so check first. */
97           if ((dep->state_status != SS_INSTALLED)
98               && (dep->state_status != SS_UNPACKED)) {
99                opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
100                err = opkg_install_pkg(conf, dep,0);
101                /* mark this package as having been automatically installed to
102                 * satisfy a dependancy */
103                dep->auto_installed = 1;
104                if (err) {
105                     pkg_vec_free(depends);
106                     return err;
107                }
108           }
109      }
110
111      pkg_vec_free(depends);
112
113      return 0;
114 }
115
116 static int
117 check_conflicts_for(opkg_conf_t *conf, pkg_t *pkg)
118 {
119      int i;
120      pkg_vec_t *conflicts = NULL;
121      int level;
122      const char *prefix;
123      if (conf->force_depends) {
124           level = OPKG_NOTICE;
125           prefix = "Warning";
126      } else {
127           level = OPKG_ERROR;
128           prefix = "ERROR";
129      }
130
131      if (!conf->force_depends)
132           conflicts = (pkg_vec_t *)pkg_hash_fetch_conflicts(&conf->pkg_hash, pkg);
133
134      if (conflicts) {
135           opkg_message(conf, level,
136                        "%s: The following packages conflict with %s:\n\t", prefix, pkg->name);
137           i = 0;
138           while (i < conflicts->len)
139                opkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
140           opkg_message(conf, level, "\n");
141           pkg_vec_free(conflicts);
142           return OPKG_PKG_DEPS_UNSATISFIED;
143      }
144      return 0;
145 }
146
147 static int
148 update_file_ownership(opkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
149 {
150      str_list_t *new_list = pkg_get_installed_files(conf, new_pkg);
151      str_list_elt_t *iter, *niter;
152
153      for (iter = str_list_first(new_list), niter = str_list_next(new_list, iter); 
154              iter; 
155              iter = niter, niter = str_list_next(new_list, niter)) {
156           char *new_file = (char *)iter->data;
157           pkg_t *owner = file_hash_get_file_owner(conf, new_file);
158           if (!new_file)
159                opkg_message(conf, OPKG_ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
160           if (!owner || (owner == old_pkg))
161                file_hash_set_file_owner(conf, new_file, new_pkg);
162      }
163      if (old_pkg) {
164           str_list_t *old_list = pkg_get_installed_files(conf, old_pkg);
165           for (iter = str_list_first(old_list), niter = str_list_next(old_list, iter); 
166                   iter; 
167                   iter = niter, niter = str_list_next(old_list, niter)) {
168                char *old_file = (char *)iter->data;
169                pkg_t *owner = file_hash_get_file_owner(conf, old_file);
170                if (owner == old_pkg) {
171                     /* obsolete */
172                     hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
173                }
174           }
175           pkg_free_installed_files(old_pkg);
176      }
177      pkg_free_installed_files(new_pkg);
178      return 0;
179 }
180
181 static int
182 verify_pkg_installable(opkg_conf_t *conf, pkg_t *pkg)
183 {
184     /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
185
186     /* sma 6.20.02:  yup; here's the first bit */
187     /* 
188      * XXX: BUG easy for cworth
189      * 1) please point the call below to the correct current root destination
190      * 2) we need to resolve how to check the required space for a pending pkg, 
191      *    my diddling with the .opk file size below isn't going to cut it.
192      * 3) return a proper error code instead of 1
193      */
194      int comp_size, blocks_available;
195      char *root_dir;
196     
197      if (!conf->force_space && pkg->installed_size != NULL) {
198           root_dir = pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir;
199           blocks_available = get_available_blocks(root_dir);
200
201           comp_size = strtoul(pkg->installed_size, NULL, 0);
202           /* round up a blocks count without doing fancy-but-slow casting jazz */ 
203           comp_size = (int)((comp_size + 1023) / 1024);
204
205           if (comp_size >= blocks_available) {
206                opkg_message(conf, OPKG_ERROR,
207                             "Only have %d available blocks on filesystem %s, pkg %s needs %d\n", 
208                             blocks_available, root_dir, pkg->name, comp_size);
209                return ENOSPC;
210           }
211      }
212      return 0;
213 }
214
215 static int
216 unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg)
217 {
218      int err;
219      char *conffiles_file_name;
220      char *root_dir;
221      FILE *conffiles_file;
222
223      sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
224
225      pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
226      if (pkg->tmp_unpack_dir == NULL) {
227           opkg_message(conf, OPKG_ERROR,
228                        "%s: Failed to create temporary directory '%s': %s\n",
229                        __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
230           return errno;
231      }
232
233      err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
234      if (err) {
235           return err;
236      }
237
238      /* XXX: CLEANUP: There might be a cleaner place to read in the
239         conffiles. Seems like I should be able to get everything to go
240         through pkg_init_from_file. If so, maybe it would make sense to
241         move all of unpack_pkg_control_files to that function. */
242
243      /* Don't need to re-read conffiles if we already have it */
244      if (!nv_pair_list_empty(&pkg->conffiles)) {
245           return 0;
246      }
247
248      sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
249      if (! file_exists(conffiles_file_name)) {
250           free(conffiles_file_name);
251           return 0;
252      }
253     
254      conffiles_file = fopen(conffiles_file_name, "r");
255      if (conffiles_file == NULL) {
256           fprintf(stderr, "%s: failed to open %s: %s\n",
257                   __FUNCTION__, conffiles_file_name, strerror(errno));
258           free(conffiles_file_name);
259           return errno;
260      }
261      free(conffiles_file_name);
262
263      while (1) {
264           char *cf_name;
265           char *cf_name_in_dest;
266
267           cf_name = file_read_line_alloc(conffiles_file);
268           if (cf_name == NULL) {
269                break;
270           }
271           str_chomp(cf_name);
272           if (cf_name[0] == '\0') {
273                continue;
274           }
275
276           /* Prepend dest->root_dir to conffile name.
277              Take pains to avoid multiple slashes. */
278           root_dir = pkg->dest->root_dir;
279           if (conf->offline_root)
280                /* skip the offline_root prefix */
281                root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
282           sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
283                         cf_name[0] == '/' ? (cf_name + 1) : cf_name);
284
285           /* Can't get an md5sum now, (file isn't extracted yet).
286              We'll wait until resolve_conffiles */
287           conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
288
289           free(cf_name);
290           free(cf_name_in_dest);
291      }
292
293      fclose(conffiles_file);
294
295      return 0;
296 }
297
298 /*
299  * Remove packages which were auto_installed due to a dependency by old_pkg,
300  * which are no longer a dependency in the new (upgraded) pkg.
301  */
302 static int
303 pkg_remove_orphan_dependent(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg) 
304 {
305         int i, j, k, l, found;
306         int n_deps;
307         pkg_t *p;
308         struct compound_depend *cd0, *cd1;
309         abstract_pkg_t **dependents;
310
311         int count0 = old_pkg->pre_depends_count +
312                                 old_pkg->depends_count +
313                                 old_pkg->recommends_count +
314                                 old_pkg->suggests_count;
315         int count1 = pkg->pre_depends_count +
316                                 pkg->depends_count +
317                                 pkg->recommends_count +
318                                 pkg->suggests_count;
319
320         for (i=0; i<count0; i++) {
321                 cd0 = &old_pkg->depends[i];
322                 if (cd0->type != DEPEND)
323                         continue;
324                 for (j=0; j<cd0->possibility_count; j++) {
325
326                         found = 0;
327
328                         for (k=0; k<count1; k++) {
329                                 cd1 = &pkg->depends[i];
330                                 if (cd1->type != DEPEND)
331                                         continue;
332                                 for (l=0; l<cd1->possibility_count; l++) {
333                                         if (cd0->possibilities[j]
334                                          == cd1->possibilities[l]) {
335                                                 found = 1;
336                                                 break;
337                                         }
338                                 }
339                                 if (found)
340                                         break;
341                         }
342
343                         if (found)
344                                 continue;
345
346                         /*
347                          * old_pkg has a dependency that pkg does not.
348                          */
349                         p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash,
350                                         cd0->possibilities[j]->pkg->name);
351
352                         if (!p)
353                                 continue;
354
355                         if (!p->auto_installed)
356                                 continue;
357
358                         n_deps = pkg_has_installed_dependents(conf, NULL, p,
359                                         &dependents);
360                         n_deps--; /* don't count old_pkg */
361
362                         if (n_deps == 0) {
363                                 opkg_message (conf, OPKG_NOTICE,
364                                                 "%s was autoinstalled and is "
365                                                 "now orphaned, removing.\n",
366                                                 p->name);
367
368                                 /* p has one installed dependency (old_pkg),
369                                  * which we need to ignore during removal. */
370                                 p->state_flag |= SF_REPLACE;
371
372                                 opkg_remove_pkg(conf, p, 0);
373                         } else 
374                                 opkg_message(conf, OPKG_INFO,
375                                                 "%s was autoinstalled and is "
376                                                 "still required by %d "
377                                                 "installed packages.\n",
378                                                 p->name, n_deps);
379
380                 }
381         }
382
383         return 0;
384 }
385
386 /* returns number of installed replacees */
387 static int
388 pkg_get_installed_replacees(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *installed_replacees)
389 {
390      abstract_pkg_t **replaces = pkg->replaces;
391      int replaces_count = pkg->replaces_count;
392      int i, j;
393      for (i = 0; i < replaces_count; i++) {
394           abstract_pkg_t *ab_pkg = replaces[i];
395           pkg_vec_t *pkg_vec = ab_pkg->pkgs;
396           if (pkg_vec) {
397                for (j = 0; j < pkg_vec->len; j++) {
398                     pkg_t *replacee = pkg_vec->pkgs[j];
399                     if (!pkg_conflicts(pkg, replacee))
400                          continue;
401                     if (replacee->state_status == SS_INSTALLED) {
402                          pkg_vec_insert(installed_replacees, replacee);
403                     }
404                }
405           }
406      }
407      return installed_replacees->len;
408 }
409
410 static int
411 pkg_remove_installed_replacees(opkg_conf_t *conf, pkg_vec_t *replacees)
412 {
413      int i;
414      int replaces_count = replacees->len;
415      for (i = 0; i < replaces_count; i++) {
416           pkg_t *replacee = replacees->pkgs[i];
417           int err;
418           replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
419           err = opkg_remove_pkg(conf, replacee,0);
420           if (err)
421                return err;
422      }
423      return 0;
424 }
425
426 /* to unwind the removal: make sure they are installed */
427 static int
428 pkg_remove_installed_replacees_unwind(opkg_conf_t *conf, pkg_vec_t *replacees)
429 {
430      int i, err;
431      int replaces_count = replacees->len;
432      for (i = 0; i < replaces_count; i++) {
433           pkg_t *replacee = replacees->pkgs[i];
434           if (replacee->state_status != SS_INSTALLED) {
435                opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
436                err = opkg_install_pkg(conf, replacee,0);
437                if (err)
438                     return err;
439           }
440      }
441      return 0;
442 }
443
444 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
445 static int
446 opkg_install_check_downgrade(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg, int message)
447 {         
448      if (old_pkg) {
449           char message_out[15];
450           char *old_version = pkg_version_str_alloc(old_pkg);
451           char *new_version = pkg_version_str_alloc(pkg);
452           int cmp = pkg_compare_versions(old_pkg, pkg);
453           int rc = 0;
454
455           memset(message_out,'\x0',15);
456           strncpy (message_out,"Upgrading ",strlen("Upgrading ")); 
457           if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
458              cmp = -1 ;                                       /* then we force opkg to downgrade */ 
459              strncpy (message_out,"Downgrading ",strlen("Downgrading "));         /* We need to use a value < 0 because in the 0 case we are asking to */
460                                                               /* reinstall, and some check could fail asking the "force-reinstall" option */
461           } 
462
463           if (cmp > 0) {
464                opkg_message(conf, OPKG_NOTICE,
465                             "Not downgrading package %s on %s from %s to %s.\n",
466                             old_pkg->name, old_pkg->dest->name, old_version, new_version);
467                rc = 1;
468           } else if (cmp < 0) {
469                opkg_message(conf, OPKG_NOTICE,
470                             "%s%s on %s from %s to %s...\n",
471                             message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
472                pkg->dest = old_pkg->dest;
473                rc = 0;
474           } else /* cmp == 0 */ {
475                if (conf->force_reinstall) {
476                     opkg_message(conf, OPKG_NOTICE,
477                                  "Reinstalling %s (%s) on %s...\n",
478                                  pkg->name, new_version, old_pkg->dest->name);
479                     pkg->dest = old_pkg->dest;
480                     rc = 0;
481                } else {
482                     opkg_message(conf, OPKG_NOTICE,
483                                  "Not installing %s (%s) on %s -- already installed.\n",
484                                  pkg->name, new_version, old_pkg->dest->name);
485                     rc = 1;
486                }
487           } 
488           free(old_version);
489           free(new_version);
490           return rc;
491      } else {
492       char message_out[15] ;
493       memset(message_out,'\x0',15);
494       if ( message ) 
495           strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
496       else
497           strncpy( message_out,"Installing ",strlen("Installing ") );
498           char *version = pkg_version_str_alloc(pkg);
499       
500           opkg_message(conf, OPKG_NOTICE,
501                        "%s%s (%s) to %s...\n", message_out,
502                        pkg->name, version, pkg->dest->name);
503           free(version);
504           return 0;
505      }
506 }
507
508
509 static int
510 prerm_upgrade_old_pkg(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
511 {
512      /* DPKG_INCOMPATIBILITY:
513         dpkg does some things here that we don't do yet. Do we care?
514         
515         1. If a version of the package is already installed, call
516            old-prerm upgrade new-version
517         2. If the script runs but exits with a non-zero exit status
518            new-prerm failed-upgrade old-version
519            Error unwind, for both the above cases:
520            old-postinst abort-upgrade new-version
521      */
522      return 0;
523 }
524
525 static int
526 prerm_upgrade_old_pkg_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
527 {
528      /* DPKG_INCOMPATIBILITY:
529         dpkg does some things here that we don't do yet. Do we care?
530         (See prerm_upgrade_old_package for details)
531      */
532      return 0;
533 }
534
535 static int
536 prerm_deconfigure_conflictors(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
537 {
538      /* DPKG_INCOMPATIBILITY:
539         dpkg does some things here that we don't do yet. Do we care?
540         2. If a 'conflicting' package is being removed at the same time:
541                 1. If any packages depended on that conflicting package and
542                    --auto-deconfigure is specified, call, for each such package:
543                    deconfigured's-prerm deconfigure \
544                    in-favour package-being-installed version \
545                    removing conflicting-package version
546                 Error unwind:
547                    deconfigured's-postinst abort-deconfigure \
548                    in-favour package-being-installed-but-failed version \
549                    removing conflicting-package version
550
551                    The deconfigured packages are marked as requiring
552                    configuration, so that if --install is used they will be
553                    configured again if possible.
554                 2. To prepare for removal of the conflicting package, call:
555                    conflictor's-prerm remove in-favour package new-version
556                 Error unwind:
557                    conflictor's-postinst abort-remove in-favour package new-version
558      */
559      return 0;
560 }
561
562 static int
563 prerm_deconfigure_conflictors_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
564 {
565      /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
566         do yet. Do we care?  (See prerm_deconfigure_conflictors for
567         details) */
568      return 0;
569 }
570
571 static int
572 preinst_configure(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
573 {
574      int err;
575      char *preinst_args;
576
577      if (old_pkg) {
578           char *old_version = pkg_version_str_alloc(old_pkg);
579           sprintf_alloc(&preinst_args, "upgrade %s", old_version);
580           free(old_version);
581      } else if (pkg->state_status == SS_CONFIG_FILES) {
582           char *pkg_version = pkg_version_str_alloc(pkg);
583           sprintf_alloc(&preinst_args, "install %s", pkg_version);
584           free(pkg_version);
585      } else {
586           preinst_args = xstrdup("install");
587      }
588
589      err = pkg_run_script(conf, pkg, "preinst", preinst_args);
590      if (err) {
591           opkg_message(conf, OPKG_ERROR,
592                        "Aborting installation of %s\n", pkg->name);
593           return 1;
594      }
595
596      free(preinst_args);
597
598      return 0;
599 }
600
601 static int
602 preinst_configure_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
603 {
604      /* DPKG_INCOMPATIBILITY:
605         dpkg does the following error unwind, should we?
606         pkg->postrm abort-upgrade old-version
607         OR pkg->postrm abort-install old-version
608         OR pkg->postrm abort-install
609      */
610      return 0;
611 }
612
613 static char *
614 backup_filename_alloc(const char *file_name)
615 {
616      char *backup;
617
618      sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
619
620      return backup;
621 }
622
623
624 static int
625 backup_make_backup(opkg_conf_t *conf, const char *file_name)
626 {
627      int err;
628      char *backup;
629     
630      backup = backup_filename_alloc(file_name);
631      err = file_copy(file_name, backup);
632      if (err) {
633           opkg_message(conf, OPKG_ERROR,
634                        "%s: Failed to copy %s to %s\n",
635                        __FUNCTION__, file_name, backup);
636      }
637
638      free(backup);
639
640      return err;
641 }
642
643 static int
644 backup_exists_for(const char *file_name)
645 {
646      int ret;
647      char *backup;
648
649      backup = backup_filename_alloc(file_name);
650
651      ret = file_exists(backup);
652
653      free(backup);
654
655      return ret;
656 }
657
658 static int
659 backup_remove(const char *file_name)
660 {
661      char *backup;
662
663      backup = backup_filename_alloc(file_name);
664      unlink(backup);
665      free(backup);
666
667      return 0;
668 }
669
670 static int
671 backup_modified_conffiles(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
672 {
673      int err;
674      conffile_list_elt_t *iter;
675      conffile_t *cf;
676
677      if (conf->noaction) return 0;
678
679      /* Backup all modified conffiles */
680      if (old_pkg) {
681           for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
682                char *cf_name;
683                
684                cf = iter->data;
685                cf_name = root_filename_alloc(conf, cf->name);
686
687                /* Don't worry if the conffile is just plain gone */
688                if (file_exists(cf_name) && conffile_has_been_modified(conf, cf)) {
689                     err = backup_make_backup(conf, cf_name);
690                     if (err) {
691                          return err;
692                     }
693                }
694                free(cf_name);
695           }
696      }
697
698      /* Backup all conffiles that were not conffiles in old_pkg */
699      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
700           char *cf_name;
701           cf = (conffile_t *)iter->data;
702           cf_name = root_filename_alloc(conf, cf->name);
703           /* Ignore if this was a conffile in old_pkg as well */
704           if (pkg_get_conffile(old_pkg, cf->name)) {
705                continue;
706           }
707
708           if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
709                err = backup_make_backup(conf, cf_name);
710                if (err) {
711                     return err;
712                }
713           }
714           free(cf_name);
715      }
716
717      return 0;
718 }
719
720 static int
721 backup_modified_conffiles_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
722 {
723      conffile_list_elt_t *iter;
724
725      if (old_pkg) {
726           for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
727                backup_remove(((nv_pair_t *)iter->data)->name);
728           }
729      }
730
731      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
732           backup_remove(((nv_pair_t *)iter->data)->name);
733      }
734
735      return 0;
736 }
737
738
739 static int
740 check_data_file_clashes(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
741 {
742      /* DPKG_INCOMPATIBILITY:
743         opkg takes a slightly different approach than dpkg at this
744         point.  dpkg installs each file in the new package while
745         creating a backup for any file that is replaced, (so that it
746         can unwind if necessary).  To avoid complexity and redundant
747         storage, opkg doesn't do any installation until later, (at the
748         point at which dpkg removes the backups.
749         
750         But, we do have to check for data file clashes, since after
751         installing a package with a file clash, removing either of the
752         packages involved in the clash has the potential to break the
753         other package.
754      */
755      str_list_t *files_list;
756      str_list_elt_t *iter, *niter;
757
758      int clashes = 0;
759
760      files_list = pkg_get_installed_files(conf, pkg);
761      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
762              iter; 
763              iter = niter, niter = str_list_next(files_list, iter)) {
764           char *root_filename;
765           char *filename = (char *) iter->data;
766           root_filename = root_filename_alloc(conf, filename);
767           if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
768                pkg_t *owner;
769                pkg_t *obs;
770                /* Pre-existing conffiles are OK */
771                /* @@@@ should have way to check that it is a conffile -Jamey */
772                if (backup_exists_for(root_filename)) {
773                     continue;
774                }
775
776                /* Pre-existing files are OK if force-overwrite was asserted. */ 
777                if (conf->force_overwrite) {
778                     /* but we need to change who owns this file */
779                     file_hash_set_file_owner(conf, filename, pkg);
780                     continue;
781                }
782
783                owner = file_hash_get_file_owner(conf, filename);
784
785                /* Pre-existing files are OK if owned by the pkg being upgraded. */
786                if (owner && old_pkg) {
787                     if (strcmp(owner->name, old_pkg->name) == 0) {
788                          continue;
789                     }
790                }
791
792                /* Pre-existing files are OK if owned by a package replaced by new pkg. */
793                if (owner) {
794                     opkg_message(conf, OPKG_DEBUG2, "Checking for replaces for %s in package %s\n", filename, owner->name);
795                     if (pkg_replaces(pkg, owner)) {
796                          continue;
797                     }
798 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
799    then it's ok to overwrite. */
800                     if (strcmp(owner->name,pkg->name)==0){
801                          opkg_message(conf, OPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
802                          continue;
803                     }
804                }
805
806                /* Pre-existing files are OK if they are obsolete */
807                obs = hash_table_get(&conf->obs_file_hash, filename);
808                if (obs) {
809                     opkg_message(conf, OPKG_INFO, "Pre-exiting file %s is obsolete.  obs_pkg=%s\n", filename, obs->name);
810                     continue;
811                }
812
813                /* We have found a clash. */
814                opkg_message(conf, OPKG_ERROR,
815                             "Package %s wants to install file %s\n"
816                             "\tBut that file is already provided by package ",
817                             pkg->name, filename);
818                if (owner) {
819                     opkg_message(conf, OPKG_ERROR,
820                                  "%s\n", owner->name);
821                } else {
822                     opkg_message(conf, OPKG_ERROR,
823                                  "<no package>\nPlease move this file out of the way and try again.\n");
824                }
825                clashes++;
826           }
827           free(root_filename);
828      }
829      pkg_free_installed_files(pkg);
830
831      return clashes;
832 }
833
834 static int
835 check_data_file_clashes_change(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
836 {
837     /* Basically that's the worst hack I could do to be able to change ownership of
838        file list, but, being that we have no way to unwind the mods, due to structure
839        of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
840        What we do here is change the ownership of file in hash if a replace ( or similar events
841        happens )
842        Only the action that are needed to change name should be considered.
843        @@@ To change after 1.0 release.
844      */
845      str_list_t *files_list;
846      str_list_elt_t *iter, *niter;
847
848      char *root_filename = NULL;
849
850      int clashes = 0;
851
852      files_list = pkg_get_installed_files(conf, pkg);
853      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
854              iter; 
855              iter = niter, niter = str_list_next(files_list, niter)) {
856           char *filename = (char *) iter->data;
857           if (root_filename) {
858               free(root_filename);
859               root_filename = NULL;
860           }
861           root_filename = root_filename_alloc(conf, filename);
862           if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
863                pkg_t *owner;
864
865                owner = file_hash_get_file_owner(conf, filename);
866
867                if (conf->force_overwrite) {
868                     /* but we need to change who owns this file */
869                     file_hash_set_file_owner(conf, filename, pkg);
870                     continue;
871                }
872
873
874                /* Pre-existing files are OK if owned by a package replaced by new pkg. */
875                if (owner) {
876                     if (pkg_replaces(pkg, owner)) {
877 /* It's now time to change the owner of that file. 
878    It has been "replaced" from the new "Replaces", then I need to inform lists file about that.  */
879                          opkg_message(conf, OPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
880                          file_hash_set_file_owner(conf, filename, pkg);
881                          continue;
882                     }
883                }
884
885           }
886      }
887      if (root_filename) {
888          free(root_filename);
889          root_filename = NULL;
890      }
891      pkg_free_installed_files(pkg);
892
893      return clashes;
894 }
895
896 static int
897 check_data_file_clashes_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
898 {
899      /* Nothing to do since check_data_file_clashes doesn't change state */
900      return 0;
901 }
902
903 static int
904 postrm_upgrade_old_pkg(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
905 {
906      /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
907         1. If the package is being upgraded, call
908            old-postrm upgrade new-version
909         2. If this fails, attempt:
910            new-postrm failed-upgrade old-version
911         Error unwind, for both cases:
912            old-preinst abort-upgrade new-version    */
913      return 0;
914 }
915
916 static int
917 postrm_upgrade_old_pkg_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
918 {
919      /* DPKG_INCOMPATIBILITY:
920         dpkg does some things here that we don't do yet. Do we care?
921         (See postrm_upgrade_old_pkg for details)
922      */
923     return 0;
924 }
925
926 static int
927 remove_obsolesced_files(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
928 {
929      int err;
930      str_list_t *old_files;
931      str_list_elt_t *of;
932      str_list_t *new_files;
933      str_list_elt_t *nf;
934      hash_table_t new_files_table;
935
936      if (old_pkg == NULL) {
937           return 0;
938      }
939
940      old_files = pkg_get_installed_files(conf, old_pkg);
941      new_files = pkg_get_installed_files(conf, pkg);
942
943      new_files_table.entries = NULL;
944      hash_table_init("new_files" , &new_files_table, 20);
945      for (nf = str_list_first(new_files); nf; nf = str_list_next(new_files, nf)) {
946          if (nf && nf->data)
947             hash_table_insert(&new_files_table, nf->data, nf->data);
948      }
949
950      for (of = str_list_first(old_files); of; of = str_list_next(old_files, of)) {
951           pkg_t *owner;
952           char *old, *new;
953           old = (char *)of->data;
954           new = (char *) hash_table_get (&new_files_table, old);
955           if (new)
956                continue;
957
958           if (file_is_dir(old)) {
959                continue;
960           }
961           owner = file_hash_get_file_owner(conf, old);
962           if (owner != old_pkg) {
963                /* in case obsolete file no longer belongs to old_pkg */
964                continue;
965           }
966  
967           /* old file is obsolete */
968           opkg_message(conf, OPKG_INFO,
969                        "    removing obsolete file %s\n", old);
970           if (!conf->noaction) {
971                err = unlink(old);
972                if (err) {
973                     opkg_message(conf, OPKG_ERROR, "    Warning: remove %s failed: %s\n", old,
974                                  strerror(errno));
975                }
976           }
977      }
978
979      hash_table_deinit(&new_files_table);
980      pkg_free_installed_files(old_pkg);
981      pkg_free_installed_files(pkg);
982
983      return 0;
984 }
985
986 static int
987 install_maintainer_scripts(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
988 {
989      int ret;
990      char *prefix;
991
992      sprintf_alloc(&prefix, "%s.", pkg->name);
993      ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
994                                                         pkg->dest->info_dir,
995                                                         prefix);
996      free(prefix);
997      return ret;
998 }
999
1000 static int
1001 remove_disappeared(opkg_conf_t *conf, pkg_t *pkg)
1002 {
1003      /* DPKG_INCOMPATIBILITY:
1004         This is a fairly sophisticated dpkg operation. Shall we
1005         skip it? */
1006      
1007      /* Any packages all of whose files have been overwritten during the
1008         installation, and which aren't required for dependencies, are
1009         considered to have been removed. For each such package
1010         1. disappearer's-postrm disappear overwriter overwriter-version
1011         2. The package's maintainer scripts are removed
1012         3. It is noted in the status database as being in a sane state,
1013            namely not installed (any conffiles it may have are ignored,
1014            rather than being removed by dpkg). Note that disappearing
1015            packages do not have their prerm called, because dpkg doesn't
1016            know in advance that the package is going to vanish.
1017      */
1018      return 0;
1019 }
1020
1021 static int
1022 install_data_files(opkg_conf_t *conf, pkg_t *pkg)
1023 {
1024      int err;
1025
1026      /* opkg takes a slightly different approach to data file backups
1027         than dpkg. Rather than removing backups at this point, we
1028         actually do the data file installation now. See comments in
1029         check_data_file_clashes() for more details. */
1030     
1031      opkg_message(conf, OPKG_INFO,
1032                   "    extracting data files to %s\n", pkg->dest->root_dir);
1033      err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
1034      if (err) {
1035           return err;
1036      }
1037
1038      /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
1039         so we can't save ourself from removing important packages
1040         At this point we (should) have extracted the .control file, so it
1041         would be a good idea to reload the data in it, and set the Essential 
1042         state in *pkg. From now on the Essential is back in status file and
1043         we can protect again.
1044         We should operate this way:
1045         fopen the file ( pkg->dest->root_dir/pkg->name.control )
1046         check for "Essential" in it 
1047         set the value in pkg->essential.
1048         This new routine could be useful also for every other flag
1049         Pigi: 16/03/2004 */
1050      set_flags_from_control(conf, pkg) ;
1051      
1052      opkg_message(conf, OPKG_DEBUG, "    Calling pkg_write_filelist from %s\n", __FUNCTION__);
1053      err = pkg_write_filelist(conf, pkg);
1054      if (err)
1055           return err;
1056
1057      /* XXX: FEATURE: opkg should identify any files which existed
1058         before installation and which were overwritten, (see
1059         check_data_file_clashes()). What it must do is remove any such
1060         files from the filelist of the old package which provided the
1061         file. Otherwise, if the old package were removed at some point
1062         it would break the new package. Removing the new package will
1063         also break the old one, but this cannot be helped since the old
1064         package's file has already been deleted. This is the importance
1065         of check_data_file_clashes(), and only allowing opkg to install
1066         a clashing package with a user force. */
1067
1068      return 0;
1069 }
1070
1071 static int
1072 user_prefers_old_conffile(const char *file_name, const char *backup)
1073 {
1074      char *response;
1075      const char *short_file_name;
1076
1077      short_file_name = strrchr(file_name, '/');
1078      if (short_file_name) {
1079           short_file_name++;
1080      } else {
1081           short_file_name = file_name;
1082      }
1083
1084      while (1) {
1085           response = get_user_response("    Configuration file '%s'\n"
1086                                        "    ==> File on system created by you or by a script.\n"
1087                                        "    ==> File also in package provided by package maintainer.\n"
1088                                        "       What would you like to do about it ?  Your options are:\n"
1089                                        "        Y or I  : install the package maintainer's version\n"
1090                                        "        N or O  : keep your currently-installed version\n"
1091                                        "          D     : show the differences between the versions (if diff is installed)\n"
1092                                        "     The default action is to keep your current version.\n"
1093                                        "    *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
1094
1095           if (response == NULL)
1096                   return 1;
1097
1098           if (strcmp(response, "y") == 0
1099               || strcmp(response, "i") == 0
1100               || strcmp(response, "yes") == 0) {
1101                free(response);
1102                return 0;
1103           }
1104
1105           if (strcmp(response, "d") == 0) {
1106                const char *argv[] = {"diff", "-u", backup, file_name, NULL};
1107                xsystem(argv);
1108                printf("    [Press ENTER to continue]\n");
1109                response = file_read_line_alloc(stdin);
1110                free(response);
1111                continue;
1112           }
1113
1114           free(response);
1115           return 1;
1116      }
1117 }
1118
1119 static int
1120 resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg)
1121 {
1122      conffile_list_elt_t *iter;
1123      conffile_t *cf;
1124      char *cf_backup;
1125      char *md5sum;
1126
1127      if (conf->noaction) return 0;
1128
1129      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
1130           char *root_filename;
1131           cf = (conffile_t *)iter->data;
1132           root_filename = root_filename_alloc(conf, cf->name);
1133
1134           /* Might need to initialize the md5sum for each conffile */
1135           if (cf->value == NULL) {
1136                cf->value = file_md5sum_alloc(root_filename);
1137           }
1138
1139           if (!file_exists(root_filename)) {
1140                free(root_filename);
1141                continue;
1142           }
1143
1144           cf_backup = backup_filename_alloc(root_filename);
1145
1146
1147           if (file_exists(cf_backup)) {
1148               /* Let's compute md5 to test if files are changed */
1149               md5sum = file_md5sum_alloc(cf_backup);
1150               if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
1151                   if (conf->force_maintainer) {
1152                       opkg_message(conf, OPKG_NOTICE, "Conffile %s using maintainer's setting.\n", cf_backup);
1153                   } else if (conf->force_defaults
1154                           || user_prefers_old_conffile(root_filename, cf_backup) ) {
1155                       rename(cf_backup, root_filename);
1156                   }
1157               }
1158               unlink(cf_backup);
1159               if (md5sum)
1160                   free(md5sum);
1161           }
1162
1163           free(cf_backup);
1164           free(root_filename);
1165      }
1166
1167      return 0;
1168 }
1169
1170
1171 opkg_error_t
1172 opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
1173 {
1174      int cmp, err = 0;
1175      pkg_t *old, *new;
1176      char *old_version, *new_version;
1177
1178      opkg_message(conf, OPKG_DEBUG2, " Getting old  from pkg_hash_fetch \n" );
1179      old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
1180      if ( old ) 
1181         opkg_message(conf, OPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n",  old->version );
1182     
1183      opkg_message(conf, OPKG_DEBUG2, " Getting new  from pkg_hash_fetch \n" );
1184      new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name, &err);
1185      if ( new ) 
1186         opkg_message(conf, OPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n",  new->version );
1187
1188 /* Pigi Basically here is broken the version stuff.
1189    What's happening is that nothing provide the version to differents 
1190    functions, so the returned struct is always the latest.
1191    That's why the install by name don't work.
1192 */
1193      opkg_message(conf, OPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
1194
1195      if ( old ) 
1196         opkg_message(conf, OPKG_DEBUG2, " old %s ", old->version );
1197      if ( new ) 
1198         opkg_message(conf, OPKG_DEBUG2, " new %s ", new->version );
1199      opkg_message(conf, OPKG_DEBUG2, " \n");
1200
1201      if (new == NULL) {
1202           if (err)
1203             return err;
1204           else
1205             return OPKG_PKG_HAS_NO_CANDIDATE;
1206      }
1207
1208      new->state_flag |= SF_USER;
1209      if (old) {
1210           old_version = pkg_version_str_alloc(old);
1211           new_version = pkg_version_str_alloc(new);
1212
1213           cmp = pkg_compare_versions(old, new);
1214           if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
1215              opkg_message(conf, OPKG_DEBUG, " Forcing downgrade \n");
1216              cmp = -1 ;                                       /* then we force opkg to downgrade */ 
1217                                                               /* We need to use a value < 0 because in the 0 case we are asking to */
1218                                                               /* reinstall, and some check could fail asking the "force-reinstall" option */
1219           } 
1220           opkg_message(conf, OPKG_DEBUG, 
1221                        "Comparing visible versions of pkg %s:"
1222                        "\n\t%s is installed "
1223                        "\n\t%s is available "
1224                        "\n\t%d was comparison result\n",
1225                        pkg_name, old_version, new_version, cmp);
1226           if (cmp == 0 && !conf->force_reinstall) {
1227                opkg_message(conf, OPKG_NOTICE,
1228                             "Package %s (%s) installed in %s is up to date.\n",
1229                             old->name, old_version, old->dest->name);
1230                free(old_version);
1231                free(new_version);
1232                return 0;
1233           } else if (cmp > 0) {
1234                opkg_message(conf, OPKG_NOTICE,
1235                             "Not downgrading package %s on %s from %s to %s.\n",
1236                             old->name, old->dest->name, old_version, new_version);
1237                free(old_version);
1238                free(new_version);
1239                return 0;
1240           } else if (cmp < 0) {
1241                new->dest = old->dest;
1242                old->state_want = SW_DEINSTALL;    /* Here probably the problem for bug 1277 */
1243           }
1244           free(old_version);
1245           free(new_version);
1246      }
1247
1248      /* XXX: CLEANUP: The error code of opkg_install_by_name is really
1249         supposed to be an opkg_error_t, but opkg_install_pkg could
1250         return any kind of integer, (might be errno from a syscall,
1251         etc.). This is a real mess and will need to be cleaned up if
1252         anyone ever wants to make a nice libopkg. */
1253
1254      opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
1255      return opkg_install_pkg(conf, new,0);
1256 }
1257
1258 /**
1259  *  @brief Really install a pkg_t 
1260  */
1261 int
1262 opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
1263 {
1264      int err = 0;
1265      int message = 0;
1266      pkg_t *old_pkg = NULL;
1267      pkg_vec_t *replacees;
1268      abstract_pkg_t *ab_pkg = NULL;
1269      int old_state_flag;
1270      char* file_md5;
1271 #ifdef HAVE_SHA256
1272      char* file_sha256;
1273 #endif
1274
1275      if ( from_upgrade ) 
1276         message = 1;            /* Coming from an upgrade, and should change the output message */
1277
1278      if (!pkg) {
1279           opkg_message(conf, OPKG_ERROR,
1280                        "INTERNAL ERROR: null pkg passed to opkg_install_pkg\n");
1281           return OPKG_INSTALL_ERR_INTERNAL;
1282      }
1283
1284      opkg_message(conf, OPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
1285
1286      if (!pkg_arch_supported(conf, pkg)) {
1287           opkg_message(conf, OPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1288                        pkg->architecture, pkg->name);
1289           return OPKG_INSTALL_ERR_INTERNAL;
1290      }
1291      if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
1292           err = satisfy_dependencies_for(conf, pkg);
1293           if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
1294
1295           opkg_message(conf, OPKG_NOTICE,
1296                        "Package %s is already installed in %s.\n", 
1297                        pkg->name, pkg->dest->name);
1298           return 0;
1299      }
1300
1301      if (pkg->dest == NULL) {
1302           pkg->dest = conf->default_dest;
1303      }
1304
1305      old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
1306
1307      err = opkg_install_check_downgrade(conf, pkg, old_pkg, message);
1308      if (err) { return OPKG_INSTALL_ERR_NO_DOWNGRADE; }
1309
1310      pkg->state_want = SW_INSTALL;
1311      if (old_pkg){                          
1312          old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
1313      }
1314
1315
1316      /* Abhaya: conflicts check */
1317      err = check_conflicts_for(conf, pkg);
1318      if (err) { return OPKG_INSTALL_ERR_CONFLICTS; }
1319     
1320      /* this setup is to remove the upgrade scenario in the end when
1321         installing pkg A, A deps B & B deps on A. So both B and A are
1322         installed. Then A's installation is started resulting in an
1323         uncecessary upgrade */ 
1324      if (pkg->state_status == SS_INSTALLED
1325          && conf->force_reinstall == 0) return 0;
1326     
1327      err = verify_pkg_installable(conf, pkg);
1328      if (err) { return OPKG_INSTALL_ERR_NO_SPACE; }
1329
1330      if (pkg->local_filename == NULL) {
1331           err = opkg_download_pkg(conf, pkg, conf->tmp_dir);
1332           if (err) {
1333                opkg_message(conf, OPKG_ERROR,
1334                             "Failed to download %s. Perhaps you need to run 'opkg update'?\n",
1335                             pkg->name);
1336                return OPKG_INSTALL_ERR_DOWNLOAD;
1337           }
1338      }
1339
1340      /* check that the repository is valid */
1341      #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1342      char *list_file_name, *sig_file_name, *lists_dir;
1343
1344      /* check to ensure the package has come from a repository */
1345      if (conf->check_signature && pkg->src)
1346      {
1347        sprintf_alloc (&lists_dir, "%s",
1348                      (conf->restrict_to_default_dest)
1349                       ? conf->default_dest->lists_dir
1350                       : conf->lists_dir);
1351        sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
1352        sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
1353
1354        if (file_exists (sig_file_name))
1355        {
1356          if (opkg_verify_file (conf, list_file_name, sig_file_name)){
1357            opkg_message(conf, OPKG_ERROR, "Failed to verify the signature of: %s\n",
1358                            list_file_name);
1359            return OPKG_INSTALL_ERR_SIGNATURE;
1360          }
1361        }else{
1362          opkg_message(conf, OPKG_ERROR, "Signature file is missing. "
1363                          "Perhaps you need to run 'opkg update'?\n");
1364          return OPKG_INSTALL_ERR_SIGNATURE;
1365        }
1366
1367        free (lists_dir);
1368        free (list_file_name);
1369        free (sig_file_name);
1370      }
1371      #endif
1372
1373      /* Check for md5 values */
1374      if (pkg->md5sum)
1375      {
1376          file_md5 = file_md5sum_alloc(pkg->local_filename);
1377          if (file_md5 && strcmp(file_md5, pkg->md5sum))
1378          {
1379               opkg_message(conf, OPKG_ERROR,
1380                            "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1381                            pkg->name);
1382               free(file_md5);
1383               return OPKG_INSTALL_ERR_MD5;
1384          }
1385          if (file_md5)
1386               free(file_md5);
1387      }
1388
1389 #ifdef HAVE_SHA256
1390      /* Check for sha256 value */
1391      if(pkg->sha256sum)
1392      {
1393          file_sha256 = file_sha256sum_alloc(pkg->local_filename);
1394          if (file_sha256 && strcmp(file_sha256, pkg->sha256sum))
1395          {
1396               opkg_message(conf, OPKG_ERROR,
1397                            "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1398                            pkg->name);
1399               free(file_sha256);
1400               return OPKG_INSTALL_ERR_SHA256;
1401          }
1402          if (file_sha256)
1403               free(file_sha256);
1404      }
1405 #endif
1406
1407      if (pkg->tmp_unpack_dir == NULL) {
1408           unpack_pkg_control_files(conf, pkg);
1409      }
1410
1411      /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
1412 /* Pigi: check if it will pass from here when replacing. It seems to fail */
1413 /* That's rather strange that files don't change owner. Investigate !!!!!!*/
1414      err = update_file_ownership(conf, pkg, old_pkg);
1415      if (err) { return OPKG_ERR_UNKNOWN; }
1416
1417      if (conf->nodeps == 0) {
1418           err = satisfy_dependencies_for(conf, pkg);
1419           if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
1420           if (pkg->state_status == SS_UNPACKED)
1421                /* Circular dependency has installed it for us. */
1422                return 0;
1423      }
1424
1425      replacees = pkg_vec_alloc();
1426      pkg_get_installed_replacees(conf, pkg, replacees);
1427
1428      /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1429      {
1430           sigset_t newset, oldset;
1431
1432           sigemptyset(&newset);
1433           sigaddset(&newset, SIGINT);
1434           sigprocmask(SIG_BLOCK, &newset, &oldset);
1435
1436           opkg_state_changed++;
1437           pkg->state_flag |= SF_FILELIST_CHANGED;
1438
1439           if (old_pkg)
1440                pkg_remove_orphan_dependent(conf, pkg, old_pkg);
1441
1442           /* XXX: BUG: we really should treat replacement more like an upgrade
1443            *      Instead, we're going to remove the replacees 
1444            */
1445           err = pkg_remove_installed_replacees(conf, replacees);
1446           if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
1447
1448           err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
1449           if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
1450
1451           err = prerm_deconfigure_conflictors(conf, pkg, replacees);
1452           if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
1453
1454           err = preinst_configure(conf, pkg, old_pkg);
1455           if (err) goto UNWIND_PREINST_CONFIGURE;
1456
1457           err = backup_modified_conffiles(conf, pkg, old_pkg);
1458           if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
1459
1460           err = check_data_file_clashes(conf, pkg, old_pkg);
1461           if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
1462
1463           err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
1464           if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
1465
1466           if (conf->noaction) return 0;
1467
1468           /* point of no return: no unwinding after this */
1469           if (old_pkg && !conf->force_reinstall) {
1470                old_pkg->state_want = SW_DEINSTALL;
1471
1472                if (old_pkg->state_flag & SF_NOPRUNE) {
1473                     opkg_message(conf, OPKG_INFO,
1474                                  "  not removing obsolesced files because package marked noprune\n");
1475                } else {
1476                     opkg_message(conf, OPKG_INFO,
1477                                  "  removing obsolesced files\n");
1478                     remove_obsolesced_files(conf, pkg, old_pkg);
1479                }
1480                /* removing files from old package, to avoid ghost files */ 
1481                remove_data_files_and_list(conf, old_pkg);
1482 /* Pigi : It should be better to remove also maintainer and postrem scripts here, just in case*/
1483                remove_maintainer_scripts_except_postrm(conf, old_pkg);
1484                remove_postrm(conf, old_pkg);
1485 /* Pigi */
1486
1487           }
1488
1489
1490           opkg_message(conf, OPKG_INFO,
1491                        "  installing maintainer scripts\n");
1492           install_maintainer_scripts(conf, pkg, old_pkg);
1493
1494           /* the following just returns 0 */
1495           remove_disappeared(conf, pkg);
1496
1497           opkg_message(conf, OPKG_INFO,
1498                        "  installing data files\n");
1499           install_data_files(conf, pkg);
1500
1501 /* read comments from function for detail but I will execute this here as all other tests are ok.*/
1502           err = check_data_file_clashes_change(conf, pkg, old_pkg);
1503
1504           opkg_message(conf, OPKG_INFO,
1505                        "  resolving conf files\n");
1506           resolve_conffiles(conf, pkg);
1507
1508           pkg->state_status = SS_UNPACKED;
1509           old_state_flag = pkg->state_flag;
1510           pkg->state_flag &= ~SF_PREFER;
1511           opkg_message(conf, OPKG_DEBUG, "   pkg=%s old_state_flag=%x state_flag=%x\n", pkg->name, old_state_flag, pkg->state_flag);
1512
1513           if (old_pkg && !conf->force_reinstall) {
1514                old_pkg->state_status = SS_NOT_INSTALLED;
1515           }
1516
1517           time(&pkg->installed_time);
1518
1519           ab_pkg = pkg->parent;
1520           if (ab_pkg)
1521                ab_pkg->state_status = pkg->state_status;
1522
1523           opkg_message(conf, OPKG_INFO, "Done.\n");
1524
1525           sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1526           pkg_vec_free (replacees);
1527           return 0;
1528      
1529
1530      UNWIND_POSTRM_UPGRADE_OLD_PKG:
1531           postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
1532      UNWIND_CHECK_DATA_FILE_CLASHES:
1533           check_data_file_clashes_unwind(conf, pkg, old_pkg);
1534      UNWIND_BACKUP_MODIFIED_CONFFILES:
1535           backup_modified_conffiles_unwind(conf, pkg, old_pkg);
1536      UNWIND_PREINST_CONFIGURE:
1537           preinst_configure_unwind(conf, pkg, old_pkg);
1538      UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
1539           prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
1540      UNWIND_PRERM_UPGRADE_OLD_PKG:
1541           prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
1542      UNWIND_REMOVE_INSTALLED_REPLACEES:
1543           pkg_remove_installed_replacees_unwind(conf, replacees);
1544
1545           opkg_message(conf, OPKG_INFO,
1546                        "Failed.\n");
1547
1548           sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1549
1550           pkg_vec_free (replacees);
1551           return OPKG_ERR_UNKNOWN;
1552      }
1553 }