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"
42 #include "libbb/libbb.h"
45 satisfy_dependencies_for(pkg_t
*pkg
)
48 pkg_vec_t
*depends
= pkg_vec_alloc();
50 char **tmp
, **unresolved
= NULL
;
53 ndepends
= pkg_hash_fetch_unsatisfied_dependencies(pkg
, depends
,
57 opkg_msg(ERROR
, "Cannot satisfy the following dependencies for %s:\n",
61 opkg_msg(ERROR
, "\t%s", *unresolved
);
66 opkg_msg(ERROR
, "\n");
67 if (! conf
->force_depends
) {
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
);
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_msg(DEBUG2
,"Calling opkg_install_pkg.\n");
99 err
= opkg_install_pkg(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(pkg_t
*pkg
)
119 pkg_vec_t
*conflicts
= NULL
;
120 message_level_t level
;
122 if (conf
->force_depends
) {
128 if (!conf
->force_depends
)
129 conflicts
= pkg_hash_fetch_conflicts(pkg
);
132 opkg_msg(level
, "The following packages conflict with %s:\n",
135 while (i
< conflicts
->len
)
136 opkg_msg(level
, "\t%s", conflicts
->pkgs
[i
++]->name
);
137 opkg_msg(level
, "\n");
138 pkg_vec_free(conflicts
);
145 update_file_ownership(pkg_t
*new_pkg
, pkg_t
*old_pkg
)
147 str_list_t
*new_list
, *old_list
;
148 str_list_elt_t
*iter
, *niter
;
150 new_list
= pkg_get_installed_files(new_pkg
);
151 if (new_list
== NULL
)
154 for (iter
= str_list_first(new_list
), niter
= str_list_next(new_list
, iter
);
156 iter
= niter
, niter
= str_list_next(new_list
, niter
)) {
157 char *new_file
= (char *)iter
->data
;
158 pkg_t
*owner
= file_hash_get_file_owner(new_file
);
160 opkg_msg(ERROR
, "Null new_file for new_pkg=%s\n", new_pkg
->name
);
161 if (!owner
|| (owner
== old_pkg
))
162 file_hash_set_file_owner(new_file
, new_pkg
);
166 old_list
= pkg_get_installed_files(old_pkg
);
167 if (old_list
== NULL
) {
168 pkg_free_installed_files(new_pkg
);
172 for (iter
= str_list_first(old_list
), niter
= str_list_next(old_list
, iter
);
174 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
175 char *old_file
= (char *)iter
->data
;
176 pkg_t
*owner
= file_hash_get_file_owner(old_file
);
177 if (owner
== old_pkg
) {
179 hash_table_insert(&conf
->obs_file_hash
, old_file
, old_pkg
);
182 pkg_free_installed_files(old_pkg
);
184 pkg_free_installed_files(new_pkg
);
189 verify_pkg_installable(pkg_t
*pkg
)
191 unsigned long kbs_available
, pkg_size_kbs
;
194 if (conf
->force_space
|| pkg
->installed_size
== 0)
197 root_dir
= pkg
->dest
? pkg
->dest
->root_dir
:
198 conf
->default_dest
->root_dir
;
199 kbs_available
= get_available_kbytes(root_dir
);
201 pkg_size_kbs
= (pkg
->installed_size
+ 1023)/1024;
203 if (pkg_size_kbs
>= kbs_available
) {
204 opkg_msg(ERROR
, "Only have %dkb available on filesystem %s, "
206 kbs_available
, root_dir
, pkg
->name
, pkg_size_kbs
);
214 unpack_pkg_control_files(pkg_t
*pkg
)
217 char *conffiles_file_name
;
219 FILE *conffiles_file
;
221 sprintf_alloc(&pkg
->tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
, pkg
->name
);
223 pkg
->tmp_unpack_dir
= mkdtemp(pkg
->tmp_unpack_dir
);
224 if (pkg
->tmp_unpack_dir
== NULL
) {
225 opkg_perror(ERROR
, "Failed to create temporary directory '%s'",
226 pkg
->tmp_unpack_dir
);
230 err
= pkg_extract_control_files_to_dir(pkg
, pkg
->tmp_unpack_dir
);
235 /* XXX: CLEANUP: There might be a cleaner place to read in the
236 conffiles. Seems like I should be able to get everything to go
237 through pkg_init_from_file. If so, maybe it would make sense to
238 move all of unpack_pkg_control_files to that function. */
240 /* Don't need to re-read conffiles if we already have it */
241 if (!nv_pair_list_empty(&pkg
->conffiles
)) {
245 sprintf_alloc(&conffiles_file_name
, "%s/conffiles", pkg
->tmp_unpack_dir
);
246 if (! file_exists(conffiles_file_name
)) {
247 free(conffiles_file_name
);
251 conffiles_file
= fopen(conffiles_file_name
, "r");
252 if (conffiles_file
== NULL
) {
253 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
254 free(conffiles_file_name
);
257 free(conffiles_file_name
);
261 char *cf_name_in_dest
;
263 cf_name
= file_read_line_alloc(conffiles_file
);
264 if (cf_name
== NULL
) {
267 if (cf_name
[0] == '\0') {
271 /* Prepend dest->root_dir to conffile name.
272 Take pains to avoid multiple slashes. */
273 root_dir
= pkg
->dest
->root_dir
;
274 if (conf
->offline_root
)
275 /* skip the offline_root prefix */
276 root_dir
= pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
277 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
278 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
280 /* Can't get an md5sum now, (file isn't extracted yet).
281 We'll wait until resolve_conffiles */
282 conffile_list_append(&pkg
->conffiles
, cf_name_in_dest
, NULL
);
285 free(cf_name_in_dest
);
288 fclose(conffiles_file
);
294 * Remove packages which were auto_installed due to a dependency by old_pkg,
295 * which are no longer a dependency in the new (upgraded) pkg.
298 pkg_remove_orphan_dependent(pkg_t
*pkg
, pkg_t
*old_pkg
)
300 int i
, j
, k
, l
, found
;
303 struct compound_depend
*cd0
, *cd1
;
304 abstract_pkg_t
**dependents
;
306 int count0
= old_pkg
->pre_depends_count
+
307 old_pkg
->depends_count
+
308 old_pkg
->recommends_count
+
309 old_pkg
->suggests_count
;
310 int count1
= pkg
->pre_depends_count
+
312 pkg
->recommends_count
+
315 for (i
=0; i
<count0
; i
++) {
316 cd0
= &old_pkg
->depends
[i
];
317 if (cd0
->type
!= DEPEND
)
319 for (j
=0; j
<cd0
->possibility_count
; j
++) {
323 for (k
=0; k
<count1
; k
++) {
324 cd1
= &pkg
->depends
[i
];
325 if (cd1
->type
!= DEPEND
)
327 for (l
=0; l
<cd1
->possibility_count
; l
++) {
328 if (cd0
->possibilities
[j
]
329 == cd1
->possibilities
[l
]) {
342 * old_pkg has a dependency that pkg does not.
344 p
= pkg_hash_fetch_installed_by_name(
345 cd0
->possibilities
[j
]->pkg
->name
);
350 if (!p
->auto_installed
)
353 n_deps
= pkg_has_installed_dependents(p
, &dependents
);
354 n_deps
--; /* don't count old_pkg */
357 opkg_msg(NOTICE
, "%s was autoinstalled and is "
358 "now orphaned, removing.\n",
361 /* p has one installed dependency (old_pkg),
362 * which we need to ignore during removal. */
363 p
->state_flag
|= SF_REPLACE
;
365 opkg_remove_pkg(p
, 0);
367 opkg_msg(INFO
, "%s was autoinstalled and is "
368 "still required by %d "
369 "installed packages.\n",
378 /* returns number of installed replacees */
380 pkg_get_installed_replacees(pkg_t
*pkg
, pkg_vec_t
*installed_replacees
)
382 abstract_pkg_t
**replaces
= pkg
->replaces
;
383 int replaces_count
= pkg
->replaces_count
;
385 for (i
= 0; i
< replaces_count
; i
++) {
386 abstract_pkg_t
*ab_pkg
= replaces
[i
];
387 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
389 for (j
= 0; j
< pkg_vec
->len
; j
++) {
390 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
391 if (!pkg_conflicts(pkg
, replacee
))
393 if (replacee
->state_status
== SS_INSTALLED
) {
394 pkg_vec_insert(installed_replacees
, replacee
);
399 return installed_replacees
->len
;
403 pkg_remove_installed_replacees(pkg_vec_t
*replacees
)
406 int replaces_count
= replacees
->len
;
407 for (i
= 0; i
< replaces_count
; i
++) {
408 pkg_t
*replacee
= replacees
->pkgs
[i
];
410 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
411 err
= opkg_remove_pkg(replacee
, 0);
418 /* to unwind the removal: make sure they are installed */
420 pkg_remove_installed_replacees_unwind(pkg_vec_t
*replacees
)
423 int replaces_count
= replacees
->len
;
424 for (i
= 0; i
< replaces_count
; i
++) {
425 pkg_t
*replacee
= replacees
->pkgs
[i
];
426 if (replacee
->state_status
!= SS_INSTALLED
) {
427 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
428 err
= opkg_install_pkg(replacee
, 0);
436 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
438 opkg_install_check_downgrade(pkg_t
*pkg
, pkg_t
*old_pkg
, int message
)
441 char message_out
[15];
442 char *old_version
= pkg_version_str_alloc(old_pkg
);
443 char *new_version
= pkg_version_str_alloc(pkg
);
444 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
447 memset(message_out
,'\x0',15);
448 strncpy (message_out
,"Upgrading ",strlen("Upgrading "));
449 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
450 cmp
= -1 ; /* then we force opkg to downgrade */
451 strncpy (message_out
,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
452 /* reinstall, and some check could fail asking the "force-reinstall" option */
457 "Not downgrading package %s on %s from %s to %s.\n",
458 old_pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
460 } else if (cmp
< 0) {
461 opkg_msg(NOTICE
, "%s%s on %s from %s to %s...\n",
462 message_out
, pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
463 pkg
->dest
= old_pkg
->dest
;
465 } else /* cmp == 0 */ {
466 if (conf
->force_reinstall
) {
467 opkg_msg(NOTICE
, "Reinstalling %s (%s) on %s...\n",
468 pkg
->name
, new_version
, old_pkg
->dest
->name
);
469 pkg
->dest
= old_pkg
->dest
;
472 opkg_msg(NOTICE
, "%s (%s) already install on %s."
473 " Not reinstalling.\n",
474 pkg
->name
, new_version
, old_pkg
->dest
->name
);
482 char message_out
[15] ;
483 memset(message_out
,'\x0',15);
485 strncpy( message_out
,"Upgrading ",strlen("Upgrading ") );
487 strncpy( message_out
,"Installing ",strlen("Installing ") );
488 char *version
= pkg_version_str_alloc(pkg
);
490 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
491 pkg
->name
, version
, pkg
->dest
->name
);
499 prerm_upgrade_old_pkg(pkg_t
*pkg
, pkg_t
*old_pkg
)
501 /* DPKG_INCOMPATIBILITY:
502 dpkg does some things here that we don't do yet. Do we care?
504 1. If a version of the package is already installed, call
505 old-prerm upgrade new-version
506 2. If the script runs but exits with a non-zero exit status
507 new-prerm failed-upgrade old-version
508 Error unwind, for both the above cases:
509 old-postinst abort-upgrade new-version
515 prerm_upgrade_old_pkg_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
517 /* DPKG_INCOMPATIBILITY:
518 dpkg does some things here that we don't do yet. Do we care?
519 (See prerm_upgrade_old_package for details)
525 prerm_deconfigure_conflictors(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
527 /* DPKG_INCOMPATIBILITY:
528 dpkg does some things here that we don't do yet. Do we care?
529 2. If a 'conflicting' package is being removed at the same time:
530 1. If any packages depended on that conflicting package and
531 --auto-deconfigure is specified, call, for each such package:
532 deconfigured's-prerm deconfigure \
533 in-favour package-being-installed version \
534 removing conflicting-package version
536 deconfigured's-postinst abort-deconfigure \
537 in-favour package-being-installed-but-failed version \
538 removing conflicting-package version
540 The deconfigured packages are marked as requiring
541 configuration, so that if --install is used they will be
542 configured again if possible.
543 2. To prepare for removal of the conflicting package, call:
544 conflictor's-prerm remove in-favour package new-version
546 conflictor's-postinst abort-remove in-favour package new-version
552 prerm_deconfigure_conflictors_unwind(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
554 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
555 do yet. Do we care? (See prerm_deconfigure_conflictors for
561 preinst_configure(pkg_t
*pkg
, pkg_t
*old_pkg
)
567 char *old_version
= pkg_version_str_alloc(old_pkg
);
568 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
570 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
571 char *pkg_version
= pkg_version_str_alloc(pkg
);
572 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
575 preinst_args
= xstrdup("install");
578 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
580 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
590 preinst_configure_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
592 /* DPKG_INCOMPATIBILITY:
593 dpkg does the following error unwind, should we?
594 pkg->postrm abort-upgrade old-version
595 OR pkg->postrm abort-install old-version
596 OR pkg->postrm abort-install
602 backup_filename_alloc(const char *file_name
)
606 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
613 backup_make_backup(const char *file_name
)
618 backup
= backup_filename_alloc(file_name
);
619 err
= file_copy(file_name
, backup
);
621 opkg_msg(ERROR
, "Failed to copy %s to %s\n",
631 backup_exists_for(const char *file_name
)
636 backup
= backup_filename_alloc(file_name
);
638 ret
= file_exists(backup
);
646 backup_remove(const char *file_name
)
650 backup
= backup_filename_alloc(file_name
);
658 backup_modified_conffiles(pkg_t
*pkg
, pkg_t
*old_pkg
)
661 conffile_list_elt_t
*iter
;
664 if (conf
->noaction
) return 0;
666 /* Backup all modified conffiles */
668 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
672 cf_name
= root_filename_alloc(cf
->name
);
674 /* Don't worry if the conffile is just plain gone */
675 if (file_exists(cf_name
) && conffile_has_been_modified(cf
)) {
676 err
= backup_make_backup(cf_name
);
685 /* Backup all conffiles that were not conffiles in old_pkg */
686 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
688 cf
= (conffile_t
*)iter
->data
;
689 cf_name
= root_filename_alloc(cf
->name
);
690 /* Ignore if this was a conffile in old_pkg as well */
691 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
695 if (file_exists(cf_name
) && (! backup_exists_for(cf_name
))) {
696 err
= backup_make_backup(cf_name
);
708 backup_modified_conffiles_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
710 conffile_list_elt_t
*iter
;
713 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
714 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
718 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
719 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
727 check_data_file_clashes(pkg_t
*pkg
, pkg_t
*old_pkg
)
729 /* DPKG_INCOMPATIBILITY:
730 opkg takes a slightly different approach than dpkg at this
731 point. dpkg installs each file in the new package while
732 creating a backup for any file that is replaced, (so that it
733 can unwind if necessary). To avoid complexity and redundant
734 storage, opkg doesn't do any installation until later, (at the
735 point at which dpkg removes the backups.
737 But, we do have to check for data file clashes, since after
738 installing a package with a file clash, removing either of the
739 packages involved in the clash has the potential to break the
742 str_list_t
*files_list
;
743 str_list_elt_t
*iter
, *niter
;
747 files_list
= pkg_get_installed_files(pkg
);
748 if (files_list
== NULL
)
751 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
753 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
755 char *filename
= (char *) iter
->data
;
756 root_filename
= root_filename_alloc(filename
);
757 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
761 if (backup_exists_for(root_filename
)) {
765 /* Pre-existing files are OK if force-overwrite was asserted. */
766 if (conf
->force_overwrite
) {
767 /* but we need to change who owns this file */
768 file_hash_set_file_owner(filename
, pkg
);
772 owner
= file_hash_get_file_owner(filename
);
774 /* Pre-existing files are OK if owned by the pkg being upgraded. */
775 if (owner
&& old_pkg
) {
776 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
781 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
783 opkg_msg(DEBUG2
, "Checking replaces for %s in package %s\n",
784 filename
, owner
->name
);
785 if (pkg_replaces(pkg
, owner
)) {
788 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
789 then it's ok to overwrite. */
790 if (strcmp(owner
->name
,pkg
->name
)==0){
791 opkg_msg(INFO
, "Replacing pre-existing file %s"
792 " owned by package %s\n",
793 filename
, owner
->name
);
798 /* Pre-existing files are OK if they are obsolete */
799 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
801 opkg_msg(INFO
, "Pre-exiting file %s is obsolete."
803 filename
, obs
->name
);
807 /* We have found a clash. */
808 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
809 "\tBut that file is already provided by package ",
810 pkg
->name
, filename
);
812 opkg_msg(ERROR
, "%s\n", owner
->name
);
814 opkg_msg(ERROR
, "<no package>\n"
815 "Please move this file out of the way and try again.\n");
821 pkg_free_installed_files(pkg
);
827 * XXX: This function sucks, as does the below comment.
830 check_data_file_clashes_change(pkg_t
*pkg
, pkg_t
*old_pkg
)
832 /* Basically that's the worst hack I could do to be able to change ownership of
833 file list, but, being that we have no way to unwind the mods, due to structure
834 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
835 What we do here is change the ownership of file in hash if a replace ( or similar events
837 Only the action that are needed to change name should be considered.
838 @@@ To change after 1.0 release.
840 str_list_t
*files_list
;
841 str_list_elt_t
*iter
, *niter
;
843 char *root_filename
= NULL
;
845 files_list
= pkg_get_installed_files(pkg
);
846 if (files_list
== NULL
)
849 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
851 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
852 char *filename
= (char *) iter
->data
;
855 root_filename
= NULL
;
857 root_filename
= root_filename_alloc(filename
);
858 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
861 owner
= file_hash_get_file_owner(filename
);
863 if (conf
->force_overwrite
) {
864 /* but we need to change who owns this file */
865 file_hash_set_file_owner(filename
, pkg
);
870 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
872 if (pkg_replaces(pkg
, owner
)) {
873 /* It's now time to change the owner of that file.
874 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
875 opkg_msg(INFO
, "Replacing pre-existing file %s "
876 "owned by package %s\n",
877 filename
, owner
->name
);
878 file_hash_set_file_owner(filename
, pkg
);
887 root_filename
= NULL
;
889 pkg_free_installed_files(pkg
);
895 check_data_file_clashes_unwind(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(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(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(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 old_files
= pkg_get_installed_files(old_pkg
);
935 if (old_files
== NULL
)
938 new_files
= pkg_get_installed_files(pkg
);
939 if (new_files
== NULL
) {
940 pkg_free_installed_files(old_pkg
);
944 new_files_table
.entries
= NULL
;
945 hash_table_init("new_files" , &new_files_table
, 20);
946 for (nf
= str_list_first(new_files
); nf
; nf
= str_list_next(new_files
, nf
)) {
948 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
951 for (of
= str_list_first(old_files
); of
; of
= str_list_next(old_files
, of
)) {
954 old
= (char *)of
->data
;
955 new = (char *) hash_table_get (&new_files_table
, old
);
959 if (file_is_dir(old
)) {
962 owner
= file_hash_get_file_owner(old
);
963 if (owner
!= old_pkg
) {
964 /* in case obsolete file no longer belongs to old_pkg */
968 /* old file is obsolete */
969 opkg_msg(INFO
, "Removing obsolete file %s.\n", old
);
970 if (!conf
->noaction
) {
973 opkg_perror(ERROR
, "unlinking %s failed", 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(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(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(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_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1031 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1036 /* The "Essential" control field may only be present in the control
1037 * file and not in the Packages list. Ensure we capture it regardless.
1039 * XXX: This should be fixed outside of opkg, in the Package list.
1041 set_flags_from_control(pkg
) ;
1043 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1044 err
= pkg_write_filelist(pkg
);
1048 /* XXX: FEATURE: opkg should identify any files which existed
1049 before installation and which were overwritten, (see
1050 check_data_file_clashes()). What it must do is remove any such
1051 files from the filelist of the old package which provided the
1052 file. Otherwise, if the old package were removed at some point
1053 it would break the new package. Removing the new package will
1054 also break the old one, but this cannot be helped since the old
1055 package's file has already been deleted. This is the importance
1056 of check_data_file_clashes(), and only allowing opkg to install
1057 a clashing package with a user force. */
1063 resolve_conffiles(pkg_t
*pkg
)
1065 conffile_list_elt_t
*iter
;
1070 if (conf
->noaction
) return 0;
1072 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1073 char *root_filename
;
1074 cf
= (conffile_t
*)iter
->data
;
1075 root_filename
= root_filename_alloc(cf
->name
);
1077 /* Might need to initialize the md5sum for each conffile */
1078 if (cf
->value
== NULL
) {
1079 cf
->value
= file_md5sum_alloc(root_filename
);
1082 if (!file_exists(root_filename
)) {
1083 free(root_filename
);
1087 cf_backup
= backup_filename_alloc(root_filename
);
1089 if (file_exists(cf_backup
)) {
1090 /* Let's compute md5 to test if files are changed */
1091 md5sum
= file_md5sum_alloc(cf_backup
);
1092 if (md5sum
&& cf
->value
&& strcmp(cf
->value
,md5sum
) != 0 ) {
1093 if (conf
->force_maintainer
) {
1094 opkg_msg(NOTICE
, "Conffile %s using maintainer's setting.\n",
1098 sprintf_alloc(&new_conffile
, "%s-opkg", root_filename
);
1099 opkg_msg(NOTICE
, "Existing conffile %s "
1100 "is different from the conffile in the new package."
1101 " The new conffile will be placed at %s.\n",
1102 root_filename
, new_conffile
);
1103 rename(root_filename
, new_conffile
);
1104 rename(cf_backup
, root_filename
);
1114 free(root_filename
);
1122 opkg_install_by_name(const char *pkg_name
)
1126 char *old_version
, *new_version
;
1128 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1130 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1133 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1137 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1139 opkg_msg(DEBUG2
, " old %s ", old
->version
);
1140 opkg_msg(DEBUG2
, " new %s\n", new->version
);
1142 new->state_flag
|= SF_USER
;
1144 old_version
= pkg_version_str_alloc(old
);
1145 new_version
= pkg_version_str_alloc(new);
1147 cmp
= pkg_compare_versions(old
, new);
1148 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
1149 opkg_msg(DEBUG
, "Forcing downgrade\n");
1150 cmp
= -1 ; /* then we force opkg to downgrade */
1151 /* We need to use a value < 0 because in the 0 case we are asking to */
1152 /* reinstall, and some check could fail asking the "force-reinstall" option */
1154 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1155 "\n\t%s is installed "
1156 "\n\t%s is available "
1157 "\n\t%d was comparison result\n",
1158 pkg_name
, old_version
, new_version
, cmp
);
1159 if (cmp
== 0 && !conf
->force_reinstall
) {
1161 "Package %s (%s) installed in %s is up to date.\n",
1162 old
->name
, old_version
, old
->dest
->name
);
1166 } else if (cmp
> 0) {
1168 "Not downgrading package %s on %s from %s to %s.\n",
1169 old
->name
, old
->dest
->name
, old_version
, new_version
);
1173 } else if (cmp
< 0) {
1174 new->dest
= old
->dest
;
1175 old
->state_want
= SW_DEINSTALL
; /* Here probably the problem for bug 1277 */
1181 opkg_msg(DEBUG2
,"Calling opkg_install_pkg.\n");
1182 return opkg_install_pkg(new, 0);
1186 * @brief Really install a pkg_t
1189 opkg_install_pkg(pkg_t
*pkg
, int from_upgrade
)
1193 pkg_t
*old_pkg
= NULL
;
1194 pkg_vec_t
*replacees
;
1195 abstract_pkg_t
*ab_pkg
= NULL
;
1201 sigset_t newset
, oldset
;
1204 message
= 1; /* Coming from an upgrade, and should change the output message */
1206 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1208 if (!pkg_arch_supported(pkg
)) {
1209 opkg_msg(ERROR
, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1210 pkg
->architecture
, pkg
->name
);
1213 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0 && conf
->nodeps
== 0) {
1214 err
= satisfy_dependencies_for(pkg
);
1218 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1219 pkg
->name
, pkg
->dest
->name
);
1223 if (pkg
->dest
== NULL
) {
1224 pkg
->dest
= conf
->default_dest
;
1227 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1229 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1233 pkg
->state_want
= SW_INSTALL
;
1235 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependences */
1238 err
= check_conflicts_for(pkg
);
1242 /* this setup is to remove the upgrade scenario in the end when
1243 installing pkg A, A deps B & B deps on A. So both B and A are
1244 installed. Then A's installation is started resulting in an
1245 uncecessary upgrade */
1246 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0)
1249 err
= verify_pkg_installable(pkg
);
1253 if (pkg
->local_filename
== NULL
) {
1254 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1256 opkg_msg(ERROR
, "Failed to download %s. "
1257 "Perhaps you need to run 'opkg update'?\n",
1263 /* check that the repository is valid */
1264 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1265 char *list_file_name
, *sig_file_name
, *lists_dir
;
1267 /* check to ensure the package has come from a repository */
1268 if (conf
->check_signature
&& pkg
->src
)
1270 sprintf_alloc (&lists_dir
, "%s",
1271 (conf
->restrict_to_default_dest
)
1272 ? conf
->default_dest
->lists_dir
1274 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, pkg
->src
->name
);
1275 sprintf_alloc (&sig_file_name
, "%s/%s.sig", lists_dir
, pkg
->src
->name
);
1277 if (file_exists (sig_file_name
))
1279 if (opkg_verify_file (list_file_name
, sig_file_name
)){
1280 opkg_msg(ERROR
, "Failed to verify the signature of %s.\n",
1285 opkg_msg(ERROR
, "Signature file is missing for %s. "
1286 "Perhaps you need to run 'opkg update'?\n",
1292 free (list_file_name
);
1293 free (sig_file_name
);
1297 /* Check for md5 values */
1300 file_md5
= file_md5sum_alloc(pkg
->local_filename
);
1301 if (file_md5
&& strcmp(file_md5
, pkg
->md5sum
))
1303 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1304 "Either the opkg or the package index are corrupt. "
1305 "Try 'opkg update'.\n",
1315 /* Check for sha256 value */
1318 file_sha256
= file_sha256sum_alloc(pkg
->local_filename
);
1319 if (file_sha256
&& strcmp(file_sha256
, pkg
->sha256sum
))
1321 opkg_msg(ERROR
, "Package %s sha256sum mismatch. "
1322 "Either the opkg or the package index are corrupt. "
1323 "Try 'opkg update'.\n",
1333 if (pkg
->tmp_unpack_dir
== NULL
) {
1334 if (unpack_pkg_control_files(pkg
) == -1) {
1335 opkg_msg(ERROR
, "Failed to unpack control files from %s.\n",
1336 pkg
->local_filename
);
1341 err
= update_file_ownership(pkg
, old_pkg
);
1345 if (conf
->nodeps
== 0) {
1346 err
= satisfy_dependencies_for(pkg
);
1349 if (pkg
->state_status
== SS_UNPACKED
)
1350 /* Circular dependency has installed it for us. */
1354 replacees
= pkg_vec_alloc();
1355 pkg_get_installed_replacees(pkg
, replacees
);
1357 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1359 sigemptyset(&newset
);
1360 sigaddset(&newset
, SIGINT
);
1361 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1363 opkg_state_changed
++;
1364 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1367 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1369 /* XXX: BUG: we really should treat replacement more like an upgrade
1370 * Instead, we're going to remove the replacees
1372 err
= pkg_remove_installed_replacees(replacees
);
1374 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1376 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1378 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1380 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1382 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1384 err
= preinst_configure(pkg
, old_pkg
);
1386 goto UNWIND_PREINST_CONFIGURE
;
1388 err
= backup_modified_conffiles(pkg
, old_pkg
);
1390 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1392 err
= check_data_file_clashes(pkg
, old_pkg
);
1394 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1396 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1398 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1403 /* point of no return: no unwinding after this */
1404 if (old_pkg
&& !conf
->force_reinstall
) {
1405 old_pkg
->state_want
= SW_DEINSTALL
;
1407 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1408 opkg_msg(INFO
, "Not removing obsolesced files because "
1409 "package %s marked noprune.\n",
1412 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1414 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1415 opkg_msg(ERROR
, "Failed to determine "
1416 "obsolete files from previously "
1417 "installed %s\n", old_pkg
->name
);
1421 /* removing files from old package, to avoid ghost files */
1422 remove_data_files_and_list(old_pkg
);
1423 remove_maintainer_scripts(old_pkg
);
1427 opkg_msg(INFO
, "Installing maintainer scripts.\n");
1428 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1429 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1430 " Package debris may remain!\n",
1435 /* the following just returns 0 */
1436 remove_disappeared(pkg
);
1438 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1440 if (install_data_files(pkg
)) {
1441 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1442 "Package debris may remain!\n",
1447 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1449 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1450 "for files belonging to %s.\n",
1454 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1455 resolve_conffiles(pkg
);
1457 pkg
->state_status
= SS_UNPACKED
;
1458 old_state_flag
= pkg
->state_flag
;
1459 pkg
->state_flag
&= ~SF_PREFER
;
1460 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1461 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1463 if (old_pkg
&& !conf
->force_reinstall
) {
1464 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1467 time(&pkg
->installed_time
);
1469 ab_pkg
= pkg
->parent
;
1471 ab_pkg
->state_status
= pkg
->state_status
;
1473 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1474 pkg_vec_free (replacees
);
1478 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1479 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1480 UNWIND_CHECK_DATA_FILE_CLASHES
:
1481 check_data_file_clashes_unwind(pkg
, old_pkg
);
1482 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1483 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1484 UNWIND_PREINST_CONFIGURE
:
1485 preinst_configure_unwind(pkg
, old_pkg
);
1486 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1487 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1488 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1489 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1490 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1491 pkg_remove_installed_replacees_unwind(replacees
);
1494 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1496 pkg_vec_free (replacees
);