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