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