1 /* opkg_install.c - the opkg package management system
5 Copyright (C) 2001 University of Southern California
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.
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.
28 #include "pkg_extract.h"
30 #include "opkg_install.h"
31 #include "opkg_configure.h"
32 #include "opkg_download.h"
33 #include "opkg_remove.h"
35 #include "opkg_utils.h"
36 #include "opkg_message.h"
38 #include "opkg_defines.h"
40 #include "sprintf_alloc.h"
41 #include "file_util.h"
43 #include "libbb/libbb.h"
45 static int satisfy_dependencies_for(pkg_t * pkg)
48 pkg_vec_t *depends = pkg_vec_alloc();
50 char **tmp, **unresolved = NULL;
53 ndepends = pkg_hash_fetch_unsatisfied_dependencies(pkg, depends,
58 "Cannot satisfy the following dependencies for %s:\n",
62 opkg_message(ERROR, "\t%s", *unresolved);
67 opkg_message(ERROR, "\n");
68 if (!conf->force_depends) {
70 "This could mean that your package list is out of date or that the packages\n"
71 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
72 "of this problem try again with the '-force-depends' option.\n");
73 pkg_vec_free(depends);
79 pkg_vec_free(depends);
83 /* Mark packages as to-be-installed */
84 for (i = 0; i < depends->len; i++) {
85 /* Dependencies should be installed the same place as pkg */
86 if (depends->pkgs[i]->dest == NULL) {
87 depends->pkgs[i]->dest = pkg->dest;
89 depends->pkgs[i]->state_want = SW_INSTALL;
92 for (i = 0; i < depends->len; i++) {
93 dep = depends->pkgs[i];
94 /* The package was uninstalled when we started, but another
95 dep earlier in this loop may have depended on it and pulled
96 it in, so check first. */
97 if ((dep->state_status != SS_INSTALLED)
98 && (dep->state_status != SS_UNPACKED)) {
99 opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
100 err = opkg_install_pkg(dep, 0);
101 /* mark this package as having been automatically installed to
102 * satisfy a dependancy */
103 dep->auto_installed = 1;
105 pkg_vec_free(depends);
111 pkg_vec_free(depends);
116 static int check_conflicts_for(pkg_t * pkg)
119 pkg_vec_t *conflicts = NULL;
120 message_level_t level;
122 if (conf->force_depends) {
128 if (!conf->force_depends)
129 conflicts = pkg_hash_fetch_conflicts(pkg);
132 opkg_msg(level, "The following packages conflict with %s:\n",
135 while (i < conflicts->len)
136 opkg_msg(level, "\t%s", conflicts->pkgs[i++]->name);
137 opkg_message(level, "\n");
138 pkg_vec_free(conflicts);
144 static int update_file_ownership(pkg_t * new_pkg, pkg_t * old_pkg)
146 str_list_t *new_list, *old_list;
147 str_list_elt_t *iter, *niter;
149 new_list = pkg_get_installed_files(new_pkg);
150 if (new_list == NULL)
153 for (iter = str_list_first(new_list), niter =
154 str_list_next(new_list, iter); iter;
155 iter = niter, niter = str_list_next(new_list, niter)) {
156 char *new_file = (char *)iter->data;
157 pkg_t *owner = file_hash_get_file_owner(new_file);
158 pkg_t *obs = hash_table_get(&conf->obs_file_hash, new_file);
161 "%s: new_pkg=%s wants file %s, from owner=%s\n",
162 __func__, new_pkg->name, new_file,
163 owner ? owner->name : "<NULL>");
165 if (!owner || (owner == old_pkg) || obs)
166 file_hash_set_file_owner(new_file, new_pkg);
170 old_list = pkg_get_installed_files(old_pkg);
171 if (old_list == NULL) {
172 pkg_free_installed_files(new_pkg);
176 for (iter = str_list_first(old_list), niter =
177 str_list_next(old_list, iter); iter;
178 iter = niter, niter = str_list_next(old_list, niter)) {
179 char *old_file = (char *)iter->data;
180 pkg_t *owner = file_hash_get_file_owner(old_file);
181 if (!owner || (owner == old_pkg)) {
183 hash_table_insert(&conf->obs_file_hash,
187 pkg_free_installed_files(old_pkg);
189 pkg_free_installed_files(new_pkg);
193 static int verify_pkg_installable(pkg_t * pkg)
195 unsigned long kbs_available, pkg_size_kbs;
196 unsigned long installed_size;
197 char *root_dir = NULL;
200 installed_size = (unsigned long) pkg_get_int(pkg, PKG_INSTALLED_SIZE);
202 if (conf->force_space || installed_size == 0)
206 if (!strcmp(pkg->dest->name, "root") && conf->overlay_root
207 && !stat(conf->overlay_root, &s) && (s.st_mode & S_IFDIR))
208 root_dir = conf->overlay_root;
210 root_dir = pkg->dest->root_dir;
214 root_dir = conf->default_dest->root_dir;
216 kbs_available = get_available_kbytes(root_dir);
218 pkg_size_kbs = (installed_size + 1023) / 1024;
220 if (pkg_size_kbs >= kbs_available) {
221 opkg_msg(ERROR, "Only have %ldkb available on filesystem %s, "
222 "pkg %s needs %ld\n",
223 kbs_available, root_dir, pkg->name, pkg_size_kbs);
230 static int unpack_pkg_control_files(pkg_t * pkg)
233 char *conffiles_file_name;
235 char *tmp_unpack_dir;
236 FILE *conffiles_file;
239 sprintf_alloc(&tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir,
242 tmp_unpack_dir = mkdtemp(tmp_unpack_dir);
243 if (tmp_unpack_dir == NULL) {
244 opkg_perror(ERROR, "Failed to create temporary directory '%s'",
249 pkg_set_string(pkg, PKG_TMP_UNPACK_DIR, tmp_unpack_dir);
251 err = pkg_extract_control_files_to_dir(pkg, tmp_unpack_dir);
256 /* XXX: CLEANUP: There might be a cleaner place to read in the
257 conffiles. Seems like I should be able to get everything to go
258 through pkg_init_from_file. If so, maybe it would make sense to
259 move all of unpack_pkg_control_files to that function. */
261 /* Don't need to re-read conffiles if we already have it */
262 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
263 if (cl && !nv_pair_list_empty(cl)) {
267 sprintf_alloc(&conffiles_file_name, "%s/conffiles",
269 if (!file_exists(conffiles_file_name)) {
270 free(conffiles_file_name);
274 conffiles_file = fopen(conffiles_file_name, "r");
275 if (conffiles_file == NULL) {
276 opkg_perror(ERROR, "Failed to open %s", conffiles_file_name);
277 free(conffiles_file_name);
280 free(conffiles_file_name);
282 cl = xcalloc(1, sizeof(*cl));
283 conffile_list_init(cl);
287 char *cf_name_in_dest;
290 cf_name = file_read_line_alloc(conffiles_file);
291 if (cf_name == NULL) {
294 if (cf_name[0] == '\0') {
297 for (i = strlen(cf_name) - 1;
298 (i >= 0) && (cf_name[i] == ' ' || cf_name[i] == '\t');
303 /* Prepend dest->root_dir to conffile name.
304 Take pains to avoid multiple slashes. */
305 root_dir = pkg->dest->root_dir;
306 if (conf->offline_root)
307 /* skip the offline_root prefix */
309 pkg->dest->root_dir + strlen(conf->offline_root);
310 sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
311 cf_name[0] == '/' ? (cf_name + 1) : cf_name);
313 /* Can't get an md5sum now, (file isn't extracted yet).
314 We'll wait until resolve_conffiles */
315 conffile_list_append(cl, cf_name_in_dest, NULL);
318 free(cf_name_in_dest);
321 pkg_set_ptr(pkg, PKG_CONFFILES, cl);
323 fclose(conffiles_file);
329 * Remove packages which were auto_installed due to a dependency by old_pkg,
330 * which are no longer a dependency in the new (upgraded) pkg.
332 static int pkg_remove_orphan_dependent(pkg_t * pkg, pkg_t * old_pkg)
334 int j, l, found, r, err = 0;
337 struct compound_depend *cd0, *cd1;
338 abstract_pkg_t **dependents;
340 for (cd0 = pkg_get_ptr(old_pkg, PKG_DEPENDS); cd0 && cd0->type; cd0++) {
341 if (cd0->type != DEPEND)
343 for (j = 0; j < cd0->possibility_count; j++) {
347 for (cd1 = pkg_get_ptr(pkg, PKG_DEPENDS); cd1 && cd1->type; cd1++) {
348 if (cd1->type != DEPEND)
350 for (l = 0; l < cd1->possibility_count; l++) {
351 if (cd0->possibilities[j]
352 == cd1->possibilities[l]) {
365 * old_pkg has a dependency that pkg does not.
367 p = pkg_hash_fetch_installed_by_name(cd0->
374 if (!p->auto_installed)
377 n_deps = pkg_has_installed_dependents(p, &dependents);
378 n_deps--; /* don't count old_pkg */
381 opkg_msg(NOTICE, "%s was autoinstalled and is "
382 "now orphaned, removing.\n", p->name);
384 /* p has one installed dependency (old_pkg),
385 * which we need to ignore during removal. */
386 p->state_flag |= SF_REPLACE;
388 r = opkg_remove_pkg(p, 0);
392 opkg_msg(INFO, "%s was autoinstalled and is "
393 "still required by %d "
394 "installed packages.\n",
403 /* returns number of installed replacees */
405 pkg_get_installed_replacees(pkg_t * pkg, pkg_vec_t * installed_replacees)
407 abstract_pkg_t **replaces = pkg_get_ptr(pkg, PKG_REPLACES);
410 while (replaces && *replaces) {
411 abstract_pkg_t *ab_pkg = *replaces++;
412 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
414 for (j = 0; j < pkg_vec->len; j++) {
415 pkg_t *replacee = pkg_vec->pkgs[j];
416 if (!pkg_conflicts(pkg, replacee))
418 if (replacee->state_status == SS_INSTALLED) {
419 pkg_vec_insert(installed_replacees,
426 return installed_replacees->len;
429 static int pkg_remove_installed_replacees(pkg_vec_t * replacees)
432 int replaces_count = replacees->len;
433 for (i = 0; i < replaces_count; i++) {
434 pkg_t *replacee = replacees->pkgs[i];
436 replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
437 err = opkg_remove_pkg(replacee, 0);
444 /* to unwind the removal: make sure they are installed */
445 static int pkg_remove_installed_replacees_unwind(pkg_vec_t * replacees)
448 int replaces_count = replacees->len;
449 for (i = 0; i < replaces_count; i++) {
450 pkg_t *replacee = replacees->pkgs[i];
451 if (replacee->state_status != SS_INSTALLED) {
452 opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
453 err = opkg_install_pkg(replacee, 0);
461 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
463 opkg_install_check_downgrade(pkg_t * pkg, pkg_t * old_pkg, int message)
466 char message_out[15];
467 char *old_version = pkg_version_str_alloc(old_pkg);
468 char *new_version = pkg_version_str_alloc(pkg);
469 int cmp = pkg_compare_versions(old_pkg, pkg);
472 memset(message_out, '\x0', 15);
473 strncpy(message_out, "Upgrading ", strlen("Upgrading "));
474 if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */
475 cmp = -1; /* then we force opkg to downgrade */
476 strncpy(message_out, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
477 /* reinstall, and some check could fail asking the "force-reinstall" option */
481 if (!conf->download_only)
483 "Not downgrading package %s on %s from %s to %s.\n",
484 old_pkg->name, old_pkg->dest->name,
485 old_version, new_version);
487 } else if (cmp < 0) {
488 if (!conf->download_only)
490 "%s%s on %s from %s to %s...\n",
491 message_out, pkg->name,
492 old_pkg->dest->name, old_version,
494 pkg->dest = old_pkg->dest;
496 } else { /* cmp == 0 */
498 if (!conf->download_only)
500 "%s (%s) already install on %s.\n",
501 pkg->name, new_version,
502 old_pkg->dest->name);
509 char message_out[15];
510 memset(message_out, '\x0', 15);
512 strncpy(message_out, "Upgrading ",
513 strlen("Upgrading "));
515 strncpy(message_out, "Installing ",
516 strlen("Installing "));
517 char *version = pkg_version_str_alloc(pkg);
519 if (!conf->download_only)
520 opkg_msg(NOTICE, "%s%s (%s) to %s...\n", message_out,
521 pkg->name, version, pkg->dest->name);
527 static int prerm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg)
529 /* DPKG_INCOMPATIBILITY:
530 dpkg does some things here that we don't do yet. Do we care?
532 1. If a version of the package is already installed, call
533 old-prerm upgrade new-version
534 2. If the script runs but exits with a non-zero exit status
535 new-prerm failed-upgrade old-version
536 Error unwind, for both the above cases:
537 old-postinst abort-upgrade new-version
543 if (!old_pkg || !pkg)
546 new_version = pkg_version_str_alloc(pkg);
548 sprintf_alloc(&script_args, "upgrade %s", new_version);
550 err = pkg_run_script(old_pkg, "prerm", script_args);
553 opkg_msg(ERROR, "prerm script for package \"%s\" failed\n",
560 static int prerm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg)
562 /* DPKG_INCOMPATIBILITY:
563 dpkg does some things here that we don't do yet. Do we care?
564 (See prerm_upgrade_old_package for details)
569 static int prerm_deconfigure_conflictors(pkg_t * pkg, pkg_vec_t * conflictors)
571 /* DPKG_INCOMPATIBILITY:
572 dpkg does some things here that we don't do yet. Do we care?
573 2. If a 'conflicting' package is being removed at the same time:
574 1. If any packages depended on that conflicting package and
575 --auto-deconfigure is specified, call, for each such package:
576 deconfigured's-prerm deconfigure \
577 in-favour package-being-installed version \
578 removing conflicting-package version
580 deconfigured's-postinst abort-deconfigure \
581 in-favour package-being-installed-but-failed version \
582 removing conflicting-package version
584 The deconfigured packages are marked as requiring
585 configuration, so that if --install is used they will be
586 configured again if possible.
587 2. To prepare for removal of the conflicting package, call:
588 conflictor's-prerm remove in-favour package new-version
590 conflictor's-postinst abort-remove in-favour package new-version
596 prerm_deconfigure_conflictors_unwind(pkg_t * pkg, pkg_vec_t * conflictors)
598 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
599 do yet. Do we care? (See prerm_deconfigure_conflictors for
604 static int preinst_configure(pkg_t * pkg, pkg_t * old_pkg)
610 char *old_version = pkg_version_str_alloc(old_pkg);
611 sprintf_alloc(&preinst_args, "upgrade %s", old_version);
613 } else if (pkg->state_status == SS_CONFIG_FILES) {
614 char *pkg_version = pkg_version_str_alloc(pkg);
615 sprintf_alloc(&preinst_args, "install %s", pkg_version);
618 preinst_args = xstrdup("install");
621 err = pkg_run_script(pkg, "preinst", preinst_args);
623 opkg_msg(ERROR, "Aborting installation of %s.\n", pkg->name);
632 static int preinst_configure_unwind(pkg_t * pkg, pkg_t * old_pkg)
634 /* DPKG_INCOMPATIBILITY:
635 dpkg does the following error unwind, should we?
636 pkg->postrm abort-upgrade old-version
637 OR pkg->postrm abort-install old-version
638 OR pkg->postrm abort-install
643 static char *backup_filename_alloc(const char *file_name)
647 sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
652 static int backup_make_backup(const char *file_name)
657 backup = backup_filename_alloc(file_name);
658 err = file_copy(file_name, backup);
660 opkg_msg(ERROR, "Failed to copy %s to %s\n", file_name, backup);
668 static int backup_exists_for(const char *file_name)
673 backup = backup_filename_alloc(file_name);
675 ret = file_exists(backup);
682 static int backup_remove(const char *file_name)
686 backup = backup_filename_alloc(file_name);
693 static int backup_modified_conffiles(pkg_t * pkg, pkg_t * old_pkg)
696 conffile_list_elt_t *iter;
703 /* Backup all modified conffiles */
705 cl = pkg_get_ptr(old_pkg, PKG_CONFFILES);
707 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
708 iter = nv_pair_list_next(cl, iter)) {
712 cf_name = root_filename_alloc(cf->name);
714 /* Don't worry if the conffile is just plain gone */
715 if (file_exists(cf_name)
716 && conffile_has_been_modified(cf)) {
717 err = backup_make_backup(cf_name);
726 /* Backup all conffiles that were not conffiles in old_pkg */
727 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
729 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
730 iter = nv_pair_list_next(cl, iter)) {
732 cf = (conffile_t *) iter->data;
733 cf_name = root_filename_alloc(cf->name);
734 /* Ignore if this was a conffile in old_pkg as well */
735 if (pkg_get_conffile(old_pkg, cf->name)) {
739 if (file_exists(cf_name) && (!backup_exists_for(cf_name))) {
740 err = backup_make_backup(cf_name);
751 static int backup_modified_conffiles_unwind(pkg_t * pkg, pkg_t * old_pkg)
754 conffile_list_elt_t *iter;
757 cl = pkg_get_ptr(old_pkg, PKG_CONFFILES);
759 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
760 iter = nv_pair_list_next(cl, iter)) {
761 backup_remove(((nv_pair_t *) iter->data)->name);
765 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
767 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
768 iter = nv_pair_list_next(cl, iter)) {
769 backup_remove(((nv_pair_t *) iter->data)->name);
775 static int check_data_file_clashes(pkg_t * pkg, pkg_t * old_pkg)
777 /* DPKG_INCOMPATIBILITY:
778 opkg takes a slightly different approach than dpkg at this
779 point. dpkg installs each file in the new package while
780 creating a backup for any file that is replaced, (so that it
781 can unwind if necessary). To avoid complexity and redundant
782 storage, opkg doesn't do any installation until later, (at the
783 point at which dpkg removes the backups.
785 But, we do have to check for data file clashes, since after
786 installing a package with a file clash, removing either of the
787 packages involved in the clash has the potential to break the
790 str_list_t *files_list;
791 str_list_elt_t *iter, *niter;
795 files_list = pkg_get_installed_files(pkg);
796 if (files_list == NULL)
799 for (iter = str_list_first(files_list), niter =
800 str_list_next(files_list, iter); iter;
801 iter = niter, niter = str_list_next(files_list, iter)) {
802 filename = (char *)iter->data;
803 if (file_exists(filename) && (!file_is_dir(filename))) {
807 if (backup_exists_for(filename)) {
811 /* Pre-existing files are OK if force-overwrite was asserted. */
812 if (conf->force_overwrite) {
813 /* but we need to change who owns this file */
814 file_hash_set_file_owner(filename, pkg);
818 owner = file_hash_get_file_owner(filename);
820 /* Pre-existing files are OK if owned by the pkg being upgraded. */
821 if (owner && old_pkg) {
822 if (strcmp(owner->name, old_pkg->name) == 0) {
827 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
830 "Checking replaces for %s in package %s\n",
831 filename, owner->name);
832 if (pkg_replaces(pkg, owner)) {
835 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
836 then it's ok to overwrite. */
837 if (strcmp(owner->name, pkg->name) == 0) {
839 "Replacing pre-existing file %s"
840 " owned by package %s\n",
841 filename, owner->name);
846 /* Pre-existing files are OK if they are obsolete */
847 obs = hash_table_get(&conf->obs_file_hash, filename);
850 "Pre-exiting file %s is obsolete."
851 " obs_pkg=%s\n", filename, obs->name);
855 /* We have found a clash. */
856 opkg_msg(ERROR, "Package %s wants to install file %s\n"
857 "\tBut that file is already provided by package ",
858 pkg->name, filename);
860 opkg_message(ERROR, "%s\n", owner->name);
862 opkg_message(ERROR, "<no package>\n"
863 "Please move this file out of the way and try again.\n");
868 pkg_free_installed_files(pkg);
874 * XXX: This function sucks, as does the below comment.
876 static int check_data_file_clashes_change(pkg_t * pkg, pkg_t * old_pkg)
878 /* Basically that's the worst hack I could do to be able to change ownership of
879 file list, but, being that we have no way to unwind the mods, due to structure
880 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
881 What we do here is change the ownership of file in hash if a replace ( or similar events
883 Only the action that are needed to change name should be considered.
884 @@@ To change after 1.0 release.
886 str_list_t *files_list;
887 str_list_elt_t *iter, *niter;
889 char *root_filename = NULL;
891 files_list = pkg_get_installed_files(pkg);
892 if (files_list == NULL)
895 for (iter = str_list_first(files_list), niter =
896 str_list_next(files_list, iter); iter;
897 iter = niter, niter = str_list_next(files_list, niter)) {
898 char *filename = (char *)iter->data;
901 root_filename = NULL;
903 root_filename = root_filename_alloc(filename);
904 if (file_exists(root_filename) && (!file_is_dir(root_filename))) {
907 owner = file_hash_get_file_owner(filename);
909 if (conf->force_overwrite) {
910 /* but we need to change who owns this file */
911 file_hash_set_file_owner(filename, pkg);
915 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
917 if (pkg_replaces(pkg, owner)) {
918 /* It's now time to change the owner of that file.
919 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
921 "Replacing pre-existing file %s "
922 "owned by package %s\n",
923 filename, owner->name);
924 file_hash_set_file_owner(filename, pkg);
933 root_filename = NULL;
935 pkg_free_installed_files(pkg);
940 static int check_data_file_clashes_unwind(pkg_t * pkg, pkg_t * old_pkg)
942 /* Nothing to do since check_data_file_clashes doesn't change state */
946 static int postrm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg)
948 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
949 1. If the package is being upgraded, call
950 old-postrm upgrade new-version
951 2. If this fails, attempt:
952 new-postrm failed-upgrade old-version
953 Error unwind, for both cases:
954 old-preinst abort-upgrade new-version */
959 if (!old_pkg || !pkg)
962 new_version = pkg_version_str_alloc(pkg);
964 sprintf_alloc(&script_args, "upgrade %s", new_version);
966 err = pkg_run_script(old_pkg, "postrm", script_args);
969 opkg_msg(ERROR, "postrm script for package \"%s\" failed\n",
976 static int postrm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg)
978 /* DPKG_INCOMPATIBILITY:
979 dpkg does some things here that we don't do yet. Do we care?
980 (See postrm_upgrade_old_pkg for details)
985 static int remove_obsolesced_files(pkg_t * pkg, pkg_t * old_pkg)
988 str_list_t *old_files;
990 str_list_t *new_files;
992 hash_table_t new_files_table;
994 old_files = pkg_get_installed_files(old_pkg);
995 if (old_files == NULL)
998 new_files = pkg_get_installed_files(pkg);
999 if (new_files == NULL) {
1000 pkg_free_installed_files(old_pkg);
1004 new_files_table.entries = NULL;
1005 hash_table_init("new_files", &new_files_table, 20);
1006 for (nf = str_list_first(new_files); nf;
1007 nf = str_list_next(new_files, nf)) {
1009 hash_table_insert(&new_files_table, nf->data, nf->data);
1012 for (of = str_list_first(old_files); of;
1013 of = str_list_next(old_files, of)) {
1016 old = (char *)of->data;
1017 new = (char *)hash_table_get(&new_files_table, old);
1021 if (file_is_dir(old)) {
1024 owner = file_hash_get_file_owner(old);
1025 if (owner != old_pkg) {
1026 /* in case obsolete file no longer belongs to old_pkg */
1030 /* old file is obsolete */
1031 opkg_msg(NOTICE, "Removing obsolete file %s.\n", old);
1032 if (!conf->noaction) {
1035 opkg_perror(ERROR, "unlinking %s failed", old);
1040 hash_table_deinit(&new_files_table);
1041 pkg_free_installed_files(old_pkg);
1042 pkg_free_installed_files(pkg);
1047 static int install_maintainer_scripts(pkg_t * pkg, pkg_t * old_pkg)
1052 sprintf_alloc(&prefix, "%s.", pkg->name);
1053 ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
1054 pkg->dest->info_dir,
1060 static int remove_disappeared(pkg_t * pkg)
1062 /* DPKG_INCOMPATIBILITY:
1063 This is a fairly sophisticated dpkg operation. Shall we
1066 /* Any packages all of whose files have been overwritten during the
1067 installation, and which aren't required for dependencies, are
1068 considered to have been removed. For each such package
1069 1. disappearer's-postrm disappear overwriter overwriter-version
1070 2. The package's maintainer scripts are removed
1071 3. It is noted in the status database as being in a sane state,
1072 namely not installed (any conffiles it may have are ignored,
1073 rather than being removed by dpkg). Note that disappearing
1074 packages do not have their prerm called, because dpkg doesn't
1075 know in advance that the package is going to vanish.
1080 static int install_data_files(pkg_t * pkg)
1084 /* opkg takes a slightly different approach to data file backups
1085 than dpkg. Rather than removing backups at this point, we
1086 actually do the data file installation now. See comments in
1087 check_data_file_clashes() for more details. */
1089 opkg_msg(INFO, "Extracting data files to %s.\n", pkg->dest->root_dir);
1090 err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
1095 /* The "Essential" control field may only be present in the control
1096 * file and not in the Packages list. Ensure we capture it regardless.
1098 * XXX: This should be fixed outside of opkg, in the Package list.
1100 set_flags_from_control(pkg);
1102 opkg_msg(DEBUG, "Calling pkg_write_filelist.\n");
1103 err = pkg_write_filelist(pkg);
1107 /* XXX: FEATURE: opkg should identify any files which existed
1108 before installation and which were overwritten, (see
1109 check_data_file_clashes()). What it must do is remove any such
1110 files from the filelist of the old package which provided the
1111 file. Otherwise, if the old package were removed at some point
1112 it would break the new package. Removing the new package will
1113 also break the old one, but this cannot be helped since the old
1114 package's file has already been deleted. This is the importance
1115 of check_data_file_clashes(), and only allowing opkg to install
1116 a clashing package with a user force. */
1121 static int resolve_conffiles(pkg_t * pkg)
1123 conffile_list_elt_t *iter;
1124 conffile_list_t *cl;
1132 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
1134 for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
1135 iter = nv_pair_list_next(cl, iter)) {
1136 char *root_filename;
1137 cf = (conffile_t *) iter->data;
1138 root_filename = root_filename_alloc(cf->name);
1140 /* Might need to initialize the md5sum for each conffile */
1141 if (cf->value == NULL) {
1142 cf->value = file_sha256sum_alloc(root_filename);
1145 if (!file_exists(root_filename)) {
1146 free(root_filename);
1150 cf_backup = backup_filename_alloc(root_filename);
1152 if (file_exists(cf_backup)) {
1153 /* Let's compute md5 to test if files are changed */
1155 if (cf->value && strlen(cf->value) > 33) {
1156 chksum = file_sha256sum_alloc(cf_backup);
1158 chksum = file_md5sum_alloc(cf_backup);
1161 chksum = file_sha256sum_alloc(cf_backup);
1163 if (chksum && cf->value
1164 && strcmp(cf->value, chksum) != 0) {
1165 if (conf->force_maintainer) {
1167 "Conffile %s using maintainer's setting.\n",
1171 sprintf_alloc(&new_conffile, "%s-opkg",
1174 "Existing conffile %s "
1175 "is different from the conffile in the new package."
1176 " The new conffile will be placed at %s.\n",
1177 root_filename, new_conffile);
1178 rename(root_filename, new_conffile);
1179 rename(cf_backup, root_filename);
1189 free(root_filename);
1195 int opkg_install_by_name(const char *pkg_name)
1199 char *old_version, *new_version;
1201 old = pkg_hash_fetch_installed_by_name(pkg_name);
1203 opkg_msg(DEBUG2, "Old versions from pkg_hash_fetch %s.\n",
1204 pkg_get_string(old, PKG_VERSION));
1206 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name);
1208 opkg_msg(NOTICE, "Unknown package '%s'.\n", pkg_name);
1212 opkg_msg(DEBUG2, "Versions from pkg_hash_fetch:");
1214 opkg_message(DEBUG2, " old %s ", pkg_get_string(old, PKG_VERSION));
1215 opkg_message(DEBUG2, " new %s\n", pkg_get_string(new, PKG_VERSION));
1217 new->state_flag |= SF_USER;
1219 old_version = pkg_version_str_alloc(old);
1220 new_version = pkg_version_str_alloc(new);
1222 cmp = pkg_compare_versions(old, new);
1223 if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */
1224 opkg_msg(DEBUG, "Forcing downgrade\n");
1225 cmp = -1; /* then we force opkg to downgrade */
1226 /* We need to use a value < 0 because in the 0 case we are asking to */
1227 /* reinstall, and some check could fail asking the "force-reinstall" option */
1229 opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
1230 "\n\t%s is installed "
1231 "\n\t%s is available "
1232 "\n\t%d was comparison result\n",
1233 pkg_name, old_version, new_version, cmp);
1236 "Package %s (%s) installed in %s is up to date.\n",
1237 old->name, old_version, old->dest->name);
1241 } else if (cmp > 0) {
1243 "Not downgrading package %s on %s from %s to %s.\n",
1244 old->name, old->dest->name, old_version,
1249 } else if (cmp < 0) {
1250 new->dest = old->dest;
1251 old->state_want = SW_DEINSTALL;
1257 opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
1258 return opkg_install_pkg(new, 0);
1262 * @brief Really install a pkg_t
1264 int opkg_install_pkg(pkg_t * pkg, int from_upgrade)
1268 pkg_t *old_pkg = NULL;
1269 pkg_vec_t *replacees;
1270 abstract_pkg_t *ab_pkg = NULL;
1272 char *file_md5, *pkg_md5;
1274 char *file_sha256, *pkg_sha256;
1276 sigset_t newset, oldset;
1277 const char *local_filename;
1281 message = 1; /* Coming from an upgrade, and should change the output message */
1283 opkg_msg(DEBUG2, "Calling pkg_arch_supported.\n");
1285 if (!pkg_arch_supported(pkg)) {
1287 "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1288 pkg_get_architecture(pkg), pkg->name);
1291 if (pkg->state_status == SS_INSTALLED && conf->nodeps == 0) {
1292 err = satisfy_dependencies_for(pkg);
1296 opkg_msg(NOTICE, "Package %s is already installed on %s.\n",
1297 pkg->name, pkg->dest->name);
1301 if (pkg->dest == NULL) {
1302 pkg->dest = conf->default_dest;
1305 old_pkg = pkg_hash_fetch_installed_by_name(pkg->name);
1307 err = opkg_install_check_downgrade(pkg, old_pkg, message);
1311 pkg->state_want = SW_INSTALL;
1313 old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependencies */
1316 err = check_conflicts_for(pkg);
1320 /* this setup is to remove the upgrade scenario in the end when
1321 installing pkg A, A deps B & B deps on A. So both B and A are
1322 installed. Then A's installation is started resulting in an
1323 uncecessary upgrade */
1324 if (pkg->state_status == SS_INSTALLED)
1327 err = verify_pkg_installable(pkg);
1331 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1333 if (local_filename == NULL) {
1334 if (!conf->cache && conf->download_only) {
1336 if (getcwd(cwd, sizeof(cwd)) != NULL)
1337 err = opkg_download_pkg(pkg, cwd);
1341 err = opkg_download_pkg(pkg, conf->tmp_dir);
1344 opkg_msg(ERROR, "Failed to download %s. "
1345 "Perhaps you need to run 'opkg update'?\n",
1350 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1353 /* check that the repository is valid */
1354 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
1355 char *list_file_name, *sig_file_name, *lists_dir;
1357 /* check to ensure the package has come from a repository */
1358 if (conf->check_signature && pkg->src) {
1359 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
1360 ? conf->default_dest->lists_dir
1362 sprintf_alloc(&list_file_name, "%s/%s", lists_dir,
1364 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
1367 if (file_exists(sig_file_name)) {
1368 if (opkg_verify_file(list_file_name, sig_file_name)) {
1370 "Failed to verify the signature of %s.\n",
1372 if (!conf->force_signature)
1376 opkg_msg(ERROR, "Signature file is missing for %s. "
1377 "Perhaps you need to run 'opkg update'?\n",
1379 if (!conf->force_signature)
1384 free(list_file_name);
1385 free(sig_file_name);
1390 /* Check for md5 values */
1391 pkg_md5 = pkg_get_md5(pkg);
1393 file_md5 = file_md5sum_alloc(local_filename);
1394 if (file_md5 && strcmp(file_md5, pkg_md5)) {
1395 if (!conf->force_checksum) {
1396 opkg_msg(ERROR, "Package %s md5sum mismatch. "
1397 "Either the opkg or the package index are corrupt. "
1398 "Try 'opkg update'.\n", pkg->name);
1403 "Ignored %s md5sum mismatch.\n",
1413 /* Check for sha256 value */
1414 pkg_sha256 = pkg_get_sha256(pkg);
1416 file_sha256 = file_sha256sum_alloc(local_filename);
1417 if (file_sha256 && strcmp(file_sha256, pkg_sha256)) {
1418 if (!conf->force_checksum) {
1420 "Package %s sha256sum mismatch. "
1421 "Either the opkg or the package index are corrupt. "
1422 "Try 'opkg update'.\n", pkg->name);
1427 "Ignored %s sha256sum mismatch.\n",
1435 if (conf->download_only) {
1436 if (conf->nodeps == 0) {
1437 err = satisfy_dependencies_for(pkg);
1444 if (!pkg_get_string(pkg, PKG_TMP_UNPACK_DIR)) {
1445 if (unpack_pkg_control_files(pkg) == -1) {
1447 "Failed to unpack control files from %s.\n",
1453 err = update_file_ownership(pkg, old_pkg);
1457 if (conf->nodeps == 0) {
1458 err = satisfy_dependencies_for(pkg);
1461 if (pkg->state_status == SS_UNPACKED)
1462 /* Circular dependency has installed it for us. */
1466 replacees = pkg_vec_alloc();
1467 pkg_get_installed_replacees(pkg, replacees);
1469 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1471 sigemptyset(&newset);
1472 sigaddset(&newset, SIGINT);
1473 sigprocmask(SIG_BLOCK, &newset, &oldset);
1475 opkg_state_changed++;
1476 pkg->state_flag |= SF_FILELIST_CHANGED;
1479 pkg_remove_orphan_dependent(pkg, old_pkg);
1480 old_pkg->is_upgrade = 1;
1481 pkg->is_upgrade = 1;
1483 /* XXX: BUG: we really should treat replacement more like an upgrade
1484 * Instead, we're going to remove the replacees
1486 err = pkg_remove_installed_replacees(replacees);
1488 goto UNWIND_REMOVE_INSTALLED_REPLACEES;
1490 err = prerm_upgrade_old_pkg(pkg, old_pkg);
1492 goto UNWIND_PRERM_UPGRADE_OLD_PKG;
1494 err = prerm_deconfigure_conflictors(pkg, replacees);
1496 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
1498 err = preinst_configure(pkg, old_pkg);
1500 goto UNWIND_PREINST_CONFIGURE;
1502 err = backup_modified_conffiles(pkg, old_pkg);
1504 goto UNWIND_BACKUP_MODIFIED_CONFFILES;
1506 err = check_data_file_clashes(pkg, old_pkg);
1508 goto UNWIND_CHECK_DATA_FILE_CLASHES;
1510 err = postrm_upgrade_old_pkg(pkg, old_pkg);
1512 goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
1517 /* point of no return: no unwinding after this */
1519 old_pkg->state_want = SW_DEINSTALL;
1521 if (old_pkg->state_flag & SF_NOPRUNE) {
1522 opkg_msg(INFO, "Not removing obsolesced files because "
1523 "package %s marked noprune.\n", old_pkg->name);
1525 opkg_msg(INFO, "Removing obsolesced files for %s\n",
1527 if (remove_obsolesced_files(pkg, old_pkg)) {
1528 opkg_msg(ERROR, "Failed to determine "
1529 "obsolete files from previously "
1530 "installed %s\n", old_pkg->name);
1534 /* removing files from old package, to avoid ghost files */
1535 remove_data_files_and_list(old_pkg);
1536 remove_maintainer_scripts(old_pkg);
1539 opkg_msg(INFO, "%s maintainer scripts.\n",
1540 (pkg->is_upgrade) ? ("Upgrading") : ("Installing"));
1541 if (install_maintainer_scripts(pkg, old_pkg)) {
1542 opkg_msg(ERROR, "Failed to extract maintainer scripts for %s."
1543 " Package debris may remain!\n", pkg->name);
1547 /* the following just returns 0 */
1548 remove_disappeared(pkg);
1550 opkg_msg(INFO, "Installing data files for %s.\n", pkg->name);
1552 if (install_data_files(pkg)) {
1553 opkg_msg(ERROR, "Failed to extract data files for %s. "
1554 "Package debris may remain!\n", pkg->name);
1558 err = check_data_file_clashes_change(pkg, old_pkg);
1560 opkg_msg(ERROR, "check_data_file_clashes_change() failed for "
1561 "for files belonging to %s.\n", pkg->name);
1564 opkg_msg(INFO, "Resolving conf files for %s\n", pkg->name);
1565 resolve_conffiles(pkg);
1567 pkg->state_status = SS_UNPACKED;
1568 old_state_flag = pkg->state_flag;
1569 pkg->state_flag &= ~SF_PREFER;
1570 opkg_msg(DEBUG, "pkg=%s old_state_flag=%x state_flag=%x\n",
1571 pkg->name, old_state_flag, pkg->state_flag);
1574 old_pkg->state_status = SS_NOT_INSTALLED;
1577 pkg_set_int(pkg, PKG_INSTALLED_TIME, now);
1579 ab_pkg = pkg->parent;
1581 ab_pkg->state_status = pkg->state_status;
1583 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1584 pkg_vec_free(replacees);
1587 UNWIND_POSTRM_UPGRADE_OLD_PKG:
1588 postrm_upgrade_old_pkg_unwind(pkg, old_pkg);
1589 UNWIND_CHECK_DATA_FILE_CLASHES:
1590 check_data_file_clashes_unwind(pkg, old_pkg);
1591 UNWIND_BACKUP_MODIFIED_CONFFILES:
1592 backup_modified_conffiles_unwind(pkg, old_pkg);
1593 UNWIND_PREINST_CONFIGURE:
1594 preinst_configure_unwind(pkg, old_pkg);
1595 UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
1596 prerm_deconfigure_conflictors_unwind(pkg, replacees);
1597 UNWIND_PRERM_UPGRADE_OLD_PKG:
1598 prerm_upgrade_old_pkg_unwind(pkg, old_pkg);
1599 UNWIND_REMOVE_INSTALLED_REPLACEES:
1600 pkg_remove_installed_replacees_unwind(replacees);
1603 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1605 pkg_vec_free(replacees);