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.
27 #include "pkg_extract.h"
29 #include "opkg_install.h"
30 #include "opkg_configure.h"
31 #include "opkg_download.h"
32 #include "opkg_remove.h"
34 #include "opkg_utils.h"
35 #include "opkg_message.h"
37 #include "opkg_defines.h"
39 #include "sprintf_alloc.h"
40 #include "file_util.h"
44 #include "libbb/libbb.h"
47 satisfy_dependencies_for(opkg_conf_t *conf, pkg_t *pkg)
50 pkg_vec_t *depends = pkg_vec_alloc();
52 char **unresolved = NULL;
55 ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf,
60 opkg_message(conf, OPKG_ERROR,
61 "%s: Cannot satisfy the following dependencies for %s:\n\t",
62 conf->force_depends ? "Warning" : "ERROR", pkg->name);
64 opkg_message(conf, OPKG_ERROR, " %s", *unresolved);
67 opkg_message(conf, OPKG_ERROR, "\n");
68 if (! conf->force_depends) {
69 opkg_message(conf, OPKG_INFO,
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);
74 return OPKG_PKG_DEPS_UNSATISFIED;
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_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
100 err = opkg_install_pkg(conf, 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);
117 check_conflicts_for(opkg_conf_t *conf, pkg_t *pkg)
120 pkg_vec_t *conflicts = NULL;
123 if (conf->force_depends) {
131 if (!conf->force_depends)
132 conflicts = (pkg_vec_t *)pkg_hash_fetch_conflicts(&conf->pkg_hash, pkg);
135 opkg_message(conf, level,
136 "%s: The following packages conflict with %s:\n\t", prefix, pkg->name);
138 while (i < conflicts->len)
139 opkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
140 opkg_message(conf, level, "\n");
141 pkg_vec_free(conflicts);
142 return OPKG_PKG_DEPS_UNSATISFIED;
148 update_file_ownership(opkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
150 str_list_t *new_list = pkg_get_installed_files(conf, new_pkg);
151 str_list_elt_t *iter, *niter;
153 for (iter = str_list_first(new_list), niter = str_list_next(new_list, 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(conf, new_file);
159 opkg_message(conf, OPKG_ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
160 if (!owner || (owner == old_pkg))
161 file_hash_set_file_owner(conf, new_file, new_pkg);
164 str_list_t *old_list = pkg_get_installed_files(conf, old_pkg);
165 for (iter = str_list_first(old_list), niter = str_list_next(old_list, iter);
167 iter = niter, niter = str_list_next(old_list, niter)) {
168 char *old_file = (char *)iter->data;
169 pkg_t *owner = file_hash_get_file_owner(conf, old_file);
170 if (owner == old_pkg) {
172 hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
175 pkg_free_installed_files(old_pkg);
177 pkg_free_installed_files(new_pkg);
182 verify_pkg_installable(opkg_conf_t *conf, pkg_t *pkg)
184 /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
186 /* sma 6.20.02: yup; here's the first bit */
188 * XXX: BUG easy for cworth
189 * 1) please point the call below to the correct current root destination
190 * 2) we need to resolve how to check the required space for a pending pkg,
191 * my diddling with the .opk file size below isn't going to cut it.
192 * 3) return a proper error code instead of 1
194 int comp_size, blocks_available;
197 if (!conf->force_space && pkg->installed_size != NULL) {
198 root_dir = pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir;
199 blocks_available = get_available_blocks(root_dir);
201 comp_size = strtoul(pkg->installed_size, NULL, 0);
202 /* round up a blocks count without doing fancy-but-slow casting jazz */
203 comp_size = (int)((comp_size + 1023) / 1024);
205 if (comp_size >= blocks_available) {
206 opkg_message(conf, OPKG_ERROR,
207 "Only have %d available blocks on filesystem %s, pkg %s needs %d\n",
208 blocks_available, root_dir, pkg->name, comp_size);
216 unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg)
219 char *conffiles_file_name;
221 FILE *conffiles_file;
223 sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
225 pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
226 if (pkg->tmp_unpack_dir == NULL) {
227 opkg_message(conf, OPKG_ERROR,
228 "%s: Failed to create temporary directory '%s': %s\n",
229 __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
233 err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
238 /* XXX: CLEANUP: There might be a cleaner place to read in the
239 conffiles. Seems like I should be able to get everything to go
240 through pkg_init_from_file. If so, maybe it would make sense to
241 move all of unpack_pkg_control_files to that function. */
243 /* Don't need to re-read conffiles if we already have it */
244 if (!nv_pair_list_empty(&pkg->conffiles)) {
248 sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
249 if (! file_exists(conffiles_file_name)) {
250 free(conffiles_file_name);
254 conffiles_file = fopen(conffiles_file_name, "r");
255 if (conffiles_file == NULL) {
256 fprintf(stderr, "%s: failed to open %s: %s\n",
257 __FUNCTION__, conffiles_file_name, strerror(errno));
258 free(conffiles_file_name);
261 free(conffiles_file_name);
265 char *cf_name_in_dest;
267 cf_name = file_read_line_alloc(conffiles_file);
268 if (cf_name == NULL) {
271 if (cf_name[0] == '\0') {
275 /* Prepend dest->root_dir to conffile name.
276 Take pains to avoid multiple slashes. */
277 root_dir = pkg->dest->root_dir;
278 if (conf->offline_root)
279 /* skip the offline_root prefix */
280 root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
281 sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
282 cf_name[0] == '/' ? (cf_name + 1) : cf_name);
284 /* Can't get an md5sum now, (file isn't extracted yet).
285 We'll wait until resolve_conffiles */
286 conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
289 free(cf_name_in_dest);
292 fclose(conffiles_file);
298 * Remove packages which were auto_installed due to a dependency by old_pkg,
299 * which are no longer a dependency in the new (upgraded) pkg.
302 pkg_remove_orphan_dependent(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
304 int i, j, k, l, found;
307 struct compound_depend *cd0, *cd1;
308 abstract_pkg_t **dependents;
310 int count0 = old_pkg->pre_depends_count +
311 old_pkg->depends_count +
312 old_pkg->recommends_count +
313 old_pkg->suggests_count;
314 int count1 = pkg->pre_depends_count +
316 pkg->recommends_count +
319 for (i=0; i<count0; i++) {
320 cd0 = &old_pkg->depends[i];
321 if (cd0->type != DEPEND)
323 for (j=0; j<cd0->possibility_count; j++) {
327 for (k=0; k<count1; k++) {
328 cd1 = &pkg->depends[i];
329 if (cd1->type != DEPEND)
331 for (l=0; l<cd1->possibility_count; l++) {
332 if (cd0->possibilities[j]
333 == cd1->possibilities[l]) {
346 * old_pkg has a dependency that pkg does not.
348 p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash,
349 cd0->possibilities[j]->pkg->name);
354 if (!p->auto_installed)
357 n_deps = pkg_has_installed_dependents(conf, NULL, p,
359 n_deps--; /* don't count old_pkg */
362 opkg_message (conf, OPKG_NOTICE,
363 "%s was autoinstalled and is "
364 "now orphaned, removing.\n",
367 /* p has one installed dependency (old_pkg),
368 * which we need to ignore during removal. */
369 p->state_flag |= SF_REPLACE;
371 opkg_remove_pkg(conf, p, 0);
373 opkg_message(conf, OPKG_INFO,
374 "%s was autoinstalled and is "
375 "still required by %d "
376 "installed packages.\n",
385 /* returns number of installed replacees */
387 pkg_get_installed_replacees(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *installed_replacees)
389 abstract_pkg_t **replaces = pkg->replaces;
390 int replaces_count = pkg->replaces_count;
392 for (i = 0; i < replaces_count; i++) {
393 abstract_pkg_t *ab_pkg = replaces[i];
394 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
396 for (j = 0; j < pkg_vec->len; j++) {
397 pkg_t *replacee = pkg_vec->pkgs[j];
398 if (!pkg_conflicts(pkg, replacee))
400 if (replacee->state_status == SS_INSTALLED) {
401 pkg_vec_insert(installed_replacees, replacee);
406 return installed_replacees->len;
410 pkg_remove_installed_replacees(opkg_conf_t *conf, pkg_vec_t *replacees)
413 int replaces_count = replacees->len;
414 for (i = 0; i < replaces_count; i++) {
415 pkg_t *replacee = replacees->pkgs[i];
417 replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
418 err = opkg_remove_pkg(conf, replacee,0);
425 /* to unwind the removal: make sure they are installed */
427 pkg_remove_installed_replacees_unwind(opkg_conf_t *conf, pkg_vec_t *replacees)
430 int replaces_count = replacees->len;
431 for (i = 0; i < replaces_count; i++) {
432 pkg_t *replacee = replacees->pkgs[i];
433 if (replacee->state_status != SS_INSTALLED) {
434 opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
435 err = opkg_install_pkg(conf, replacee,0);
443 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
445 opkg_install_check_downgrade(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg, int message)
448 char message_out[15];
449 char *old_version = pkg_version_str_alloc(old_pkg);
450 char *new_version = pkg_version_str_alloc(pkg);
451 int cmp = pkg_compare_versions(old_pkg, pkg);
454 memset(message_out,'\x0',15);
455 strncpy (message_out,"Upgrading ",strlen("Upgrading "));
456 if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
457 cmp = -1 ; /* then we force opkg to downgrade */
458 strncpy (message_out,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
459 /* reinstall, and some check could fail asking the "force-reinstall" option */
463 opkg_message(conf, OPKG_NOTICE,
464 "Not downgrading package %s on %s from %s to %s.\n",
465 old_pkg->name, old_pkg->dest->name, old_version, new_version);
467 } else if (cmp < 0) {
468 opkg_message(conf, OPKG_NOTICE,
469 "%s%s on %s from %s to %s...\n",
470 message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
471 pkg->dest = old_pkg->dest;
473 } else /* cmp == 0 */ {
474 if (conf->force_reinstall) {
475 opkg_message(conf, OPKG_NOTICE,
476 "Reinstalling %s (%s) on %s...\n",
477 pkg->name, new_version, old_pkg->dest->name);
478 pkg->dest = old_pkg->dest;
481 opkg_message(conf, OPKG_NOTICE,
482 "Not installing %s (%s) on %s -- already installed.\n",
483 pkg->name, new_version, old_pkg->dest->name);
491 char message_out[15] ;
492 memset(message_out,'\x0',15);
494 strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
496 strncpy( message_out,"Installing ",strlen("Installing ") );
497 char *version = pkg_version_str_alloc(pkg);
499 opkg_message(conf, OPKG_NOTICE,
500 "%s%s (%s) to %s...\n", message_out,
501 pkg->name, version, pkg->dest->name);
509 prerm_upgrade_old_pkg(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
511 /* DPKG_INCOMPATIBILITY:
512 dpkg does some things here that we don't do yet. Do we care?
514 1. If a version of the package is already installed, call
515 old-prerm upgrade new-version
516 2. If the script runs but exits with a non-zero exit status
517 new-prerm failed-upgrade old-version
518 Error unwind, for both the above cases:
519 old-postinst abort-upgrade new-version
525 prerm_upgrade_old_pkg_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
527 /* DPKG_INCOMPATIBILITY:
528 dpkg does some things here that we don't do yet. Do we care?
529 (See prerm_upgrade_old_package for details)
535 prerm_deconfigure_conflictors(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
537 /* DPKG_INCOMPATIBILITY:
538 dpkg does some things here that we don't do yet. Do we care?
539 2. If a 'conflicting' package is being removed at the same time:
540 1. If any packages depended on that conflicting package and
541 --auto-deconfigure is specified, call, for each such package:
542 deconfigured's-prerm deconfigure \
543 in-favour package-being-installed version \
544 removing conflicting-package version
546 deconfigured's-postinst abort-deconfigure \
547 in-favour package-being-installed-but-failed version \
548 removing conflicting-package version
550 The deconfigured packages are marked as requiring
551 configuration, so that if --install is used they will be
552 configured again if possible.
553 2. To prepare for removal of the conflicting package, call:
554 conflictor's-prerm remove in-favour package new-version
556 conflictor's-postinst abort-remove in-favour package new-version
562 prerm_deconfigure_conflictors_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
564 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
565 do yet. Do we care? (See prerm_deconfigure_conflictors for
571 preinst_configure(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
577 char *old_version = pkg_version_str_alloc(old_pkg);
578 sprintf_alloc(&preinst_args, "upgrade %s", old_version);
580 } else if (pkg->state_status == SS_CONFIG_FILES) {
581 char *pkg_version = pkg_version_str_alloc(pkg);
582 sprintf_alloc(&preinst_args, "install %s", pkg_version);
585 preinst_args = xstrdup("install");
588 err = pkg_run_script(conf, pkg, "preinst", preinst_args);
590 opkg_message(conf, OPKG_ERROR,
591 "Aborting installation of %s\n", pkg->name);
601 preinst_configure_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
603 /* DPKG_INCOMPATIBILITY:
604 dpkg does the following error unwind, should we?
605 pkg->postrm abort-upgrade old-version
606 OR pkg->postrm abort-install old-version
607 OR pkg->postrm abort-install
613 backup_filename_alloc(const char *file_name)
617 sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
624 backup_make_backup(opkg_conf_t *conf, const char *file_name)
629 backup = backup_filename_alloc(file_name);
630 err = file_copy(file_name, backup);
632 opkg_message(conf, OPKG_ERROR,
633 "%s: Failed to copy %s to %s\n",
634 __FUNCTION__, file_name, backup);
643 backup_exists_for(const char *file_name)
648 backup = backup_filename_alloc(file_name);
650 ret = file_exists(backup);
658 backup_remove(const char *file_name)
662 backup = backup_filename_alloc(file_name);
670 backup_modified_conffiles(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
673 conffile_list_elt_t *iter;
676 if (conf->noaction) return 0;
678 /* Backup all modified conffiles */
680 for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
684 cf_name = root_filename_alloc(conf, cf->name);
686 /* Don't worry if the conffile is just plain gone */
687 if (file_exists(cf_name) && conffile_has_been_modified(conf, cf)) {
688 err = backup_make_backup(conf, cf_name);
697 /* Backup all conffiles that were not conffiles in old_pkg */
698 for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
700 cf = (conffile_t *)iter->data;
701 cf_name = root_filename_alloc(conf, cf->name);
702 /* Ignore if this was a conffile in old_pkg as well */
703 if (pkg_get_conffile(old_pkg, cf->name)) {
707 if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
708 err = backup_make_backup(conf, cf_name);
720 backup_modified_conffiles_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
722 conffile_list_elt_t *iter;
725 for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
726 backup_remove(((nv_pair_t *)iter->data)->name);
730 for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
731 backup_remove(((nv_pair_t *)iter->data)->name);
739 check_data_file_clashes(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
741 /* DPKG_INCOMPATIBILITY:
742 opkg takes a slightly different approach than dpkg at this
743 point. dpkg installs each file in the new package while
744 creating a backup for any file that is replaced, (so that it
745 can unwind if necessary). To avoid complexity and redundant
746 storage, opkg doesn't do any installation until later, (at the
747 point at which dpkg removes the backups.
749 But, we do have to check for data file clashes, since after
750 installing a package with a file clash, removing either of the
751 packages involved in the clash has the potential to break the
754 str_list_t *files_list;
755 str_list_elt_t *iter, *niter;
759 files_list = pkg_get_installed_files(conf, pkg);
760 for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter);
762 iter = niter, niter = str_list_next(files_list, iter)) {
764 char *filename = (char *) iter->data;
765 root_filename = root_filename_alloc(conf, filename);
766 if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
769 /* Pre-existing conffiles are OK */
770 /* @@@@ should have way to check that it is a conffile -Jamey */
771 if (backup_exists_for(root_filename)) {
775 /* Pre-existing files are OK if force-overwrite was asserted. */
776 if (conf->force_overwrite) {
777 /* but we need to change who owns this file */
778 file_hash_set_file_owner(conf, filename, pkg);
782 owner = file_hash_get_file_owner(conf, filename);
784 /* Pre-existing files are OK if owned by the pkg being upgraded. */
785 if (owner && old_pkg) {
786 if (strcmp(owner->name, old_pkg->name) == 0) {
791 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
793 opkg_message(conf, OPKG_DEBUG2, "Checking for replaces for %s in package %s\n", filename, owner->name);
794 if (pkg_replaces(pkg, owner)) {
797 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
798 then it's ok to overwrite. */
799 if (strcmp(owner->name,pkg->name)==0){
800 opkg_message(conf, OPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
805 /* Pre-existing files are OK if they are obsolete */
806 obs = hash_table_get(&conf->obs_file_hash, filename);
808 opkg_message(conf, OPKG_INFO, "Pre-exiting file %s is obsolete. obs_pkg=%s\n", filename, obs->name);
812 /* We have found a clash. */
813 opkg_message(conf, OPKG_ERROR,
814 "Package %s wants to install file %s\n"
815 "\tBut that file is already provided by package ",
816 pkg->name, filename);
818 opkg_message(conf, OPKG_ERROR,
819 "%s\n", owner->name);
821 opkg_message(conf, OPKG_ERROR,
822 "<no package>\nPlease move this file out of the way and try again.\n");
828 pkg_free_installed_files(pkg);
834 check_data_file_clashes_change(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
836 /* Basically that's the worst hack I could do to be able to change ownership of
837 file list, but, being that we have no way to unwind the mods, due to structure
838 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
839 What we do here is change the ownership of file in hash if a replace ( or similar events
841 Only the action that are needed to change name should be considered.
842 @@@ To change after 1.0 release.
844 str_list_t *files_list;
845 str_list_elt_t *iter, *niter;
847 char *root_filename = NULL;
851 files_list = pkg_get_installed_files(conf, pkg);
852 for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter);
854 iter = niter, niter = str_list_next(files_list, niter)) {
855 char *filename = (char *) iter->data;
858 root_filename = NULL;
860 root_filename = root_filename_alloc(conf, filename);
861 if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
864 owner = file_hash_get_file_owner(conf, filename);
866 if (conf->force_overwrite) {
867 /* but we need to change who owns this file */
868 file_hash_set_file_owner(conf, filename, pkg);
873 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
875 if (pkg_replaces(pkg, owner)) {
876 /* It's now time to change the owner of that file.
877 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
878 opkg_message(conf, OPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
879 file_hash_set_file_owner(conf, filename, pkg);
888 root_filename = NULL;
890 pkg_free_installed_files(pkg);
896 check_data_file_clashes_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
898 /* Nothing to do since check_data_file_clashes doesn't change state */
903 postrm_upgrade_old_pkg(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
905 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
906 1. If the package is being upgraded, call
907 old-postrm upgrade new-version
908 2. If this fails, attempt:
909 new-postrm failed-upgrade old-version
910 Error unwind, for both cases:
911 old-preinst abort-upgrade new-version */
916 postrm_upgrade_old_pkg_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
918 /* DPKG_INCOMPATIBILITY:
919 dpkg does some things here that we don't do yet. Do we care?
920 (See postrm_upgrade_old_pkg for details)
926 remove_obsolesced_files(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
929 str_list_t *old_files;
931 str_list_t *new_files;
933 hash_table_t new_files_table;
935 if (old_pkg == NULL) {
939 old_files = pkg_get_installed_files(conf, old_pkg);
940 new_files = pkg_get_installed_files(conf, pkg);
942 new_files_table.entries = NULL;
943 hash_table_init("new_files" , &new_files_table, 20);
944 for (nf = str_list_first(new_files); nf; nf = str_list_next(new_files, nf)) {
946 hash_table_insert(&new_files_table, nf->data, nf->data);
949 for (of = str_list_first(old_files); of; of = str_list_next(old_files, of)) {
952 old = (char *)of->data;
953 new = (char *) hash_table_get (&new_files_table, old);
957 if (file_is_dir(old)) {
960 owner = file_hash_get_file_owner(conf, old);
961 if (owner != old_pkg) {
962 /* in case obsolete file no longer belongs to old_pkg */
966 /* old file is obsolete */
967 opkg_message(conf, OPKG_INFO,
968 " removing obsolete file %s\n", old);
969 if (!conf->noaction) {
972 opkg_message(conf, OPKG_ERROR, " Warning: remove %s failed: %s\n", old,
978 hash_table_deinit(&new_files_table);
979 pkg_free_installed_files(old_pkg);
980 pkg_free_installed_files(pkg);
986 install_maintainer_scripts(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
991 sprintf_alloc(&prefix, "%s.", pkg->name);
992 ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
1000 remove_disappeared(opkg_conf_t *conf, pkg_t *pkg)
1002 /* DPKG_INCOMPATIBILITY:
1003 This is a fairly sophisticated dpkg operation. Shall we
1006 /* Any packages all of whose files have been overwritten during the
1007 installation, and which aren't required for dependencies, are
1008 considered to have been removed. For each such package
1009 1. disappearer's-postrm disappear overwriter overwriter-version
1010 2. The package's maintainer scripts are removed
1011 3. It is noted in the status database as being in a sane state,
1012 namely not installed (any conffiles it may have are ignored,
1013 rather than being removed by dpkg). Note that disappearing
1014 packages do not have their prerm called, because dpkg doesn't
1015 know in advance that the package is going to vanish.
1021 install_data_files(opkg_conf_t *conf, pkg_t *pkg)
1025 /* opkg takes a slightly different approach to data file backups
1026 than dpkg. Rather than removing backups at this point, we
1027 actually do the data file installation now. See comments in
1028 check_data_file_clashes() for more details. */
1030 opkg_message(conf, OPKG_INFO,
1031 " extracting data files to %s\n", pkg->dest->root_dir);
1032 err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
1037 /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
1038 so we can't save ourself from removing important packages
1039 At this point we (should) have extracted the .control file, so it
1040 would be a good idea to reload the data in it, and set the Essential
1041 state in *pkg. From now on the Essential is back in status file and
1042 we can protect again.
1043 We should operate this way:
1044 fopen the file ( pkg->dest->root_dir/pkg->name.control )
1045 check for "Essential" in it
1046 set the value in pkg->essential.
1047 This new routine could be useful also for every other flag
1049 set_flags_from_control(conf, pkg) ;
1051 opkg_message(conf, OPKG_DEBUG, " Calling pkg_write_filelist from %s\n", __FUNCTION__);
1052 err = pkg_write_filelist(conf, pkg);
1056 /* XXX: FEATURE: opkg should identify any files which existed
1057 before installation and which were overwritten, (see
1058 check_data_file_clashes()). What it must do is remove any such
1059 files from the filelist of the old package which provided the
1060 file. Otherwise, if the old package were removed at some point
1061 it would break the new package. Removing the new package will
1062 also break the old one, but this cannot be helped since the old
1063 package's file has already been deleted. This is the importance
1064 of check_data_file_clashes(), and only allowing opkg to install
1065 a clashing package with a user force. */
1071 user_prefers_old_conffile(const char *file_name, const char *backup)
1074 const char *short_file_name;
1076 short_file_name = strrchr(file_name, '/');
1077 if (short_file_name) {
1080 short_file_name = file_name;
1084 response = get_user_response(" Configuration file '%s'\n"
1085 " ==> File on system created by you or by a script.\n"
1086 " ==> File also in package provided by package maintainer.\n"
1087 " What would you like to do about it ? Your options are:\n"
1088 " Y or I : install the package maintainer's version\n"
1089 " N or O : keep your currently-installed version\n"
1090 " D : show the differences between the versions (if diff is installed)\n"
1091 " The default action is to keep your current version.\n"
1092 " *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
1094 if (response == NULL)
1097 if (strcmp(response, "y") == 0
1098 || strcmp(response, "i") == 0
1099 || strcmp(response, "yes") == 0) {
1104 if (strcmp(response, "d") == 0) {
1105 const char *argv[] = {"diff", "-u", backup, file_name, NULL};
1107 printf(" [Press ENTER to continue]\n");
1108 response = file_read_line_alloc(stdin);
1119 resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg)
1121 conffile_list_elt_t *iter;
1126 if (conf->noaction) return 0;
1128 for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
1129 char *root_filename;
1130 cf = (conffile_t *)iter->data;
1131 root_filename = root_filename_alloc(conf, cf->name);
1133 /* Might need to initialize the md5sum for each conffile */
1134 if (cf->value == NULL) {
1135 cf->value = file_md5sum_alloc(root_filename);
1138 if (!file_exists(root_filename)) {
1139 free(root_filename);
1143 cf_backup = backup_filename_alloc(root_filename);
1146 if (file_exists(cf_backup)) {
1147 /* Let's compute md5 to test if files are changed */
1148 md5sum = file_md5sum_alloc(cf_backup);
1149 if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
1150 if (conf->force_maintainer) {
1151 opkg_message(conf, OPKG_NOTICE, "Conffile %s using maintainer's setting.\n", cf_backup);
1152 } else if (conf->force_defaults
1153 || user_prefers_old_conffile(root_filename, cf_backup) ) {
1154 rename(cf_backup, root_filename);
1163 free(root_filename);
1171 opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
1175 char *old_version, *new_version;
1177 opkg_message(conf, OPKG_DEBUG2, " Getting old from pkg_hash_fetch \n" );
1178 old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
1180 opkg_message(conf, OPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n", old->version );
1182 opkg_message(conf, OPKG_DEBUG2, " Getting new from pkg_hash_fetch \n" );
1183 new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name, &err);
1185 opkg_message(conf, OPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n", new->version );
1187 /* Pigi Basically here is broken the version stuff.
1188 What's happening is that nothing provide the version to differents
1189 functions, so the returned struct is always the latest.
1190 That's why the install by name don't work.
1192 opkg_message(conf, OPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
1195 opkg_message(conf, OPKG_DEBUG2, " old %s ", old->version );
1197 opkg_message(conf, OPKG_DEBUG2, " new %s ", new->version );
1198 opkg_message(conf, OPKG_DEBUG2, " \n");
1204 return OPKG_PKG_HAS_NO_CANDIDATE;
1207 new->state_flag |= SF_USER;
1209 old_version = pkg_version_str_alloc(old);
1210 new_version = pkg_version_str_alloc(new);
1212 cmp = pkg_compare_versions(old, new);
1213 if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
1214 opkg_message(conf, OPKG_DEBUG, " Forcing downgrade \n");
1215 cmp = -1 ; /* then we force opkg to downgrade */
1216 /* We need to use a value < 0 because in the 0 case we are asking to */
1217 /* reinstall, and some check could fail asking the "force-reinstall" option */
1219 opkg_message(conf, OPKG_DEBUG,
1220 "Comparing visible versions of pkg %s:"
1221 "\n\t%s is installed "
1222 "\n\t%s is available "
1223 "\n\t%d was comparison result\n",
1224 pkg_name, old_version, new_version, cmp);
1225 if (cmp == 0 && !conf->force_reinstall) {
1226 opkg_message(conf, OPKG_NOTICE,
1227 "Package %s (%s) installed in %s is up to date.\n",
1228 old->name, old_version, old->dest->name);
1232 } else if (cmp > 0) {
1233 opkg_message(conf, OPKG_NOTICE,
1234 "Not downgrading package %s on %s from %s to %s.\n",
1235 old->name, old->dest->name, old_version, new_version);
1239 } else if (cmp < 0) {
1240 new->dest = old->dest;
1241 old->state_want = SW_DEINSTALL; /* Here probably the problem for bug 1277 */
1247 /* XXX: CLEANUP: The error code of opkg_install_by_name is really
1248 supposed to be an opkg_error_t, but opkg_install_pkg could
1249 return any kind of integer, (might be errno from a syscall,
1250 etc.). This is a real mess and will need to be cleaned up if
1251 anyone ever wants to make a nice libopkg. */
1253 opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
1254 return opkg_install_pkg(conf, new,0);
1258 * @brief Really install a pkg_t
1261 opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
1265 pkg_t *old_pkg = NULL;
1266 pkg_vec_t *replacees;
1267 abstract_pkg_t *ab_pkg = NULL;
1275 message = 1; /* Coming from an upgrade, and should change the output message */
1278 opkg_message(conf, OPKG_ERROR,
1279 "INTERNAL ERROR: null pkg passed to opkg_install_pkg\n");
1280 return OPKG_INSTALL_ERR_INTERNAL;
1283 opkg_message(conf, OPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
1285 if (!pkg_arch_supported(conf, pkg)) {
1286 opkg_message(conf, OPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1287 pkg->architecture, pkg->name);
1288 return OPKG_INSTALL_ERR_INTERNAL;
1290 if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
1291 err = satisfy_dependencies_for(conf, pkg);
1292 if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
1294 opkg_message(conf, OPKG_NOTICE,
1295 "Package %s is already installed in %s.\n",
1296 pkg->name, pkg->dest->name);
1300 if (pkg->dest == NULL) {
1301 pkg->dest = conf->default_dest;
1304 old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
1306 err = opkg_install_check_downgrade(conf, pkg, old_pkg, message);
1307 if (err) { return OPKG_INSTALL_ERR_NO_DOWNGRADE; }
1309 pkg->state_want = SW_INSTALL;
1311 old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
1315 /* Abhaya: conflicts check */
1316 err = check_conflicts_for(conf, pkg);
1317 if (err) { return OPKG_INSTALL_ERR_CONFLICTS; }
1319 /* this setup is to remove the upgrade scenario in the end when
1320 installing pkg A, A deps B & B deps on A. So both B and A are
1321 installed. Then A's installation is started resulting in an
1322 uncecessary upgrade */
1323 if (pkg->state_status == SS_INSTALLED
1324 && conf->force_reinstall == 0) return 0;
1326 err = verify_pkg_installable(conf, pkg);
1327 if (err) { return OPKG_INSTALL_ERR_NO_SPACE; }
1329 if (pkg->local_filename == NULL) {
1330 err = opkg_download_pkg(conf, pkg, conf->tmp_dir);
1332 opkg_message(conf, OPKG_ERROR,
1333 "Failed to download %s. Perhaps you need to run 'opkg update'?\n",
1335 return OPKG_INSTALL_ERR_DOWNLOAD;
1339 /* check that the repository is valid */
1340 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1341 char *list_file_name, *sig_file_name, *lists_dir;
1343 /* check to ensure the package has come from a repository */
1344 if (conf->check_signature && pkg->src)
1346 sprintf_alloc (&lists_dir, "%s",
1347 (conf->restrict_to_default_dest)
1348 ? conf->default_dest->lists_dir
1350 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
1351 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
1353 if (file_exists (sig_file_name))
1355 if (opkg_verify_file (conf, list_file_name, sig_file_name)){
1356 opkg_message(conf, OPKG_ERROR, "Failed to verify the signature of: %s\n",
1358 return OPKG_INSTALL_ERR_SIGNATURE;
1361 opkg_message(conf, OPKG_ERROR, "Signature file is missing. "
1362 "Perhaps you need to run 'opkg update'?\n");
1363 return OPKG_INSTALL_ERR_SIGNATURE;
1367 free (list_file_name);
1368 free (sig_file_name);
1372 /* Check for md5 values */
1375 file_md5 = file_md5sum_alloc(pkg->local_filename);
1376 if (file_md5 && strcmp(file_md5, pkg->md5sum))
1378 opkg_message(conf, OPKG_ERROR,
1379 "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1382 return OPKG_INSTALL_ERR_MD5;
1389 /* Check for sha256 value */
1392 file_sha256 = file_sha256sum_alloc(pkg->local_filename);
1393 if (file_sha256 && strcmp(file_sha256, pkg->sha256sum))
1395 opkg_message(conf, OPKG_ERROR,
1396 "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1399 return OPKG_INSTALL_ERR_SHA256;
1406 if (pkg->tmp_unpack_dir == NULL) {
1407 unpack_pkg_control_files(conf, pkg);
1410 /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
1411 /* Pigi: check if it will pass from here when replacing. It seems to fail */
1412 /* That's rather strange that files don't change owner. Investigate !!!!!!*/
1413 err = update_file_ownership(conf, pkg, old_pkg);
1414 if (err) { return OPKG_ERR_UNKNOWN; }
1416 if (conf->nodeps == 0) {
1417 err = satisfy_dependencies_for(conf, pkg);
1418 if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
1419 if (pkg->state_status == SS_UNPACKED)
1420 /* Circular dependency has installed it for us. */
1424 replacees = pkg_vec_alloc();
1425 pkg_get_installed_replacees(conf, pkg, replacees);
1427 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1429 sigset_t newset, oldset;
1431 sigemptyset(&newset);
1432 sigaddset(&newset, SIGINT);
1433 sigprocmask(SIG_BLOCK, &newset, &oldset);
1435 opkg_state_changed++;
1436 pkg->state_flag |= SF_FILELIST_CHANGED;
1439 pkg_remove_orphan_dependent(conf, pkg, old_pkg);
1441 /* XXX: BUG: we really should treat replacement more like an upgrade
1442 * Instead, we're going to remove the replacees
1444 err = pkg_remove_installed_replacees(conf, replacees);
1445 if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
1447 err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
1448 if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
1450 err = prerm_deconfigure_conflictors(conf, pkg, replacees);
1451 if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
1453 err = preinst_configure(conf, pkg, old_pkg);
1454 if (err) goto UNWIND_PREINST_CONFIGURE;
1456 err = backup_modified_conffiles(conf, pkg, old_pkg);
1457 if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
1459 err = check_data_file_clashes(conf, pkg, old_pkg);
1460 if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
1462 err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
1463 if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
1465 if (conf->noaction) return 0;
1467 /* point of no return: no unwinding after this */
1468 if (old_pkg && !conf->force_reinstall) {
1469 old_pkg->state_want = SW_DEINSTALL;
1471 if (old_pkg->state_flag & SF_NOPRUNE) {
1472 opkg_message(conf, OPKG_INFO,
1473 " not removing obsolesced files because package marked noprune\n");
1475 opkg_message(conf, OPKG_INFO,
1476 " removing obsolesced files\n");
1477 remove_obsolesced_files(conf, pkg, old_pkg);
1480 /* removing files from old package, to avoid ghost files */
1481 remove_data_files_and_list(conf, old_pkg);
1482 remove_maintainer_scripts(conf, old_pkg);
1486 opkg_message(conf, OPKG_INFO,
1487 " installing maintainer scripts\n");
1488 install_maintainer_scripts(conf, pkg, old_pkg);
1490 /* the following just returns 0 */
1491 remove_disappeared(conf, pkg);
1493 opkg_message(conf, OPKG_INFO,
1494 " installing data files\n");
1495 install_data_files(conf, pkg);
1497 /* read comments from function for detail but I will execute this here as all other tests are ok.*/
1498 err = check_data_file_clashes_change(conf, pkg, old_pkg);
1500 opkg_message(conf, OPKG_INFO,
1501 " resolving conf files\n");
1502 resolve_conffiles(conf, pkg);
1504 pkg->state_status = SS_UNPACKED;
1505 old_state_flag = pkg->state_flag;
1506 pkg->state_flag &= ~SF_PREFER;
1507 opkg_message(conf, OPKG_DEBUG, " pkg=%s old_state_flag=%x state_flag=%x\n", pkg->name, old_state_flag, pkg->state_flag);
1509 if (old_pkg && !conf->force_reinstall) {
1510 old_pkg->state_status = SS_NOT_INSTALLED;
1513 time(&pkg->installed_time);
1515 ab_pkg = pkg->parent;
1517 ab_pkg->state_status = pkg->state_status;
1519 opkg_message(conf, OPKG_INFO, "Done.\n");
1521 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1522 pkg_vec_free (replacees);
1526 UNWIND_POSTRM_UPGRADE_OLD_PKG:
1527 postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
1528 UNWIND_CHECK_DATA_FILE_CLASHES:
1529 check_data_file_clashes_unwind(conf, pkg, old_pkg);
1530 UNWIND_BACKUP_MODIFIED_CONFFILES:
1531 backup_modified_conffiles_unwind(conf, pkg, old_pkg);
1532 UNWIND_PREINST_CONFIGURE:
1533 preinst_configure_unwind(conf, pkg, old_pkg);
1534 UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
1535 prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
1536 UNWIND_PRERM_UPGRADE_OLD_PKG:
1537 prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
1538 UNWIND_REMOVE_INSTALLED_REPLACEES:
1539 pkg_remove_installed_replacees_unwind(conf, replacees);
1541 opkg_message(conf, OPKG_INFO,
1544 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1546 pkg_vec_free (replacees);
1547 return OPKG_ERR_UNKNOWN;