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