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