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