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