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_message(ERROR
, "\t%s", *unresolved
);
66 opkg_message(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
);
159 pkg_t
*obs
= hash_table_get(&conf
->obs_file_hash
, new_file
);
161 opkg_msg(DEBUG2
, "%s: new_pkg=%s wants file %s, from owner=%s\n",
162 __func__
, new_pkg
->name
, new_file
, owner
?owner
->name
:"<NULL>");
164 if (!owner
|| (owner
== old_pkg
) || obs
)
165 file_hash_set_file_owner(new_file
, new_pkg
);
169 old_list
= pkg_get_installed_files(old_pkg
);
170 if (old_list
== NULL
) {
171 pkg_free_installed_files(new_pkg
);
175 for (iter
= str_list_first(old_list
), niter
= str_list_next(old_list
, iter
);
177 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
178 char *old_file
= (char *)iter
->data
;
179 pkg_t
*owner
= file_hash_get_file_owner(old_file
);
180 if (!owner
|| (owner
== old_pkg
)) {
182 hash_table_insert(&conf
->obs_file_hash
, old_file
, old_pkg
);
185 pkg_free_installed_files(old_pkg
);
187 pkg_free_installed_files(new_pkg
);
192 verify_pkg_installable(pkg_t
*pkg
)
194 unsigned long kbs_available
, pkg_size_kbs
;
197 if (conf
->force_space
|| pkg
->installed_size
== 0)
200 root_dir
= pkg
->dest
? pkg
->dest
->root_dir
:
201 conf
->default_dest
->root_dir
;
202 kbs_available
= get_available_kbytes(root_dir
);
204 pkg_size_kbs
= (pkg
->installed_size
+ 1023)/1024;
206 if (pkg_size_kbs
>= kbs_available
) {
207 opkg_msg(ERROR
, "Only have %dkb available on filesystem %s, "
209 kbs_available
, root_dir
, pkg
->name
, pkg_size_kbs
);
217 unpack_pkg_control_files(pkg_t
*pkg
)
220 char *conffiles_file_name
;
222 FILE *conffiles_file
;
224 sprintf_alloc(&pkg
->tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
, pkg
->name
);
226 pkg
->tmp_unpack_dir
= mkdtemp(pkg
->tmp_unpack_dir
);
227 if (pkg
->tmp_unpack_dir
== NULL
) {
228 opkg_perror(ERROR
, "Failed to create temporary directory '%s'",
229 pkg
->tmp_unpack_dir
);
233 err
= pkg_extract_control_files_to_dir(pkg
, pkg
->tmp_unpack_dir
);
238 /* XXX: CLEANUP: There might be a cleaner place to read in the
239 conffiles. Seems like I should be able to get everything to go
240 through pkg_init_from_file. If so, maybe it would make sense to
241 move all of unpack_pkg_control_files to that function. */
243 /* Don't need to re-read conffiles if we already have it */
244 if (!nv_pair_list_empty(&pkg
->conffiles
)) {
248 sprintf_alloc(&conffiles_file_name
, "%s/conffiles", pkg
->tmp_unpack_dir
);
249 if (! file_exists(conffiles_file_name
)) {
250 free(conffiles_file_name
);
254 conffiles_file
= fopen(conffiles_file_name
, "r");
255 if (conffiles_file
== NULL
) {
256 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
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(pkg_t
*pkg
, pkg_t
*old_pkg
)
303 int i
, j
, k
, l
, found
,r
, err
= 0;
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(
348 cd0
->possibilities
[j
]->pkg
->name
);
353 if (!p
->auto_installed
)
356 n_deps
= pkg_has_installed_dependents(p
, &dependents
);
357 n_deps
--; /* don't count old_pkg */
360 opkg_msg(NOTICE
, "%s was autoinstalled and is "
361 "now orphaned, removing.\n",
364 /* p has one installed dependency (old_pkg),
365 * which we need to ignore during removal. */
366 p
->state_flag
|= SF_REPLACE
;
368 r
= opkg_remove_pkg(p
, 0);
372 opkg_msg(INFO
, "%s was autoinstalled and is "
373 "still required by %d "
374 "installed packages.\n",
383 /* returns number of installed replacees */
385 pkg_get_installed_replacees(pkg_t
*pkg
, pkg_vec_t
*installed_replacees
)
387 abstract_pkg_t
**replaces
= pkg
->replaces
;
388 int replaces_count
= pkg
->replaces_count
;
390 for (i
= 0; i
< replaces_count
; i
++) {
391 abstract_pkg_t
*ab_pkg
= replaces
[i
];
392 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
394 for (j
= 0; j
< pkg_vec
->len
; j
++) {
395 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
396 if (!pkg_conflicts(pkg
, replacee
))
398 if (replacee
->state_status
== SS_INSTALLED
) {
399 pkg_vec_insert(installed_replacees
, replacee
);
404 return installed_replacees
->len
;
408 pkg_remove_installed_replacees(pkg_vec_t
*replacees
)
411 int replaces_count
= replacees
->len
;
412 for (i
= 0; i
< replaces_count
; i
++) {
413 pkg_t
*replacee
= replacees
->pkgs
[i
];
415 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
416 err
= opkg_remove_pkg(replacee
, 0);
423 /* to unwind the removal: make sure they are installed */
425 pkg_remove_installed_replacees_unwind(pkg_vec_t
*replacees
)
428 int replaces_count
= replacees
->len
;
429 for (i
= 0; i
< replaces_count
; i
++) {
430 pkg_t
*replacee
= replacees
->pkgs
[i
];
431 if (replacee
->state_status
!= SS_INSTALLED
) {
432 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
433 err
= opkg_install_pkg(replacee
, 0);
441 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
443 opkg_install_check_downgrade(pkg_t
*pkg
, pkg_t
*old_pkg
, int message
)
446 char message_out
[15];
447 char *old_version
= pkg_version_str_alloc(old_pkg
);
448 char *new_version
= pkg_version_str_alloc(pkg
);
449 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
452 memset(message_out
,'\x0',15);
453 strncpy (message_out
,"Upgrading ",strlen("Upgrading "));
454 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
455 cmp
= -1 ; /* then we force opkg to downgrade */
456 strncpy (message_out
,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
457 /* reinstall, and some check could fail asking the "force-reinstall" option */
461 if(!conf
->download_only
)
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 if(!conf
->download_only
)
468 opkg_msg(NOTICE
, "%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 if(!conf
->download_only
)
475 opkg_msg(NOTICE
, "Reinstalling %s (%s) on %s...\n",
476 pkg
->name
, new_version
, old_pkg
->dest
->name
);
477 pkg
->dest
= old_pkg
->dest
;
480 if(!conf
->download_only
)
481 opkg_msg(NOTICE
, "%s (%s) already install on %s."
482 " Not reinstalling.\n",
483 pkg
->name
, new_version
, old_pkg
->dest
->name
);
491 char message_out
[15] ;
492 memset(message_out
,'\x0',15);
494 strncpy( message_out
,"Upgrading ",strlen("Upgrading ") );
496 strncpy( message_out
,"Installing ",strlen("Installing ") );
497 char *version
= pkg_version_str_alloc(pkg
);
499 if(!conf
->download_only
)
500 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
501 pkg
->name
, version
, pkg
->dest
->name
);
509 prerm_upgrade_old_pkg(pkg_t
*pkg
, pkg_t
*old_pkg
)
511 /* DPKG_INCOMPATIBILITY:
512 dpkg does some things here that we don't do yet. Do we care?
514 1. If a version of the package is already installed, call
515 old-prerm upgrade new-version
516 2. If the script runs but exits with a non-zero exit status
517 new-prerm failed-upgrade old-version
518 Error unwind, for both the above cases:
519 old-postinst abort-upgrade new-version
525 prerm_upgrade_old_pkg_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
527 /* DPKG_INCOMPATIBILITY:
528 dpkg does some things here that we don't do yet. Do we care?
529 (See prerm_upgrade_old_package for details)
535 prerm_deconfigure_conflictors(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
537 /* DPKG_INCOMPATIBILITY:
538 dpkg does some things here that we don't do yet. Do we care?
539 2. If a 'conflicting' package is being removed at the same time:
540 1. If any packages depended on that conflicting package and
541 --auto-deconfigure is specified, call, for each such package:
542 deconfigured's-prerm deconfigure \
543 in-favour package-being-installed version \
544 removing conflicting-package version
546 deconfigured's-postinst abort-deconfigure \
547 in-favour package-being-installed-but-failed version \
548 removing conflicting-package version
550 The deconfigured packages are marked as requiring
551 configuration, so that if --install is used they will be
552 configured again if possible.
553 2. To prepare for removal of the conflicting package, call:
554 conflictor's-prerm remove in-favour package new-version
556 conflictor's-postinst abort-remove in-favour package new-version
562 prerm_deconfigure_conflictors_unwind(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
564 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
565 do yet. Do we care? (See prerm_deconfigure_conflictors for
571 preinst_configure(pkg_t
*pkg
, pkg_t
*old_pkg
)
577 char *old_version
= pkg_version_str_alloc(old_pkg
);
578 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
580 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
581 char *pkg_version
= pkg_version_str_alloc(pkg
);
582 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
585 preinst_args
= xstrdup("install");
588 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
590 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
600 preinst_configure_unwind(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(const char *file_name
)
628 backup
= backup_filename_alloc(file_name
);
629 err
= file_copy(file_name
, backup
);
631 opkg_msg(ERROR
, "Failed to copy %s to %s\n",
641 backup_exists_for(const char *file_name
)
646 backup
= backup_filename_alloc(file_name
);
648 ret
= file_exists(backup
);
656 backup_remove(const char *file_name
)
660 backup
= backup_filename_alloc(file_name
);
668 backup_modified_conffiles(pkg_t
*pkg
, pkg_t
*old_pkg
)
671 conffile_list_elt_t
*iter
;
674 if (conf
->noaction
) return 0;
676 /* Backup all modified conffiles */
678 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
682 cf_name
= root_filename_alloc(cf
->name
);
684 /* Don't worry if the conffile is just plain gone */
685 if (file_exists(cf_name
) && conffile_has_been_modified(cf
)) {
686 err
= backup_make_backup(cf_name
);
695 /* Backup all conffiles that were not conffiles in old_pkg */
696 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
698 cf
= (conffile_t
*)iter
->data
;
699 cf_name
= root_filename_alloc(cf
->name
);
700 /* Ignore if this was a conffile in old_pkg as well */
701 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
705 if (file_exists(cf_name
) && (! backup_exists_for(cf_name
))) {
706 err
= backup_make_backup(cf_name
);
718 backup_modified_conffiles_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
720 conffile_list_elt_t
*iter
;
723 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
724 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
728 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
729 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
737 check_data_file_clashes(pkg_t
*pkg
, pkg_t
*old_pkg
)
739 /* DPKG_INCOMPATIBILITY:
740 opkg takes a slightly different approach than dpkg at this
741 point. dpkg installs each file in the new package while
742 creating a backup for any file that is replaced, (so that it
743 can unwind if necessary). To avoid complexity and redundant
744 storage, opkg doesn't do any installation until later, (at the
745 point at which dpkg removes the backups.
747 But, we do have to check for data file clashes, since after
748 installing a package with a file clash, removing either of the
749 packages involved in the clash has the potential to break the
752 str_list_t
*files_list
;
753 str_list_elt_t
*iter
, *niter
;
757 files_list
= pkg_get_installed_files(pkg
);
758 if (files_list
== NULL
)
761 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
763 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
764 filename
= (char *) iter
->data
;
765 if (file_exists(filename
) && (! file_is_dir(filename
))) {
769 if (backup_exists_for(filename
)) {
773 /* Pre-existing files are OK if force-overwrite was asserted. */
774 if (conf
->force_overwrite
) {
775 /* but we need to change who owns this file */
776 file_hash_set_file_owner(filename
, pkg
);
780 owner
= file_hash_get_file_owner(filename
);
782 /* Pre-existing files are OK if owned by the pkg being upgraded. */
783 if (owner
&& old_pkg
) {
784 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
789 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
791 opkg_msg(DEBUG2
, "Checking replaces for %s in package %s\n",
792 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_msg(INFO
, "Replacing pre-existing file %s"
800 " owned by package %s\n",
801 filename
, owner
->name
);
806 /* Pre-existing files are OK if they are obsolete */
807 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
809 opkg_msg(INFO
, "Pre-exiting file %s is obsolete."
811 filename
, obs
->name
);
815 /* We have found a clash. */
816 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
817 "\tBut that file is already provided by package ",
818 pkg
->name
, filename
);
820 opkg_message(ERROR
, "%s\n", owner
->name
);
822 opkg_message(ERROR
, "<no package>\n"
823 "Please move this file out of the way and try again.\n");
828 pkg_free_installed_files(pkg
);
834 * XXX: This function sucks, as does the below comment.
837 check_data_file_clashes_change(pkg_t
*pkg
, pkg_t
*old_pkg
)
839 /* Basically that's the worst hack I could do to be able to change ownership of
840 file list, but, being that we have no way to unwind the mods, due to structure
841 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
842 What we do here is change the ownership of file in hash if a replace ( or similar events
844 Only the action that are needed to change name should be considered.
845 @@@ To change after 1.0 release.
847 str_list_t
*files_list
;
848 str_list_elt_t
*iter
, *niter
;
850 char *root_filename
= NULL
;
852 files_list
= pkg_get_installed_files(pkg
);
853 if (files_list
== NULL
)
856 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
858 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
859 char *filename
= (char *) iter
->data
;
862 root_filename
= NULL
;
864 root_filename
= root_filename_alloc(filename
);
865 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
868 owner
= file_hash_get_file_owner(filename
);
870 if (conf
->force_overwrite
) {
871 /* but we need to change who owns this file */
872 file_hash_set_file_owner(filename
, pkg
);
877 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
879 if (pkg_replaces(pkg
, owner
)) {
880 /* It's now time to change the owner of that file.
881 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
882 opkg_msg(INFO
, "Replacing pre-existing file %s "
883 "owned by package %s\n",
884 filename
, owner
->name
);
885 file_hash_set_file_owner(filename
, pkg
);
894 root_filename
= NULL
;
896 pkg_free_installed_files(pkg
);
902 check_data_file_clashes_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
904 /* Nothing to do since check_data_file_clashes doesn't change state */
909 postrm_upgrade_old_pkg(pkg_t
*pkg
, pkg_t
*old_pkg
)
911 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
912 1. If the package is being upgraded, call
913 old-postrm upgrade new-version
914 2. If this fails, attempt:
915 new-postrm failed-upgrade old-version
916 Error unwind, for both cases:
917 old-preinst abort-upgrade new-version */
922 postrm_upgrade_old_pkg_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
924 /* DPKG_INCOMPATIBILITY:
925 dpkg does some things here that we don't do yet. Do we care?
926 (See postrm_upgrade_old_pkg for details)
932 remove_obsolesced_files(pkg_t
*pkg
, pkg_t
*old_pkg
)
935 str_list_t
*old_files
;
937 str_list_t
*new_files
;
939 hash_table_t new_files_table
;
941 old_files
= pkg_get_installed_files(old_pkg
);
942 if (old_files
== NULL
)
945 new_files
= pkg_get_installed_files(pkg
);
946 if (new_files
== NULL
) {
947 pkg_free_installed_files(old_pkg
);
951 new_files_table
.entries
= NULL
;
952 hash_table_init("new_files" , &new_files_table
, 20);
953 for (nf
= str_list_first(new_files
); nf
; nf
= str_list_next(new_files
, nf
)) {
955 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
958 for (of
= str_list_first(old_files
); of
; of
= str_list_next(old_files
, of
)) {
961 old
= (char *)of
->data
;
962 new = (char *) hash_table_get (&new_files_table
, old
);
966 if (file_is_dir(old
)) {
969 owner
= file_hash_get_file_owner(old
);
970 if (owner
!= old_pkg
) {
971 /* in case obsolete file no longer belongs to old_pkg */
975 /* old file is obsolete */
976 opkg_msg(NOTICE
, "Removing obsolete file %s.\n", old
);
977 if (!conf
->noaction
) {
980 opkg_perror(ERROR
, "unlinking %s failed", old
);
985 hash_table_deinit(&new_files_table
);
986 pkg_free_installed_files(old_pkg
);
987 pkg_free_installed_files(pkg
);
993 install_maintainer_scripts(pkg_t
*pkg
, pkg_t
*old_pkg
)
998 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
999 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1000 pkg
->dest
->info_dir
,
1007 remove_disappeared(pkg_t
*pkg
)
1009 /* DPKG_INCOMPATIBILITY:
1010 This is a fairly sophisticated dpkg operation. Shall we
1013 /* Any packages all of whose files have been overwritten during the
1014 installation, and which aren't required for dependencies, are
1015 considered to have been removed. For each such package
1016 1. disappearer's-postrm disappear overwriter overwriter-version
1017 2. The package's maintainer scripts are removed
1018 3. It is noted in the status database as being in a sane state,
1019 namely not installed (any conffiles it may have are ignored,
1020 rather than being removed by dpkg). Note that disappearing
1021 packages do not have their prerm called, because dpkg doesn't
1022 know in advance that the package is going to vanish.
1028 install_data_files(pkg_t
*pkg
)
1032 /* opkg takes a slightly different approach to data file backups
1033 than dpkg. Rather than removing backups at this point, we
1034 actually do the data file installation now. See comments in
1035 check_data_file_clashes() for more details. */
1037 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1038 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1043 /* The "Essential" control field may only be present in the control
1044 * file and not in the Packages list. Ensure we capture it regardless.
1046 * XXX: This should be fixed outside of opkg, in the Package list.
1048 set_flags_from_control(pkg
) ;
1050 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1051 err
= pkg_write_filelist(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 resolve_conffiles(pkg_t
*pkg
)
1072 conffile_list_elt_t
*iter
;
1077 if (conf
->noaction
) return 0;
1079 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1080 char *root_filename
;
1081 cf
= (conffile_t
*)iter
->data
;
1082 root_filename
= root_filename_alloc(cf
->name
);
1084 /* Might need to initialize the md5sum for each conffile */
1085 if (cf
->value
== NULL
) {
1086 cf
->value
= file_md5sum_alloc(root_filename
);
1089 if (!file_exists(root_filename
)) {
1090 free(root_filename
);
1094 cf_backup
= backup_filename_alloc(root_filename
);
1096 if (file_exists(cf_backup
)) {
1097 /* Let's compute md5 to test if files are changed */
1098 md5sum
= file_md5sum_alloc(cf_backup
);
1099 if (md5sum
&& cf
->value
&& strcmp(cf
->value
,md5sum
) != 0 ) {
1100 if (conf
->force_maintainer
) {
1101 opkg_msg(NOTICE
, "Conffile %s using maintainer's setting.\n",
1105 sprintf_alloc(&new_conffile
, "%s-opkg", root_filename
);
1106 opkg_msg(ERROR
, "Existing conffile %s "
1107 "is different from the conffile in the new package."
1108 " The new conffile will be placed at %s.\n",
1109 root_filename
, new_conffile
);
1110 rename(root_filename
, new_conffile
);
1111 rename(cf_backup
, root_filename
);
1121 free(root_filename
);
1129 opkg_install_by_name(const char *pkg_name
)
1133 char *old_version
, *new_version
;
1135 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1137 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1140 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1142 opkg_msg(NOTICE
, "Unknown package '%s'.\n", pkg_name
);
1146 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1148 opkg_msg(DEBUG2
, " old %s ", old
->version
);
1149 opkg_msg(DEBUG2
, " new %s\n", new->version
);
1151 new->state_flag
|= SF_USER
;
1153 old_version
= pkg_version_str_alloc(old
);
1154 new_version
= pkg_version_str_alloc(new);
1156 cmp
= pkg_compare_versions(old
, new);
1157 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
1158 opkg_msg(DEBUG
, "Forcing downgrade\n");
1159 cmp
= -1 ; /* then we force opkg to downgrade */
1160 /* We need to use a value < 0 because in the 0 case we are asking to */
1161 /* reinstall, and some check could fail asking the "force-reinstall" option */
1163 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1164 "\n\t%s is installed "
1165 "\n\t%s is available "
1166 "\n\t%d was comparison result\n",
1167 pkg_name
, old_version
, new_version
, cmp
);
1168 if (cmp
== 0 && !conf
->force_reinstall
) {
1170 "Package %s (%s) installed in %s is up to date.\n",
1171 old
->name
, old_version
, old
->dest
->name
);
1175 } else if (cmp
> 0) {
1177 "Not downgrading package %s on %s from %s to %s.\n",
1178 old
->name
, old
->dest
->name
, old_version
, new_version
);
1182 } else if (cmp
< 0) {
1183 new->dest
= old
->dest
;
1184 old
->state_want
= SW_DEINSTALL
; /* Here probably the problem for bug 1277 */
1190 opkg_msg(DEBUG2
,"Calling opkg_install_pkg.\n");
1191 return opkg_install_pkg(new, 0);
1195 * @brief Really install a pkg_t
1198 opkg_install_pkg(pkg_t
*pkg
, int from_upgrade
)
1202 pkg_t
*old_pkg
= NULL
;
1203 pkg_vec_t
*replacees
;
1204 abstract_pkg_t
*ab_pkg
= NULL
;
1210 sigset_t newset
, oldset
;
1213 message
= 1; /* Coming from an upgrade, and should change the output message */
1215 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1217 if (!pkg_arch_supported(pkg
)) {
1218 opkg_msg(ERROR
, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1219 pkg
->architecture
, pkg
->name
);
1222 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0 && conf
->nodeps
== 0) {
1223 err
= satisfy_dependencies_for(pkg
);
1227 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1228 pkg
->name
, pkg
->dest
->name
);
1232 if (pkg
->dest
== NULL
) {
1233 pkg
->dest
= conf
->default_dest
;
1236 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1238 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1242 pkg
->state_want
= SW_INSTALL
;
1244 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependencies */
1247 err
= check_conflicts_for(pkg
);
1251 /* this setup is to remove the upgrade scenario in the end when
1252 installing pkg A, A deps B & B deps on A. So both B and A are
1253 installed. Then A's installation is started resulting in an
1254 uncecessary upgrade */
1255 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0)
1258 err
= verify_pkg_installable(pkg
);
1262 if (pkg
->local_filename
== NULL
) {
1263 if(!conf
->cache
&& conf
->download_only
){
1265 if(getcwd(cwd
, sizeof(cwd
)) != NULL
)
1266 err
= opkg_download_pkg(pkg
, cwd
);
1270 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1273 opkg_msg(ERROR
, "Failed to download %s. "
1274 "Perhaps you need to run 'opkg update'?\n",
1280 /* check that the repository is valid */
1281 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1282 char *list_file_name
, *sig_file_name
, *lists_dir
;
1284 /* check to ensure the package has come from a repository */
1285 if (conf
->check_signature
&& pkg
->src
)
1287 sprintf_alloc (&lists_dir
, "%s",
1288 (conf
->restrict_to_default_dest
)
1289 ? conf
->default_dest
->lists_dir
1291 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, pkg
->src
->name
);
1292 sprintf_alloc (&sig_file_name
, "%s/%s.sig", lists_dir
, pkg
->src
->name
);
1294 if (file_exists (sig_file_name
))
1296 if (opkg_verify_file (list_file_name
, sig_file_name
)){
1297 opkg_msg(ERROR
, "Failed to verify the signature of %s.\n",
1302 opkg_msg(ERROR
, "Signature file is missing for %s. "
1303 "Perhaps you need to run 'opkg update'?\n",
1309 free (list_file_name
);
1310 free (sig_file_name
);
1314 /* Check for md5 values */
1317 file_md5
= file_md5sum_alloc(pkg
->local_filename
);
1318 if (file_md5
&& strcmp(file_md5
, pkg
->md5sum
))
1320 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1321 "Either the opkg or the package index are corrupt. "
1322 "Try 'opkg update'.\n",
1332 /* Check for sha256 value */
1335 file_sha256
= file_sha256sum_alloc(pkg
->local_filename
);
1336 if (file_sha256
&& strcmp(file_sha256
, pkg
->sha256sum
))
1338 opkg_msg(ERROR
, "Package %s sha256sum mismatch. "
1339 "Either the opkg or the package index are corrupt. "
1340 "Try 'opkg update'.\n",
1349 if(conf
->download_only
) {
1350 if (conf
->nodeps
== 0) {
1351 err
= satisfy_dependencies_for(pkg
);
1358 if (pkg
->tmp_unpack_dir
== NULL
) {
1359 if (unpack_pkg_control_files(pkg
) == -1) {
1360 opkg_msg(ERROR
, "Failed to unpack control files from %s.\n",
1361 pkg
->local_filename
);
1366 err
= update_file_ownership(pkg
, old_pkg
);
1370 if (conf
->nodeps
== 0) {
1371 err
= satisfy_dependencies_for(pkg
);
1374 if (pkg
->state_status
== SS_UNPACKED
)
1375 /* Circular dependency has installed it for us. */
1379 replacees
= pkg_vec_alloc();
1380 pkg_get_installed_replacees(pkg
, replacees
);
1382 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1384 sigemptyset(&newset
);
1385 sigaddset(&newset
, SIGINT
);
1386 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1388 opkg_state_changed
++;
1389 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1392 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1394 /* XXX: BUG: we really should treat replacement more like an upgrade
1395 * Instead, we're going to remove the replacees
1397 err
= pkg_remove_installed_replacees(replacees
);
1399 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1401 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1403 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1405 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1407 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1409 err
= preinst_configure(pkg
, old_pkg
);
1411 goto UNWIND_PREINST_CONFIGURE
;
1413 err
= backup_modified_conffiles(pkg
, old_pkg
);
1415 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1417 err
= check_data_file_clashes(pkg
, old_pkg
);
1419 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1421 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1423 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1428 /* point of no return: no unwinding after this */
1429 if (old_pkg
&& !conf
->force_reinstall
) {
1430 old_pkg
->state_want
= SW_DEINSTALL
;
1432 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1433 opkg_msg(INFO
, "Not removing obsolesced files because "
1434 "package %s marked noprune.\n",
1437 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1439 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1440 opkg_msg(ERROR
, "Failed to determine "
1441 "obsolete files from previously "
1442 "installed %s\n", old_pkg
->name
);
1446 /* removing files from old package, to avoid ghost files */
1447 remove_data_files_and_list(old_pkg
);
1448 remove_maintainer_scripts(old_pkg
);
1452 opkg_msg(INFO
, "Installing maintainer scripts.\n");
1453 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1454 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1455 " Package debris may remain!\n",
1460 /* the following just returns 0 */
1461 remove_disappeared(pkg
);
1463 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1465 if (install_data_files(pkg
)) {
1466 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1467 "Package debris may remain!\n",
1472 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1474 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1475 "for files belonging to %s.\n",
1479 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1480 resolve_conffiles(pkg
);
1482 pkg
->state_status
= SS_UNPACKED
;
1483 old_state_flag
= pkg
->state_flag
;
1484 pkg
->state_flag
&= ~SF_PREFER
;
1485 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1486 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1488 if (old_pkg
&& !conf
->force_reinstall
) {
1489 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1492 time(&pkg
->installed_time
);
1494 ab_pkg
= pkg
->parent
;
1496 ab_pkg
->state_status
= pkg
->state_status
;
1498 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1499 pkg_vec_free (replacees
);
1503 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1504 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1505 UNWIND_CHECK_DATA_FILE_CLASHES
:
1506 check_data_file_clashes_unwind(pkg
, old_pkg
);
1507 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1508 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1509 UNWIND_PREINST_CONFIGURE
:
1510 preinst_configure_unwind(pkg
, old_pkg
);
1511 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1512 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1513 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1514 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1515 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1516 pkg_remove_installed_replacees_unwind(replacees
);
1519 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1521 pkg_vec_free (replacees
);