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