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.
26 #include "pkg_extract.h"
28 #include "opkg_install.h"
29 #include "opkg_configure.h"
30 #include "opkg_download.h"
31 #include "opkg_remove.h"
33 #include "opkg_utils.h"
34 #include "opkg_message.h"
36 #include "opkg_defines.h"
38 #include "sprintf_alloc.h"
39 #include "file_util.h"
41 #include "libbb/libbb.h"
43 static pkg_vec_t
* check_dependencies_for(pkg_t
*pkg
, bool silent
)
45 char **tmp
, **unresolved
= NULL
, *prev
= NULL
;
46 pkg_vec_t
*depends
= pkg_vec_alloc();
48 pkg_hash_fetch_unsatisfied_dependencies(pkg
, depends
, &unresolved
);
53 "Cannot satisfy the following dependencies for %s:\n",
57 if (!prev
|| strcmp(*unresolved
, prev
))
59 opkg_message(ERROR
, "\t%s\n", *unresolved
);
69 pkg_vec_free(depends
);
76 static int satisfy_dependencies_for(pkg_t
* pkg
)
82 depends
= check_dependencies_for(pkg
, false);
84 if (depends
== NULL
) {
85 if (!conf
->force_depends
) {
87 "This could mean that your package list is out of date or that the packages\n"
88 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
89 "of this problem try again with the '-force-depends' option.\n");
95 /* Mark packages as to-be-installed */
96 for (i
= 0; i
< depends
->len
; i
++) {
97 /* Dependencies should be installed the same place as pkg */
98 if (depends
->pkgs
[i
]->dest
== NULL
) {
99 depends
->pkgs
[i
]->dest
= pkg
->dest
;
101 depends
->pkgs
[i
]->state_want
= SW_INSTALL
;
104 for (i
= 0; i
< depends
->len
; i
++) {
105 dep
= depends
->pkgs
[i
];
106 /* The package was uninstalled when we started, but another
107 dep earlier in this loop may have depended on it and pulled
108 it in, so check first. */
109 if ((dep
->state_status
!= SS_INSTALLED
)
110 && (dep
->state_status
!= SS_UNPACKED
)) {
111 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
112 err
= opkg_install_pkg(dep
, 0);
113 /* mark this package as having been automatically installed to
114 * satisfy a dependancy */
115 dep
->auto_installed
= 1;
117 pkg_vec_free(depends
);
123 pkg_vec_free(depends
);
128 static int check_conflicts_for(pkg_t
* pkg
)
131 pkg_vec_t
*conflicts
= NULL
;
132 message_level_t level
;
134 if (conf
->force_depends
) {
140 if (!conf
->force_depends
)
141 conflicts
= pkg_hash_fetch_conflicts(pkg
);
144 opkg_msg(level
, "The following packages conflict with %s:\n",
147 while (i
< conflicts
->len
)
148 opkg_msg(level
, "\t%s", conflicts
->pkgs
[i
++]->name
);
149 opkg_message(level
, "\n");
150 pkg_vec_free(conflicts
);
156 static int check_reverse_depends_for(pkg_t
*pkg
, pkg_t
*old_pkg
)
160 abstract_pkg_t
**dependents
, *dependent
;
164 opkg_msg(DEBUG
, "Checking reverse depends for %s\n", old_pkg
->name
);
166 n_dependents
= pkg_has_installed_dependents(old_pkg
, &dependents
);
168 /* pretend that old package does not exist */
169 old_pkg
->state_flag
|= SF_IGNORE
;
171 if (n_dependents
== 0)
174 while ((dependent
= *dependents
++) != NULL
) {
175 if (dependent
->state_status
!= SS_INSTALLED
)
178 if (dependent
->pkgs
== NULL
)
181 for (i
= 0; i
< dependent
->pkgs
->len
; i
++) {
182 if (dependent
->pkgs
->pkgs
[i
]->state_status
!= SS_INSTALLED
)
185 depends
= check_dependencies_for(dependent
->pkgs
->pkgs
[i
], true);
186 if (depends
== NULL
) {
188 updated
= pkg_hash_fetch_best_installation_candidate_by_name(dependent
->pkgs
->pkgs
[i
]->name
);
189 depends
= updated
? check_dependencies_for(updated
, false) : NULL
;
190 if (depends
== NULL
) {
191 opkg_msg(ERROR
, "Package %s reverse dependency check failed\n",
197 err
= opkg_install_pkg(updated
, 1);
202 opkg_msg(ERROR
, "Package %s reverse dependency check failed\n", pkg
->name
);
208 pkg_vec_free(depends
);
213 old_pkg
->state_flag
&= ~SF_IGNORE
;
217 static int update_file_ownership(pkg_t
* new_pkg
, pkg_t
* old_pkg
)
219 str_list_t
*new_list
, *old_list
;
220 str_list_elt_t
*iter
, *niter
;
222 new_list
= pkg_get_installed_files(new_pkg
);
223 if (new_list
== NULL
)
226 for (iter
= str_list_first(new_list
), niter
=
227 str_list_next(new_list
, iter
); iter
;
228 iter
= niter
, niter
= str_list_next(new_list
, niter
)) {
229 char *new_file
= (char *)iter
->data
;
230 pkg_t
*owner
= file_hash_get_file_owner(new_file
);
231 pkg_t
*obs
= hash_table_get(&conf
->obs_file_hash
, new_file
);
234 "%s: new_pkg=%s wants file %s, from owner=%s\n",
235 __func__
, new_pkg
->name
, new_file
,
236 owner
? owner
->name
: "<NULL>");
238 if (!owner
|| (owner
== old_pkg
) || obs
)
239 file_hash_set_file_owner(new_file
, new_pkg
);
243 old_list
= pkg_get_installed_files(old_pkg
);
244 if (old_list
== NULL
) {
245 pkg_free_installed_files(new_pkg
);
249 for (iter
= str_list_first(old_list
), niter
=
250 str_list_next(old_list
, iter
); iter
;
251 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
252 char *old_file
= (char *)iter
->data
;
253 pkg_t
*owner
= file_hash_get_file_owner(old_file
);
254 if (!owner
|| (owner
== old_pkg
)) {
256 hash_table_insert(&conf
->obs_file_hash
,
260 pkg_free_installed_files(old_pkg
);
262 pkg_free_installed_files(new_pkg
);
266 static int verify_pkg_installable(pkg_t
* pkg
)
268 unsigned long kbs_available
, pkg_size_kbs
;
269 unsigned long installed_size
;
270 char *root_dir
= NULL
;
273 installed_size
= (unsigned long) pkg_get_int(pkg
, PKG_INSTALLED_SIZE
);
275 if (conf
->force_space
|| installed_size
== 0)
279 if (!strcmp(pkg
->dest
->name
, "root") && conf
->overlay_root
280 && !stat(conf
->overlay_root
, &s
) && (s
.st_mode
& S_IFDIR
))
281 root_dir
= conf
->overlay_root
;
283 root_dir
= pkg
->dest
->root_dir
;
287 root_dir
= conf
->default_dest
->root_dir
;
289 kbs_available
= get_available_kbytes(root_dir
);
291 pkg_size_kbs
= (installed_size
+ 1023) / 1024;
293 if (pkg_size_kbs
>= kbs_available
) {
294 opkg_msg(ERROR
, "Only have %ldkb available on filesystem %s, "
295 "pkg %s needs %ld\n",
296 kbs_available
, root_dir
, pkg
->name
, pkg_size_kbs
);
303 static int unpack_pkg_control_files(pkg_t
* pkg
)
306 char *conffiles_file_name
;
308 char *tmp_unpack_dir
;
309 FILE *conffiles_file
;
312 sprintf_alloc(&tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
,
315 tmp_unpack_dir
= mkdtemp(tmp_unpack_dir
);
316 if (tmp_unpack_dir
== NULL
) {
317 opkg_perror(ERROR
, "Failed to create temporary directory '%s'",
322 pkg_set_string(pkg
, PKG_TMP_UNPACK_DIR
, tmp_unpack_dir
);
324 err
= pkg_extract_control_files_to_dir(pkg
, tmp_unpack_dir
);
329 /* XXX: CLEANUP: There might be a cleaner place to read in the
330 conffiles. Seems like I should be able to get everything to go
331 through pkg_init_from_file. If so, maybe it would make sense to
332 move all of unpack_pkg_control_files to that function. */
334 /* Don't need to re-read conffiles if we already have it */
335 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
336 if (cl
&& !nv_pair_list_empty(cl
)) {
340 sprintf_alloc(&conffiles_file_name
, "%s/conffiles",
342 if (!file_exists(conffiles_file_name
)) {
343 free(conffiles_file_name
);
347 conffiles_file
= fopen(conffiles_file_name
, "r");
348 if (conffiles_file
== NULL
) {
349 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
350 free(conffiles_file_name
);
353 free(conffiles_file_name
);
355 cl
= xcalloc(1, sizeof(*cl
));
356 conffile_list_init(cl
);
360 char *cf_name_in_dest
;
363 cf_name
= file_read_line_alloc(conffiles_file
);
364 if (cf_name
== NULL
) {
367 if (cf_name
[0] == '\0') {
370 for (i
= strlen(cf_name
) - 1;
371 (i
>= 0) && (cf_name
[i
] == ' ' || cf_name
[i
] == '\t');
376 /* Prepend dest->root_dir to conffile name.
377 Take pains to avoid multiple slashes. */
378 root_dir
= pkg
->dest
->root_dir
;
379 if (conf
->offline_root
)
380 /* skip the offline_root prefix */
382 pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
383 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
384 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
386 /* Can't get an md5sum now, (file isn't extracted yet).
387 We'll wait until resolve_conffiles */
388 conffile_list_append(cl
, cf_name_in_dest
, NULL
);
391 free(cf_name_in_dest
);
394 pkg_set_ptr(pkg
, PKG_CONFFILES
, cl
);
396 fclose(conffiles_file
);
402 * Remove packages which were auto_installed due to a dependency by old_pkg,
403 * which are no longer a dependency in the new (upgraded) pkg.
405 static int pkg_remove_orphan_dependent(pkg_t
* pkg
, pkg_t
* old_pkg
)
407 int j
, l
, found
, r
, err
= 0;
410 struct compound_depend
*cd0
, *cd1
;
411 abstract_pkg_t
**dependents
;
413 for (cd0
= pkg_get_ptr(old_pkg
, PKG_DEPENDS
); cd0
&& cd0
->type
; cd0
++) {
414 if (cd0
->type
!= DEPEND
)
416 for (j
= 0; j
< cd0
->possibility_count
; j
++) {
420 for (cd1
= pkg_get_ptr(pkg
, PKG_DEPENDS
); cd1
&& cd1
->type
; cd1
++) {
421 if (cd1
->type
!= DEPEND
)
423 for (l
= 0; l
< cd1
->possibility_count
; l
++) {
424 if (cd0
->possibilities
[j
]
425 == cd1
->possibilities
[l
]) {
438 * old_pkg has a dependency that pkg does not.
440 p
= pkg_hash_fetch_installed_by_name(cd0
->
447 if (!p
->auto_installed
)
450 n_deps
= pkg_has_installed_dependents(p
, &dependents
);
451 n_deps
--; /* don't count old_pkg */
454 opkg_msg(NOTICE
, "%s was autoinstalled and is "
455 "now orphaned, removing.\n", p
->name
);
457 /* p has one installed dependency (old_pkg),
458 * which we need to ignore during removal. */
459 p
->state_flag
|= SF_REPLACE
;
461 r
= opkg_remove_pkg(p
, 0);
465 opkg_msg(INFO
, "%s was autoinstalled and is "
466 "still required by %d "
467 "installed packages.\n",
476 /* returns number of installed replacees */
478 pkg_get_installed_replacees(pkg_t
* pkg
, pkg_vec_t
* installed_replacees
)
480 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
483 while (replaces
&& *replaces
) {
484 abstract_pkg_t
*ab_pkg
= *replaces
++;
485 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
487 for (j
= 0; j
< pkg_vec
->len
; j
++) {
488 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
489 if (!pkg_conflicts(pkg
, replacee
))
491 if (replacee
->state_status
== SS_INSTALLED
) {
492 pkg_vec_insert(installed_replacees
,
499 return installed_replacees
->len
;
502 static int pkg_remove_installed_replacees(pkg_vec_t
* replacees
)
505 int replaces_count
= replacees
->len
;
506 for (i
= 0; i
< replaces_count
; i
++) {
507 pkg_t
*replacee
= replacees
->pkgs
[i
];
509 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
510 err
= opkg_remove_pkg(replacee
, 0);
517 /* to unwind the removal: make sure they are installed */
518 static int pkg_remove_installed_replacees_unwind(pkg_vec_t
* replacees
)
521 int replaces_count
= replacees
->len
;
522 for (i
= 0; i
< replaces_count
; i
++) {
523 pkg_t
*replacee
= replacees
->pkgs
[i
];
524 if (replacee
->state_status
!= SS_INSTALLED
) {
525 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
526 err
= opkg_install_pkg(replacee
, 0);
534 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
536 opkg_install_check_downgrade(pkg_t
* pkg
, pkg_t
* old_pkg
, int message
)
539 char message_out
[15];
540 char *old_version
= pkg_version_str_alloc(old_pkg
);
541 char *new_version
= pkg_version_str_alloc(pkg
);
542 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
545 memset(message_out
, '\x0', 15);
546 strncpy(message_out
, "Upgrading ", strlen("Upgrading "));
547 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
548 cmp
= -1; /* then we force opkg to downgrade */
549 strncpy(message_out
, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
550 /* reinstall, and some check could fail asking the "force-reinstall" option */
554 if (!conf
->download_only
)
556 "Not downgrading package %s on %s from %s to %s.\n",
557 old_pkg
->name
, old_pkg
->dest
->name
,
558 old_version
, new_version
);
560 } else if (cmp
< 0) {
561 if (!conf
->download_only
)
563 "%s%s on %s from %s to %s...\n",
564 message_out
, pkg
->name
,
565 old_pkg
->dest
->name
, old_version
,
567 pkg
->dest
= old_pkg
->dest
;
569 } else { /* cmp == 0 */
571 if (!conf
->download_only
)
573 "%s (%s) already install on %s.\n",
574 pkg
->name
, new_version
,
575 old_pkg
->dest
->name
);
582 char message_out
[15];
583 memset(message_out
, '\x0', 15);
585 strncpy(message_out
, "Upgrading ",
586 strlen("Upgrading "));
588 strncpy(message_out
, "Installing ",
589 strlen("Installing "));
590 char *version
= pkg_version_str_alloc(pkg
);
592 if (!conf
->download_only
)
593 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
594 pkg
->name
, version
, pkg
->dest
->name
);
600 static int prerm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
602 /* DPKG_INCOMPATIBILITY:
603 dpkg does some things here that we don't do yet. Do we care?
605 1. If a version of the package is already installed, call
606 old-prerm upgrade new-version
607 2. If the script runs but exits with a non-zero exit status
608 new-prerm failed-upgrade old-version
609 Error unwind, for both the above cases:
610 old-postinst abort-upgrade new-version
616 if (!old_pkg
|| !pkg
)
619 new_version
= pkg_version_str_alloc(pkg
);
621 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
623 err
= pkg_run_script(old_pkg
, "prerm", script_args
);
626 opkg_msg(ERROR
, "prerm script for package \"%s\" failed\n",
633 static int prerm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
635 /* DPKG_INCOMPATIBILITY:
636 dpkg does some things here that we don't do yet. Do we care?
637 (See prerm_upgrade_old_package for details)
642 static int prerm_deconfigure_conflictors(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
644 /* DPKG_INCOMPATIBILITY:
645 dpkg does some things here that we don't do yet. Do we care?
646 2. If a 'conflicting' package is being removed at the same time:
647 1. If any packages depended on that conflicting package and
648 --auto-deconfigure is specified, call, for each such package:
649 deconfigured's-prerm deconfigure \
650 in-favour package-being-installed version \
651 removing conflicting-package version
653 deconfigured's-postinst abort-deconfigure \
654 in-favour package-being-installed-but-failed version \
655 removing conflicting-package version
657 The deconfigured packages are marked as requiring
658 configuration, so that if --install is used they will be
659 configured again if possible.
660 2. To prepare for removal of the conflicting package, call:
661 conflictor's-prerm remove in-favour package new-version
663 conflictor's-postinst abort-remove in-favour package new-version
669 prerm_deconfigure_conflictors_unwind(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
671 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
672 do yet. Do we care? (See prerm_deconfigure_conflictors for
677 static int preinst_configure(pkg_t
* pkg
, pkg_t
* old_pkg
)
683 char *old_version
= pkg_version_str_alloc(old_pkg
);
684 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
686 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
687 char *pkg_version
= pkg_version_str_alloc(pkg
);
688 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
691 preinst_args
= xstrdup("install");
694 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
696 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
705 static int preinst_configure_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
707 /* DPKG_INCOMPATIBILITY:
708 dpkg does the following error unwind, should we?
709 pkg->postrm abort-upgrade old-version
710 OR pkg->postrm abort-install old-version
711 OR pkg->postrm abort-install
716 static char *backup_filename_alloc(const char *file_name
)
720 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
725 static int backup_make_backup(const char *file_name
)
730 backup
= backup_filename_alloc(file_name
);
731 err
= file_copy(file_name
, backup
);
733 opkg_msg(ERROR
, "Failed to copy %s to %s\n", file_name
, backup
);
741 static int backup_exists_for(const char *file_name
)
746 backup
= backup_filename_alloc(file_name
);
748 ret
= file_exists(backup
);
755 static int backup_remove(const char *file_name
)
759 backup
= backup_filename_alloc(file_name
);
766 static int backup_modified_conffiles(pkg_t
* pkg
, pkg_t
* old_pkg
)
769 conffile_list_elt_t
*iter
;
776 /* Backup all modified conffiles */
778 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
780 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
781 iter
= nv_pair_list_next(cl
, iter
)) {
785 cf_name
= root_filename_alloc(cf
->name
);
787 /* Don't worry if the conffile is just plain gone */
788 if (file_exists(cf_name
)
789 && conffile_has_been_modified(cf
)) {
790 err
= backup_make_backup(cf_name
);
799 /* Backup all conffiles that were not conffiles in old_pkg */
800 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
802 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
803 iter
= nv_pair_list_next(cl
, iter
)) {
805 cf
= (conffile_t
*) iter
->data
;
806 cf_name
= root_filename_alloc(cf
->name
);
807 /* Ignore if this was a conffile in old_pkg as well */
808 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
812 if (file_exists(cf_name
) && (!backup_exists_for(cf_name
))) {
813 err
= backup_make_backup(cf_name
);
824 static int backup_modified_conffiles_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
827 conffile_list_elt_t
*iter
;
830 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
832 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
833 iter
= nv_pair_list_next(cl
, iter
)) {
834 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
838 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
840 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
841 iter
= nv_pair_list_next(cl
, iter
)) {
842 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
848 static int check_data_file_clashes(pkg_t
* pkg
, pkg_t
* old_pkg
)
850 /* DPKG_INCOMPATIBILITY:
851 opkg takes a slightly different approach than dpkg at this
852 point. dpkg installs each file in the new package while
853 creating a backup for any file that is replaced, (so that it
854 can unwind if necessary). To avoid complexity and redundant
855 storage, opkg doesn't do any installation until later, (at the
856 point at which dpkg removes the backups.
858 But, we do have to check for data file clashes, since after
859 installing a package with a file clash, removing either of the
860 packages involved in the clash has the potential to break the
863 str_list_t
*files_list
;
864 str_list_elt_t
*iter
, *niter
;
868 files_list
= pkg_get_installed_files(pkg
);
869 if (files_list
== NULL
)
872 for (iter
= str_list_first(files_list
), niter
=
873 str_list_next(files_list
, iter
); iter
;
874 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
875 filename
= (char *)iter
->data
;
876 if (file_exists(filename
) && (!file_is_dir(filename
))) {
880 if (backup_exists_for(filename
)) {
884 /* Pre-existing files are OK if force-overwrite was asserted. */
885 if (conf
->force_overwrite
) {
886 /* but we need to change who owns this file */
887 file_hash_set_file_owner(filename
, pkg
);
891 owner
= file_hash_get_file_owner(filename
);
893 /* Pre-existing files are OK if owned by the pkg being upgraded. */
894 if (owner
&& old_pkg
) {
895 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
900 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
903 "Checking replaces for %s in package %s\n",
904 filename
, owner
->name
);
905 if (pkg_replaces(pkg
, owner
)) {
908 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
909 then it's ok to overwrite. */
910 if (strcmp(owner
->name
, pkg
->name
) == 0) {
912 "Replacing pre-existing file %s"
913 " owned by package %s\n",
914 filename
, owner
->name
);
919 /* Pre-existing files are OK if they are obsolete */
920 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
923 "Pre-exiting file %s is obsolete."
924 " obs_pkg=%s\n", filename
, obs
->name
);
928 /* We have found a clash. */
929 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
930 "\tBut that file is already provided by package ",
931 pkg
->name
, filename
);
933 opkg_message(ERROR
, "%s\n", owner
->name
);
935 opkg_message(ERROR
, "<no package>\n"
936 "Please move this file out of the way and try again.\n");
941 pkg_free_installed_files(pkg
);
947 * XXX: This function sucks, as does the below comment.
949 static int check_data_file_clashes_change(pkg_t
* pkg
, pkg_t
* old_pkg
)
951 /* Basically that's the worst hack I could do to be able to change ownership of
952 file list, but, being that we have no way to unwind the mods, due to structure
953 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
954 What we do here is change the ownership of file in hash if a replace ( or similar events
956 Only the action that are needed to change name should be considered.
957 @@@ To change after 1.0 release.
959 str_list_t
*files_list
;
960 str_list_elt_t
*iter
, *niter
;
962 files_list
= pkg_get_installed_files(pkg
);
963 if (files_list
== NULL
)
966 for (iter
= str_list_first(files_list
), niter
=
967 str_list_next(files_list
, iter
); iter
;
968 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
969 char *filename
= (char *)iter
->data
;
970 if (file_exists(filename
) && (!file_is_dir(filename
))) {
973 owner
= file_hash_get_file_owner(filename
);
975 if (conf
->force_overwrite
) {
976 /* but we need to change who owns this file */
977 file_hash_set_file_owner(filename
, pkg
);
981 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
983 if (pkg_replaces(pkg
, owner
)) {
984 /* It's now time to change the owner of that file.
985 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
987 "Replacing pre-existing file %s "
988 "owned by package %s\n",
989 filename
, owner
->name
);
990 file_hash_set_file_owner(filename
, pkg
);
997 pkg_free_installed_files(pkg
);
1002 static int check_data_file_clashes_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
1004 /* Nothing to do since check_data_file_clashes doesn't change state */
1008 static int postrm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
1010 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
1011 1. If the package is being upgraded, call
1012 old-postrm upgrade new-version
1013 2. If this fails, attempt:
1014 new-postrm failed-upgrade old-version
1015 Error unwind, for both cases:
1016 old-preinst abort-upgrade new-version */
1021 if (!old_pkg
|| !pkg
)
1024 new_version
= pkg_version_str_alloc(pkg
);
1026 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
1028 err
= pkg_run_script(old_pkg
, "postrm", script_args
);
1031 opkg_msg(ERROR
, "postrm script for package \"%s\" failed\n",
1038 static int postrm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
1040 /* DPKG_INCOMPATIBILITY:
1041 dpkg does some things here that we don't do yet. Do we care?
1042 (See postrm_upgrade_old_pkg for details)
1047 static int remove_obsolesced_files(pkg_t
* pkg
, pkg_t
* old_pkg
)
1050 str_list_t
*old_files
;
1052 str_list_t
*new_files
;
1054 hash_table_t new_files_table
;
1056 old_files
= pkg_get_installed_files(old_pkg
);
1057 if (old_files
== NULL
)
1060 new_files
= pkg_get_installed_files(pkg
);
1061 if (new_files
== NULL
) {
1062 pkg_free_installed_files(old_pkg
);
1066 new_files_table
.entries
= NULL
;
1067 hash_table_init("new_files", &new_files_table
, 20);
1068 for (nf
= str_list_first(new_files
); nf
;
1069 nf
= str_list_next(new_files
, nf
)) {
1071 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
1074 for (of
= str_list_first(old_files
); of
;
1075 of
= str_list_next(old_files
, of
)) {
1078 old
= (char *)of
->data
;
1079 new = (char *)hash_table_get(&new_files_table
, old
);
1083 if (file_is_dir(old
)) {
1086 owner
= file_hash_get_file_owner(old
);
1087 if (owner
!= old_pkg
) {
1088 /* in case obsolete file no longer belongs to old_pkg */
1092 /* old file is obsolete */
1093 opkg_msg(NOTICE
, "Removing obsolete file %s.\n", old
);
1094 if (!conf
->noaction
) {
1097 opkg_perror(ERROR
, "unlinking %s failed", old
);
1102 hash_table_deinit(&new_files_table
);
1103 pkg_free_installed_files(old_pkg
);
1104 pkg_free_installed_files(pkg
);
1109 static int install_maintainer_scripts(pkg_t
* pkg
, pkg_t
* old_pkg
)
1114 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1115 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1116 pkg
->dest
->info_dir
,
1122 static int remove_disappeared(pkg_t
* pkg
)
1124 /* DPKG_INCOMPATIBILITY:
1125 This is a fairly sophisticated dpkg operation. Shall we
1128 /* Any packages all of whose files have been overwritten during the
1129 installation, and which aren't required for dependencies, are
1130 considered to have been removed. For each such package
1131 1. disappearer's-postrm disappear overwriter overwriter-version
1132 2. The package's maintainer scripts are removed
1133 3. It is noted in the status database as being in a sane state,
1134 namely not installed (any conffiles it may have are ignored,
1135 rather than being removed by dpkg). Note that disappearing
1136 packages do not have their prerm called, because dpkg doesn't
1137 know in advance that the package is going to vanish.
1142 static int install_data_files(pkg_t
* pkg
)
1146 /* opkg takes a slightly different approach to data file backups
1147 than dpkg. Rather than removing backups at this point, we
1148 actually do the data file installation now. See comments in
1149 check_data_file_clashes() for more details. */
1151 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1152 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1157 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1158 err
= pkg_write_filelist(pkg
);
1162 /* XXX: FEATURE: opkg should identify any files which existed
1163 before installation and which were overwritten, (see
1164 check_data_file_clashes()). What it must do is remove any such
1165 files from the filelist of the old package which provided the
1166 file. Otherwise, if the old package were removed at some point
1167 it would break the new package. Removing the new package will
1168 also break the old one, but this cannot be helped since the old
1169 package's file has already been deleted. This is the importance
1170 of check_data_file_clashes(), and only allowing opkg to install
1171 a clashing package with a user force. */
1176 static int resolve_conffiles(pkg_t
* pkg
)
1178 conffile_list_elt_t
*iter
;
1179 conffile_list_t
*cl
;
1187 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
1189 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
1190 iter
= nv_pair_list_next(cl
, iter
)) {
1191 char *root_filename
;
1192 cf
= (conffile_t
*) iter
->data
;
1193 root_filename
= root_filename_alloc(cf
->name
);
1195 /* Might need to initialize the md5sum for each conffile */
1196 if (cf
->value
== NULL
) {
1197 cf
->value
= file_sha256sum_alloc(root_filename
);
1200 if (!file_exists(root_filename
)) {
1201 free(root_filename
);
1205 cf_backup
= backup_filename_alloc(root_filename
);
1207 if (file_exists(cf_backup
)) {
1208 /* Let's compute md5 to test if files are changed */
1209 if (cf
->value
&& strlen(cf
->value
) > 33) {
1210 chksum
= file_sha256sum_alloc(cf_backup
);
1212 chksum
= file_md5sum_alloc(cf_backup
);
1215 if (chksum
&& cf
->value
1216 && strcmp(cf
->value
, chksum
) != 0) {
1217 if (conf
->force_maintainer
) {
1219 "Conffile %s using maintainer's setting.\n",
1223 sprintf_alloc(&new_conffile
, "%s-opkg",
1226 "Existing conffile %s "
1227 "is different from the conffile in the new package."
1228 " The new conffile will be placed at %s.\n",
1229 root_filename
, new_conffile
);
1230 rename(root_filename
, new_conffile
);
1231 rename(cf_backup
, root_filename
);
1241 free(root_filename
);
1247 int opkg_install_by_name(const char *pkg_name
)
1251 char *old_version
, *new_version
;
1253 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1255 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1256 pkg_get_string(old
, PKG_VERSION
));
1258 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1260 opkg_msg(NOTICE
, "Unknown package '%s'.\n", pkg_name
);
1264 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1266 opkg_message(DEBUG2
, " old %s ", pkg_get_string(old
, PKG_VERSION
));
1267 opkg_message(DEBUG2
, " new %s\n", pkg_get_string(new, PKG_VERSION
));
1269 new->state_flag
|= SF_USER
;
1271 old_version
= pkg_version_str_alloc(old
);
1272 new_version
= pkg_version_str_alloc(new);
1274 cmp
= pkg_compare_versions(old
, new);
1275 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
1276 opkg_msg(DEBUG
, "Forcing downgrade\n");
1277 cmp
= -1; /* then we force opkg to downgrade */
1278 /* We need to use a value < 0 because in the 0 case we are asking to */
1279 /* reinstall, and some check could fail asking the "force-reinstall" option */
1281 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1282 "\n\t%s is installed "
1283 "\n\t%s is available "
1284 "\n\t%d was comparison result\n",
1285 pkg_name
, old_version
, new_version
, cmp
);
1288 "Package %s (%s) installed in %s is up to date.\n",
1289 old
->name
, old_version
, old
->dest
->name
);
1293 } else if (cmp
> 0) {
1295 "Not downgrading package %s on %s from %s to %s.\n",
1296 old
->name
, old
->dest
->name
, old_version
,
1301 } else if (cmp
< 0) {
1302 new->dest
= old
->dest
;
1303 old
->state_want
= SW_DEINSTALL
;
1309 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
1310 return opkg_install_pkg(new, 0);
1314 * @brief Really install a pkg_t
1316 int opkg_install_pkg(pkg_t
* pkg
, int from_upgrade
)
1320 pkg_t
*old_pkg
= NULL
;
1321 pkg_vec_t
*replacees
;
1322 abstract_pkg_t
*ab_pkg
= NULL
;
1324 char *file_md5
, *pkg_md5
;
1325 char *file_sha256
, *pkg_sha256
;
1326 sigset_t newset
, oldset
;
1327 const char *local_filename
;
1331 message
= 1; /* Coming from an upgrade, and should change the output message */
1333 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1335 if (!pkg_arch_supported(pkg
)) {
1337 "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1338 pkg_get_architecture(pkg
), pkg
->name
);
1341 if (pkg
->state_status
== SS_INSTALLED
&& conf
->nodeps
== 0) {
1342 err
= satisfy_dependencies_for(pkg
);
1346 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1347 pkg
->name
, pkg
->dest
->name
);
1351 if (pkg
->dest
== NULL
) {
1352 pkg
->dest
= conf
->default_dest
;
1355 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1357 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1361 pkg
->state_want
= SW_INSTALL
;
1363 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependencies */
1365 err
= check_reverse_depends_for(pkg
, old_pkg
);
1370 err
= check_conflicts_for(pkg
);
1374 /* this setup is to remove the upgrade scenario in the end when
1375 installing pkg A, A deps B & B deps on A. So both B and A are
1376 installed. Then A's installation is started resulting in an
1377 uncecessary upgrade */
1378 if (pkg
->state_status
== SS_INSTALLED
)
1381 err
= verify_pkg_installable(pkg
);
1385 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1387 if (local_filename
== NULL
) {
1388 if (!conf
->cache
&& conf
->download_only
) {
1390 if (getcwd(cwd
, sizeof(cwd
)) != NULL
)
1391 err
= opkg_download_pkg(pkg
, cwd
);
1395 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1398 opkg_msg(ERROR
, "Failed to download %s. "
1399 "Perhaps you need to run 'opkg update'?\n",
1404 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1407 /* check that the repository is valid */
1408 #if defined(HAVE_USIGN)
1409 char *list_file_name
, *sig_file_name
, *lists_dir
;
1411 /* check to ensure the package has come from a repository */
1412 if (conf
->check_signature
&& pkg
->src
) {
1413 sprintf_alloc(&lists_dir
, "%s", (conf
->restrict_to_default_dest
)
1414 ? conf
->default_dest
->lists_dir
1416 sprintf_alloc(&list_file_name
, "%s/%s", lists_dir
,
1418 sprintf_alloc(&sig_file_name
, "%s/%s.sig", lists_dir
,
1421 if (file_exists(sig_file_name
)) {
1422 if (opkg_verify_file(list_file_name
, sig_file_name
)) {
1424 "Failed to verify the signature of %s.\n",
1426 if (!conf
->force_signature
)
1430 opkg_msg(ERROR
, "Signature file is missing for %s. "
1431 "Perhaps you need to run 'opkg update'?\n",
1433 if (!conf
->force_signature
)
1438 free(list_file_name
);
1439 free(sig_file_name
);
1443 /* Check for md5 values */
1444 pkg_md5
= pkg_get_md5(pkg
);
1446 file_md5
= file_md5sum_alloc(local_filename
);
1447 if (file_md5
&& strcmp(file_md5
, pkg_md5
)) {
1448 if (!conf
->force_checksum
) {
1449 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1450 "Either the opkg or the package index are corrupt. "
1451 "Try 'opkg update'.\n", pkg
->name
);
1456 "Ignored %s md5sum mismatch.\n",
1464 /* Check for sha256 value */
1465 pkg_sha256
= pkg_get_sha256(pkg
);
1467 file_sha256
= file_sha256sum_alloc(local_filename
);
1468 if (file_sha256
&& strcmp(file_sha256
, pkg_sha256
)) {
1469 if (!conf
->force_checksum
) {
1471 "Package %s sha256sum mismatch. "
1472 "Either the opkg or the package index are corrupt. "
1473 "Try 'opkg update'.\n", pkg
->name
);
1478 "Ignored %s sha256sum mismatch.\n",
1486 if (conf
->download_only
) {
1487 if (conf
->nodeps
== 0) {
1488 err
= satisfy_dependencies_for(pkg
);
1495 if (!pkg_get_string(pkg
, PKG_TMP_UNPACK_DIR
)) {
1496 if (unpack_pkg_control_files(pkg
) == -1) {
1498 "Failed to unpack control files from %s.\n",
1504 err
= update_file_ownership(pkg
, old_pkg
);
1508 if (conf
->nodeps
== 0) {
1509 err
= satisfy_dependencies_for(pkg
);
1512 if (pkg
->state_status
== SS_UNPACKED
)
1513 /* Circular dependency has installed it for us. */
1517 replacees
= pkg_vec_alloc();
1518 pkg_get_installed_replacees(pkg
, replacees
);
1520 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1522 sigemptyset(&newset
);
1523 sigaddset(&newset
, SIGINT
);
1524 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1526 opkg_state_changed
++;
1527 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1530 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1531 old_pkg
->is_upgrade
= 1;
1532 pkg
->is_upgrade
= 1;
1534 /* XXX: BUG: we really should treat replacement more like an upgrade
1535 * Instead, we're going to remove the replacees
1537 err
= pkg_remove_installed_replacees(replacees
);
1539 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1541 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1543 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1545 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1547 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1549 err
= preinst_configure(pkg
, old_pkg
);
1551 goto UNWIND_PREINST_CONFIGURE
;
1553 err
= backup_modified_conffiles(pkg
, old_pkg
);
1555 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1557 err
= check_data_file_clashes(pkg
, old_pkg
);
1559 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1561 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1563 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1568 /* point of no return: no unwinding after this */
1570 old_pkg
->state_want
= SW_DEINSTALL
;
1572 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1573 opkg_msg(INFO
, "Not removing obsolesced files because "
1574 "package %s marked noprune.\n", old_pkg
->name
);
1576 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1578 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1579 opkg_msg(ERROR
, "Failed to determine "
1580 "obsolete files from previously "
1581 "installed %s\n", old_pkg
->name
);
1585 /* removing files from old package, to avoid ghost files */
1586 remove_data_files_and_list(old_pkg
);
1587 remove_maintainer_scripts(old_pkg
);
1590 opkg_msg(INFO
, "%s maintainer scripts.\n",
1591 (pkg
->is_upgrade
) ? ("Upgrading") : ("Installing"));
1592 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1593 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1594 " Package debris may remain!\n", pkg
->name
);
1598 /* the following just returns 0 */
1599 remove_disappeared(pkg
);
1601 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1603 if (install_data_files(pkg
)) {
1604 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1605 "Package debris may remain!\n", pkg
->name
);
1609 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1611 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1612 "for files belonging to %s.\n", pkg
->name
);
1615 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1616 resolve_conffiles(pkg
);
1618 pkg
->state_status
= SS_UNPACKED
;
1619 old_state_flag
= pkg
->state_flag
;
1620 pkg
->state_flag
&= ~SF_PREFER
;
1621 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1622 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1625 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1628 pkg_set_int(pkg
, PKG_INSTALLED_TIME
, now
);
1630 ab_pkg
= pkg
->parent
;
1632 ab_pkg
->state_status
= pkg
->state_status
;
1634 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1635 pkg_vec_free(replacees
);
1638 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1639 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1640 UNWIND_CHECK_DATA_FILE_CLASHES
:
1641 check_data_file_clashes_unwind(pkg
, old_pkg
);
1642 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1643 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1644 UNWIND_PREINST_CONFIGURE
:
1645 preinst_configure_unwind(pkg
, old_pkg
);
1646 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1647 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1648 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1649 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1650 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1651 pkg_remove_installed_replacees_unwind(replacees
);
1654 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1656 pkg_vec_free(replacees
);