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 char *root_filename
= NULL
;
889 files_list
= pkg_get_installed_files(pkg
);
890 if (files_list
== NULL
)
893 for (iter
= str_list_first(files_list
), niter
=
894 str_list_next(files_list
, iter
); iter
;
895 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
896 char *filename
= (char *)iter
->data
;
899 root_filename
= NULL
;
901 root_filename
= root_filename_alloc(filename
);
902 if (file_exists(root_filename
) && (!file_is_dir(root_filename
))) {
905 owner
= file_hash_get_file_owner(filename
);
907 if (conf
->force_overwrite
) {
908 /* but we need to change who owns this file */
909 file_hash_set_file_owner(filename
, pkg
);
913 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
915 if (pkg_replaces(pkg
, owner
)) {
916 /* It's now time to change the owner of that file.
917 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
919 "Replacing pre-existing file %s "
920 "owned by package %s\n",
921 filename
, owner
->name
);
922 file_hash_set_file_owner(filename
, pkg
);
931 root_filename
= NULL
;
933 pkg_free_installed_files(pkg
);
938 static int check_data_file_clashes_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
940 /* Nothing to do since check_data_file_clashes doesn't change state */
944 static int postrm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
946 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
947 1. If the package is being upgraded, call
948 old-postrm upgrade new-version
949 2. If this fails, attempt:
950 new-postrm failed-upgrade old-version
951 Error unwind, for both cases:
952 old-preinst abort-upgrade new-version */
957 if (!old_pkg
|| !pkg
)
960 new_version
= pkg_version_str_alloc(pkg
);
962 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
964 err
= pkg_run_script(old_pkg
, "postrm", script_args
);
967 opkg_msg(ERROR
, "postrm script for package \"%s\" failed\n",
974 static int postrm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
976 /* DPKG_INCOMPATIBILITY:
977 dpkg does some things here that we don't do yet. Do we care?
978 (See postrm_upgrade_old_pkg for details)
983 static int remove_obsolesced_files(pkg_t
* pkg
, pkg_t
* old_pkg
)
986 str_list_t
*old_files
;
988 str_list_t
*new_files
;
990 hash_table_t new_files_table
;
992 old_files
= pkg_get_installed_files(old_pkg
);
993 if (old_files
== NULL
)
996 new_files
= pkg_get_installed_files(pkg
);
997 if (new_files
== NULL
) {
998 pkg_free_installed_files(old_pkg
);
1002 new_files_table
.entries
= NULL
;
1003 hash_table_init("new_files", &new_files_table
, 20);
1004 for (nf
= str_list_first(new_files
); nf
;
1005 nf
= str_list_next(new_files
, nf
)) {
1007 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
1010 for (of
= str_list_first(old_files
); of
;
1011 of
= str_list_next(old_files
, of
)) {
1014 old
= (char *)of
->data
;
1015 new = (char *)hash_table_get(&new_files_table
, old
);
1019 if (file_is_dir(old
)) {
1022 owner
= file_hash_get_file_owner(old
);
1023 if (owner
!= old_pkg
) {
1024 /* in case obsolete file no longer belongs to old_pkg */
1028 /* old file is obsolete */
1029 opkg_msg(NOTICE
, "Removing obsolete file %s.\n", old
);
1030 if (!conf
->noaction
) {
1033 opkg_perror(ERROR
, "unlinking %s failed", old
);
1038 hash_table_deinit(&new_files_table
);
1039 pkg_free_installed_files(old_pkg
);
1040 pkg_free_installed_files(pkg
);
1045 static int install_maintainer_scripts(pkg_t
* pkg
, pkg_t
* old_pkg
)
1050 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1051 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1052 pkg
->dest
->info_dir
,
1058 static int remove_disappeared(pkg_t
* pkg
)
1060 /* DPKG_INCOMPATIBILITY:
1061 This is a fairly sophisticated dpkg operation. Shall we
1064 /* Any packages all of whose files have been overwritten during the
1065 installation, and which aren't required for dependencies, are
1066 considered to have been removed. For each such package
1067 1. disappearer's-postrm disappear overwriter overwriter-version
1068 2. The package's maintainer scripts are removed
1069 3. It is noted in the status database as being in a sane state,
1070 namely not installed (any conffiles it may have are ignored,
1071 rather than being removed by dpkg). Note that disappearing
1072 packages do not have their prerm called, because dpkg doesn't
1073 know in advance that the package is going to vanish.
1078 static int install_data_files(pkg_t
* pkg
)
1082 /* opkg takes a slightly different approach to data file backups
1083 than dpkg. Rather than removing backups at this point, we
1084 actually do the data file installation now. See comments in
1085 check_data_file_clashes() for more details. */
1087 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1088 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1093 /* The "Essential" control field may only be present in the control
1094 * file and not in the Packages list. Ensure we capture it regardless.
1096 * XXX: This should be fixed outside of opkg, in the Package list.
1098 set_flags_from_control(pkg
);
1100 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1101 err
= pkg_write_filelist(pkg
);
1105 /* XXX: FEATURE: opkg should identify any files which existed
1106 before installation and which were overwritten, (see
1107 check_data_file_clashes()). What it must do is remove any such
1108 files from the filelist of the old package which provided the
1109 file. Otherwise, if the old package were removed at some point
1110 it would break the new package. Removing the new package will
1111 also break the old one, but this cannot be helped since the old
1112 package's file has already been deleted. This is the importance
1113 of check_data_file_clashes(), and only allowing opkg to install
1114 a clashing package with a user force. */
1119 static int resolve_conffiles(pkg_t
* pkg
)
1121 conffile_list_elt_t
*iter
;
1122 conffile_list_t
*cl
;
1130 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
1132 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
1133 iter
= nv_pair_list_next(cl
, iter
)) {
1134 char *root_filename
;
1135 cf
= (conffile_t
*) iter
->data
;
1136 root_filename
= root_filename_alloc(cf
->name
);
1138 /* Might need to initialize the md5sum for each conffile */
1139 if (cf
->value
== NULL
) {
1140 cf
->value
= file_sha256sum_alloc(root_filename
);
1143 if (!file_exists(root_filename
)) {
1144 free(root_filename
);
1148 cf_backup
= backup_filename_alloc(root_filename
);
1150 if (file_exists(cf_backup
)) {
1151 /* Let's compute md5 to test if files are changed */
1152 if (cf
->value
&& strlen(cf
->value
) > 33) {
1153 chksum
= file_sha256sum_alloc(cf_backup
);
1155 chksum
= file_md5sum_alloc(cf_backup
);
1158 if (chksum
&& cf
->value
1159 && strcmp(cf
->value
, chksum
) != 0) {
1160 if (conf
->force_maintainer
) {
1162 "Conffile %s using maintainer's setting.\n",
1166 sprintf_alloc(&new_conffile
, "%s-opkg",
1169 "Existing conffile %s "
1170 "is different from the conffile in the new package."
1171 " The new conffile will be placed at %s.\n",
1172 root_filename
, new_conffile
);
1173 rename(root_filename
, new_conffile
);
1174 rename(cf_backup
, root_filename
);
1184 free(root_filename
);
1190 int opkg_install_by_name(const char *pkg_name
)
1194 char *old_version
, *new_version
;
1196 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1198 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1199 pkg_get_string(old
, PKG_VERSION
));
1201 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1203 opkg_msg(NOTICE
, "Unknown package '%s'.\n", pkg_name
);
1207 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1209 opkg_message(DEBUG2
, " old %s ", pkg_get_string(old
, PKG_VERSION
));
1210 opkg_message(DEBUG2
, " new %s\n", pkg_get_string(new, PKG_VERSION
));
1212 new->state_flag
|= SF_USER
;
1214 old_version
= pkg_version_str_alloc(old
);
1215 new_version
= pkg_version_str_alloc(new);
1217 cmp
= pkg_compare_versions(old
, new);
1218 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
1219 opkg_msg(DEBUG
, "Forcing downgrade\n");
1220 cmp
= -1; /* then we force opkg to downgrade */
1221 /* We need to use a value < 0 because in the 0 case we are asking to */
1222 /* reinstall, and some check could fail asking the "force-reinstall" option */
1224 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1225 "\n\t%s is installed "
1226 "\n\t%s is available "
1227 "\n\t%d was comparison result\n",
1228 pkg_name
, old_version
, new_version
, cmp
);
1231 "Package %s (%s) installed in %s is up to date.\n",
1232 old
->name
, old_version
, old
->dest
->name
);
1236 } else if (cmp
> 0) {
1238 "Not downgrading package %s on %s from %s to %s.\n",
1239 old
->name
, old
->dest
->name
, old_version
,
1244 } else if (cmp
< 0) {
1245 new->dest
= old
->dest
;
1246 old
->state_want
= SW_DEINSTALL
;
1252 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
1253 return opkg_install_pkg(new, 0);
1257 * @brief Really install a pkg_t
1259 int opkg_install_pkg(pkg_t
* pkg
, int from_upgrade
)
1263 pkg_t
*old_pkg
= NULL
;
1264 pkg_vec_t
*replacees
;
1265 abstract_pkg_t
*ab_pkg
= NULL
;
1267 char *file_md5
, *pkg_md5
;
1268 char *file_sha256
, *pkg_sha256
;
1269 sigset_t newset
, oldset
;
1270 const char *local_filename
;
1274 message
= 1; /* Coming from an upgrade, and should change the output message */
1276 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1278 if (!pkg_arch_supported(pkg
)) {
1280 "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1281 pkg_get_architecture(pkg
), pkg
->name
);
1284 if (pkg
->state_status
== SS_INSTALLED
&& conf
->nodeps
== 0) {
1285 err
= satisfy_dependencies_for(pkg
);
1289 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1290 pkg
->name
, pkg
->dest
->name
);
1294 if (pkg
->dest
== NULL
) {
1295 pkg
->dest
= conf
->default_dest
;
1298 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1300 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1304 pkg
->state_want
= SW_INSTALL
;
1306 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependencies */
1309 err
= check_conflicts_for(pkg
);
1313 /* this setup is to remove the upgrade scenario in the end when
1314 installing pkg A, A deps B & B deps on A. So both B and A are
1315 installed. Then A's installation is started resulting in an
1316 uncecessary upgrade */
1317 if (pkg
->state_status
== SS_INSTALLED
)
1320 err
= verify_pkg_installable(pkg
);
1324 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1326 if (local_filename
== NULL
) {
1327 if (!conf
->cache
&& conf
->download_only
) {
1329 if (getcwd(cwd
, sizeof(cwd
)) != NULL
)
1330 err
= opkg_download_pkg(pkg
, cwd
);
1334 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1337 opkg_msg(ERROR
, "Failed to download %s. "
1338 "Perhaps you need to run 'opkg update'?\n",
1343 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1346 /* check that the repository is valid */
1347 #if defined(HAVE_USIGN)
1348 char *list_file_name
, *sig_file_name
, *lists_dir
;
1350 /* check to ensure the package has come from a repository */
1351 if (conf
->check_signature
&& pkg
->src
) {
1352 sprintf_alloc(&lists_dir
, "%s", (conf
->restrict_to_default_dest
)
1353 ? conf
->default_dest
->lists_dir
1355 sprintf_alloc(&list_file_name
, "%s/%s", lists_dir
,
1357 sprintf_alloc(&sig_file_name
, "%s/%s.sig", lists_dir
,
1360 if (file_exists(sig_file_name
)) {
1361 if (opkg_verify_file(list_file_name
, sig_file_name
)) {
1363 "Failed to verify the signature of %s.\n",
1365 if (!conf
->force_signature
)
1369 opkg_msg(ERROR
, "Signature file is missing for %s. "
1370 "Perhaps you need to run 'opkg update'?\n",
1372 if (!conf
->force_signature
)
1377 free(list_file_name
);
1378 free(sig_file_name
);
1382 /* Check for md5 values */
1383 pkg_md5
= pkg_get_md5(pkg
);
1385 file_md5
= file_md5sum_alloc(local_filename
);
1386 if (file_md5
&& strcmp(file_md5
, pkg_md5
)) {
1387 if (!conf
->force_checksum
) {
1388 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1389 "Either the opkg or the package index are corrupt. "
1390 "Try 'opkg update'.\n", pkg
->name
);
1395 "Ignored %s md5sum mismatch.\n",
1403 /* Check for sha256 value */
1404 pkg_sha256
= pkg_get_sha256(pkg
);
1406 file_sha256
= file_sha256sum_alloc(local_filename
);
1407 if (file_sha256
&& strcmp(file_sha256
, pkg_sha256
)) {
1408 if (!conf
->force_checksum
) {
1410 "Package %s sha256sum mismatch. "
1411 "Either the opkg or the package index are corrupt. "
1412 "Try 'opkg update'.\n", pkg
->name
);
1417 "Ignored %s sha256sum mismatch.\n",
1425 if (conf
->download_only
) {
1426 if (conf
->nodeps
== 0) {
1427 err
= satisfy_dependencies_for(pkg
);
1434 if (!pkg_get_string(pkg
, PKG_TMP_UNPACK_DIR
)) {
1435 if (unpack_pkg_control_files(pkg
) == -1) {
1437 "Failed to unpack control files from %s.\n",
1443 err
= update_file_ownership(pkg
, old_pkg
);
1447 if (conf
->nodeps
== 0) {
1448 err
= satisfy_dependencies_for(pkg
);
1451 if (pkg
->state_status
== SS_UNPACKED
)
1452 /* Circular dependency has installed it for us. */
1456 replacees
= pkg_vec_alloc();
1457 pkg_get_installed_replacees(pkg
, replacees
);
1459 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1461 sigemptyset(&newset
);
1462 sigaddset(&newset
, SIGINT
);
1463 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1465 opkg_state_changed
++;
1466 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1469 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1470 old_pkg
->is_upgrade
= 1;
1471 pkg
->is_upgrade
= 1;
1473 /* XXX: BUG: we really should treat replacement more like an upgrade
1474 * Instead, we're going to remove the replacees
1476 err
= pkg_remove_installed_replacees(replacees
);
1478 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1480 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1482 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1484 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1486 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1488 err
= preinst_configure(pkg
, old_pkg
);
1490 goto UNWIND_PREINST_CONFIGURE
;
1492 err
= backup_modified_conffiles(pkg
, old_pkg
);
1494 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1496 err
= check_data_file_clashes(pkg
, old_pkg
);
1498 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1500 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1502 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1507 /* point of no return: no unwinding after this */
1509 old_pkg
->state_want
= SW_DEINSTALL
;
1511 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1512 opkg_msg(INFO
, "Not removing obsolesced files because "
1513 "package %s marked noprune.\n", old_pkg
->name
);
1515 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1517 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1518 opkg_msg(ERROR
, "Failed to determine "
1519 "obsolete files from previously "
1520 "installed %s\n", old_pkg
->name
);
1524 /* removing files from old package, to avoid ghost files */
1525 remove_data_files_and_list(old_pkg
);
1526 remove_maintainer_scripts(old_pkg
);
1529 opkg_msg(INFO
, "%s maintainer scripts.\n",
1530 (pkg
->is_upgrade
) ? ("Upgrading") : ("Installing"));
1531 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1532 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1533 " Package debris may remain!\n", pkg
->name
);
1537 /* the following just returns 0 */
1538 remove_disappeared(pkg
);
1540 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1542 if (install_data_files(pkg
)) {
1543 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1544 "Package debris may remain!\n", pkg
->name
);
1548 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1550 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1551 "for files belonging to %s.\n", pkg
->name
);
1554 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1555 resolve_conffiles(pkg
);
1557 pkg
->state_status
= SS_UNPACKED
;
1558 old_state_flag
= pkg
->state_flag
;
1559 pkg
->state_flag
&= ~SF_PREFER
;
1560 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1561 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1564 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1567 pkg_set_int(pkg
, PKG_INSTALLED_TIME
, now
);
1569 ab_pkg
= pkg
->parent
;
1571 ab_pkg
->state_status
= pkg
->state_status
;
1573 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1574 pkg_vec_free(replacees
);
1577 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1578 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1579 UNWIND_CHECK_DATA_FILE_CLASHES
:
1580 check_data_file_clashes_unwind(pkg
, old_pkg
);
1581 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1582 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1583 UNWIND_PREINST_CONFIGURE
:
1584 preinst_configure_unwind(pkg
, old_pkg
);
1585 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1586 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1587 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1588 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1589 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1590 pkg_remove_installed_replacees_unwind(replacees
);
1593 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1595 pkg_vec_free(replacees
);