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