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 int satisfy_dependencies_for(pkg_t
* pkg
)
46 pkg_vec_t
*depends
= pkg_vec_alloc();
48 char **tmp
, **unresolved
= NULL
;
51 ndepends
= pkg_hash_fetch_unsatisfied_dependencies(pkg
, depends
,
56 "Cannot satisfy the following dependencies for %s:\n",
60 opkg_message(ERROR
, "\t%s", *unresolved
);
65 opkg_message(ERROR
, "\n");
66 if (!conf
->force_depends
) {
68 "This could mean that your package list is out of date or that the packages\n"
69 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
70 "of this problem try again with the '-force-depends' option.\n");
71 pkg_vec_free(depends
);
77 pkg_vec_free(depends
);
81 /* Mark packages as to-be-installed */
82 for (i
= 0; i
< depends
->len
; i
++) {
83 /* Dependencies should be installed the same place as pkg */
84 if (depends
->pkgs
[i
]->dest
== NULL
) {
85 depends
->pkgs
[i
]->dest
= pkg
->dest
;
87 depends
->pkgs
[i
]->state_want
= SW_INSTALL
;
90 for (i
= 0; i
< depends
->len
; i
++) {
91 dep
= depends
->pkgs
[i
];
92 /* The package was uninstalled when we started, but another
93 dep earlier in this loop may have depended on it and pulled
94 it in, so check first. */
95 if ((dep
->state_status
!= SS_INSTALLED
)
96 && (dep
->state_status
!= SS_UNPACKED
)) {
97 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
98 err
= opkg_install_pkg(dep
, 0);
99 /* mark this package as having been automatically installed to
100 * satisfy a dependancy */
101 dep
->auto_installed
= 1;
103 pkg_vec_free(depends
);
109 pkg_vec_free(depends
);
114 static int check_conflicts_for(pkg_t
* pkg
)
117 pkg_vec_t
*conflicts
= NULL
;
118 message_level_t level
;
120 if (conf
->force_depends
) {
126 if (!conf
->force_depends
)
127 conflicts
= pkg_hash_fetch_conflicts(pkg
);
130 opkg_msg(level
, "The following packages conflict with %s:\n",
133 while (i
< conflicts
->len
)
134 opkg_msg(level
, "\t%s", conflicts
->pkgs
[i
++]->name
);
135 opkg_message(level
, "\n");
136 pkg_vec_free(conflicts
);
142 static int update_file_ownership(pkg_t
* new_pkg
, pkg_t
* old_pkg
)
144 str_list_t
*new_list
, *old_list
;
145 str_list_elt_t
*iter
, *niter
;
147 new_list
= pkg_get_installed_files(new_pkg
);
148 if (new_list
== NULL
)
151 for (iter
= str_list_first(new_list
), niter
=
152 str_list_next(new_list
, iter
); iter
;
153 iter
= niter
, niter
= str_list_next(new_list
, niter
)) {
154 char *new_file
= (char *)iter
->data
;
155 pkg_t
*owner
= file_hash_get_file_owner(new_file
);
156 pkg_t
*obs
= hash_table_get(&conf
->obs_file_hash
, new_file
);
159 "%s: new_pkg=%s wants file %s, from owner=%s\n",
160 __func__
, new_pkg
->name
, new_file
,
161 owner
? owner
->name
: "<NULL>");
163 if (!owner
|| (owner
== old_pkg
) || obs
)
164 file_hash_set_file_owner(new_file
, new_pkg
);
168 old_list
= pkg_get_installed_files(old_pkg
);
169 if (old_list
== NULL
) {
170 pkg_free_installed_files(new_pkg
);
174 for (iter
= str_list_first(old_list
), niter
=
175 str_list_next(old_list
, iter
); iter
;
176 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
177 char *old_file
= (char *)iter
->data
;
178 pkg_t
*owner
= file_hash_get_file_owner(old_file
);
179 if (!owner
|| (owner
== old_pkg
)) {
181 hash_table_insert(&conf
->obs_file_hash
,
185 pkg_free_installed_files(old_pkg
);
187 pkg_free_installed_files(new_pkg
);
191 static int verify_pkg_installable(pkg_t
* pkg
)
193 unsigned long kbs_available
, pkg_size_kbs
;
194 unsigned long installed_size
;
195 char *root_dir
= NULL
;
198 installed_size
= (unsigned long) pkg_get_int(pkg
, PKG_INSTALLED_SIZE
);
200 if (conf
->force_space
|| installed_size
== 0)
204 if (!strcmp(pkg
->dest
->name
, "root") && conf
->overlay_root
205 && !stat(conf
->overlay_root
, &s
) && (s
.st_mode
& S_IFDIR
))
206 root_dir
= conf
->overlay_root
;
208 root_dir
= pkg
->dest
->root_dir
;
212 root_dir
= conf
->default_dest
->root_dir
;
214 kbs_available
= get_available_kbytes(root_dir
);
216 pkg_size_kbs
= (installed_size
+ 1023) / 1024;
218 if (pkg_size_kbs
>= kbs_available
) {
219 opkg_msg(ERROR
, "Only have %ldkb available on filesystem %s, "
220 "pkg %s needs %ld\n",
221 kbs_available
, root_dir
, pkg
->name
, pkg_size_kbs
);
228 static int unpack_pkg_control_files(pkg_t
* pkg
)
231 char *conffiles_file_name
;
233 char *tmp_unpack_dir
;
234 FILE *conffiles_file
;
237 sprintf_alloc(&tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
,
240 tmp_unpack_dir
= mkdtemp(tmp_unpack_dir
);
241 if (tmp_unpack_dir
== NULL
) {
242 opkg_perror(ERROR
, "Failed to create temporary directory '%s'",
247 pkg_set_string(pkg
, PKG_TMP_UNPACK_DIR
, tmp_unpack_dir
);
249 err
= pkg_extract_control_files_to_dir(pkg
, tmp_unpack_dir
);
254 /* XXX: CLEANUP: There might be a cleaner place to read in the
255 conffiles. Seems like I should be able to get everything to go
256 through pkg_init_from_file. If so, maybe it would make sense to
257 move all of unpack_pkg_control_files to that function. */
259 /* Don't need to re-read conffiles if we already have it */
260 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
261 if (cl
&& !nv_pair_list_empty(cl
)) {
265 sprintf_alloc(&conffiles_file_name
, "%s/conffiles",
267 if (!file_exists(conffiles_file_name
)) {
268 free(conffiles_file_name
);
272 conffiles_file
= fopen(conffiles_file_name
, "r");
273 if (conffiles_file
== NULL
) {
274 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
275 free(conffiles_file_name
);
278 free(conffiles_file_name
);
280 cl
= xcalloc(1, sizeof(*cl
));
281 conffile_list_init(cl
);
285 char *cf_name_in_dest
;
288 cf_name
= file_read_line_alloc(conffiles_file
);
289 if (cf_name
== NULL
) {
292 if (cf_name
[0] == '\0') {
295 for (i
= strlen(cf_name
) - 1;
296 (i
>= 0) && (cf_name
[i
] == ' ' || cf_name
[i
] == '\t');
301 /* Prepend dest->root_dir to conffile name.
302 Take pains to avoid multiple slashes. */
303 root_dir
= pkg
->dest
->root_dir
;
304 if (conf
->offline_root
)
305 /* skip the offline_root prefix */
307 pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
308 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
309 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
311 /* Can't get an md5sum now, (file isn't extracted yet).
312 We'll wait until resolve_conffiles */
313 conffile_list_append(cl
, cf_name_in_dest
, NULL
);
316 free(cf_name_in_dest
);
319 pkg_set_ptr(pkg
, PKG_CONFFILES
, cl
);
321 fclose(conffiles_file
);
327 * Remove packages which were auto_installed due to a dependency by old_pkg,
328 * which are no longer a dependency in the new (upgraded) pkg.
330 static int pkg_remove_orphan_dependent(pkg_t
* pkg
, pkg_t
* old_pkg
)
332 int j
, l
, found
, r
, err
= 0;
335 struct compound_depend
*cd0
, *cd1
;
336 abstract_pkg_t
**dependents
;
338 for (cd0
= pkg_get_ptr(old_pkg
, PKG_DEPENDS
); cd0
&& cd0
->type
; cd0
++) {
339 if (cd0
->type
!= DEPEND
)
341 for (j
= 0; j
< cd0
->possibility_count
; j
++) {
345 for (cd1
= pkg_get_ptr(pkg
, PKG_DEPENDS
); cd1
&& cd1
->type
; cd1
++) {
346 if (cd1
->type
!= DEPEND
)
348 for (l
= 0; l
< cd1
->possibility_count
; l
++) {
349 if (cd0
->possibilities
[j
]
350 == cd1
->possibilities
[l
]) {
363 * old_pkg has a dependency that pkg does not.
365 p
= pkg_hash_fetch_installed_by_name(cd0
->
372 if (!p
->auto_installed
)
375 n_deps
= pkg_has_installed_dependents(p
, &dependents
);
376 n_deps
--; /* don't count old_pkg */
379 opkg_msg(NOTICE
, "%s was autoinstalled and is "
380 "now orphaned, removing.\n", p
->name
);
382 /* p has one installed dependency (old_pkg),
383 * which we need to ignore during removal. */
384 p
->state_flag
|= SF_REPLACE
;
386 r
= opkg_remove_pkg(p
, 0);
390 opkg_msg(INFO
, "%s was autoinstalled and is "
391 "still required by %d "
392 "installed packages.\n",
401 /* returns number of installed replacees */
403 pkg_get_installed_replacees(pkg_t
* pkg
, pkg_vec_t
* installed_replacees
)
405 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
408 while (replaces
&& *replaces
) {
409 abstract_pkg_t
*ab_pkg
= *replaces
++;
410 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
412 for (j
= 0; j
< pkg_vec
->len
; j
++) {
413 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
414 if (!pkg_conflicts(pkg
, replacee
))
416 if (replacee
->state_status
== SS_INSTALLED
) {
417 pkg_vec_insert(installed_replacees
,
424 return installed_replacees
->len
;
427 static int pkg_remove_installed_replacees(pkg_vec_t
* replacees
)
430 int replaces_count
= replacees
->len
;
431 for (i
= 0; i
< replaces_count
; i
++) {
432 pkg_t
*replacee
= replacees
->pkgs
[i
];
434 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
435 err
= opkg_remove_pkg(replacee
, 0);
442 /* to unwind the removal: make sure they are installed */
443 static int pkg_remove_installed_replacees_unwind(pkg_vec_t
* replacees
)
446 int replaces_count
= replacees
->len
;
447 for (i
= 0; i
< replaces_count
; i
++) {
448 pkg_t
*replacee
= replacees
->pkgs
[i
];
449 if (replacee
->state_status
!= SS_INSTALLED
) {
450 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
451 err
= opkg_install_pkg(replacee
, 0);
459 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
461 opkg_install_check_downgrade(pkg_t
* pkg
, pkg_t
* old_pkg
, int message
)
464 char message_out
[15];
465 char *old_version
= pkg_version_str_alloc(old_pkg
);
466 char *new_version
= pkg_version_str_alloc(pkg
);
467 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
470 memset(message_out
, '\x0', 15);
471 strncpy(message_out
, "Upgrading ", strlen("Upgrading "));
472 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
473 cmp
= -1; /* then we force opkg to downgrade */
474 strncpy(message_out
, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
475 /* reinstall, and some check could fail asking the "force-reinstall" option */
479 if (!conf
->download_only
)
481 "Not downgrading package %s on %s from %s to %s.\n",
482 old_pkg
->name
, old_pkg
->dest
->name
,
483 old_version
, new_version
);
485 } else if (cmp
< 0) {
486 if (!conf
->download_only
)
488 "%s%s on %s from %s to %s...\n",
489 message_out
, pkg
->name
,
490 old_pkg
->dest
->name
, old_version
,
492 pkg
->dest
= old_pkg
->dest
;
494 } else { /* cmp == 0 */
496 if (!conf
->download_only
)
498 "%s (%s) already install on %s.\n",
499 pkg
->name
, new_version
,
500 old_pkg
->dest
->name
);
507 char message_out
[15];
508 memset(message_out
, '\x0', 15);
510 strncpy(message_out
, "Upgrading ",
511 strlen("Upgrading "));
513 strncpy(message_out
, "Installing ",
514 strlen("Installing "));
515 char *version
= pkg_version_str_alloc(pkg
);
517 if (!conf
->download_only
)
518 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
519 pkg
->name
, version
, pkg
->dest
->name
);
525 static int prerm_upgrade_old_pkg(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?
530 1. If a version of the package is already installed, call
531 old-prerm upgrade new-version
532 2. If the script runs but exits with a non-zero exit status
533 new-prerm failed-upgrade old-version
534 Error unwind, for both the above cases:
535 old-postinst abort-upgrade new-version
541 if (!old_pkg
|| !pkg
)
544 new_version
= pkg_version_str_alloc(pkg
);
546 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
548 err
= pkg_run_script(old_pkg
, "prerm", script_args
);
551 opkg_msg(ERROR
, "prerm script for package \"%s\" failed\n",
558 static int prerm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
560 /* DPKG_INCOMPATIBILITY:
561 dpkg does some things here that we don't do yet. Do we care?
562 (See prerm_upgrade_old_package for details)
567 static int prerm_deconfigure_conflictors(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
569 /* DPKG_INCOMPATIBILITY:
570 dpkg does some things here that we don't do yet. Do we care?
571 2. If a 'conflicting' package is being removed at the same time:
572 1. If any packages depended on that conflicting package and
573 --auto-deconfigure is specified, call, for each such package:
574 deconfigured's-prerm deconfigure \
575 in-favour package-being-installed version \
576 removing conflicting-package version
578 deconfigured's-postinst abort-deconfigure \
579 in-favour package-being-installed-but-failed version \
580 removing conflicting-package version
582 The deconfigured packages are marked as requiring
583 configuration, so that if --install is used they will be
584 configured again if possible.
585 2. To prepare for removal of the conflicting package, call:
586 conflictor's-prerm remove in-favour package new-version
588 conflictor's-postinst abort-remove in-favour package new-version
594 prerm_deconfigure_conflictors_unwind(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
596 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
597 do yet. Do we care? (See prerm_deconfigure_conflictors for
602 static int preinst_configure(pkg_t
* pkg
, pkg_t
* old_pkg
)
608 char *old_version
= pkg_version_str_alloc(old_pkg
);
609 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
611 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
612 char *pkg_version
= pkg_version_str_alloc(pkg
);
613 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
616 preinst_args
= xstrdup("install");
619 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
621 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
630 static int preinst_configure_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
632 /* DPKG_INCOMPATIBILITY:
633 dpkg does the following error unwind, should we?
634 pkg->postrm abort-upgrade old-version
635 OR pkg->postrm abort-install old-version
636 OR pkg->postrm abort-install
641 static char *backup_filename_alloc(const char *file_name
)
645 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
650 static int backup_make_backup(const char *file_name
)
655 backup
= backup_filename_alloc(file_name
);
656 err
= file_copy(file_name
, backup
);
658 opkg_msg(ERROR
, "Failed to copy %s to %s\n", file_name
, backup
);
666 static int backup_exists_for(const char *file_name
)
671 backup
= backup_filename_alloc(file_name
);
673 ret
= file_exists(backup
);
680 static int backup_remove(const char *file_name
)
684 backup
= backup_filename_alloc(file_name
);
691 static int backup_modified_conffiles(pkg_t
* pkg
, pkg_t
* old_pkg
)
694 conffile_list_elt_t
*iter
;
701 /* Backup all modified conffiles */
703 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
705 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
706 iter
= nv_pair_list_next(cl
, iter
)) {
710 cf_name
= root_filename_alloc(cf
->name
);
712 /* Don't worry if the conffile is just plain gone */
713 if (file_exists(cf_name
)
714 && conffile_has_been_modified(cf
)) {
715 err
= backup_make_backup(cf_name
);
724 /* Backup all conffiles that were not conffiles in old_pkg */
725 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
727 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
728 iter
= nv_pair_list_next(cl
, iter
)) {
730 cf
= (conffile_t
*) iter
->data
;
731 cf_name
= root_filename_alloc(cf
->name
);
732 /* Ignore if this was a conffile in old_pkg as well */
733 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
737 if (file_exists(cf_name
) && (!backup_exists_for(cf_name
))) {
738 err
= backup_make_backup(cf_name
);
749 static int backup_modified_conffiles_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
752 conffile_list_elt_t
*iter
;
755 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
757 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
758 iter
= nv_pair_list_next(cl
, iter
)) {
759 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
763 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
765 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
766 iter
= nv_pair_list_next(cl
, iter
)) {
767 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
773 static int check_data_file_clashes(pkg_t
* pkg
, pkg_t
* old_pkg
)
775 /* DPKG_INCOMPATIBILITY:
776 opkg takes a slightly different approach than dpkg at this
777 point. dpkg installs each file in the new package while
778 creating a backup for any file that is replaced, (so that it
779 can unwind if necessary). To avoid complexity and redundant
780 storage, opkg doesn't do any installation until later, (at the
781 point at which dpkg removes the backups.
783 But, we do have to check for data file clashes, since after
784 installing a package with a file clash, removing either of the
785 packages involved in the clash has the potential to break the
788 str_list_t
*files_list
;
789 str_list_elt_t
*iter
, *niter
;
793 files_list
= pkg_get_installed_files(pkg
);
794 if (files_list
== NULL
)
797 for (iter
= str_list_first(files_list
), niter
=
798 str_list_next(files_list
, iter
); iter
;
799 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
800 filename
= (char *)iter
->data
;
801 if (file_exists(filename
) && (!file_is_dir(filename
))) {
805 if (backup_exists_for(filename
)) {
809 /* Pre-existing files are OK if force-overwrite was asserted. */
810 if (conf
->force_overwrite
) {
811 /* but we need to change who owns this file */
812 file_hash_set_file_owner(filename
, pkg
);
816 owner
= file_hash_get_file_owner(filename
);
818 /* Pre-existing files are OK if owned by the pkg being upgraded. */
819 if (owner
&& old_pkg
) {
820 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
825 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
828 "Checking replaces for %s in package %s\n",
829 filename
, owner
->name
);
830 if (pkg_replaces(pkg
, owner
)) {
833 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
834 then it's ok to overwrite. */
835 if (strcmp(owner
->name
, pkg
->name
) == 0) {
837 "Replacing pre-existing file %s"
838 " owned by package %s\n",
839 filename
, owner
->name
);
844 /* Pre-existing files are OK if they are obsolete */
845 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
848 "Pre-exiting file %s is obsolete."
849 " obs_pkg=%s\n", filename
, obs
->name
);
853 /* We have found a clash. */
854 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
855 "\tBut that file is already provided by package ",
856 pkg
->name
, filename
);
858 opkg_message(ERROR
, "%s\n", owner
->name
);
860 opkg_message(ERROR
, "<no package>\n"
861 "Please move this file out of the way and try again.\n");
866 pkg_free_installed_files(pkg
);
872 * XXX: This function sucks, as does the below comment.
874 static int check_data_file_clashes_change(pkg_t
* pkg
, pkg_t
* old_pkg
)
876 /* Basically that's the worst hack I could do to be able to change ownership of
877 file list, but, being that we have no way to unwind the mods, due to structure
878 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
879 What we do here is change the ownership of file in hash if a replace ( or similar events
881 Only the action that are needed to change name should be considered.
882 @@@ To change after 1.0 release.
884 str_list_t
*files_list
;
885 str_list_elt_t
*iter
, *niter
;
887 files_list
= pkg_get_installed_files(pkg
);
888 if (files_list
== NULL
)
891 for (iter
= str_list_first(files_list
), niter
=
892 str_list_next(files_list
, iter
); iter
;
893 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
894 char *filename
= (char *)iter
->data
;
895 if (file_exists(filename
) && (!file_is_dir(filename
))) {
898 owner
= file_hash_get_file_owner(filename
);
900 if (conf
->force_overwrite
) {
901 /* but we need to change who owns this file */
902 file_hash_set_file_owner(filename
, pkg
);
906 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
908 if (pkg_replaces(pkg
, owner
)) {
909 /* It's now time to change the owner of that file.
910 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
912 "Replacing pre-existing file %s "
913 "owned by package %s\n",
914 filename
, owner
->name
);
915 file_hash_set_file_owner(filename
, pkg
);
922 pkg_free_installed_files(pkg
);
927 static int check_data_file_clashes_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
929 /* Nothing to do since check_data_file_clashes doesn't change state */
933 static int postrm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
935 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
936 1. If the package is being upgraded, call
937 old-postrm upgrade new-version
938 2. If this fails, attempt:
939 new-postrm failed-upgrade old-version
940 Error unwind, for both cases:
941 old-preinst abort-upgrade new-version */
946 if (!old_pkg
|| !pkg
)
949 new_version
= pkg_version_str_alloc(pkg
);
951 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
953 err
= pkg_run_script(old_pkg
, "postrm", script_args
);
956 opkg_msg(ERROR
, "postrm script for package \"%s\" failed\n",
963 static int postrm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
965 /* DPKG_INCOMPATIBILITY:
966 dpkg does some things here that we don't do yet. Do we care?
967 (See postrm_upgrade_old_pkg for details)
972 static int remove_obsolesced_files(pkg_t
* pkg
, pkg_t
* old_pkg
)
975 str_list_t
*old_files
;
977 str_list_t
*new_files
;
979 hash_table_t new_files_table
;
981 old_files
= pkg_get_installed_files(old_pkg
);
982 if (old_files
== NULL
)
985 new_files
= pkg_get_installed_files(pkg
);
986 if (new_files
== NULL
) {
987 pkg_free_installed_files(old_pkg
);
991 new_files_table
.entries
= NULL
;
992 hash_table_init("new_files", &new_files_table
, 20);
993 for (nf
= str_list_first(new_files
); nf
;
994 nf
= str_list_next(new_files
, nf
)) {
996 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
999 for (of
= str_list_first(old_files
); of
;
1000 of
= str_list_next(old_files
, of
)) {
1003 old
= (char *)of
->data
;
1004 new = (char *)hash_table_get(&new_files_table
, old
);
1008 if (file_is_dir(old
)) {
1011 owner
= file_hash_get_file_owner(old
);
1012 if (owner
!= old_pkg
) {
1013 /* in case obsolete file no longer belongs to old_pkg */
1017 /* old file is obsolete */
1018 opkg_msg(NOTICE
, "Removing obsolete file %s.\n", old
);
1019 if (!conf
->noaction
) {
1022 opkg_perror(ERROR
, "unlinking %s failed", old
);
1027 hash_table_deinit(&new_files_table
);
1028 pkg_free_installed_files(old_pkg
);
1029 pkg_free_installed_files(pkg
);
1034 static int install_maintainer_scripts(pkg_t
* pkg
, pkg_t
* old_pkg
)
1039 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1040 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1041 pkg
->dest
->info_dir
,
1047 static int remove_disappeared(pkg_t
* pkg
)
1049 /* DPKG_INCOMPATIBILITY:
1050 This is a fairly sophisticated dpkg operation. Shall we
1053 /* Any packages all of whose files have been overwritten during the
1054 installation, and which aren't required for dependencies, are
1055 considered to have been removed. For each such package
1056 1. disappearer's-postrm disappear overwriter overwriter-version
1057 2. The package's maintainer scripts are removed
1058 3. It is noted in the status database as being in a sane state,
1059 namely not installed (any conffiles it may have are ignored,
1060 rather than being removed by dpkg). Note that disappearing
1061 packages do not have their prerm called, because dpkg doesn't
1062 know in advance that the package is going to vanish.
1067 static int install_data_files(pkg_t
* pkg
)
1071 /* opkg takes a slightly different approach to data file backups
1072 than dpkg. Rather than removing backups at this point, we
1073 actually do the data file installation now. See comments in
1074 check_data_file_clashes() for more details. */
1076 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1077 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1082 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1083 err
= pkg_write_filelist(pkg
);
1087 /* XXX: FEATURE: opkg should identify any files which existed
1088 before installation and which were overwritten, (see
1089 check_data_file_clashes()). What it must do is remove any such
1090 files from the filelist of the old package which provided the
1091 file. Otherwise, if the old package were removed at some point
1092 it would break the new package. Removing the new package will
1093 also break the old one, but this cannot be helped since the old
1094 package's file has already been deleted. This is the importance
1095 of check_data_file_clashes(), and only allowing opkg to install
1096 a clashing package with a user force. */
1101 static int resolve_conffiles(pkg_t
* pkg
)
1103 conffile_list_elt_t
*iter
;
1104 conffile_list_t
*cl
;
1112 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
1114 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
1115 iter
= nv_pair_list_next(cl
, iter
)) {
1116 char *root_filename
;
1117 cf
= (conffile_t
*) iter
->data
;
1118 root_filename
= root_filename_alloc(cf
->name
);
1120 /* Might need to initialize the md5sum for each conffile */
1121 if (cf
->value
== NULL
) {
1122 cf
->value
= file_sha256sum_alloc(root_filename
);
1125 if (!file_exists(root_filename
)) {
1126 free(root_filename
);
1130 cf_backup
= backup_filename_alloc(root_filename
);
1132 if (file_exists(cf_backup
)) {
1133 /* Let's compute md5 to test if files are changed */
1134 if (cf
->value
&& strlen(cf
->value
) > 33) {
1135 chksum
= file_sha256sum_alloc(cf_backup
);
1137 chksum
= file_md5sum_alloc(cf_backup
);
1140 if (chksum
&& cf
->value
1141 && strcmp(cf
->value
, chksum
) != 0) {
1142 if (conf
->force_maintainer
) {
1144 "Conffile %s using maintainer's setting.\n",
1148 sprintf_alloc(&new_conffile
, "%s-opkg",
1151 "Existing conffile %s "
1152 "is different from the conffile in the new package."
1153 " The new conffile will be placed at %s.\n",
1154 root_filename
, new_conffile
);
1155 rename(root_filename
, new_conffile
);
1156 rename(cf_backup
, root_filename
);
1166 free(root_filename
);
1172 int opkg_install_by_name(const char *pkg_name
)
1176 char *old_version
, *new_version
;
1178 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1180 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1181 pkg_get_string(old
, PKG_VERSION
));
1183 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1185 opkg_msg(NOTICE
, "Unknown package '%s'.\n", pkg_name
);
1189 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1191 opkg_message(DEBUG2
, " old %s ", pkg_get_string(old
, PKG_VERSION
));
1192 opkg_message(DEBUG2
, " new %s\n", pkg_get_string(new, PKG_VERSION
));
1194 new->state_flag
|= SF_USER
;
1196 old_version
= pkg_version_str_alloc(old
);
1197 new_version
= pkg_version_str_alloc(new);
1199 cmp
= pkg_compare_versions(old
, new);
1200 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
1201 opkg_msg(DEBUG
, "Forcing downgrade\n");
1202 cmp
= -1; /* then we force opkg to downgrade */
1203 /* We need to use a value < 0 because in the 0 case we are asking to */
1204 /* reinstall, and some check could fail asking the "force-reinstall" option */
1206 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1207 "\n\t%s is installed "
1208 "\n\t%s is available "
1209 "\n\t%d was comparison result\n",
1210 pkg_name
, old_version
, new_version
, cmp
);
1213 "Package %s (%s) installed in %s is up to date.\n",
1214 old
->name
, old_version
, old
->dest
->name
);
1218 } else if (cmp
> 0) {
1220 "Not downgrading package %s on %s from %s to %s.\n",
1221 old
->name
, old
->dest
->name
, old_version
,
1226 } else if (cmp
< 0) {
1227 new->dest
= old
->dest
;
1228 old
->state_want
= SW_DEINSTALL
;
1234 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
1235 return opkg_install_pkg(new, 0);
1239 * @brief Really install a pkg_t
1241 int opkg_install_pkg(pkg_t
* pkg
, int from_upgrade
)
1245 pkg_t
*old_pkg
= NULL
;
1246 pkg_vec_t
*replacees
;
1247 abstract_pkg_t
*ab_pkg
= NULL
;
1249 char *file_md5
, *pkg_md5
;
1250 char *file_sha256
, *pkg_sha256
;
1251 sigset_t newset
, oldset
;
1252 const char *local_filename
;
1256 message
= 1; /* Coming from an upgrade, and should change the output message */
1258 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1260 if (!pkg_arch_supported(pkg
)) {
1262 "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1263 pkg_get_architecture(pkg
), pkg
->name
);
1266 if (pkg
->state_status
== SS_INSTALLED
&& conf
->nodeps
== 0) {
1267 err
= satisfy_dependencies_for(pkg
);
1271 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1272 pkg
->name
, pkg
->dest
->name
);
1276 if (pkg
->dest
== NULL
) {
1277 pkg
->dest
= conf
->default_dest
;
1280 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1282 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1286 pkg
->state_want
= SW_INSTALL
;
1288 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependencies */
1291 err
= check_conflicts_for(pkg
);
1295 /* this setup is to remove the upgrade scenario in the end when
1296 installing pkg A, A deps B & B deps on A. So both B and A are
1297 installed. Then A's installation is started resulting in an
1298 uncecessary upgrade */
1299 if (pkg
->state_status
== SS_INSTALLED
)
1302 err
= verify_pkg_installable(pkg
);
1306 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1308 if (local_filename
== NULL
) {
1309 if (!conf
->cache
&& conf
->download_only
) {
1311 if (getcwd(cwd
, sizeof(cwd
)) != NULL
)
1312 err
= opkg_download_pkg(pkg
, cwd
);
1316 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1319 opkg_msg(ERROR
, "Failed to download %s. "
1320 "Perhaps you need to run 'opkg update'?\n",
1325 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1328 /* check that the repository is valid */
1329 #if defined(HAVE_USIGN)
1330 char *list_file_name
, *sig_file_name
, *lists_dir
;
1332 /* check to ensure the package has come from a repository */
1333 if (conf
->check_signature
&& pkg
->src
) {
1334 sprintf_alloc(&lists_dir
, "%s", (conf
->restrict_to_default_dest
)
1335 ? conf
->default_dest
->lists_dir
1337 sprintf_alloc(&list_file_name
, "%s/%s", lists_dir
,
1339 sprintf_alloc(&sig_file_name
, "%s/%s.sig", lists_dir
,
1342 if (file_exists(sig_file_name
)) {
1343 if (opkg_verify_file(list_file_name
, sig_file_name
)) {
1345 "Failed to verify the signature of %s.\n",
1347 if (!conf
->force_signature
)
1351 opkg_msg(ERROR
, "Signature file is missing for %s. "
1352 "Perhaps you need to run 'opkg update'?\n",
1354 if (!conf
->force_signature
)
1359 free(list_file_name
);
1360 free(sig_file_name
);
1364 /* Check for md5 values */
1365 pkg_md5
= pkg_get_md5(pkg
);
1367 file_md5
= file_md5sum_alloc(local_filename
);
1368 if (file_md5
&& strcmp(file_md5
, pkg_md5
)) {
1369 if (!conf
->force_checksum
) {
1370 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1371 "Either the opkg or the package index are corrupt. "
1372 "Try 'opkg update'.\n", pkg
->name
);
1377 "Ignored %s md5sum mismatch.\n",
1385 /* Check for sha256 value */
1386 pkg_sha256
= pkg_get_sha256(pkg
);
1388 file_sha256
= file_sha256sum_alloc(local_filename
);
1389 if (file_sha256
&& strcmp(file_sha256
, pkg_sha256
)) {
1390 if (!conf
->force_checksum
) {
1392 "Package %s sha256sum mismatch. "
1393 "Either the opkg or the package index are corrupt. "
1394 "Try 'opkg update'.\n", pkg
->name
);
1399 "Ignored %s sha256sum mismatch.\n",
1407 if (conf
->download_only
) {
1408 if (conf
->nodeps
== 0) {
1409 err
= satisfy_dependencies_for(pkg
);
1416 if (!pkg_get_string(pkg
, PKG_TMP_UNPACK_DIR
)) {
1417 if (unpack_pkg_control_files(pkg
) == -1) {
1419 "Failed to unpack control files from %s.\n",
1425 err
= update_file_ownership(pkg
, old_pkg
);
1429 if (conf
->nodeps
== 0) {
1430 err
= satisfy_dependencies_for(pkg
);
1433 if (pkg
->state_status
== SS_UNPACKED
)
1434 /* Circular dependency has installed it for us. */
1438 replacees
= pkg_vec_alloc();
1439 pkg_get_installed_replacees(pkg
, replacees
);
1441 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1443 sigemptyset(&newset
);
1444 sigaddset(&newset
, SIGINT
);
1445 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1447 opkg_state_changed
++;
1448 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1451 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1452 old_pkg
->is_upgrade
= 1;
1453 pkg
->is_upgrade
= 1;
1455 /* XXX: BUG: we really should treat replacement more like an upgrade
1456 * Instead, we're going to remove the replacees
1458 err
= pkg_remove_installed_replacees(replacees
);
1460 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1462 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1464 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1466 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1468 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1470 err
= preinst_configure(pkg
, old_pkg
);
1472 goto UNWIND_PREINST_CONFIGURE
;
1474 err
= backup_modified_conffiles(pkg
, old_pkg
);
1476 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1478 err
= check_data_file_clashes(pkg
, old_pkg
);
1480 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1482 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1484 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1489 /* point of no return: no unwinding after this */
1491 old_pkg
->state_want
= SW_DEINSTALL
;
1493 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1494 opkg_msg(INFO
, "Not removing obsolesced files because "
1495 "package %s marked noprune.\n", old_pkg
->name
);
1497 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1499 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1500 opkg_msg(ERROR
, "Failed to determine "
1501 "obsolete files from previously "
1502 "installed %s\n", old_pkg
->name
);
1506 /* removing files from old package, to avoid ghost files */
1507 remove_data_files_and_list(old_pkg
);
1508 remove_maintainer_scripts(old_pkg
);
1511 opkg_msg(INFO
, "%s maintainer scripts.\n",
1512 (pkg
->is_upgrade
) ? ("Upgrading") : ("Installing"));
1513 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1514 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1515 " Package debris may remain!\n", pkg
->name
);
1519 /* the following just returns 0 */
1520 remove_disappeared(pkg
);
1522 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1524 if (install_data_files(pkg
)) {
1525 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1526 "Package debris may remain!\n", pkg
->name
);
1530 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1532 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1533 "for files belonging to %s.\n", pkg
->name
);
1536 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1537 resolve_conffiles(pkg
);
1539 pkg
->state_status
= SS_UNPACKED
;
1540 old_state_flag
= pkg
->state_flag
;
1541 pkg
->state_flag
&= ~SF_PREFER
;
1542 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1543 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1546 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1549 pkg_set_int(pkg
, PKG_INSTALLED_TIME
, now
);
1551 ab_pkg
= pkg
->parent
;
1553 ab_pkg
->state_status
= pkg
->state_status
;
1555 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1556 pkg_vec_free(replacees
);
1559 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1560 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1561 UNWIND_CHECK_DATA_FILE_CLASHES
:
1562 check_data_file_clashes_unwind(pkg
, old_pkg
);
1563 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1564 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1565 UNWIND_PREINST_CONFIGURE
:
1566 preinst_configure_unwind(pkg
, old_pkg
);
1567 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1568 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1569 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1570 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1571 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1572 pkg_remove_installed_replacees_unwind(replacees
);
1575 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1577 pkg_vec_free(replacees
);