Fix check_data_file_clashes() for offline root mode. (almost anyway)
[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_message(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,r, err = 0;
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                                 r = opkg_remove_pkg(p, 0);
366                                 if (!err)
367                                         err = r;
368                         } else 
369                                 opkg_msg(INFO, "%s was autoinstalled and is "
370                                                 "still required by %d "
371                                                 "installed packages.\n",
372                                                 p->name, n_deps);
373
374                 }
375         }
376
377         return err;
378 }
379
380 /* returns number of installed replacees */
381 static int
382 pkg_get_installed_replacees(pkg_t *pkg, pkg_vec_t *installed_replacees)
383 {
384      abstract_pkg_t **replaces = pkg->replaces;
385      int replaces_count = pkg->replaces_count;
386      int i, j;
387      for (i = 0; i < replaces_count; i++) {
388           abstract_pkg_t *ab_pkg = replaces[i];
389           pkg_vec_t *pkg_vec = ab_pkg->pkgs;
390           if (pkg_vec) {
391                for (j = 0; j < pkg_vec->len; j++) {
392                     pkg_t *replacee = pkg_vec->pkgs[j];
393                     if (!pkg_conflicts(pkg, replacee))
394                          continue;
395                     if (replacee->state_status == SS_INSTALLED) {
396                          pkg_vec_insert(installed_replacees, replacee);
397                     }
398                }
399           }
400      }
401      return installed_replacees->len;
402 }
403
404 static int
405 pkg_remove_installed_replacees(pkg_vec_t *replacees)
406 {
407      int i;
408      int replaces_count = replacees->len;
409      for (i = 0; i < replaces_count; i++) {
410           pkg_t *replacee = replacees->pkgs[i];
411           int err;
412           replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
413           err = opkg_remove_pkg(replacee, 0);
414           if (err)
415                return err;
416      }
417      return 0;
418 }
419
420 /* to unwind the removal: make sure they are installed */
421 static int
422 pkg_remove_installed_replacees_unwind(pkg_vec_t *replacees)
423 {
424      int i, err;
425      int replaces_count = replacees->len;
426      for (i = 0; i < replaces_count; i++) {
427           pkg_t *replacee = replacees->pkgs[i];
428           if (replacee->state_status != SS_INSTALLED) {
429                opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
430                err = opkg_install_pkg(replacee, 0);
431                if (err)
432                     return err;
433           }
434      }
435      return 0;
436 }
437
438 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
439 static int
440 opkg_install_check_downgrade(pkg_t *pkg, pkg_t *old_pkg, int message)
441 {         
442      if (old_pkg) {
443           char message_out[15];
444           char *old_version = pkg_version_str_alloc(old_pkg);
445           char *new_version = pkg_version_str_alloc(pkg);
446           int cmp = pkg_compare_versions(old_pkg, pkg);
447           int rc = 0;
448
449           memset(message_out,'\x0',15);
450           strncpy (message_out,"Upgrading ",strlen("Upgrading ")); 
451           if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
452              cmp = -1 ;                                       /* then we force opkg to downgrade */ 
453              strncpy (message_out,"Downgrading ",strlen("Downgrading "));         /* We need to use a value < 0 because in the 0 case we are asking to */
454                                                               /* reinstall, and some check could fail asking the "force-reinstall" option */
455           } 
456
457           if (cmp > 0) {
458               if(!conf->download_only)
459                   opkg_msg(NOTICE,
460                           "Not downgrading package %s on %s from %s to %s.\n",
461                           old_pkg->name, old_pkg->dest->name, old_version, new_version);
462                rc = 1;
463           } else if (cmp < 0) {
464               if(!conf->download_only)
465                   opkg_msg(NOTICE, "%s%s on %s from %s to %s...\n",
466                           message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
467                pkg->dest = old_pkg->dest;
468                rc = 0;
469           } else /* cmp == 0 */ {
470                if (conf->force_reinstall) {
471                if(!conf->download_only)
472                    opkg_msg(NOTICE, "Reinstalling %s (%s) on %s...\n",
473                            pkg->name, new_version, old_pkg->dest->name);
474                     pkg->dest = old_pkg->dest;
475                     rc = 0;
476                } else {
477                    if(!conf->download_only)
478                        opkg_msg(NOTICE, "%s (%s) already install on %s."
479                                " Not reinstalling.\n",
480                                pkg->name, new_version, old_pkg->dest->name);
481                     rc = 1;
482                }
483           } 
484           free(old_version);
485           free(new_version);
486           return rc;
487      } else {
488           char message_out[15] ;
489           memset(message_out,'\x0',15);
490           if ( message ) 
491                strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
492           else
493                strncpy( message_out,"Installing ",strlen("Installing ") );
494           char *version = pkg_version_str_alloc(pkg);
495
496           if(!conf->download_only)
497                opkg_msg(NOTICE, "%s%s (%s) to %s...\n", message_out,
498                   pkg->name, version, pkg->dest->name);
499           free(version);
500      }
501      return 0;
502 }
503
504
505 static int
506 prerm_upgrade_old_pkg(pkg_t *pkg, pkg_t *old_pkg)
507 {
508      /* DPKG_INCOMPATIBILITY:
509         dpkg does some things here that we don't do yet. Do we care?
510         
511         1. If a version of the package is already installed, call
512            old-prerm upgrade new-version
513         2. If the script runs but exits with a non-zero exit status
514            new-prerm failed-upgrade old-version
515            Error unwind, for both the above cases:
516            old-postinst abort-upgrade new-version
517      */
518      return 0;
519 }
520
521 static int
522 prerm_upgrade_old_pkg_unwind(pkg_t *pkg, pkg_t *old_pkg)
523 {
524      /* DPKG_INCOMPATIBILITY:
525         dpkg does some things here that we don't do yet. Do we care?
526         (See prerm_upgrade_old_package for details)
527      */
528      return 0;
529 }
530
531 static int
532 prerm_deconfigure_conflictors(pkg_t *pkg, pkg_vec_t *conflictors)
533 {
534      /* DPKG_INCOMPATIBILITY:
535         dpkg does some things here that we don't do yet. Do we care?
536         2. If a 'conflicting' package is being removed at the same time:
537                 1. If any packages depended on that conflicting package and
538                    --auto-deconfigure is specified, call, for each such package:
539                    deconfigured's-prerm deconfigure \
540                    in-favour package-being-installed version \
541                    removing conflicting-package version
542                 Error unwind:
543                    deconfigured's-postinst abort-deconfigure \
544                    in-favour package-being-installed-but-failed version \
545                    removing conflicting-package version
546
547                    The deconfigured packages are marked as requiring
548                    configuration, so that if --install is used they will be
549                    configured again if possible.
550                 2. To prepare for removal of the conflicting package, call:
551                    conflictor's-prerm remove in-favour package new-version
552                 Error unwind:
553                    conflictor's-postinst abort-remove in-favour package new-version
554      */
555      return 0;
556 }
557
558 static int
559 prerm_deconfigure_conflictors_unwind(pkg_t *pkg, pkg_vec_t *conflictors)
560 {
561      /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
562         do yet. Do we care?  (See prerm_deconfigure_conflictors for
563         details) */
564      return 0;
565 }
566
567 static int
568 preinst_configure(pkg_t *pkg, pkg_t *old_pkg)
569 {
570      int err;
571      char *preinst_args;
572
573      if (old_pkg) {
574           char *old_version = pkg_version_str_alloc(old_pkg);
575           sprintf_alloc(&preinst_args, "upgrade %s", old_version);
576           free(old_version);
577      } else if (pkg->state_status == SS_CONFIG_FILES) {
578           char *pkg_version = pkg_version_str_alloc(pkg);
579           sprintf_alloc(&preinst_args, "install %s", pkg_version);
580           free(pkg_version);
581      } else {
582           preinst_args = xstrdup("install");
583      }
584
585      err = pkg_run_script(pkg, "preinst", preinst_args);
586      if (err) {
587           opkg_msg(ERROR, "Aborting installation of %s.\n", pkg->name);
588           return -1;
589      }
590
591      free(preinst_args);
592
593      return 0;
594 }
595
596 static int
597 preinst_configure_unwind(pkg_t *pkg, pkg_t *old_pkg)
598 {
599      /* DPKG_INCOMPATIBILITY:
600         dpkg does the following error unwind, should we?
601         pkg->postrm abort-upgrade old-version
602         OR pkg->postrm abort-install old-version
603         OR pkg->postrm abort-install
604      */
605      return 0;
606 }
607
608 static char *
609 backup_filename_alloc(const char *file_name)
610 {
611      char *backup;
612
613      sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
614
615      return backup;
616 }
617
618
619 static int
620 backup_make_backup(const char *file_name)
621 {
622      int err;
623      char *backup;
624     
625      backup = backup_filename_alloc(file_name);
626      err = file_copy(file_name, backup);
627      if (err) {
628           opkg_msg(ERROR, "Failed to copy %s to %s\n",
629                        file_name, backup);
630      }
631
632      free(backup);
633
634      return err;
635 }
636
637 static int
638 backup_exists_for(const char *file_name)
639 {
640      int ret;
641      char *backup;
642
643      backup = backup_filename_alloc(file_name);
644
645      ret = file_exists(backup);
646
647      free(backup);
648
649      return ret;
650 }
651
652 static int
653 backup_remove(const char *file_name)
654 {
655      char *backup;
656
657      backup = backup_filename_alloc(file_name);
658      unlink(backup);
659      free(backup);
660
661      return 0;
662 }
663
664 static int
665 backup_modified_conffiles(pkg_t *pkg, pkg_t *old_pkg)
666 {
667      int err;
668      conffile_list_elt_t *iter;
669      conffile_t *cf;
670
671      if (conf->noaction) return 0;
672
673      /* Backup all modified conffiles */
674      if (old_pkg) {
675           for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
676                char *cf_name;
677                
678                cf = iter->data;
679                cf_name = root_filename_alloc(cf->name);
680
681                /* Don't worry if the conffile is just plain gone */
682                if (file_exists(cf_name) && conffile_has_been_modified(cf)) {
683                     err = backup_make_backup(cf_name);
684                     if (err) {
685                          return err;
686                     }
687                }
688                free(cf_name);
689           }
690      }
691
692      /* Backup all conffiles that were not conffiles in old_pkg */
693      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
694           char *cf_name;
695           cf = (conffile_t *)iter->data;
696           cf_name = root_filename_alloc(cf->name);
697           /* Ignore if this was a conffile in old_pkg as well */
698           if (pkg_get_conffile(old_pkg, cf->name)) {
699                continue;
700           }
701
702           if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
703                err = backup_make_backup(cf_name);
704                if (err) {
705                     return err;
706                }
707           }
708           free(cf_name);
709      }
710
711      return 0;
712 }
713
714 static int
715 backup_modified_conffiles_unwind(pkg_t *pkg, pkg_t *old_pkg)
716 {
717      conffile_list_elt_t *iter;
718
719      if (old_pkg) {
720           for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
721                backup_remove(((nv_pair_t *)iter->data)->name);
722           }
723      }
724
725      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
726           backup_remove(((nv_pair_t *)iter->data)->name);
727      }
728
729      return 0;
730 }
731
732
733 static int
734 check_data_file_clashes(pkg_t *pkg, pkg_t *old_pkg)
735 {
736      /* DPKG_INCOMPATIBILITY:
737         opkg takes a slightly different approach than dpkg at this
738         point.  dpkg installs each file in the new package while
739         creating a backup for any file that is replaced, (so that it
740         can unwind if necessary).  To avoid complexity and redundant
741         storage, opkg doesn't do any installation until later, (at the
742         point at which dpkg removes the backups.
743         
744         But, we do have to check for data file clashes, since after
745         installing a package with a file clash, removing either of the
746         packages involved in the clash has the potential to break the
747         other package.
748      */
749      str_list_t *files_list;
750      str_list_elt_t *iter, *niter;
751      char *filename;
752      int clashes = 0;
753
754      files_list = pkg_get_installed_files(pkg);
755      if (files_list == NULL)
756              return -1;
757
758      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
759              iter; 
760              iter = niter, niter = str_list_next(files_list, iter)) {
761           filename = (char *) iter->data;
762           if (file_exists(filename) && (! file_is_dir(filename))) {
763                pkg_t *owner;
764                pkg_t *obs;
765
766                if (backup_exists_for(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_message(ERROR, "%s\n", owner->name);
818                } else {
819                     opkg_message(ERROR, "<no package>\n"
820                         "Please move this file out of the way and try again.\n");
821                }
822                clashes++;
823           }
824      }
825      pkg_free_installed_files(pkg);
826
827      return clashes;
828 }
829
830 /*
831  * XXX: This function sucks, as does the below comment.
832  */
833 static int
834 check_data_file_clashes_change(pkg_t *pkg, pkg_t *old_pkg)
835 {
836     /* Basically that's the worst hack I could do to be able to change ownership of
837        file list, but, being that we have no way to unwind the mods, due to structure
838        of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
839        What we do here is change the ownership of file in hash if a replace ( or similar events
840        happens )
841        Only the action that are needed to change name should be considered.
842        @@@ To change after 1.0 release.
843      */
844      str_list_t *files_list;
845      str_list_elt_t *iter, *niter;
846
847      char *root_filename = NULL;
848
849      files_list = pkg_get_installed_files(pkg);
850      if (files_list == NULL)
851              return -1;
852
853      for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter); 
854              iter; 
855              iter = niter, niter = str_list_next(files_list, niter)) {
856           char *filename = (char *) iter->data;
857           if (root_filename) {
858               free(root_filename);
859               root_filename = NULL;
860           }
861           root_filename = root_filename_alloc(filename);
862           if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
863                pkg_t *owner;
864
865                owner = file_hash_get_file_owner(filename);
866
867                if (conf->force_overwrite) {
868                     /* but we need to change who owns this file */
869                     file_hash_set_file_owner(filename, pkg);
870                     continue;
871                }
872
873
874                /* Pre-existing files are OK if owned by a package replaced by new pkg. */
875                if (owner) {
876                     if (pkg_replaces(pkg, owner)) {
877 /* It's now time to change the owner of that file. 
878    It has been "replaced" from the new "Replaces", then I need to inform lists file about that.  */
879                          opkg_msg(INFO, "Replacing pre-existing file %s "
880                                          "owned by package %s\n",
881                                          filename, owner->name);
882                          file_hash_set_file_owner(filename, pkg);
883                          continue;
884                     }
885                }
886
887           }
888      }
889      if (root_filename) {
890          free(root_filename);
891          root_filename = NULL;
892      }
893      pkg_free_installed_files(pkg);
894
895      return 0;
896 }
897
898 static int
899 check_data_file_clashes_unwind(pkg_t *pkg, pkg_t *old_pkg)
900 {
901      /* Nothing to do since check_data_file_clashes doesn't change state */
902      return 0;
903 }
904
905 static int
906 postrm_upgrade_old_pkg(pkg_t *pkg, pkg_t *old_pkg)
907 {
908      /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
909         1. If the package is being upgraded, call
910            old-postrm upgrade new-version
911         2. If this fails, attempt:
912            new-postrm failed-upgrade old-version
913         Error unwind, for both cases:
914            old-preinst abort-upgrade new-version    */
915      return 0;
916 }
917
918 static int
919 postrm_upgrade_old_pkg_unwind(pkg_t *pkg, pkg_t *old_pkg)
920 {
921      /* DPKG_INCOMPATIBILITY:
922         dpkg does some things here that we don't do yet. Do we care?
923         (See postrm_upgrade_old_pkg for details)
924      */
925     return 0;
926 }
927
928 static int
929 remove_obsolesced_files(pkg_t *pkg, pkg_t *old_pkg)
930 {
931      int err = 0;
932      str_list_t *old_files;
933      str_list_elt_t *of;
934      str_list_t *new_files;
935      str_list_elt_t *nf;
936      hash_table_t new_files_table;
937
938      old_files = pkg_get_installed_files(old_pkg);
939      if (old_files == NULL)
940           return -1;
941
942      new_files = pkg_get_installed_files(pkg);
943      if (new_files == NULL) {
944           pkg_free_installed_files(old_pkg);
945           return -1;
946      }
947
948      new_files_table.entries = NULL;
949      hash_table_init("new_files" , &new_files_table, 20);
950      for (nf = str_list_first(new_files); nf; nf = str_list_next(new_files, nf)) {
951          if (nf && nf->data)
952             hash_table_insert(&new_files_table, nf->data, nf->data);
953      }
954
955      for (of = str_list_first(old_files); of; of = str_list_next(old_files, of)) {
956           pkg_t *owner;
957           char *old, *new;
958           old = (char *)of->data;
959           new = (char *) hash_table_get (&new_files_table, old);
960           if (new)
961                continue;
962
963           if (file_is_dir(old)) {
964                continue;
965           }
966           owner = file_hash_get_file_owner(old);
967           if (owner != old_pkg) {
968                /* in case obsolete file no longer belongs to old_pkg */
969                continue;
970           }
971  
972           /* old file is obsolete */
973           opkg_msg(INFO, "Removing obsolete file %s.\n", old);
974           if (!conf->noaction) {
975                err = unlink(old);
976                if (err) {
977                     opkg_perror(ERROR, "unlinking %s failed", old);
978                }
979           }
980      }
981
982      hash_table_deinit(&new_files_table);
983      pkg_free_installed_files(old_pkg);
984      pkg_free_installed_files(pkg);
985
986      return err;
987 }
988
989 static int
990 install_maintainer_scripts(pkg_t *pkg, pkg_t *old_pkg)
991 {
992      int ret;
993      char *prefix;
994
995      sprintf_alloc(&prefix, "%s.", pkg->name);
996      ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
997                                                         pkg->dest->info_dir,
998                                                         prefix);
999      free(prefix);
1000      return ret;
1001 }
1002
1003 static int
1004 remove_disappeared(pkg_t *pkg)
1005 {
1006      /* DPKG_INCOMPATIBILITY:
1007         This is a fairly sophisticated dpkg operation. Shall we
1008         skip it? */
1009      
1010      /* Any packages all of whose files have been overwritten during the
1011         installation, and which aren't required for dependencies, are
1012         considered to have been removed. For each such package
1013         1. disappearer's-postrm disappear overwriter overwriter-version
1014         2. The package's maintainer scripts are removed
1015         3. It is noted in the status database as being in a sane state,
1016            namely not installed (any conffiles it may have are ignored,
1017            rather than being removed by dpkg). Note that disappearing
1018            packages do not have their prerm called, because dpkg doesn't
1019            know in advance that the package is going to vanish.
1020      */
1021      return 0;
1022 }
1023
1024 static int
1025 install_data_files(pkg_t *pkg)
1026 {
1027      int err;
1028
1029      /* opkg takes a slightly different approach to data file backups
1030         than dpkg. Rather than removing backups at this point, we
1031         actually do the data file installation now. See comments in
1032         check_data_file_clashes() for more details. */
1033     
1034      opkg_msg(INFO, "Extracting data files to %s.\n", pkg->dest->root_dir);
1035      err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
1036      if (err) {
1037           return err;
1038      }
1039
1040      /* The "Essential" control field may only be present in the control
1041       * file and not in the Packages list. Ensure we capture it regardless.
1042       *
1043       * XXX: This should be fixed outside of opkg, in the Package list.
1044       */
1045      set_flags_from_control(pkg) ;
1046      
1047      opkg_msg(DEBUG, "Calling pkg_write_filelist.\n");
1048      err = pkg_write_filelist(pkg);
1049      if (err)
1050           return err;
1051
1052      /* XXX: FEATURE: opkg should identify any files which existed
1053         before installation and which were overwritten, (see
1054         check_data_file_clashes()). What it must do is remove any such
1055         files from the filelist of the old package which provided the
1056         file. Otherwise, if the old package were removed at some point
1057         it would break the new package. Removing the new package will
1058         also break the old one, but this cannot be helped since the old
1059         package's file has already been deleted. This is the importance
1060         of check_data_file_clashes(), and only allowing opkg to install
1061         a clashing package with a user force. */
1062
1063      return 0;
1064 }
1065
1066 static int
1067 resolve_conffiles(pkg_t *pkg)
1068 {
1069      conffile_list_elt_t *iter;
1070      conffile_t *cf;
1071      char *cf_backup;
1072      char *md5sum;
1073
1074      if (conf->noaction) return 0;
1075
1076      for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
1077           char *root_filename;
1078           cf = (conffile_t *)iter->data;
1079           root_filename = root_filename_alloc(cf->name);
1080
1081           /* Might need to initialize the md5sum for each conffile */
1082           if (cf->value == NULL) {
1083                cf->value = file_md5sum_alloc(root_filename);
1084           }
1085
1086           if (!file_exists(root_filename)) {
1087                free(root_filename);
1088                continue;
1089           }
1090
1091           cf_backup = backup_filename_alloc(root_filename);
1092
1093           if (file_exists(cf_backup)) {
1094               /* Let's compute md5 to test if files are changed */
1095               md5sum = file_md5sum_alloc(cf_backup);
1096               if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
1097                   if (conf->force_maintainer) {
1098                       opkg_msg(NOTICE, "Conffile %s using maintainer's setting.\n",
1099                                       cf_backup);
1100                   } else {
1101                       char *new_conffile;
1102                       sprintf_alloc(&new_conffile, "%s-opkg", root_filename);
1103                       opkg_msg(ERROR, "Existing conffile %s "
1104                            "is different from the conffile in the new package."
1105                            " The new conffile will be placed at %s.\n",
1106                            root_filename, new_conffile);
1107                       rename(root_filename, new_conffile);
1108                       rename(cf_backup, root_filename);
1109                       free(new_conffile);
1110                   }
1111               }
1112               unlink(cf_backup);
1113               if (md5sum)
1114                   free(md5sum);
1115           }
1116
1117           free(cf_backup);
1118           free(root_filename);
1119      }
1120
1121      return 0;
1122 }
1123
1124
1125 int
1126 opkg_install_by_name(const char *pkg_name)
1127 {
1128      int cmp;
1129      pkg_t *old, *new;
1130      char *old_version, *new_version;
1131
1132      old = pkg_hash_fetch_installed_by_name(pkg_name);
1133      if (old)
1134         opkg_msg(DEBUG2, "Old versions from pkg_hash_fetch %s.\n",
1135                         old->version);
1136     
1137      new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name);
1138      if (new == NULL) {
1139         opkg_msg(NOTICE, "Unknown package '%s'.\n", pkg_name);
1140         return -1;
1141      }
1142
1143      opkg_msg(DEBUG2, "Versions from pkg_hash_fetch:");
1144      if ( old ) 
1145         opkg_msg(DEBUG2, " old %s ", old->version);
1146      opkg_msg(DEBUG2, " new %s\n", new->version);
1147
1148      new->state_flag |= SF_USER;
1149      if (old) {
1150           old_version = pkg_version_str_alloc(old);
1151           new_version = pkg_version_str_alloc(new);
1152
1153           cmp = pkg_compare_versions(old, new);
1154           if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
1155              opkg_msg(DEBUG, "Forcing downgrade\n");
1156              cmp = -1 ;                                       /* then we force opkg to downgrade */ 
1157                                                               /* We need to use a value < 0 because in the 0 case we are asking to */
1158                                                               /* reinstall, and some check could fail asking the "force-reinstall" option */
1159           } 
1160           opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
1161                        "\n\t%s is installed "
1162                        "\n\t%s is available "
1163                        "\n\t%d was comparison result\n",
1164                        pkg_name, old_version, new_version, cmp);
1165           if (cmp == 0 && !conf->force_reinstall) {
1166                opkg_msg(NOTICE,
1167                             "Package %s (%s) installed in %s is up to date.\n",
1168                             old->name, old_version, old->dest->name);
1169                free(old_version);
1170                free(new_version);
1171                return 0;
1172           } else if (cmp > 0) {
1173                opkg_msg(NOTICE,
1174                             "Not downgrading package %s on %s from %s to %s.\n",
1175                             old->name, old->dest->name, old_version, new_version);
1176                free(old_version);
1177                free(new_version);
1178                return 0;
1179           } else if (cmp < 0) {
1180                new->dest = old->dest;
1181                old->state_want = SW_DEINSTALL;    /* Here probably the problem for bug 1277 */
1182           }
1183           free(old_version);
1184           free(new_version);
1185      }
1186
1187      opkg_msg(DEBUG2,"Calling opkg_install_pkg.\n");
1188      return opkg_install_pkg(new, 0);
1189 }
1190
1191 /**
1192  *  @brief Really install a pkg_t 
1193  */
1194 int
1195 opkg_install_pkg(pkg_t *pkg, int from_upgrade)
1196 {
1197      int err = 0;
1198      int message = 0;
1199      pkg_t *old_pkg = NULL;
1200      pkg_vec_t *replacees;
1201      abstract_pkg_t *ab_pkg = NULL;
1202      int old_state_flag;
1203      char* file_md5;
1204 #ifdef HAVE_SHA256
1205      char* file_sha256;
1206 #endif
1207      sigset_t newset, oldset;
1208
1209      if ( from_upgrade ) 
1210         message = 1;            /* Coming from an upgrade, and should change the output message */
1211
1212      opkg_msg(DEBUG2, "Calling pkg_arch_supported.\n");
1213
1214      if (!pkg_arch_supported(pkg)) {
1215           opkg_msg(ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1216                        pkg->architecture, pkg->name);
1217           return -1;
1218      }
1219      if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
1220           err = satisfy_dependencies_for(pkg);
1221           if (err)
1222                   return -1;
1223
1224           opkg_msg(NOTICE, "Package %s is already installed on %s.\n", 
1225                        pkg->name, pkg->dest->name);
1226           return 0;
1227      }
1228
1229      if (pkg->dest == NULL) {
1230           pkg->dest = conf->default_dest;
1231      }
1232
1233      old_pkg = pkg_hash_fetch_installed_by_name(pkg->name);
1234
1235      err = opkg_install_check_downgrade(pkg, old_pkg, message);
1236      if (err)
1237              return -1;
1238
1239      pkg->state_want = SW_INSTALL;
1240      if (old_pkg){                          
1241          old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependencies */
1242      }
1243
1244      err = check_conflicts_for(pkg);
1245      if (err)
1246              return -1;
1247     
1248      /* this setup is to remove the upgrade scenario in the end when
1249         installing pkg A, A deps B & B deps on A. So both B and A are
1250         installed. Then A's installation is started resulting in an
1251         uncecessary upgrade */ 
1252      if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0)
1253              return 0;
1254     
1255      err = verify_pkg_installable(pkg);
1256      if (err)
1257              return -1;
1258
1259      if (pkg->local_filename == NULL) {
1260          if(!conf->cache && conf->download_only){
1261              char cwd[4096];
1262              if(getcwd(cwd, sizeof(cwd)) != NULL)
1263                 err = opkg_download_pkg(pkg, cwd);
1264              else
1265                 return -1;
1266          } else {
1267              err = opkg_download_pkg(pkg, conf->tmp_dir);
1268          }
1269           if (err) {
1270                opkg_msg(ERROR, "Failed to download %s. "
1271                                "Perhaps you need to run 'opkg update'?\n",
1272                             pkg->name);
1273                return -1;
1274           }
1275      }
1276
1277      /* check that the repository is valid */
1278      #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1279      char *list_file_name, *sig_file_name, *lists_dir;
1280
1281      /* check to ensure the package has come from a repository */
1282      if (conf->check_signature && pkg->src)
1283      {
1284        sprintf_alloc (&lists_dir, "%s",
1285                      (conf->restrict_to_default_dest)
1286                       ? conf->default_dest->lists_dir
1287                       : conf->lists_dir);
1288        sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
1289        sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
1290
1291        if (file_exists (sig_file_name))
1292        {
1293          if (opkg_verify_file (list_file_name, sig_file_name)){
1294            opkg_msg(ERROR, "Failed to verify the signature of %s.\n",
1295                            list_file_name);
1296            return -1;
1297          }
1298        }else{
1299          opkg_msg(ERROR, "Signature file is missing for %s. "
1300                          "Perhaps you need to run 'opkg update'?\n",
1301                          pkg->name);
1302          return -1;
1303        }
1304
1305        free (lists_dir);
1306        free (list_file_name);
1307        free (sig_file_name);
1308      }
1309      #endif
1310
1311      /* Check for md5 values */
1312      if (pkg->md5sum)
1313      {
1314          file_md5 = file_md5sum_alloc(pkg->local_filename);
1315          if (file_md5 && strcmp(file_md5, pkg->md5sum))
1316          {
1317               opkg_msg(ERROR, "Package %s md5sum mismatch. "
1318                         "Either the opkg or the package index are corrupt. "
1319                         "Try 'opkg update'.\n",
1320                         pkg->name);
1321               free(file_md5);
1322               return -1;
1323          }
1324          if (file_md5)
1325               free(file_md5);
1326      }
1327
1328 #ifdef HAVE_SHA256
1329      /* Check for sha256 value */
1330      if(pkg->sha256sum)
1331      {
1332          file_sha256 = file_sha256sum_alloc(pkg->local_filename);
1333          if (file_sha256 && strcmp(file_sha256, pkg->sha256sum))
1334          {
1335               opkg_msg(ERROR, "Package %s sha256sum mismatch. "
1336                         "Either the opkg or the package index are corrupt. "
1337                         "Try 'opkg update'.\n",
1338                         pkg->name);
1339               free(file_sha256);
1340               return -1;
1341          }
1342          if (file_sha256)
1343               free(file_sha256);
1344      }
1345 #endif
1346      if(conf->download_only) {
1347          if (conf->nodeps == 0) {
1348              err = satisfy_dependencies_for(pkg);
1349              if (err)
1350                  return -1;
1351          }
1352          return 0;
1353      }
1354
1355      if (pkg->tmp_unpack_dir == NULL) {
1356           if (unpack_pkg_control_files(pkg) == -1) {
1357                opkg_msg(ERROR, "Failed to unpack control files from %s.\n",
1358                                pkg->local_filename);
1359                return -1;
1360           }
1361      }
1362
1363      err = update_file_ownership(pkg, old_pkg);
1364      if (err)
1365              return -1;
1366
1367      if (conf->nodeps == 0) {
1368           err = satisfy_dependencies_for(pkg);
1369           if (err)
1370                 return -1;
1371           if (pkg->state_status == SS_UNPACKED)
1372                /* Circular dependency has installed it for us. */
1373                 return 0;
1374      }
1375
1376      replacees = pkg_vec_alloc();
1377      pkg_get_installed_replacees(pkg, replacees);
1378
1379      /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1380
1381           sigemptyset(&newset);
1382           sigaddset(&newset, SIGINT);
1383           sigprocmask(SIG_BLOCK, &newset, &oldset);
1384
1385           opkg_state_changed++;
1386           pkg->state_flag |= SF_FILELIST_CHANGED;
1387
1388           if (old_pkg)
1389                pkg_remove_orphan_dependent(pkg, old_pkg);
1390
1391           /* XXX: BUG: we really should treat replacement more like an upgrade
1392            *      Instead, we're going to remove the replacees 
1393            */
1394           err = pkg_remove_installed_replacees(replacees);
1395           if (err)
1396                   goto UNWIND_REMOVE_INSTALLED_REPLACEES;
1397
1398           err = prerm_upgrade_old_pkg(pkg, old_pkg);
1399           if (err)
1400                   goto UNWIND_PRERM_UPGRADE_OLD_PKG;
1401
1402           err = prerm_deconfigure_conflictors(pkg, replacees);
1403           if (err)
1404                   goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
1405
1406           err = preinst_configure(pkg, old_pkg);
1407           if (err)
1408                   goto UNWIND_PREINST_CONFIGURE;
1409
1410           err = backup_modified_conffiles(pkg, old_pkg);
1411           if (err)
1412                   goto UNWIND_BACKUP_MODIFIED_CONFFILES;
1413
1414           err = check_data_file_clashes(pkg, old_pkg);
1415           if (err)
1416                   goto UNWIND_CHECK_DATA_FILE_CLASHES;
1417
1418           err = postrm_upgrade_old_pkg(pkg, old_pkg);
1419           if (err)
1420                   goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
1421
1422           if (conf->noaction)
1423                   return 0;
1424
1425           /* point of no return: no unwinding after this */
1426           if (old_pkg && !conf->force_reinstall) {
1427                old_pkg->state_want = SW_DEINSTALL;
1428
1429                if (old_pkg->state_flag & SF_NOPRUNE) {
1430                     opkg_msg(INFO, "Not removing obsolesced files because "
1431                                     "package %s marked noprune.\n",
1432                                     old_pkg->name);
1433                } else {
1434                     opkg_msg(INFO, "Removing obsolesced files for %s\n",
1435                                     old_pkg->name);
1436                     if (remove_obsolesced_files(pkg, old_pkg)) {
1437                         opkg_msg(ERROR, "Failed to determine "
1438                                         "obsolete files from previously "
1439                                         "installed %s\n", old_pkg->name);
1440                     }
1441                }
1442
1443                /* removing files from old package, to avoid ghost files */ 
1444                remove_data_files_and_list(old_pkg);
1445                remove_maintainer_scripts(old_pkg);
1446           }
1447
1448
1449           opkg_msg(INFO, "Installing maintainer scripts.\n");
1450           if (install_maintainer_scripts(pkg, old_pkg)) {
1451                 opkg_msg(ERROR, "Failed to extract maintainer scripts for %s."
1452                                " Package debris may remain!\n",
1453                                pkg->name);
1454                 goto pkg_is_hosed;
1455           }
1456
1457           /* the following just returns 0 */
1458           remove_disappeared(pkg);
1459
1460           opkg_msg(INFO, "Installing data files for %s.\n", pkg->name);
1461
1462           if (install_data_files(pkg)) {
1463                 opkg_msg(ERROR, "Failed to extract data files for %s. "
1464                                 "Package debris may remain!\n",
1465                                pkg->name);
1466                 goto pkg_is_hosed;
1467           }
1468
1469           err = check_data_file_clashes_change(pkg, old_pkg);
1470           if (err) {
1471                 opkg_msg(ERROR, "check_data_file_clashes_change() failed for "
1472                                "for files belonging to %s.\n",
1473                                pkg->name);
1474           }
1475
1476           opkg_msg(INFO, "Resolving conf files for %s\n", pkg->name);
1477           resolve_conffiles(pkg);
1478
1479           pkg->state_status = SS_UNPACKED;
1480           old_state_flag = pkg->state_flag;
1481           pkg->state_flag &= ~SF_PREFER;
1482           opkg_msg(DEBUG, "pkg=%s old_state_flag=%x state_flag=%x\n",
1483                           pkg->name, old_state_flag, pkg->state_flag);
1484
1485           if (old_pkg && !conf->force_reinstall) {
1486                old_pkg->state_status = SS_NOT_INSTALLED;
1487           }
1488
1489           time(&pkg->installed_time);
1490
1491           ab_pkg = pkg->parent;
1492           if (ab_pkg)
1493                ab_pkg->state_status = pkg->state_status;
1494
1495           sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1496           pkg_vec_free (replacees);
1497           return 0;
1498      
1499
1500      UNWIND_POSTRM_UPGRADE_OLD_PKG:
1501           postrm_upgrade_old_pkg_unwind(pkg, old_pkg);
1502      UNWIND_CHECK_DATA_FILE_CLASHES:
1503           check_data_file_clashes_unwind(pkg, old_pkg);
1504      UNWIND_BACKUP_MODIFIED_CONFFILES:
1505           backup_modified_conffiles_unwind(pkg, old_pkg);
1506      UNWIND_PREINST_CONFIGURE:
1507           preinst_configure_unwind(pkg, old_pkg);
1508      UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
1509           prerm_deconfigure_conflictors_unwind(pkg, replacees);
1510      UNWIND_PRERM_UPGRADE_OLD_PKG:
1511           prerm_upgrade_old_pkg_unwind(pkg, old_pkg);
1512      UNWIND_REMOVE_INSTALLED_REPLACEES:
1513           pkg_remove_installed_replacees_unwind(replacees);
1514
1515 pkg_is_hosed:
1516           sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1517
1518           pkg_vec_free (replacees);
1519           return -1;
1520 }