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