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