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