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.
25 #include "pkg_extract.h"
27 #include "opkg_install.h"
28 #include "opkg_configure.h"
29 #include "opkg_download.h"
30 #include "opkg_remove.h"
32 #include "opkg_utils.h"
33 #include "opkg_message.h"
35 #include "opkg_defines.h"
37 #include "sprintf_alloc.h"
38 #include "file_util.h"
40 #include "libbb/libbb.h"
43 satisfy_dependencies_for(pkg_t
*pkg
)
46 pkg_vec_t
*depends
= pkg_vec_alloc();
48 char **tmp
, **unresolved
= NULL
;
51 ndepends
= pkg_hash_fetch_unsatisfied_dependencies(pkg
, depends
,
55 opkg_msg(ERROR
, "Cannot satisfy the following dependencies for %s:\n",
59 opkg_msg(ERROR
, "\t%s", *unresolved
);
64 opkg_message(ERROR
, "\n");
65 if (! conf
->force_depends
) {
67 "This could mean that your package list is out of date or that the packages\n"
68 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
69 "of this problem try again with the '-force-depends' option.\n");
70 pkg_vec_free(depends
);
76 pkg_vec_free(depends
);
80 /* Mark packages as to-be-installed */
81 for (i
=0; i
< depends
->len
; i
++) {
82 /* Dependencies should be installed the same place as pkg */
83 if (depends
->pkgs
[i
]->dest
== NULL
) {
84 depends
->pkgs
[i
]->dest
= pkg
->dest
;
86 depends
->pkgs
[i
]->state_want
= SW_INSTALL
;
89 for (i
= 0; i
< depends
->len
; i
++) {
90 dep
= depends
->pkgs
[i
];
91 /* The package was uninstalled when we started, but another
92 dep earlier in this loop may have depended on it and pulled
93 it in, so check first. */
94 if ((dep
->state_status
!= SS_INSTALLED
)
95 && (dep
->state_status
!= SS_UNPACKED
)) {
96 opkg_msg(DEBUG2
,"Calling opkg_install_pkg.\n");
97 err
= opkg_install_pkg(dep
, 0);
98 /* mark this package as having been automatically installed to
99 * satisfy a dependancy */
100 dep
->auto_installed
= 1;
102 pkg_vec_free(depends
);
108 pkg_vec_free(depends
);
114 check_conflicts_for(pkg_t
*pkg
)
117 pkg_vec_t
*conflicts
= NULL
;
118 message_level_t level
;
120 if (conf
->force_depends
) {
126 if (!conf
->force_depends
)
127 conflicts
= pkg_hash_fetch_conflicts(pkg
);
130 opkg_msg(level
, "The following packages conflict with %s:\n",
133 while (i
< conflicts
->len
)
134 opkg_msg(level
, "\t%s", conflicts
->pkgs
[i
++]->name
);
135 opkg_msg(level
, "\n");
136 pkg_vec_free(conflicts
);
143 update_file_ownership(pkg_t
*new_pkg
, pkg_t
*old_pkg
)
145 str_list_t
*new_list
, *old_list
;
146 str_list_elt_t
*iter
, *niter
;
148 new_list
= pkg_get_installed_files(new_pkg
);
149 if (new_list
== NULL
)
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(new_file
);
158 opkg_msg(ERROR
, "Null new_file for new_pkg=%s\n", new_pkg
->name
);
159 if (!owner
|| (owner
== old_pkg
))
160 file_hash_set_file_owner(new_file
, new_pkg
);
164 old_list
= pkg_get_installed_files(old_pkg
);
165 if (old_list
== NULL
) {
166 pkg_free_installed_files(new_pkg
);
170 for (iter
= str_list_first(old_list
), niter
= str_list_next(old_list
, iter
);
172 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
173 char *old_file
= (char *)iter
->data
;
174 pkg_t
*owner
= file_hash_get_file_owner(old_file
);
175 if (owner
== old_pkg
) {
177 hash_table_insert(&conf
->obs_file_hash
, old_file
, old_pkg
);
180 pkg_free_installed_files(old_pkg
);
182 pkg_free_installed_files(new_pkg
);
187 verify_pkg_installable(pkg_t
*pkg
)
189 unsigned long kbs_available
, pkg_size_kbs
;
192 if (conf
->force_space
|| pkg
->installed_size
== 0)
195 root_dir
= pkg
->dest
? pkg
->dest
->root_dir
:
196 conf
->default_dest
->root_dir
;
197 kbs_available
= get_available_kbytes(root_dir
);
199 pkg_size_kbs
= (pkg
->installed_size
+ 1023)/1024;
201 if (pkg_size_kbs
>= kbs_available
) {
202 opkg_msg(ERROR
, "Only have %dkb available on filesystem %s, "
204 kbs_available
, root_dir
, pkg
->name
, pkg_size_kbs
);
212 unpack_pkg_control_files(pkg_t
*pkg
)
215 char *conffiles_file_name
;
217 FILE *conffiles_file
;
219 sprintf_alloc(&pkg
->tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
, pkg
->name
);
221 pkg
->tmp_unpack_dir
= mkdtemp(pkg
->tmp_unpack_dir
);
222 if (pkg
->tmp_unpack_dir
== NULL
) {
223 opkg_perror(ERROR
, "Failed to create temporary directory '%s'",
224 pkg
->tmp_unpack_dir
);
228 err
= pkg_extract_control_files_to_dir(pkg
, pkg
->tmp_unpack_dir
);
233 /* XXX: CLEANUP: There might be a cleaner place to read in the
234 conffiles. Seems like I should be able to get everything to go
235 through pkg_init_from_file. If so, maybe it would make sense to
236 move all of unpack_pkg_control_files to that function. */
238 /* Don't need to re-read conffiles if we already have it */
239 if (!nv_pair_list_empty(&pkg
->conffiles
)) {
243 sprintf_alloc(&conffiles_file_name
, "%s/conffiles", pkg
->tmp_unpack_dir
);
244 if (! file_exists(conffiles_file_name
)) {
245 free(conffiles_file_name
);
249 conffiles_file
= fopen(conffiles_file_name
, "r");
250 if (conffiles_file
== NULL
) {
251 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
252 free(conffiles_file_name
);
255 free(conffiles_file_name
);
259 char *cf_name_in_dest
;
261 cf_name
= file_read_line_alloc(conffiles_file
);
262 if (cf_name
== NULL
) {
265 if (cf_name
[0] == '\0') {
269 /* Prepend dest->root_dir to conffile name.
270 Take pains to avoid multiple slashes. */
271 root_dir
= pkg
->dest
->root_dir
;
272 if (conf
->offline_root
)
273 /* skip the offline_root prefix */
274 root_dir
= pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
275 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
276 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
278 /* Can't get an md5sum now, (file isn't extracted yet).
279 We'll wait until resolve_conffiles */
280 conffile_list_append(&pkg
->conffiles
, cf_name_in_dest
, NULL
);
283 free(cf_name_in_dest
);
286 fclose(conffiles_file
);
292 * Remove packages which were auto_installed due to a dependency by old_pkg,
293 * which are no longer a dependency in the new (upgraded) pkg.
296 pkg_remove_orphan_dependent(pkg_t
*pkg
, pkg_t
*old_pkg
)
298 int i
, j
, k
, l
, found
;
301 struct compound_depend
*cd0
, *cd1
;
302 abstract_pkg_t
**dependents
;
304 int count0
= old_pkg
->pre_depends_count
+
305 old_pkg
->depends_count
+
306 old_pkg
->recommends_count
+
307 old_pkg
->suggests_count
;
308 int count1
= pkg
->pre_depends_count
+
310 pkg
->recommends_count
+
313 for (i
=0; i
<count0
; i
++) {
314 cd0
= &old_pkg
->depends
[i
];
315 if (cd0
->type
!= DEPEND
)
317 for (j
=0; j
<cd0
->possibility_count
; j
++) {
321 for (k
=0; k
<count1
; k
++) {
322 cd1
= &pkg
->depends
[i
];
323 if (cd1
->type
!= DEPEND
)
325 for (l
=0; l
<cd1
->possibility_count
; l
++) {
326 if (cd0
->possibilities
[j
]
327 == cd1
->possibilities
[l
]) {
340 * old_pkg has a dependency that pkg does not.
342 p
= pkg_hash_fetch_installed_by_name(
343 cd0
->possibilities
[j
]->pkg
->name
);
348 if (!p
->auto_installed
)
351 n_deps
= pkg_has_installed_dependents(p
, &dependents
);
352 n_deps
--; /* don't count old_pkg */
355 opkg_msg(NOTICE
, "%s was autoinstalled and is "
356 "now orphaned, removing.\n",
359 /* p has one installed dependency (old_pkg),
360 * which we need to ignore during removal. */
361 p
->state_flag
|= SF_REPLACE
;
363 opkg_remove_pkg(p
, 0);
365 opkg_msg(INFO
, "%s was autoinstalled and is "
366 "still required by %d "
367 "installed packages.\n",
376 /* returns number of installed replacees */
378 pkg_get_installed_replacees(pkg_t
*pkg
, pkg_vec_t
*installed_replacees
)
380 abstract_pkg_t
**replaces
= pkg
->replaces
;
381 int replaces_count
= pkg
->replaces_count
;
383 for (i
= 0; i
< replaces_count
; i
++) {
384 abstract_pkg_t
*ab_pkg
= replaces
[i
];
385 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
387 for (j
= 0; j
< pkg_vec
->len
; j
++) {
388 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
389 if (!pkg_conflicts(pkg
, replacee
))
391 if (replacee
->state_status
== SS_INSTALLED
) {
392 pkg_vec_insert(installed_replacees
, replacee
);
397 return installed_replacees
->len
;
401 pkg_remove_installed_replacees(pkg_vec_t
*replacees
)
404 int replaces_count
= replacees
->len
;
405 for (i
= 0; i
< replaces_count
; i
++) {
406 pkg_t
*replacee
= replacees
->pkgs
[i
];
408 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
409 err
= opkg_remove_pkg(replacee
, 0);
416 /* to unwind the removal: make sure they are installed */
418 pkg_remove_installed_replacees_unwind(pkg_vec_t
*replacees
)
421 int replaces_count
= replacees
->len
;
422 for (i
= 0; i
< replaces_count
; i
++) {
423 pkg_t
*replacee
= replacees
->pkgs
[i
];
424 if (replacee
->state_status
!= SS_INSTALLED
) {
425 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
426 err
= opkg_install_pkg(replacee
, 0);
434 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
436 opkg_install_check_downgrade(pkg_t
*pkg
, pkg_t
*old_pkg
, int message
)
439 char message_out
[15];
440 char *old_version
= pkg_version_str_alloc(old_pkg
);
441 char *new_version
= pkg_version_str_alloc(pkg
);
442 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
445 memset(message_out
,'\x0',15);
446 strncpy (message_out
,"Upgrading ",strlen("Upgrading "));
447 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
448 cmp
= -1 ; /* then we force opkg to downgrade */
449 strncpy (message_out
,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
450 /* reinstall, and some check could fail asking the "force-reinstall" option */
455 "Not downgrading package %s on %s from %s to %s.\n",
456 old_pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
458 } else if (cmp
< 0) {
459 opkg_msg(NOTICE
, "%s%s on %s from %s to %s...\n",
460 message_out
, pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
461 pkg
->dest
= old_pkg
->dest
;
463 } else /* cmp == 0 */ {
464 if (conf
->force_reinstall
) {
465 opkg_msg(NOTICE
, "Reinstalling %s (%s) on %s...\n",
466 pkg
->name
, new_version
, old_pkg
->dest
->name
);
467 pkg
->dest
= old_pkg
->dest
;
470 opkg_msg(NOTICE
, "%s (%s) already install on %s."
471 " Not reinstalling.\n",
472 pkg
->name
, new_version
, old_pkg
->dest
->name
);
480 char message_out
[15] ;
481 memset(message_out
,'\x0',15);
483 strncpy( message_out
,"Upgrading ",strlen("Upgrading ") );
485 strncpy( message_out
,"Installing ",strlen("Installing ") );
486 char *version
= pkg_version_str_alloc(pkg
);
488 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
489 pkg
->name
, version
, pkg
->dest
->name
);
497 prerm_upgrade_old_pkg(pkg_t
*pkg
, pkg_t
*old_pkg
)
499 /* DPKG_INCOMPATIBILITY:
500 dpkg does some things here that we don't do yet. Do we care?
502 1. If a version of the package is already installed, call
503 old-prerm upgrade new-version
504 2. If the script runs but exits with a non-zero exit status
505 new-prerm failed-upgrade old-version
506 Error unwind, for both the above cases:
507 old-postinst abort-upgrade new-version
513 prerm_upgrade_old_pkg_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
515 /* DPKG_INCOMPATIBILITY:
516 dpkg does some things here that we don't do yet. Do we care?
517 (See prerm_upgrade_old_package for details)
523 prerm_deconfigure_conflictors(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
525 /* DPKG_INCOMPATIBILITY:
526 dpkg does some things here that we don't do yet. Do we care?
527 2. If a 'conflicting' package is being removed at the same time:
528 1. If any packages depended on that conflicting package and
529 --auto-deconfigure is specified, call, for each such package:
530 deconfigured's-prerm deconfigure \
531 in-favour package-being-installed version \
532 removing conflicting-package version
534 deconfigured's-postinst abort-deconfigure \
535 in-favour package-being-installed-but-failed version \
536 removing conflicting-package version
538 The deconfigured packages are marked as requiring
539 configuration, so that if --install is used they will be
540 configured again if possible.
541 2. To prepare for removal of the conflicting package, call:
542 conflictor's-prerm remove in-favour package new-version
544 conflictor's-postinst abort-remove in-favour package new-version
550 prerm_deconfigure_conflictors_unwind(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
552 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
553 do yet. Do we care? (See prerm_deconfigure_conflictors for
559 preinst_configure(pkg_t
*pkg
, pkg_t
*old_pkg
)
565 char *old_version
= pkg_version_str_alloc(old_pkg
);
566 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
568 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
569 char *pkg_version
= pkg_version_str_alloc(pkg
);
570 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
573 preinst_args
= xstrdup("install");
576 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
578 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
588 preinst_configure_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
590 /* DPKG_INCOMPATIBILITY:
591 dpkg does the following error unwind, should we?
592 pkg->postrm abort-upgrade old-version
593 OR pkg->postrm abort-install old-version
594 OR pkg->postrm abort-install
600 backup_filename_alloc(const char *file_name
)
604 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
611 backup_make_backup(const char *file_name
)
616 backup
= backup_filename_alloc(file_name
);
617 err
= file_copy(file_name
, backup
);
619 opkg_msg(ERROR
, "Failed to copy %s to %s\n",
629 backup_exists_for(const char *file_name
)
634 backup
= backup_filename_alloc(file_name
);
636 ret
= file_exists(backup
);
644 backup_remove(const char *file_name
)
648 backup
= backup_filename_alloc(file_name
);
656 backup_modified_conffiles(pkg_t
*pkg
, pkg_t
*old_pkg
)
659 conffile_list_elt_t
*iter
;
662 if (conf
->noaction
) return 0;
664 /* Backup all modified conffiles */
666 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
670 cf_name
= root_filename_alloc(cf
->name
);
672 /* Don't worry if the conffile is just plain gone */
673 if (file_exists(cf_name
) && conffile_has_been_modified(cf
)) {
674 err
= backup_make_backup(cf_name
);
683 /* Backup all conffiles that were not conffiles in old_pkg */
684 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
686 cf
= (conffile_t
*)iter
->data
;
687 cf_name
= root_filename_alloc(cf
->name
);
688 /* Ignore if this was a conffile in old_pkg as well */
689 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
693 if (file_exists(cf_name
) && (! backup_exists_for(cf_name
))) {
694 err
= backup_make_backup(cf_name
);
706 backup_modified_conffiles_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
708 conffile_list_elt_t
*iter
;
711 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
712 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
716 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
717 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
725 check_data_file_clashes(pkg_t
*pkg
, pkg_t
*old_pkg
)
727 /* DPKG_INCOMPATIBILITY:
728 opkg takes a slightly different approach than dpkg at this
729 point. dpkg installs each file in the new package while
730 creating a backup for any file that is replaced, (so that it
731 can unwind if necessary). To avoid complexity and redundant
732 storage, opkg doesn't do any installation until later, (at the
733 point at which dpkg removes the backups.
735 But, we do have to check for data file clashes, since after
736 installing a package with a file clash, removing either of the
737 packages involved in the clash has the potential to break the
740 str_list_t
*files_list
;
741 str_list_elt_t
*iter
, *niter
;
745 files_list
= pkg_get_installed_files(pkg
);
746 if (files_list
== NULL
)
749 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
751 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
753 char *filename
= (char *) iter
->data
;
754 root_filename
= root_filename_alloc(filename
);
755 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
759 if (backup_exists_for(root_filename
)) {
763 /* Pre-existing files are OK if force-overwrite was asserted. */
764 if (conf
->force_overwrite
) {
765 /* but we need to change who owns this file */
766 file_hash_set_file_owner(filename
, pkg
);
770 owner
= file_hash_get_file_owner(filename
);
772 /* Pre-existing files are OK if owned by the pkg being upgraded. */
773 if (owner
&& old_pkg
) {
774 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
779 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
781 opkg_msg(DEBUG2
, "Checking replaces for %s in package %s\n",
782 filename
, owner
->name
);
783 if (pkg_replaces(pkg
, owner
)) {
786 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
787 then it's ok to overwrite. */
788 if (strcmp(owner
->name
,pkg
->name
)==0){
789 opkg_msg(INFO
, "Replacing pre-existing file %s"
790 " owned by package %s\n",
791 filename
, owner
->name
);
796 /* Pre-existing files are OK if they are obsolete */
797 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
799 opkg_msg(INFO
, "Pre-exiting file %s is obsolete."
801 filename
, obs
->name
);
805 /* We have found a clash. */
806 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
807 "\tBut that file is already provided by package ",
808 pkg
->name
, filename
);
810 opkg_msg(ERROR
, "%s\n", owner
->name
);
812 opkg_msg(ERROR
, "<no package>\n"
813 "Please move this file out of the way and try again.\n");
819 pkg_free_installed_files(pkg
);
825 * XXX: This function sucks, as does the below comment.
828 check_data_file_clashes_change(pkg_t
*pkg
, pkg_t
*old_pkg
)
830 /* Basically that's the worst hack I could do to be able to change ownership of
831 file list, but, being that we have no way to unwind the mods, due to structure
832 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
833 What we do here is change the ownership of file in hash if a replace ( or similar events
835 Only the action that are needed to change name should be considered.
836 @@@ To change after 1.0 release.
838 str_list_t
*files_list
;
839 str_list_elt_t
*iter
, *niter
;
841 char *root_filename
= NULL
;
843 files_list
= pkg_get_installed_files(pkg
);
844 if (files_list
== NULL
)
847 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
849 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
850 char *filename
= (char *) iter
->data
;
853 root_filename
= NULL
;
855 root_filename
= root_filename_alloc(filename
);
856 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
859 owner
= file_hash_get_file_owner(filename
);
861 if (conf
->force_overwrite
) {
862 /* but we need to change who owns this file */
863 file_hash_set_file_owner(filename
, pkg
);
868 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
870 if (pkg_replaces(pkg
, owner
)) {
871 /* It's now time to change the owner of that file.
872 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
873 opkg_msg(INFO
, "Replacing pre-existing file %s "
874 "owned by package %s\n",
875 filename
, owner
->name
);
876 file_hash_set_file_owner(filename
, pkg
);
885 root_filename
= NULL
;
887 pkg_free_installed_files(pkg
);
893 check_data_file_clashes_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
895 /* Nothing to do since check_data_file_clashes doesn't change state */
900 postrm_upgrade_old_pkg(pkg_t
*pkg
, pkg_t
*old_pkg
)
902 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
903 1. If the package is being upgraded, call
904 old-postrm upgrade new-version
905 2. If this fails, attempt:
906 new-postrm failed-upgrade old-version
907 Error unwind, for both cases:
908 old-preinst abort-upgrade new-version */
913 postrm_upgrade_old_pkg_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
915 /* DPKG_INCOMPATIBILITY:
916 dpkg does some things here that we don't do yet. Do we care?
917 (See postrm_upgrade_old_pkg for details)
923 remove_obsolesced_files(pkg_t
*pkg
, pkg_t
*old_pkg
)
926 str_list_t
*old_files
;
928 str_list_t
*new_files
;
930 hash_table_t new_files_table
;
932 old_files
= pkg_get_installed_files(old_pkg
);
933 if (old_files
== NULL
)
936 new_files
= pkg_get_installed_files(pkg
);
937 if (new_files
== NULL
) {
938 pkg_free_installed_files(old_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(old
);
961 if (owner
!= old_pkg
) {
962 /* in case obsolete file no longer belongs to old_pkg */
966 /* old file is obsolete */
967 opkg_msg(INFO
, "Removing obsolete file %s.\n", old
);
968 if (!conf
->noaction
) {
971 opkg_perror(ERROR
, "unlinking %s failed", old
);
976 hash_table_deinit(&new_files_table
);
977 pkg_free_installed_files(old_pkg
);
978 pkg_free_installed_files(pkg
);
984 install_maintainer_scripts(pkg_t
*pkg
, pkg_t
*old_pkg
)
989 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
990 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
998 remove_disappeared(pkg_t
*pkg
)
1000 /* DPKG_INCOMPATIBILITY:
1001 This is a fairly sophisticated dpkg operation. Shall we
1004 /* Any packages all of whose files have been overwritten during the
1005 installation, and which aren't required for dependencies, are
1006 considered to have been removed. For each such package
1007 1. disappearer's-postrm disappear overwriter overwriter-version
1008 2. The package's maintainer scripts are removed
1009 3. It is noted in the status database as being in a sane state,
1010 namely not installed (any conffiles it may have are ignored,
1011 rather than being removed by dpkg). Note that disappearing
1012 packages do not have their prerm called, because dpkg doesn't
1013 know in advance that the package is going to vanish.
1019 install_data_files(pkg_t
*pkg
)
1023 /* opkg takes a slightly different approach to data file backups
1024 than dpkg. Rather than removing backups at this point, we
1025 actually do the data file installation now. See comments in
1026 check_data_file_clashes() for more details. */
1028 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1029 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1034 /* The "Essential" control field may only be present in the control
1035 * file and not in the Packages list. Ensure we capture it regardless.
1037 * XXX: This should be fixed outside of opkg, in the Package list.
1039 set_flags_from_control(pkg
) ;
1041 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1042 err
= pkg_write_filelist(pkg
);
1046 /* XXX: FEATURE: opkg should identify any files which existed
1047 before installation and which were overwritten, (see
1048 check_data_file_clashes()). What it must do is remove any such
1049 files from the filelist of the old package which provided the
1050 file. Otherwise, if the old package were removed at some point
1051 it would break the new package. Removing the new package will
1052 also break the old one, but this cannot be helped since the old
1053 package's file has already been deleted. This is the importance
1054 of check_data_file_clashes(), and only allowing opkg to install
1055 a clashing package with a user force. */
1061 resolve_conffiles(pkg_t
*pkg
)
1063 conffile_list_elt_t
*iter
;
1068 if (conf
->noaction
) return 0;
1070 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1071 char *root_filename
;
1072 cf
= (conffile_t
*)iter
->data
;
1073 root_filename
= root_filename_alloc(cf
->name
);
1075 /* Might need to initialize the md5sum for each conffile */
1076 if (cf
->value
== NULL
) {
1077 cf
->value
= file_md5sum_alloc(root_filename
);
1080 if (!file_exists(root_filename
)) {
1081 free(root_filename
);
1085 cf_backup
= backup_filename_alloc(root_filename
);
1087 if (file_exists(cf_backup
)) {
1088 /* Let's compute md5 to test if files are changed */
1089 md5sum
= file_md5sum_alloc(cf_backup
);
1090 if (md5sum
&& cf
->value
&& strcmp(cf
->value
,md5sum
) != 0 ) {
1091 if (conf
->force_maintainer
) {
1092 opkg_msg(NOTICE
, "Conffile %s using maintainer's setting.\n",
1096 sprintf_alloc(&new_conffile
, "%s-opkg", root_filename
);
1097 opkg_msg(NOTICE
, "Existing conffile %s "
1098 "is different from the conffile in the new package."
1099 " The new conffile will be placed at %s.\n",
1100 root_filename
, new_conffile
);
1101 rename(root_filename
, new_conffile
);
1102 rename(cf_backup
, root_filename
);
1112 free(root_filename
);
1120 opkg_install_by_name(const char *pkg_name
)
1124 char *old_version
, *new_version
;
1126 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1128 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1131 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1135 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1137 opkg_msg(DEBUG2
, " old %s ", old
->version
);
1138 opkg_msg(DEBUG2
, " new %s\n", new->version
);
1140 new->state_flag
|= SF_USER
;
1142 old_version
= pkg_version_str_alloc(old
);
1143 new_version
= pkg_version_str_alloc(new);
1145 cmp
= pkg_compare_versions(old
, new);
1146 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
1147 opkg_msg(DEBUG
, "Forcing downgrade\n");
1148 cmp
= -1 ; /* then we force opkg to downgrade */
1149 /* We need to use a value < 0 because in the 0 case we are asking to */
1150 /* reinstall, and some check could fail asking the "force-reinstall" option */
1152 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1153 "\n\t%s is installed "
1154 "\n\t%s is available "
1155 "\n\t%d was comparison result\n",
1156 pkg_name
, old_version
, new_version
, cmp
);
1157 if (cmp
== 0 && !conf
->force_reinstall
) {
1159 "Package %s (%s) installed in %s is up to date.\n",
1160 old
->name
, old_version
, old
->dest
->name
);
1164 } else if (cmp
> 0) {
1166 "Not downgrading package %s on %s from %s to %s.\n",
1167 old
->name
, old
->dest
->name
, old_version
, new_version
);
1171 } else if (cmp
< 0) {
1172 new->dest
= old
->dest
;
1173 old
->state_want
= SW_DEINSTALL
; /* Here probably the problem for bug 1277 */
1179 opkg_msg(DEBUG2
,"Calling opkg_install_pkg.\n");
1180 return opkg_install_pkg(new, 0);
1184 * @brief Really install a pkg_t
1187 opkg_install_pkg(pkg_t
*pkg
, int from_upgrade
)
1191 pkg_t
*old_pkg
= NULL
;
1192 pkg_vec_t
*replacees
;
1193 abstract_pkg_t
*ab_pkg
= NULL
;
1199 sigset_t newset
, oldset
;
1202 message
= 1; /* Coming from an upgrade, and should change the output message */
1204 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1206 if (!pkg_arch_supported(pkg
)) {
1207 opkg_msg(ERROR
, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1208 pkg
->architecture
, pkg
->name
);
1211 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0 && conf
->nodeps
== 0) {
1212 err
= satisfy_dependencies_for(pkg
);
1216 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1217 pkg
->name
, pkg
->dest
->name
);
1221 if (pkg
->dest
== NULL
) {
1222 pkg
->dest
= conf
->default_dest
;
1225 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1227 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1231 pkg
->state_want
= SW_INSTALL
;
1233 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependences */
1236 err
= check_conflicts_for(pkg
);
1240 /* this setup is to remove the upgrade scenario in the end when
1241 installing pkg A, A deps B & B deps on A. So both B and A are
1242 installed. Then A's installation is started resulting in an
1243 uncecessary upgrade */
1244 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0)
1247 err
= verify_pkg_installable(pkg
);
1251 if (pkg
->local_filename
== NULL
) {
1252 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1254 opkg_msg(ERROR
, "Failed to download %s. "
1255 "Perhaps you need to run 'opkg update'?\n",
1261 /* check that the repository is valid */
1262 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1263 char *list_file_name
, *sig_file_name
, *lists_dir
;
1265 /* check to ensure the package has come from a repository */
1266 if (conf
->check_signature
&& pkg
->src
)
1268 sprintf_alloc (&lists_dir
, "%s",
1269 (conf
->restrict_to_default_dest
)
1270 ? conf
->default_dest
->lists_dir
1272 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, pkg
->src
->name
);
1273 sprintf_alloc (&sig_file_name
, "%s/%s.sig", lists_dir
, pkg
->src
->name
);
1275 if (file_exists (sig_file_name
))
1277 if (opkg_verify_file (list_file_name
, sig_file_name
)){
1278 opkg_msg(ERROR
, "Failed to verify the signature of %s.\n",
1283 opkg_msg(ERROR
, "Signature file is missing for %s. "
1284 "Perhaps you need to run 'opkg update'?\n",
1290 free (list_file_name
);
1291 free (sig_file_name
);
1295 /* Check for md5 values */
1298 file_md5
= file_md5sum_alloc(pkg
->local_filename
);
1299 if (file_md5
&& strcmp(file_md5
, pkg
->md5sum
))
1301 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1302 "Either the opkg or the package index are corrupt. "
1303 "Try 'opkg update'.\n",
1313 /* Check for sha256 value */
1316 file_sha256
= file_sha256sum_alloc(pkg
->local_filename
);
1317 if (file_sha256
&& strcmp(file_sha256
, pkg
->sha256sum
))
1319 opkg_msg(ERROR
, "Package %s sha256sum mismatch. "
1320 "Either the opkg or the package index are corrupt. "
1321 "Try 'opkg update'.\n",
1331 if (pkg
->tmp_unpack_dir
== NULL
) {
1332 if (unpack_pkg_control_files(pkg
) == -1) {
1333 opkg_msg(ERROR
, "Failed to unpack control files from %s.\n",
1334 pkg
->local_filename
);
1339 err
= update_file_ownership(pkg
, old_pkg
);
1343 if (conf
->nodeps
== 0) {
1344 err
= satisfy_dependencies_for(pkg
);
1347 if (pkg
->state_status
== SS_UNPACKED
)
1348 /* Circular dependency has installed it for us. */
1352 replacees
= pkg_vec_alloc();
1353 pkg_get_installed_replacees(pkg
, replacees
);
1355 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1357 sigemptyset(&newset
);
1358 sigaddset(&newset
, SIGINT
);
1359 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1361 opkg_state_changed
++;
1362 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1365 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1367 /* XXX: BUG: we really should treat replacement more like an upgrade
1368 * Instead, we're going to remove the replacees
1370 err
= pkg_remove_installed_replacees(replacees
);
1372 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1374 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1376 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1378 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1380 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1382 err
= preinst_configure(pkg
, old_pkg
);
1384 goto UNWIND_PREINST_CONFIGURE
;
1386 err
= backup_modified_conffiles(pkg
, old_pkg
);
1388 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1390 err
= check_data_file_clashes(pkg
, old_pkg
);
1392 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1394 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1396 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1401 /* point of no return: no unwinding after this */
1402 if (old_pkg
&& !conf
->force_reinstall
) {
1403 old_pkg
->state_want
= SW_DEINSTALL
;
1405 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1406 opkg_msg(INFO
, "Not removing obsolesced files because "
1407 "package %s marked noprune.\n",
1410 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1412 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1413 opkg_msg(ERROR
, "Failed to determine "
1414 "obsolete files from previously "
1415 "installed %s\n", old_pkg
->name
);
1419 /* removing files from old package, to avoid ghost files */
1420 remove_data_files_and_list(old_pkg
);
1421 remove_maintainer_scripts(old_pkg
);
1425 opkg_msg(INFO
, "Installing maintainer scripts.\n");
1426 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1427 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1428 " Package debris may remain!\n",
1433 /* the following just returns 0 */
1434 remove_disappeared(pkg
);
1436 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1438 if (install_data_files(pkg
)) {
1439 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1440 "Package debris may remain!\n",
1445 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1447 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1448 "for files belonging to %s.\n",
1452 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1453 resolve_conffiles(pkg
);
1455 pkg
->state_status
= SS_UNPACKED
;
1456 old_state_flag
= pkg
->state_flag
;
1457 pkg
->state_flag
&= ~SF_PREFER
;
1458 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1459 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1461 if (old_pkg
&& !conf
->force_reinstall
) {
1462 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1465 time(&pkg
->installed_time
);
1467 ab_pkg
= pkg
->parent
;
1469 ab_pkg
->state_status
= pkg
->state_status
;
1471 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1472 pkg_vec_free (replacees
);
1476 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1477 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1478 UNWIND_CHECK_DATA_FILE_CLASHES
:
1479 check_data_file_clashes_unwind(pkg
, old_pkg
);
1480 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1481 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1482 UNWIND_PREINST_CONFIGURE
:
1483 preinst_configure_unwind(pkg
, old_pkg
);
1484 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1485 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1486 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1487 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1488 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1489 pkg_remove_installed_replacees_unwind(replacees
);
1492 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1494 pkg_vec_free (replacees
);