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 FILE *conffiles_file;
237 sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir,
240 pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
241 if (pkg->tmp_unpack_dir == NULL) {
242 opkg_perror(ERROR, "Failed to create temporary directory '%s'",
243 pkg->tmp_unpack_dir);
247 err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
252 /* XXX: CLEANUP: There might be a cleaner place to read in the
253 conffiles. Seems like I should be able to get everything to go
254 through pkg_init_from_file. If so, maybe it would make sense to
255 move all of unpack_pkg_control_files to that function. */
257 /* Don't need to re-read conffiles if we already have it */
258 if (!nv_pair_list_empty(&pkg->conffiles)) {
262 sprintf_alloc(&conffiles_file_name, "%s/conffiles",
263 pkg->tmp_unpack_dir);
264 if (!file_exists(conffiles_file_name)) {
265 free(conffiles_file_name);
269 conffiles_file = fopen(conffiles_file_name, "r");
270 if (conffiles_file == NULL) {
271 opkg_perror(ERROR, "Failed to open %s", conffiles_file_name);
272 free(conffiles_file_name);
275 free(conffiles_file_name);
279 char *cf_name_in_dest;
282 cf_name = file_read_line_alloc(conffiles_file);
283 if (cf_name == NULL) {
286 if (cf_name[0] == '\0') {
289 for (i = strlen(cf_name) - 1;
290 (i >= 0) && (cf_name[i] == ' ' || cf_name[i] == '\t');
295 /* Prepend dest->root_dir to conffile name.
296 Take pains to avoid multiple slashes. */
297 root_dir = pkg->dest->root_dir;
298 if (conf->offline_root)
299 /* skip the offline_root prefix */
301 pkg->dest->root_dir + strlen(conf->offline_root);
302 sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
303 cf_name[0] == '/' ? (cf_name + 1) : cf_name);
305 /* Can't get an md5sum now, (file isn't extracted yet).
306 We'll wait until resolve_conffiles */
307 conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
310 free(cf_name_in_dest);
313 fclose(conffiles_file);
319 * Remove packages which were auto_installed due to a dependency by old_pkg,
320 * which are no longer a dependency in the new (upgraded) pkg.
322 static int pkg_remove_orphan_dependent(pkg_t * pkg, pkg_t * old_pkg)
324 int i, j, k, l, found, r, err = 0;
327 struct compound_depend *cd0, *cd1;
328 abstract_pkg_t **dependents;
330 int count0 = old_pkg->pre_depends_count +
331 old_pkg->depends_count +
332 old_pkg->recommends_count + old_pkg->suggests_count;
333 int count1 = pkg->pre_depends_count +
334 pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
336 for (i = 0; i < count0; i++) {
337 cd0 = &old_pkg->depends[i];
338 if (cd0->type != DEPEND)
340 for (j = 0; j < cd0->possibility_count; j++) {
344 for (k = 0; k < count1; k++) {
345 cd1 = &pkg->depends[k];
346 if (cd1->type != DEPEND)
348 for (l = 0; l < cd1->possibility_count; l++) {
349 if (cd0->possibilities[j]
350 == cd1->possibilities[l]) {
363 * old_pkg has a dependency that pkg does not.
365 p = pkg_hash_fetch_installed_by_name(cd0->
372 if (!p->auto_installed)
375 n_deps = pkg_has_installed_dependents(p, &dependents);
376 n_deps--; /* don't count old_pkg */
379 opkg_msg(NOTICE, "%s was autoinstalled and is "
380 "now orphaned, removing.\n", p->name);
382 /* p has one installed dependency (old_pkg),
383 * which we need to ignore during removal. */
384 p->state_flag |= SF_REPLACE;
386 r = opkg_remove_pkg(p, 0);
390 opkg_msg(INFO, "%s was autoinstalled and is "
391 "still required by %d "
392 "installed packages.\n",
401 /* returns number of installed replacees */
403 pkg_get_installed_replacees(pkg_t * pkg, pkg_vec_t * installed_replacees)
405 abstract_pkg_t **replaces = pkg->replaces;
406 int replaces_count = pkg->replaces_count;
408 for (i = 0; i < replaces_count; i++) {
409 abstract_pkg_t *ab_pkg = replaces[i];
410 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
412 for (j = 0; j < pkg_vec->len; j++) {
413 pkg_t *replacee = pkg_vec->pkgs[j];
414 if (!pkg_conflicts(pkg, replacee))
416 if (replacee->state_status == SS_INSTALLED) {
417 pkg_vec_insert(installed_replacees,
423 return installed_replacees->len;
426 static int pkg_remove_installed_replacees(pkg_vec_t * replacees)
429 int replaces_count = replacees->len;
430 for (i = 0; i < replaces_count; i++) {
431 pkg_t *replacee = replacees->pkgs[i];
433 replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
434 err = opkg_remove_pkg(replacee, 0);
441 /* to unwind the removal: make sure they are installed */
442 static int pkg_remove_installed_replacees_unwind(pkg_vec_t * replacees)
445 int replaces_count = replacees->len;
446 for (i = 0; i < replaces_count; i++) {
447 pkg_t *replacee = replacees->pkgs[i];
448 if (replacee->state_status != SS_INSTALLED) {
449 opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
450 err = opkg_install_pkg(replacee, 0);
458 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
460 opkg_install_check_downgrade(pkg_t * pkg, pkg_t * old_pkg, int message)
463 char message_out[15];
464 char *old_version = pkg_version_str_alloc(old_pkg);
465 char *new_version = pkg_version_str_alloc(pkg);
466 int cmp = pkg_compare_versions(old_pkg, pkg);
469 memset(message_out, '\x0', 15);
470 strncpy(message_out, "Upgrading ", strlen("Upgrading "));
471 if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */
472 cmp = -1; /* then we force opkg to downgrade */
473 strncpy(message_out, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
474 /* reinstall, and some check could fail asking the "force-reinstall" option */
478 if (!conf->download_only)
480 "Not downgrading package %s on %s from %s to %s.\n",
481 old_pkg->name, old_pkg->dest->name,
482 old_version, new_version);
484 } else if (cmp < 0) {
485 if (!conf->download_only)
487 "%s%s on %s from %s to %s...\n",
488 message_out, pkg->name,
489 old_pkg->dest->name, old_version,
491 pkg->dest = old_pkg->dest;
493 } else { /* cmp == 0 */
495 if (!conf->download_only)
497 "%s (%s) already install on %s.\n",
498 pkg->name, new_version,
499 old_pkg->dest->name);
506 char message_out[15];
507 memset(message_out, '\x0', 15);
509 strncpy(message_out, "Upgrading ",
510 strlen("Upgrading "));
512 strncpy(message_out, "Installing ",
513 strlen("Installing "));
514 char *version = pkg_version_str_alloc(pkg);
516 if (!conf->download_only)
517 opkg_msg(NOTICE, "%s%s (%s) to %s...\n", message_out,
518 pkg->name, version, pkg->dest->name);
524 static int prerm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg)
526 /* DPKG_INCOMPATIBILITY:
527 dpkg does some things here that we don't do yet. Do we care?
529 1. If a version of the package is already installed, call
530 old-prerm upgrade new-version
531 2. If the script runs but exits with a non-zero exit status
532 new-prerm failed-upgrade old-version
533 Error unwind, for both the above cases:
534 old-postinst abort-upgrade new-version
540 if (!old_pkg || !pkg)
543 new_version = pkg_version_str_alloc(pkg);
545 sprintf_alloc(&script_args, "upgrade %s", new_version);
547 err = pkg_run_script(old_pkg, "prerm", script_args);
550 opkg_msg(ERROR, "prerm script for package \"%s\" failed\n",
557 static int prerm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg)
559 /* DPKG_INCOMPATIBILITY:
560 dpkg does some things here that we don't do yet. Do we care?
561 (See prerm_upgrade_old_package for details)
566 static int prerm_deconfigure_conflictors(pkg_t * pkg, pkg_vec_t * conflictors)
568 /* DPKG_INCOMPATIBILITY:
569 dpkg does some things here that we don't do yet. Do we care?
570 2. If a 'conflicting' package is being removed at the same time:
571 1. If any packages depended on that conflicting package and
572 --auto-deconfigure is specified, call, for each such package:
573 deconfigured's-prerm deconfigure \
574 in-favour package-being-installed version \
575 removing conflicting-package version
577 deconfigured's-postinst abort-deconfigure \
578 in-favour package-being-installed-but-failed version \
579 removing conflicting-package version
581 The deconfigured packages are marked as requiring
582 configuration, so that if --install is used they will be
583 configured again if possible.
584 2. To prepare for removal of the conflicting package, call:
585 conflictor's-prerm remove in-favour package new-version
587 conflictor's-postinst abort-remove in-favour package new-version
593 prerm_deconfigure_conflictors_unwind(pkg_t * pkg, pkg_vec_t * conflictors)
595 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
596 do yet. Do we care? (See prerm_deconfigure_conflictors for
601 static int preinst_configure(pkg_t * pkg, pkg_t * old_pkg)
607 char *old_version = pkg_version_str_alloc(old_pkg);
608 sprintf_alloc(&preinst_args, "upgrade %s", old_version);
610 } else if (pkg->state_status == SS_CONFIG_FILES) {
611 char *pkg_version = pkg_version_str_alloc(pkg);
612 sprintf_alloc(&preinst_args, "install %s", pkg_version);
615 preinst_args = xstrdup("install");
618 err = pkg_run_script(pkg, "preinst", preinst_args);
620 opkg_msg(ERROR, "Aborting installation of %s.\n", pkg->name);
629 static int preinst_configure_unwind(pkg_t * pkg, pkg_t * old_pkg)
631 /* DPKG_INCOMPATIBILITY:
632 dpkg does the following error unwind, should we?
633 pkg->postrm abort-upgrade old-version
634 OR pkg->postrm abort-install old-version
635 OR pkg->postrm abort-install
640 static char *backup_filename_alloc(const char *file_name)
644 sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
649 static int backup_make_backup(const char *file_name)
654 backup = backup_filename_alloc(file_name);
655 err = file_copy(file_name, backup);
657 opkg_msg(ERROR, "Failed to copy %s to %s\n", file_name, backup);
665 static int backup_exists_for(const char *file_name)
670 backup = backup_filename_alloc(file_name);
672 ret = file_exists(backup);
679 static int backup_remove(const char *file_name)
683 backup = backup_filename_alloc(file_name);
690 static int backup_modified_conffiles(pkg_t * pkg, pkg_t * old_pkg)
693 conffile_list_elt_t *iter;
699 /* Backup all modified conffiles */
701 for (iter = nv_pair_list_first(&old_pkg->conffiles); iter;
702 iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
706 cf_name = root_filename_alloc(cf->name);
708 /* Don't worry if the conffile is just plain gone */
709 if (file_exists(cf_name)
710 && conffile_has_been_modified(cf)) {
711 err = backup_make_backup(cf_name);
720 /* Backup all conffiles that were not conffiles in old_pkg */
721 for (iter = nv_pair_list_first(&pkg->conffiles); iter;
722 iter = nv_pair_list_next(&pkg->conffiles, iter)) {
724 cf = (conffile_t *) iter->data;
725 cf_name = root_filename_alloc(cf->name);
726 /* Ignore if this was a conffile in old_pkg as well */
727 if (pkg_get_conffile(old_pkg, cf->name)) {
731 if (file_exists(cf_name) && (!backup_exists_for(cf_name))) {
732 err = backup_make_backup(cf_name);
743 static int backup_modified_conffiles_unwind(pkg_t * pkg, pkg_t * old_pkg)
745 conffile_list_elt_t *iter;
748 for (iter = nv_pair_list_first(&old_pkg->conffiles); iter;
749 iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
750 backup_remove(((nv_pair_t *) iter->data)->name);
754 for (iter = nv_pair_list_first(&pkg->conffiles); iter;
755 iter = nv_pair_list_next(&pkg->conffiles, iter)) {
756 backup_remove(((nv_pair_t *) iter->data)->name);
762 static int check_data_file_clashes(pkg_t * pkg, pkg_t * old_pkg)
764 /* DPKG_INCOMPATIBILITY:
765 opkg takes a slightly different approach than dpkg at this
766 point. dpkg installs each file in the new package while
767 creating a backup for any file that is replaced, (so that it
768 can unwind if necessary). To avoid complexity and redundant
769 storage, opkg doesn't do any installation until later, (at the
770 point at which dpkg removes the backups.
772 But, we do have to check for data file clashes, since after
773 installing a package with a file clash, removing either of the
774 packages involved in the clash has the potential to break the
777 str_list_t *files_list;
778 str_list_elt_t *iter, *niter;
782 files_list = pkg_get_installed_files(pkg);
783 if (files_list == NULL)
786 for (iter = str_list_first(files_list), niter =
787 str_list_next(files_list, iter); iter;
788 iter = niter, niter = str_list_next(files_list, iter)) {
789 filename = (char *)iter->data;
790 if (file_exists(filename) && (!file_is_dir(filename))) {
794 if (backup_exists_for(filename)) {
798 /* Pre-existing files are OK if force-overwrite was asserted. */
799 if (conf->force_overwrite) {
800 /* but we need to change who owns this file */
801 file_hash_set_file_owner(filename, pkg);
805 owner = file_hash_get_file_owner(filename);
807 /* Pre-existing files are OK if owned by the pkg being upgraded. */
808 if (owner && old_pkg) {
809 if (strcmp(owner->name, old_pkg->name) == 0) {
814 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
817 "Checking replaces for %s in package %s\n",
818 filename, owner->name);
819 if (pkg_replaces(pkg, owner)) {
822 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
823 then it's ok to overwrite. */
824 if (strcmp(owner->name, pkg->name) == 0) {
826 "Replacing pre-existing file %s"
827 " owned by package %s\n",
828 filename, owner->name);
833 /* Pre-existing files are OK if they are obsolete */
834 obs = hash_table_get(&conf->obs_file_hash, filename);
837 "Pre-exiting file %s is obsolete."
838 " obs_pkg=%s\n", filename, obs->name);
842 /* We have found a clash. */
843 opkg_msg(ERROR, "Package %s wants to install file %s\n"
844 "\tBut that file is already provided by package ",
845 pkg->name, filename);
847 opkg_message(ERROR, "%s\n", owner->name);
849 opkg_message(ERROR, "<no package>\n"
850 "Please move this file out of the way and try again.\n");
855 pkg_free_installed_files(pkg);
861 * XXX: This function sucks, as does the below comment.
863 static int check_data_file_clashes_change(pkg_t * pkg, pkg_t * old_pkg)
865 /* Basically that's the worst hack I could do to be able to change ownership of
866 file list, but, being that we have no way to unwind the mods, due to structure
867 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
868 What we do here is change the ownership of file in hash if a replace ( or similar events
870 Only the action that are needed to change name should be considered.
871 @@@ To change after 1.0 release.
873 str_list_t *files_list;
874 str_list_elt_t *iter, *niter;
876 char *root_filename = NULL;
878 files_list = pkg_get_installed_files(pkg);
879 if (files_list == NULL)
882 for (iter = str_list_first(files_list), niter =
883 str_list_next(files_list, iter); iter;
884 iter = niter, niter = str_list_next(files_list, niter)) {
885 char *filename = (char *)iter->data;
888 root_filename = NULL;
890 root_filename = root_filename_alloc(filename);
891 if (file_exists(root_filename) && (!file_is_dir(root_filename))) {
894 owner = file_hash_get_file_owner(filename);
896 if (conf->force_overwrite) {
897 /* but we need to change who owns this file */
898 file_hash_set_file_owner(filename, pkg);
902 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
904 if (pkg_replaces(pkg, owner)) {
905 /* It's now time to change the owner of that file.
906 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
908 "Replacing pre-existing file %s "
909 "owned by package %s\n",
910 filename, owner->name);
911 file_hash_set_file_owner(filename, pkg);
920 root_filename = NULL;
922 pkg_free_installed_files(pkg);
927 static int check_data_file_clashes_unwind(pkg_t * pkg, pkg_t * old_pkg)
929 /* Nothing to do since check_data_file_clashes doesn't change state */
933 static int postrm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg)
935 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
936 1. If the package is being upgraded, call
937 old-postrm upgrade new-version
938 2. If this fails, attempt:
939 new-postrm failed-upgrade old-version
940 Error unwind, for both cases:
941 old-preinst abort-upgrade new-version */
946 if (!old_pkg || !pkg)
949 new_version = pkg_version_str_alloc(pkg);
951 sprintf_alloc(&script_args, "upgrade %s", new_version);
953 err = pkg_run_script(old_pkg, "postrm", script_args);
956 opkg_msg(ERROR, "postrm script for package \"%s\" failed\n",
963 static int postrm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg)
965 /* DPKG_INCOMPATIBILITY:
966 dpkg does some things here that we don't do yet. Do we care?
967 (See postrm_upgrade_old_pkg for details)
972 static int remove_obsolesced_files(pkg_t * pkg, pkg_t * old_pkg)
975 str_list_t *old_files;
977 str_list_t *new_files;
979 hash_table_t new_files_table;
981 old_files = pkg_get_installed_files(old_pkg);
982 if (old_files == NULL)
985 new_files = pkg_get_installed_files(pkg);
986 if (new_files == NULL) {
987 pkg_free_installed_files(old_pkg);
991 new_files_table.entries = NULL;
992 hash_table_init("new_files", &new_files_table, 20);
993 for (nf = str_list_first(new_files); nf;
994 nf = str_list_next(new_files, nf)) {
996 hash_table_insert(&new_files_table, nf->data, nf->data);
999 for (of = str_list_first(old_files); of;
1000 of = str_list_next(old_files, of)) {
1003 old = (char *)of->data;
1004 new = (char *)hash_table_get(&new_files_table, old);
1008 if (file_is_dir(old)) {
1011 owner = file_hash_get_file_owner(old);
1012 if (owner != old_pkg) {
1013 /* in case obsolete file no longer belongs to old_pkg */
1017 /* old file is obsolete */
1018 opkg_msg(NOTICE, "Removing obsolete file %s.\n", old);
1019 if (!conf->noaction) {
1022 opkg_perror(ERROR, "unlinking %s failed", old);
1027 hash_table_deinit(&new_files_table);
1028 pkg_free_installed_files(old_pkg);
1029 pkg_free_installed_files(pkg);
1034 static int install_maintainer_scripts(pkg_t * pkg, pkg_t * old_pkg)
1039 sprintf_alloc(&prefix, "%s.", pkg->name);
1040 ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
1041 pkg->dest->info_dir,
1047 static int remove_disappeared(pkg_t * pkg)
1049 /* DPKG_INCOMPATIBILITY:
1050 This is a fairly sophisticated dpkg operation. Shall we
1053 /* Any packages all of whose files have been overwritten during the
1054 installation, and which aren't required for dependencies, are
1055 considered to have been removed. For each such package
1056 1. disappearer's-postrm disappear overwriter overwriter-version
1057 2. The package's maintainer scripts are removed
1058 3. It is noted in the status database as being in a sane state,
1059 namely not installed (any conffiles it may have are ignored,
1060 rather than being removed by dpkg). Note that disappearing
1061 packages do not have their prerm called, because dpkg doesn't
1062 know in advance that the package is going to vanish.
1067 static int install_data_files(pkg_t * pkg)
1071 /* opkg takes a slightly different approach to data file backups
1072 than dpkg. Rather than removing backups at this point, we
1073 actually do the data file installation now. See comments in
1074 check_data_file_clashes() for more details. */
1076 opkg_msg(INFO, "Extracting data files to %s.\n", pkg->dest->root_dir);
1077 err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
1082 /* The "Essential" control field may only be present in the control
1083 * file and not in the Packages list. Ensure we capture it regardless.
1085 * XXX: This should be fixed outside of opkg, in the Package list.
1087 set_flags_from_control(pkg);
1089 opkg_msg(DEBUG, "Calling pkg_write_filelist.\n");
1090 err = pkg_write_filelist(pkg);
1094 /* XXX: FEATURE: opkg should identify any files which existed
1095 before installation and which were overwritten, (see
1096 check_data_file_clashes()). What it must do is remove any such
1097 files from the filelist of the old package which provided the
1098 file. Otherwise, if the old package were removed at some point
1099 it would break the new package. Removing the new package will
1100 also break the old one, but this cannot be helped since the old
1101 package's file has already been deleted. This is the importance
1102 of check_data_file_clashes(), and only allowing opkg to install
1103 a clashing package with a user force. */
1108 static int resolve_conffiles(pkg_t * pkg)
1110 conffile_list_elt_t *iter;
1118 for (iter = nv_pair_list_first(&pkg->conffiles); iter;
1119 iter = nv_pair_list_next(&pkg->conffiles, iter)) {
1120 char *root_filename;
1121 cf = (conffile_t *) iter->data;
1122 root_filename = root_filename_alloc(cf->name);
1124 /* Might need to initialize the md5sum for each conffile */
1125 if (cf->value == NULL) {
1126 cf->value = file_sha256sum_alloc(root_filename);
1129 if (!file_exists(root_filename)) {
1130 free(root_filename);
1134 cf_backup = backup_filename_alloc(root_filename);
1136 if (file_exists(cf_backup)) {
1137 /* Let's compute md5 to test if files are changed */
1139 if (cf->value && strlen(cf->value) > 33) {
1140 chksum = file_sha256sum_alloc(cf_backup);
1142 chksum = file_md5sum_alloc(cf_backup);
1145 chksum = file_sha256sum_alloc(cf_backup);
1147 if (chksum && cf->value
1148 && strcmp(cf->value, chksum) != 0) {
1149 if (conf->force_maintainer) {
1151 "Conffile %s using maintainer's setting.\n",
1155 sprintf_alloc(&new_conffile, "%s-opkg",
1158 "Existing conffile %s "
1159 "is different from the conffile in the new package."
1160 " The new conffile will be placed at %s.\n",
1161 root_filename, new_conffile);
1162 rename(root_filename, new_conffile);
1163 rename(cf_backup, root_filename);
1173 free(root_filename);
1179 int opkg_install_by_name(const char *pkg_name)
1183 char *old_version, *new_version;
1185 old = pkg_hash_fetch_installed_by_name(pkg_name);
1187 opkg_msg(DEBUG2, "Old versions from pkg_hash_fetch %s.\n",
1188 pkg_get_string(old, PKG_VERSION));
1190 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name);
1192 opkg_msg(NOTICE, "Unknown package '%s'.\n", pkg_name);
1196 opkg_msg(DEBUG2, "Versions from pkg_hash_fetch:");
1198 opkg_message(DEBUG2, " old %s ", pkg_get_string(old, PKG_VERSION));
1199 opkg_message(DEBUG2, " new %s\n", pkg_get_string(new, PKG_VERSION));
1201 new->state_flag |= SF_USER;
1203 old_version = pkg_version_str_alloc(old);
1204 new_version = pkg_version_str_alloc(new);
1206 cmp = pkg_compare_versions(old, new);
1207 if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */
1208 opkg_msg(DEBUG, "Forcing downgrade\n");
1209 cmp = -1; /* then we force opkg to downgrade */
1210 /* We need to use a value < 0 because in the 0 case we are asking to */
1211 /* reinstall, and some check could fail asking the "force-reinstall" option */
1213 opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
1214 "\n\t%s is installed "
1215 "\n\t%s is available "
1216 "\n\t%d was comparison result\n",
1217 pkg_name, old_version, new_version, cmp);
1220 "Package %s (%s) installed in %s is up to date.\n",
1221 old->name, old_version, old->dest->name);
1225 } else if (cmp > 0) {
1227 "Not downgrading package %s on %s from %s to %s.\n",
1228 old->name, old->dest->name, old_version,
1233 } else if (cmp < 0) {
1234 new->dest = old->dest;
1235 old->state_want = SW_DEINSTALL;
1241 opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
1242 return opkg_install_pkg(new, 0);
1246 * @brief Really install a pkg_t
1248 int opkg_install_pkg(pkg_t * pkg, int from_upgrade)
1252 pkg_t *old_pkg = NULL;
1253 pkg_vec_t *replacees;
1254 abstract_pkg_t *ab_pkg = NULL;
1256 char *file_md5, *pkg_md5;
1258 char *file_sha256, *pkg_sha256;
1260 sigset_t newset, oldset;
1261 const char *local_filename;
1265 message = 1; /* Coming from an upgrade, and should change the output message */
1267 opkg_msg(DEBUG2, "Calling pkg_arch_supported.\n");
1269 if (!pkg_arch_supported(pkg)) {
1271 "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1272 pkg_get_string(pkg, PKG_ARCHITECTURE), pkg->name);
1275 if (pkg->state_status == SS_INSTALLED && conf->nodeps == 0) {
1276 err = satisfy_dependencies_for(pkg);
1280 opkg_msg(NOTICE, "Package %s is already installed on %s.\n",
1281 pkg->name, pkg->dest->name);
1285 if (pkg->dest == NULL) {
1286 pkg->dest = conf->default_dest;
1289 old_pkg = pkg_hash_fetch_installed_by_name(pkg->name);
1291 err = opkg_install_check_downgrade(pkg, old_pkg, message);
1295 pkg->state_want = SW_INSTALL;
1297 old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependencies */
1300 err = check_conflicts_for(pkg);
1304 /* this setup is to remove the upgrade scenario in the end when
1305 installing pkg A, A deps B & B deps on A. So both B and A are
1306 installed. Then A's installation is started resulting in an
1307 uncecessary upgrade */
1308 if (pkg->state_status == SS_INSTALLED)
1311 err = verify_pkg_installable(pkg);
1315 local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
1317 if (local_filename == NULL) {
1318 if (!conf->cache && conf->download_only) {
1320 if (getcwd(cwd, sizeof(cwd)) != NULL)
1321 err = opkg_download_pkg(pkg, cwd);
1325 err = opkg_download_pkg(pkg, conf->tmp_dir);
1328 opkg_msg(ERROR, "Failed to download %s. "
1329 "Perhaps you need to run 'opkg update'?\n",
1335 /* check that the repository is valid */
1336 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
1337 char *list_file_name, *sig_file_name, *lists_dir;
1339 /* check to ensure the package has come from a repository */
1340 if (conf->check_signature && pkg->src) {
1341 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
1342 ? conf->default_dest->lists_dir
1344 sprintf_alloc(&list_file_name, "%s/%s", lists_dir,
1346 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
1349 if (file_exists(sig_file_name)) {
1350 if (opkg_verify_file(list_file_name, sig_file_name)) {
1352 "Failed to verify the signature of %s.\n",
1354 if (!conf->force_signature)
1358 opkg_msg(ERROR, "Signature file is missing for %s. "
1359 "Perhaps you need to run 'opkg update'?\n",
1361 if (!conf->force_signature)
1366 free(list_file_name);
1367 free(sig_file_name);
1372 /* Check for md5 values */
1373 pkg_md5 = pkg_get_string(pkg, PKG_MD5SUM);
1375 file_md5 = file_md5sum_alloc(local_filename);
1376 if (file_md5 && strcmp(file_md5, pkg_md5)) {
1377 if (!conf->force_checksum) {
1378 opkg_msg(ERROR, "Package %s md5sum mismatch. "
1379 "Either the opkg or the package index are corrupt. "
1380 "Try 'opkg update'.\n", pkg->name);
1385 "Ignored %s md5sum mismatch.\n",
1395 /* Check for sha256 value */
1396 pkg_sha256 = pkg_get_string(pkg, PKG_SHA256SUM);
1398 file_sha256 = file_sha256sum_alloc(local_filename);
1399 if (file_sha256 && strcmp(file_sha256, pkg_sha256)) {
1400 if (!conf->force_checksum) {
1402 "Package %s sha256sum mismatch. "
1403 "Either the opkg or the package index are corrupt. "
1404 "Try 'opkg update'.\n", pkg->name);
1409 "Ignored %s sha256sum mismatch.\n",
1417 if (conf->download_only) {
1418 if (conf->nodeps == 0) {
1419 err = satisfy_dependencies_for(pkg);
1426 if (pkg->tmp_unpack_dir == NULL) {
1427 if (unpack_pkg_control_files(pkg) == -1) {
1429 "Failed to unpack control files from %s.\n",
1435 err = update_file_ownership(pkg, old_pkg);
1439 if (conf->nodeps == 0) {
1440 err = satisfy_dependencies_for(pkg);
1443 if (pkg->state_status == SS_UNPACKED)
1444 /* Circular dependency has installed it for us. */
1448 replacees = pkg_vec_alloc();
1449 pkg_get_installed_replacees(pkg, replacees);
1451 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1453 sigemptyset(&newset);
1454 sigaddset(&newset, SIGINT);
1455 sigprocmask(SIG_BLOCK, &newset, &oldset);
1457 opkg_state_changed++;
1458 pkg->state_flag |= SF_FILELIST_CHANGED;
1461 pkg_remove_orphan_dependent(pkg, old_pkg);
1462 old_pkg->is_upgrade = 1;
1463 pkg->is_upgrade = 1;
1465 /* XXX: BUG: we really should treat replacement more like an upgrade
1466 * Instead, we're going to remove the replacees
1468 err = pkg_remove_installed_replacees(replacees);
1470 goto UNWIND_REMOVE_INSTALLED_REPLACEES;
1472 err = prerm_upgrade_old_pkg(pkg, old_pkg);
1474 goto UNWIND_PRERM_UPGRADE_OLD_PKG;
1476 err = prerm_deconfigure_conflictors(pkg, replacees);
1478 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
1480 err = preinst_configure(pkg, old_pkg);
1482 goto UNWIND_PREINST_CONFIGURE;
1484 err = backup_modified_conffiles(pkg, old_pkg);
1486 goto UNWIND_BACKUP_MODIFIED_CONFFILES;
1488 err = check_data_file_clashes(pkg, old_pkg);
1490 goto UNWIND_CHECK_DATA_FILE_CLASHES;
1492 err = postrm_upgrade_old_pkg(pkg, old_pkg);
1494 goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
1499 /* point of no return: no unwinding after this */
1501 old_pkg->state_want = SW_DEINSTALL;
1503 if (old_pkg->state_flag & SF_NOPRUNE) {
1504 opkg_msg(INFO, "Not removing obsolesced files because "
1505 "package %s marked noprune.\n", old_pkg->name);
1507 opkg_msg(INFO, "Removing obsolesced files for %s\n",
1509 if (remove_obsolesced_files(pkg, old_pkg)) {
1510 opkg_msg(ERROR, "Failed to determine "
1511 "obsolete files from previously "
1512 "installed %s\n", old_pkg->name);
1516 /* removing files from old package, to avoid ghost files */
1517 remove_data_files_and_list(old_pkg);
1518 remove_maintainer_scripts(old_pkg);
1521 opkg_msg(INFO, "%s maintainer scripts.\n",
1522 (pkg->is_upgrade) ? ("Upgrading") : ("Installing"));
1523 if (install_maintainer_scripts(pkg, old_pkg)) {
1524 opkg_msg(ERROR, "Failed to extract maintainer scripts for %s."
1525 " Package debris may remain!\n", pkg->name);
1529 /* the following just returns 0 */
1530 remove_disappeared(pkg);
1532 opkg_msg(INFO, "Installing data files for %s.\n", pkg->name);
1534 if (install_data_files(pkg)) {
1535 opkg_msg(ERROR, "Failed to extract data files for %s. "
1536 "Package debris may remain!\n", pkg->name);
1540 err = check_data_file_clashes_change(pkg, old_pkg);
1542 opkg_msg(ERROR, "check_data_file_clashes_change() failed for "
1543 "for files belonging to %s.\n", pkg->name);
1546 opkg_msg(INFO, "Resolving conf files for %s\n", pkg->name);
1547 resolve_conffiles(pkg);
1549 pkg->state_status = SS_UNPACKED;
1550 old_state_flag = pkg->state_flag;
1551 pkg->state_flag &= ~SF_PREFER;
1552 opkg_msg(DEBUG, "pkg=%s old_state_flag=%x state_flag=%x\n",
1553 pkg->name, old_state_flag, pkg->state_flag);
1556 old_pkg->state_status = SS_NOT_INSTALLED;
1559 pkg_set_int(pkg, PKG_INSTALLED_TIME, now);
1561 ab_pkg = pkg->parent;
1563 ab_pkg->state_status = pkg->state_status;
1565 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1566 pkg_vec_free(replacees);
1569 UNWIND_POSTRM_UPGRADE_OLD_PKG:
1570 postrm_upgrade_old_pkg_unwind(pkg, old_pkg);
1571 UNWIND_CHECK_DATA_FILE_CLASHES:
1572 check_data_file_clashes_unwind(pkg, old_pkg);
1573 UNWIND_BACKUP_MODIFIED_CONFFILES:
1574 backup_modified_conffiles_unwind(pkg, old_pkg);
1575 UNWIND_PREINST_CONFIGURE:
1576 preinst_configure_unwind(pkg, old_pkg);
1577 UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
1578 prerm_deconfigure_conflictors_unwind(pkg, replacees);
1579 UNWIND_PRERM_UPGRADE_OLD_PKG:
1580 prerm_upgrade_old_pkg_unwind(pkg, old_pkg);
1581 UNWIND_REMOVE_INSTALLED_REPLACEES:
1582 pkg_remove_installed_replacees_unwind(replacees);
1585 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1587 pkg_vec_free(replacees);