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"
43 #include "libbb/libbb.h"
46 satisfy_dependencies_for(opkg_conf_t *conf, pkg_t *pkg)
49 pkg_vec_t *depends = pkg_vec_alloc();
51 char **unresolved = NULL;
54 ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf,
59 opkg_message(conf, OPKG_ERROR,
60 "%s: Cannot satisfy the following dependencies for %s:\n\t",
61 conf->force_depends ? "Warning" : "ERROR", pkg->name);
63 opkg_message(conf, OPKG_ERROR, " %s", *unresolved);
66 opkg_message(conf, OPKG_ERROR, "\n");
67 if (! conf->force_depends) {
68 opkg_message(conf, OPKG_INFO,
69 "This could mean that your package list is out of date or that the packages\n"
70 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
71 "of this problem try again with the '-force-depends' option.\n");
72 pkg_vec_free(depends);
73 return OPKG_PKG_DEPS_UNSATISFIED;
78 pkg_vec_free(depends);
82 /* Mark packages as to-be-installed */
83 for (i=0; i < depends->len; i++) {
84 /* Dependencies should be installed the same place as pkg */
85 if (depends->pkgs[i]->dest == NULL) {
86 depends->pkgs[i]->dest = pkg->dest;
88 depends->pkgs[i]->state_want = SW_INSTALL;
91 for (i = 0; i < depends->len; i++) {
92 dep = depends->pkgs[i];
93 /* The package was uninstalled when we started, but another
94 dep earlier in this loop may have depended on it and pulled
95 it in, so check first. */
96 if ((dep->state_status != SS_INSTALLED)
97 && (dep->state_status != SS_UNPACKED)) {
98 opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
99 err = opkg_install_pkg(conf, dep,0);
100 /* mark this package as having been automatically installed to
101 * satisfy a dependancy */
102 dep->auto_installed = 1;
104 pkg_vec_free(depends);
110 pkg_vec_free(depends);
116 check_conflicts_for(opkg_conf_t *conf, pkg_t *pkg)
119 pkg_vec_t *conflicts = NULL;
122 if (conf->force_depends) {
130 if (!conf->force_depends)
131 conflicts = (pkg_vec_t *)pkg_hash_fetch_conflicts(&conf->pkg_hash, pkg);
134 opkg_message(conf, level,
135 "%s: The following packages conflict with %s:\n\t", prefix, pkg->name);
137 while (i < conflicts->len)
138 opkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
139 opkg_message(conf, level, "\n");
140 pkg_vec_free(conflicts);
141 return OPKG_PKG_DEPS_UNSATISFIED;
147 update_file_ownership(opkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
149 str_list_t *new_list = pkg_get_installed_files(conf, new_pkg);
150 str_list_elt_t *iter, *niter;
152 for (iter = str_list_first(new_list), niter = str_list_next(new_list, iter);
154 iter = niter, niter = str_list_next(new_list, niter)) {
155 char *new_file = (char *)iter->data;
156 pkg_t *owner = file_hash_get_file_owner(conf, new_file);
158 opkg_message(conf, OPKG_ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
159 if (!owner || (owner == old_pkg))
160 file_hash_set_file_owner(conf, new_file, new_pkg);
163 str_list_t *old_list = pkg_get_installed_files(conf, old_pkg);
164 for (iter = str_list_first(old_list), niter = str_list_next(old_list, iter);
166 iter = niter, niter = str_list_next(old_list, niter)) {
167 char *old_file = (char *)iter->data;
168 pkg_t *owner = file_hash_get_file_owner(conf, old_file);
169 if (owner == old_pkg) {
171 hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
174 pkg_free_installed_files(old_pkg);
176 pkg_free_installed_files(new_pkg);
181 verify_pkg_installable(opkg_conf_t *conf, pkg_t *pkg)
183 /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
185 /* sma 6.20.02: yup; here's the first bit */
187 * XXX: BUG easy for cworth
188 * 1) please point the call below to the correct current root destination
189 * 2) we need to resolve how to check the required space for a pending pkg,
190 * my diddling with the .opk file size below isn't going to cut it.
191 * 3) return a proper error code instead of 1
193 int comp_size, blocks_available;
196 if (!conf->force_space && pkg->installed_size != NULL) {
197 root_dir = pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir;
198 blocks_available = get_available_blocks(root_dir);
200 comp_size = strtoul(pkg->installed_size, NULL, 0);
201 /* round up a blocks count without doing fancy-but-slow casting jazz */
202 comp_size = (int)((comp_size + 1023) / 1024);
204 if (comp_size >= blocks_available) {
205 opkg_message(conf, OPKG_ERROR,
206 "Only have %d available blocks on filesystem %s, pkg %s needs %d\n",
207 blocks_available, root_dir, pkg->name, comp_size);
215 unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg)
218 char *conffiles_file_name;
220 FILE *conffiles_file;
222 sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
224 pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
225 if (pkg->tmp_unpack_dir == NULL) {
226 opkg_message(conf, OPKG_ERROR,
227 "%s: Failed to create temporary directory '%s': %s\n",
228 __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
232 err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
237 /* XXX: CLEANUP: There might be a cleaner place to read in the
238 conffiles. Seems like I should be able to get everything to go
239 through pkg_init_from_file. If so, maybe it would make sense to
240 move all of unpack_pkg_control_files to that function. */
242 /* Don't need to re-read conffiles if we already have it */
243 if (!nv_pair_list_empty(&pkg->conffiles)) {
247 sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
248 if (! file_exists(conffiles_file_name)) {
249 free(conffiles_file_name);
253 conffiles_file = fopen(conffiles_file_name, "r");
254 if (conffiles_file == NULL) {
255 fprintf(stderr, "%s: failed to open %s: %s\n",
256 __FUNCTION__, conffiles_file_name, strerror(errno));
257 free(conffiles_file_name);
260 free(conffiles_file_name);
264 char *cf_name_in_dest;
266 cf_name = file_read_line_alloc(conffiles_file);
267 if (cf_name == NULL) {
270 if (cf_name[0] == '\0') {
274 /* Prepend dest->root_dir to conffile name.
275 Take pains to avoid multiple slashes. */
276 root_dir = pkg->dest->root_dir;
277 if (conf->offline_root)
278 /* skip the offline_root prefix */
279 root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
280 sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
281 cf_name[0] == '/' ? (cf_name + 1) : cf_name);
283 /* Can't get an md5sum now, (file isn't extracted yet).
284 We'll wait until resolve_conffiles */
285 conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
288 free(cf_name_in_dest);
291 fclose(conffiles_file);
297 * Remove packages which were auto_installed due to a dependency by old_pkg,
298 * which are no longer a dependency in the new (upgraded) pkg.
301 pkg_remove_orphan_dependent(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
303 int i, j, k, l, found;
306 struct compound_depend *cd0, *cd1;
307 abstract_pkg_t **dependents;
309 int count0 = old_pkg->pre_depends_count +
310 old_pkg->depends_count +
311 old_pkg->recommends_count +
312 old_pkg->suggests_count;
313 int count1 = pkg->pre_depends_count +
315 pkg->recommends_count +
318 for (i=0; i<count0; i++) {
319 cd0 = &old_pkg->depends[i];
320 if (cd0->type != DEPEND)
322 for (j=0; j<cd0->possibility_count; j++) {
326 for (k=0; k<count1; k++) {
327 cd1 = &pkg->depends[i];
328 if (cd1->type != DEPEND)
330 for (l=0; l<cd1->possibility_count; l++) {
331 if (cd0->possibilities[j]
332 == cd1->possibilities[l]) {
345 * old_pkg has a dependency that pkg does not.
347 p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash,
348 cd0->possibilities[j]->pkg->name);
353 if (!p->auto_installed)
356 n_deps = pkg_has_installed_dependents(conf, NULL, p,
358 n_deps--; /* don't count old_pkg */
361 opkg_message (conf, OPKG_NOTICE,
362 "%s was autoinstalled and is "
363 "now orphaned, removing.\n",
366 /* p has one installed dependency (old_pkg),
367 * which we need to ignore during removal. */
368 p->state_flag |= SF_REPLACE;
370 opkg_remove_pkg(conf, p, 0);
372 opkg_message(conf, OPKG_INFO,
373 "%s was autoinstalled and is "
374 "still required by %d "
375 "installed packages.\n",
384 /* returns number of installed replacees */
386 pkg_get_installed_replacees(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *installed_replacees)
388 abstract_pkg_t **replaces = pkg->replaces;
389 int replaces_count = pkg->replaces_count;
391 for (i = 0; i < replaces_count; i++) {
392 abstract_pkg_t *ab_pkg = replaces[i];
393 pkg_vec_t *pkg_vec = ab_pkg->pkgs;
395 for (j = 0; j < pkg_vec->len; j++) {
396 pkg_t *replacee = pkg_vec->pkgs[j];
397 if (!pkg_conflicts(pkg, replacee))
399 if (replacee->state_status == SS_INSTALLED) {
400 pkg_vec_insert(installed_replacees, replacee);
405 return installed_replacees->len;
409 pkg_remove_installed_replacees(opkg_conf_t *conf, pkg_vec_t *replacees)
412 int replaces_count = replacees->len;
413 for (i = 0; i < replaces_count; i++) {
414 pkg_t *replacee = replacees->pkgs[i];
416 replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
417 err = opkg_remove_pkg(conf, replacee,0);
424 /* to unwind the removal: make sure they are installed */
426 pkg_remove_installed_replacees_unwind(opkg_conf_t *conf, 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];
432 if (replacee->state_status != SS_INSTALLED) {
433 opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
434 err = opkg_install_pkg(conf, replacee,0);
442 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
444 opkg_install_check_downgrade(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg, int message)
447 char message_out[15];
448 char *old_version = pkg_version_str_alloc(old_pkg);
449 char *new_version = pkg_version_str_alloc(pkg);
450 int cmp = pkg_compare_versions(old_pkg, pkg);
453 memset(message_out,'\x0',15);
454 strncpy (message_out,"Upgrading ",strlen("Upgrading "));
455 if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
456 cmp = -1 ; /* then we force opkg to downgrade */
457 strncpy (message_out,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
458 /* reinstall, and some check could fail asking the "force-reinstall" option */
462 opkg_message(conf, OPKG_NOTICE,
463 "Not downgrading package %s on %s from %s to %s.\n",
464 old_pkg->name, old_pkg->dest->name, old_version, new_version);
466 } else if (cmp < 0) {
467 opkg_message(conf, OPKG_NOTICE,
468 "%s%s on %s from %s to %s...\n",
469 message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
470 pkg->dest = old_pkg->dest;
472 } else /* cmp == 0 */ {
473 if (conf->force_reinstall) {
474 opkg_message(conf, OPKG_NOTICE,
475 "Reinstalling %s (%s) on %s...\n",
476 pkg->name, new_version, old_pkg->dest->name);
477 pkg->dest = old_pkg->dest;
480 opkg_message(conf, OPKG_NOTICE,
481 "Not installing %s (%s) on %s -- already installed.\n",
482 pkg->name, new_version, old_pkg->dest->name);
490 char message_out[15] ;
491 memset(message_out,'\x0',15);
493 strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
495 strncpy( message_out,"Installing ",strlen("Installing ") );
496 char *version = pkg_version_str_alloc(pkg);
498 opkg_message(conf, OPKG_NOTICE,
499 "%s%s (%s) to %s...\n", message_out,
500 pkg->name, version, pkg->dest->name);
508 prerm_upgrade_old_pkg(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
510 /* DPKG_INCOMPATIBILITY:
511 dpkg does some things here that we don't do yet. Do we care?
513 1. If a version of the package is already installed, call
514 old-prerm upgrade new-version
515 2. If the script runs but exits with a non-zero exit status
516 new-prerm failed-upgrade old-version
517 Error unwind, for both the above cases:
518 old-postinst abort-upgrade new-version
524 prerm_upgrade_old_pkg_unwind(opkg_conf_t *conf, 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?
528 (See prerm_upgrade_old_package for details)
534 prerm_deconfigure_conflictors(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
536 /* DPKG_INCOMPATIBILITY:
537 dpkg does some things here that we don't do yet. Do we care?
538 2. If a 'conflicting' package is being removed at the same time:
539 1. If any packages depended on that conflicting package and
540 --auto-deconfigure is specified, call, for each such package:
541 deconfigured's-prerm deconfigure \
542 in-favour package-being-installed version \
543 removing conflicting-package version
545 deconfigured's-postinst abort-deconfigure \
546 in-favour package-being-installed-but-failed version \
547 removing conflicting-package version
549 The deconfigured packages are marked as requiring
550 configuration, so that if --install is used they will be
551 configured again if possible.
552 2. To prepare for removal of the conflicting package, call:
553 conflictor's-prerm remove in-favour package new-version
555 conflictor's-postinst abort-remove in-favour package new-version
561 prerm_deconfigure_conflictors_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
563 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
564 do yet. Do we care? (See prerm_deconfigure_conflictors for
570 preinst_configure(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
576 char *old_version = pkg_version_str_alloc(old_pkg);
577 sprintf_alloc(&preinst_args, "upgrade %s", old_version);
579 } else if (pkg->state_status == SS_CONFIG_FILES) {
580 char *pkg_version = pkg_version_str_alloc(pkg);
581 sprintf_alloc(&preinst_args, "install %s", pkg_version);
584 preinst_args = xstrdup("install");
587 err = pkg_run_script(conf, pkg, "preinst", preinst_args);
589 opkg_message(conf, OPKG_ERROR,
590 "Aborting installation of %s\n", pkg->name);
600 preinst_configure_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
602 /* DPKG_INCOMPATIBILITY:
603 dpkg does the following error unwind, should we?
604 pkg->postrm abort-upgrade old-version
605 OR pkg->postrm abort-install old-version
606 OR pkg->postrm abort-install
612 backup_filename_alloc(const char *file_name)
616 sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
623 backup_make_backup(opkg_conf_t *conf, const char *file_name)
628 backup = backup_filename_alloc(file_name);
629 err = file_copy(file_name, backup);
631 opkg_message(conf, OPKG_ERROR,
632 "%s: Failed to copy %s to %s\n",
633 __FUNCTION__, file_name, backup);
642 backup_exists_for(const char *file_name)
647 backup = backup_filename_alloc(file_name);
649 ret = file_exists(backup);
657 backup_remove(const char *file_name)
661 backup = backup_filename_alloc(file_name);
669 backup_modified_conffiles(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
672 conffile_list_elt_t *iter;
675 if (conf->noaction) return 0;
677 /* Backup all modified conffiles */
679 for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
683 cf_name = root_filename_alloc(conf, cf->name);
685 /* Don't worry if the conffile is just plain gone */
686 if (file_exists(cf_name) && conffile_has_been_modified(conf, cf)) {
687 err = backup_make_backup(conf, cf_name);
696 /* Backup all conffiles that were not conffiles in old_pkg */
697 for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
699 cf = (conffile_t *)iter->data;
700 cf_name = root_filename_alloc(conf, cf->name);
701 /* Ignore if this was a conffile in old_pkg as well */
702 if (pkg_get_conffile(old_pkg, cf->name)) {
706 if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
707 err = backup_make_backup(conf, cf_name);
719 backup_modified_conffiles_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
721 conffile_list_elt_t *iter;
724 for (iter = nv_pair_list_first(&old_pkg->conffiles); iter; iter = nv_pair_list_next(&old_pkg->conffiles, iter)) {
725 backup_remove(((nv_pair_t *)iter->data)->name);
729 for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
730 backup_remove(((nv_pair_t *)iter->data)->name);
738 check_data_file_clashes(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
740 /* DPKG_INCOMPATIBILITY:
741 opkg takes a slightly different approach than dpkg at this
742 point. dpkg installs each file in the new package while
743 creating a backup for any file that is replaced, (so that it
744 can unwind if necessary). To avoid complexity and redundant
745 storage, opkg doesn't do any installation until later, (at the
746 point at which dpkg removes the backups.
748 But, we do have to check for data file clashes, since after
749 installing a package with a file clash, removing either of the
750 packages involved in the clash has the potential to break the
753 str_list_t *files_list;
754 str_list_elt_t *iter, *niter;
758 files_list = pkg_get_installed_files(conf, pkg);
759 for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter);
761 iter = niter, niter = str_list_next(files_list, iter)) {
763 char *filename = (char *) iter->data;
764 root_filename = root_filename_alloc(conf, filename);
765 if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
768 /* Pre-existing conffiles are OK */
769 /* @@@@ should have way to check that it is a conffile -Jamey */
770 if (backup_exists_for(root_filename)) {
774 /* Pre-existing files are OK if force-overwrite was asserted. */
775 if (conf->force_overwrite) {
776 /* but we need to change who owns this file */
777 file_hash_set_file_owner(conf, filename, pkg);
781 owner = file_hash_get_file_owner(conf, filename);
783 /* Pre-existing files are OK if owned by the pkg being upgraded. */
784 if (owner && old_pkg) {
785 if (strcmp(owner->name, old_pkg->name) == 0) {
790 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
792 opkg_message(conf, OPKG_DEBUG2, "Checking for replaces for %s in package %s\n", filename, owner->name);
793 if (pkg_replaces(pkg, owner)) {
796 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
797 then it's ok to overwrite. */
798 if (strcmp(owner->name,pkg->name)==0){
799 opkg_message(conf, OPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
804 /* Pre-existing files are OK if they are obsolete */
805 obs = hash_table_get(&conf->obs_file_hash, filename);
807 opkg_message(conf, OPKG_INFO, "Pre-exiting file %s is obsolete. obs_pkg=%s\n", filename, obs->name);
811 /* We have found a clash. */
812 opkg_message(conf, OPKG_ERROR,
813 "Package %s wants to install file %s\n"
814 "\tBut that file is already provided by package ",
815 pkg->name, filename);
817 opkg_message(conf, OPKG_ERROR,
818 "%s\n", owner->name);
820 opkg_message(conf, OPKG_ERROR,
821 "<no package>\nPlease move this file out of the way and try again.\n");
827 pkg_free_installed_files(pkg);
833 check_data_file_clashes_change(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
835 /* Basically that's the worst hack I could do to be able to change ownership of
836 file list, but, being that we have no way to unwind the mods, due to structure
837 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
838 What we do here is change the ownership of file in hash if a replace ( or similar events
840 Only the action that are needed to change name should be considered.
841 @@@ To change after 1.0 release.
843 str_list_t *files_list;
844 str_list_elt_t *iter, *niter;
846 char *root_filename = NULL;
850 files_list = pkg_get_installed_files(conf, pkg);
851 for (iter = str_list_first(files_list), niter = str_list_next(files_list, iter);
853 iter = niter, niter = str_list_next(files_list, niter)) {
854 char *filename = (char *) iter->data;
857 root_filename = NULL;
859 root_filename = root_filename_alloc(conf, filename);
860 if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
863 owner = file_hash_get_file_owner(conf, filename);
865 if (conf->force_overwrite) {
866 /* but we need to change who owns this file */
867 file_hash_set_file_owner(conf, filename, pkg);
872 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
874 if (pkg_replaces(pkg, owner)) {
875 /* It's now time to change the owner of that file.
876 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
877 opkg_message(conf, OPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
878 file_hash_set_file_owner(conf, filename, pkg);
887 root_filename = NULL;
889 pkg_free_installed_files(pkg);
895 check_data_file_clashes_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
897 /* Nothing to do since check_data_file_clashes doesn't change state */
902 postrm_upgrade_old_pkg(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
904 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
905 1. If the package is being upgraded, call
906 old-postrm upgrade new-version
907 2. If this fails, attempt:
908 new-postrm failed-upgrade old-version
909 Error unwind, for both cases:
910 old-preinst abort-upgrade new-version */
915 postrm_upgrade_old_pkg_unwind(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
917 /* DPKG_INCOMPATIBILITY:
918 dpkg does some things here that we don't do yet. Do we care?
919 (See postrm_upgrade_old_pkg for details)
925 remove_obsolesced_files(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
928 str_list_t *old_files;
930 str_list_t *new_files;
932 hash_table_t new_files_table;
934 if (old_pkg == NULL) {
938 old_files = pkg_get_installed_files(conf, old_pkg);
939 new_files = pkg_get_installed_files(conf, pkg);
941 new_files_table.entries = NULL;
942 hash_table_init("new_files" , &new_files_table, 20);
943 for (nf = str_list_first(new_files); nf; nf = str_list_next(new_files, nf)) {
945 hash_table_insert(&new_files_table, nf->data, nf->data);
948 for (of = str_list_first(old_files); of; of = str_list_next(old_files, of)) {
951 old = (char *)of->data;
952 new = (char *) hash_table_get (&new_files_table, old);
956 if (file_is_dir(old)) {
959 owner = file_hash_get_file_owner(conf, old);
960 if (owner != old_pkg) {
961 /* in case obsolete file no longer belongs to old_pkg */
965 /* old file is obsolete */
966 opkg_message(conf, OPKG_INFO,
967 " removing obsolete file %s\n", old);
968 if (!conf->noaction) {
971 opkg_message(conf, OPKG_ERROR, " Warning: remove %s failed: %s\n", old,
977 hash_table_deinit(&new_files_table);
978 pkg_free_installed_files(old_pkg);
979 pkg_free_installed_files(pkg);
985 install_maintainer_scripts(opkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
990 sprintf_alloc(&prefix, "%s.", pkg->name);
991 ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
999 remove_disappeared(opkg_conf_t *conf, pkg_t *pkg)
1001 /* DPKG_INCOMPATIBILITY:
1002 This is a fairly sophisticated dpkg operation. Shall we
1005 /* Any packages all of whose files have been overwritten during the
1006 installation, and which aren't required for dependencies, are
1007 considered to have been removed. For each such package
1008 1. disappearer's-postrm disappear overwriter overwriter-version
1009 2. The package's maintainer scripts are removed
1010 3. It is noted in the status database as being in a sane state,
1011 namely not installed (any conffiles it may have are ignored,
1012 rather than being removed by dpkg). Note that disappearing
1013 packages do not have their prerm called, because dpkg doesn't
1014 know in advance that the package is going to vanish.
1020 install_data_files(opkg_conf_t *conf, pkg_t *pkg)
1024 /* opkg takes a slightly different approach to data file backups
1025 than dpkg. Rather than removing backups at this point, we
1026 actually do the data file installation now. See comments in
1027 check_data_file_clashes() for more details. */
1029 opkg_message(conf, OPKG_INFO,
1030 " extracting data files to %s\n", pkg->dest->root_dir);
1031 err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
1036 /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
1037 so we can't save ourself from removing important packages
1038 At this point we (should) have extracted the .control file, so it
1039 would be a good idea to reload the data in it, and set the Essential
1040 state in *pkg. From now on the Essential is back in status file and
1041 we can protect again.
1042 We should operate this way:
1043 fopen the file ( pkg->dest->root_dir/pkg->name.control )
1044 check for "Essential" in it
1045 set the value in pkg->essential.
1046 This new routine could be useful also for every other flag
1048 set_flags_from_control(conf, pkg) ;
1050 opkg_message(conf, OPKG_DEBUG, " Calling pkg_write_filelist from %s\n", __FUNCTION__);
1051 err = pkg_write_filelist(conf, pkg);
1055 /* XXX: FEATURE: opkg should identify any files which existed
1056 before installation and which were overwritten, (see
1057 check_data_file_clashes()). What it must do is remove any such
1058 files from the filelist of the old package which provided the
1059 file. Otherwise, if the old package were removed at some point
1060 it would break the new package. Removing the new package will
1061 also break the old one, but this cannot be helped since the old
1062 package's file has already been deleted. This is the importance
1063 of check_data_file_clashes(), and only allowing opkg to install
1064 a clashing package with a user force. */
1070 user_prefers_old_conffile(const char *file_name, const char *backup)
1073 const char *short_file_name;
1075 short_file_name = strrchr(file_name, '/');
1076 if (short_file_name) {
1079 short_file_name = file_name;
1083 response = get_user_response(" Configuration file '%s'\n"
1084 " ==> File on system created by you or by a script.\n"
1085 " ==> File also in package provided by package maintainer.\n"
1086 " What would you like to do about it ? Your options are:\n"
1087 " Y or I : install the package maintainer's version\n"
1088 " N or O : keep your currently-installed version\n"
1089 " D : show the differences between the versions (if diff is installed)\n"
1090 " The default action is to keep your current version.\n"
1091 " *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
1093 if (response == NULL)
1096 if (strcmp(response, "y") == 0
1097 || strcmp(response, "i") == 0
1098 || strcmp(response, "yes") == 0) {
1103 if (strcmp(response, "d") == 0) {
1104 const char *argv[] = {"diff", "-u", backup, file_name, NULL};
1106 printf(" [Press ENTER to continue]\n");
1107 response = file_read_line_alloc(stdin);
1118 resolve_conffiles(opkg_conf_t *conf, pkg_t *pkg)
1120 conffile_list_elt_t *iter;
1125 if (conf->noaction) return 0;
1127 for (iter = nv_pair_list_first(&pkg->conffiles); iter; iter = nv_pair_list_next(&pkg->conffiles, iter)) {
1128 char *root_filename;
1129 cf = (conffile_t *)iter->data;
1130 root_filename = root_filename_alloc(conf, cf->name);
1132 /* Might need to initialize the md5sum for each conffile */
1133 if (cf->value == NULL) {
1134 cf->value = file_md5sum_alloc(root_filename);
1137 if (!file_exists(root_filename)) {
1138 free(root_filename);
1142 cf_backup = backup_filename_alloc(root_filename);
1145 if (file_exists(cf_backup)) {
1146 /* Let's compute md5 to test if files are changed */
1147 md5sum = file_md5sum_alloc(cf_backup);
1148 if (md5sum && cf->value && strcmp(cf->value,md5sum) != 0 ) {
1149 if (conf->force_maintainer) {
1150 opkg_message(conf, OPKG_NOTICE, "Conffile %s using maintainer's setting.\n", cf_backup);
1151 } else if (conf->force_defaults
1152 || user_prefers_old_conffile(root_filename, cf_backup) ) {
1153 rename(cf_backup, root_filename);
1162 free(root_filename);
1170 opkg_install_by_name(opkg_conf_t *conf, const char *pkg_name)
1174 char *old_version, *new_version;
1176 opkg_message(conf, OPKG_DEBUG2, " Getting old from pkg_hash_fetch \n" );
1177 old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
1179 opkg_message(conf, OPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n", old->version );
1181 opkg_message(conf, OPKG_DEBUG2, " Getting new from pkg_hash_fetch \n" );
1182 new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name, &err);
1184 opkg_message(conf, OPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n", new->version );
1186 /* Pigi Basically here is broken the version stuff.
1187 What's happening is that nothing provide the version to differents
1188 functions, so the returned struct is always the latest.
1189 That's why the install by name don't work.
1191 opkg_message(conf, OPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
1194 opkg_message(conf, OPKG_DEBUG2, " old %s ", old->version );
1196 opkg_message(conf, OPKG_DEBUG2, " new %s ", new->version );
1197 opkg_message(conf, OPKG_DEBUG2, " \n");
1203 return OPKG_PKG_HAS_NO_CANDIDATE;
1206 new->state_flag |= SF_USER;
1208 old_version = pkg_version_str_alloc(old);
1209 new_version = pkg_version_str_alloc(new);
1211 cmp = pkg_compare_versions(old, new);
1212 if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
1213 opkg_message(conf, OPKG_DEBUG, " Forcing downgrade \n");
1214 cmp = -1 ; /* then we force opkg to downgrade */
1215 /* We need to use a value < 0 because in the 0 case we are asking to */
1216 /* reinstall, and some check could fail asking the "force-reinstall" option */
1218 opkg_message(conf, OPKG_DEBUG,
1219 "Comparing visible versions of pkg %s:"
1220 "\n\t%s is installed "
1221 "\n\t%s is available "
1222 "\n\t%d was comparison result\n",
1223 pkg_name, old_version, new_version, cmp);
1224 if (cmp == 0 && !conf->force_reinstall) {
1225 opkg_message(conf, OPKG_NOTICE,
1226 "Package %s (%s) installed in %s is up to date.\n",
1227 old->name, old_version, old->dest->name);
1231 } else if (cmp > 0) {
1232 opkg_message(conf, OPKG_NOTICE,
1233 "Not downgrading package %s on %s from %s to %s.\n",
1234 old->name, old->dest->name, old_version, new_version);
1238 } else if (cmp < 0) {
1239 new->dest = old->dest;
1240 old->state_want = SW_DEINSTALL; /* Here probably the problem for bug 1277 */
1246 /* XXX: CLEANUP: The error code of opkg_install_by_name is really
1247 supposed to be an opkg_error_t, but opkg_install_pkg could
1248 return any kind of integer, (might be errno from a syscall,
1249 etc.). This is a real mess and will need to be cleaned up if
1250 anyone ever wants to make a nice libopkg. */
1252 opkg_message(conf, OPKG_DEBUG2,"Function: %s calling opkg_install_pkg \n",__FUNCTION__);
1253 return opkg_install_pkg(conf, new,0);
1257 * @brief Really install a pkg_t
1260 opkg_install_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
1264 pkg_t *old_pkg = NULL;
1265 pkg_vec_t *replacees;
1266 abstract_pkg_t *ab_pkg = NULL;
1274 message = 1; /* Coming from an upgrade, and should change the output message */
1277 opkg_message(conf, OPKG_ERROR,
1278 "INTERNAL ERROR: null pkg passed to opkg_install_pkg\n");
1279 return OPKG_INSTALL_ERR_INTERNAL;
1282 opkg_message(conf, OPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
1284 if (!pkg_arch_supported(conf, pkg)) {
1285 opkg_message(conf, OPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1286 pkg->architecture, pkg->name);
1287 return OPKG_INSTALL_ERR_INTERNAL;
1289 if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
1290 err = satisfy_dependencies_for(conf, pkg);
1291 if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
1293 opkg_message(conf, OPKG_NOTICE,
1294 "Package %s is already installed in %s.\n",
1295 pkg->name, pkg->dest->name);
1299 if (pkg->dest == NULL) {
1300 pkg->dest = conf->default_dest;
1303 old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
1305 err = opkg_install_check_downgrade(conf, pkg, old_pkg, message);
1306 if (err) { return OPKG_INSTALL_ERR_NO_DOWNGRADE; }
1308 pkg->state_want = SW_INSTALL;
1310 old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
1314 /* Abhaya: conflicts check */
1315 err = check_conflicts_for(conf, pkg);
1316 if (err) { return OPKG_INSTALL_ERR_CONFLICTS; }
1318 /* this setup is to remove the upgrade scenario in the end when
1319 installing pkg A, A deps B & B deps on A. So both B and A are
1320 installed. Then A's installation is started resulting in an
1321 uncecessary upgrade */
1322 if (pkg->state_status == SS_INSTALLED
1323 && conf->force_reinstall == 0) return 0;
1325 err = verify_pkg_installable(conf, pkg);
1326 if (err) { return OPKG_INSTALL_ERR_NO_SPACE; }
1328 if (pkg->local_filename == NULL) {
1329 err = opkg_download_pkg(conf, pkg, conf->tmp_dir);
1331 opkg_message(conf, OPKG_ERROR,
1332 "Failed to download %s. Perhaps you need to run 'opkg update'?\n",
1334 return OPKG_INSTALL_ERR_DOWNLOAD;
1338 /* check that the repository is valid */
1339 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1340 char *list_file_name, *sig_file_name, *lists_dir;
1342 /* check to ensure the package has come from a repository */
1343 if (conf->check_signature && pkg->src)
1345 sprintf_alloc (&lists_dir, "%s",
1346 (conf->restrict_to_default_dest)
1347 ? conf->default_dest->lists_dir
1349 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, pkg->src->name);
1350 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, pkg->src->name);
1352 if (file_exists (sig_file_name))
1354 if (opkg_verify_file (conf, list_file_name, sig_file_name)){
1355 opkg_message(conf, OPKG_ERROR, "Failed to verify the signature of: %s\n",
1357 return OPKG_INSTALL_ERR_SIGNATURE;
1360 opkg_message(conf, OPKG_ERROR, "Signature file is missing. "
1361 "Perhaps you need to run 'opkg update'?\n");
1362 return OPKG_INSTALL_ERR_SIGNATURE;
1366 free (list_file_name);
1367 free (sig_file_name);
1371 /* Check for md5 values */
1374 file_md5 = file_md5sum_alloc(pkg->local_filename);
1375 if (file_md5 && strcmp(file_md5, pkg->md5sum))
1377 opkg_message(conf, OPKG_ERROR,
1378 "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1381 return OPKG_INSTALL_ERR_MD5;
1388 /* Check for sha256 value */
1391 file_sha256 = file_sha256sum_alloc(pkg->local_filename);
1392 if (file_sha256 && strcmp(file_sha256, pkg->sha256sum))
1394 opkg_message(conf, OPKG_ERROR,
1395 "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1398 return OPKG_INSTALL_ERR_SHA256;
1405 if (pkg->tmp_unpack_dir == NULL) {
1406 unpack_pkg_control_files(conf, pkg);
1409 /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
1410 /* Pigi: check if it will pass from here when replacing. It seems to fail */
1411 /* That's rather strange that files don't change owner. Investigate !!!!!!*/
1412 err = update_file_ownership(conf, pkg, old_pkg);
1413 if (err) { return OPKG_ERR_UNKNOWN; }
1415 if (conf->nodeps == 0) {
1416 err = satisfy_dependencies_for(conf, pkg);
1417 if (err) { return OPKG_INSTALL_ERR_DEPENDENCIES; }
1418 if (pkg->state_status == SS_UNPACKED)
1419 /* Circular dependency has installed it for us. */
1423 replacees = pkg_vec_alloc();
1424 pkg_get_installed_replacees(conf, pkg, replacees);
1426 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1428 sigset_t newset, oldset;
1430 sigemptyset(&newset);
1431 sigaddset(&newset, SIGINT);
1432 sigprocmask(SIG_BLOCK, &newset, &oldset);
1434 opkg_state_changed++;
1435 pkg->state_flag |= SF_FILELIST_CHANGED;
1438 pkg_remove_orphan_dependent(conf, pkg, old_pkg);
1440 /* XXX: BUG: we really should treat replacement more like an upgrade
1441 * Instead, we're going to remove the replacees
1443 err = pkg_remove_installed_replacees(conf, replacees);
1444 if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
1446 err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
1447 if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
1449 err = prerm_deconfigure_conflictors(conf, pkg, replacees);
1450 if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
1452 err = preinst_configure(conf, pkg, old_pkg);
1453 if (err) goto UNWIND_PREINST_CONFIGURE;
1455 err = backup_modified_conffiles(conf, pkg, old_pkg);
1456 if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
1458 err = check_data_file_clashes(conf, pkg, old_pkg);
1459 if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
1461 err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
1462 if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
1464 if (conf->noaction) return 0;
1466 /* point of no return: no unwinding after this */
1467 if (old_pkg && !conf->force_reinstall) {
1468 old_pkg->state_want = SW_DEINSTALL;
1470 if (old_pkg->state_flag & SF_NOPRUNE) {
1471 opkg_message(conf, OPKG_INFO,
1472 " not removing obsolesced files because package marked noprune\n");
1474 opkg_message(conf, OPKG_INFO,
1475 " removing obsolesced files\n");
1476 remove_obsolesced_files(conf, pkg, old_pkg);
1479 /* removing files from old package, to avoid ghost files */
1480 remove_data_files_and_list(conf, old_pkg);
1481 remove_maintainer_scripts(conf, old_pkg);
1485 opkg_message(conf, OPKG_INFO,
1486 " installing maintainer scripts\n");
1487 install_maintainer_scripts(conf, pkg, old_pkg);
1489 /* the following just returns 0 */
1490 remove_disappeared(conf, pkg);
1492 opkg_message(conf, OPKG_INFO,
1493 " installing data files\n");
1494 install_data_files(conf, pkg);
1496 /* read comments from function for detail but I will execute this here as all other tests are ok.*/
1497 err = check_data_file_clashes_change(conf, pkg, old_pkg);
1499 opkg_message(conf, OPKG_INFO,
1500 " resolving conf files\n");
1501 resolve_conffiles(conf, pkg);
1503 pkg->state_status = SS_UNPACKED;
1504 old_state_flag = pkg->state_flag;
1505 pkg->state_flag &= ~SF_PREFER;
1506 opkg_message(conf, OPKG_DEBUG, " pkg=%s old_state_flag=%x state_flag=%x\n", pkg->name, old_state_flag, pkg->state_flag);
1508 if (old_pkg && !conf->force_reinstall) {
1509 old_pkg->state_status = SS_NOT_INSTALLED;
1512 time(&pkg->installed_time);
1514 ab_pkg = pkg->parent;
1516 ab_pkg->state_status = pkg->state_status;
1518 opkg_message(conf, OPKG_INFO, "Done.\n");
1520 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1521 pkg_vec_free (replacees);
1525 UNWIND_POSTRM_UPGRADE_OLD_PKG:
1526 postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
1527 UNWIND_CHECK_DATA_FILE_CLASHES:
1528 check_data_file_clashes_unwind(conf, pkg, old_pkg);
1529 UNWIND_BACKUP_MODIFIED_CONFFILES:
1530 backup_modified_conffiles_unwind(conf, pkg, old_pkg);
1531 UNWIND_PREINST_CONFIGURE:
1532 preinst_configure_unwind(conf, pkg, old_pkg);
1533 UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
1534 prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
1535 UNWIND_PRERM_UPGRADE_OLD_PKG:
1536 prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
1537 UNWIND_REMOVE_INSTALLED_REPLACEES:
1538 pkg_remove_installed_replacees_unwind(conf, replacees);
1540 opkg_message(conf, OPKG_INFO,
1543 sigprocmask(SIG_UNBLOCK, &newset, &oldset);
1545 pkg_vec_free (replacees);
1546 return OPKG_ERR_UNKNOWN;