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