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