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
, *prev
= NULL
;
51 ndepends
= pkg_hash_fetch_unsatisfied_dependencies(pkg
, depends
,
56 "Cannot satisfy the following dependencies for %s:\n",
60 if (!prev
|| strcmp(*unresolved
, prev
))
61 opkg_message(ERROR
, "\t%s\n", *unresolved
);
71 if (!conf
->force_depends
) {
73 "This could mean that your package list is out of date or that the packages\n"
74 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
75 "of this problem try again with the '-force-depends' option.\n");
76 pkg_vec_free(depends
);
82 pkg_vec_free(depends
);
86 /* Mark packages as to-be-installed */
87 for (i
= 0; i
< depends
->len
; i
++) {
88 /* Dependencies should be installed the same place as pkg */
89 if (depends
->pkgs
[i
]->dest
== NULL
) {
90 depends
->pkgs
[i
]->dest
= pkg
->dest
;
92 depends
->pkgs
[i
]->state_want
= SW_INSTALL
;
95 for (i
= 0; i
< depends
->len
; i
++) {
96 dep
= depends
->pkgs
[i
];
97 /* The package was uninstalled when we started, but another
98 dep earlier in this loop may have depended on it and pulled
99 it in, so check first. */
100 if ((dep
->state_status
!= SS_INSTALLED
)
101 && (dep
->state_status
!= SS_UNPACKED
)) {
102 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
103 err
= opkg_install_pkg(dep
, 0);
104 /* mark this package as having been automatically installed to
105 * satisfy a dependancy */
106 dep
->auto_installed
= 1;
108 pkg_vec_free(depends
);
114 pkg_vec_free(depends
);
119 static int check_conflicts_for(pkg_t
* pkg
)
122 pkg_vec_t
*conflicts
= NULL
;
123 message_level_t level
;
125 if (conf
->force_depends
) {
131 if (!conf
->force_depends
)
132 conflicts
= pkg_hash_fetch_conflicts(pkg
);
135 opkg_msg(level
, "The following packages conflict with %s:\n",
138 while (i
< conflicts
->len
)
139 opkg_msg(level
, "\t%s", conflicts
->pkgs
[i
++]->name
);
140 opkg_message(level
, "\n");
141 pkg_vec_free(conflicts
);
147 static int update_file_ownership(pkg_t
* new_pkg
, pkg_t
* old_pkg
)
149 str_list_t
*new_list
, *old_list
;
150 str_list_elt_t
*iter
, *niter
;
152 new_list
= pkg_get_installed_files(new_pkg
);
153 if (new_list
== NULL
)
156 for (iter
= str_list_first(new_list
), niter
=
157 str_list_next(new_list
, iter
); iter
;
158 iter
= niter
, niter
= str_list_next(new_list
, niter
)) {
159 char *new_file
= (char *)iter
->data
;
160 pkg_t
*owner
= file_hash_get_file_owner(new_file
);
161 pkg_t
*obs
= hash_table_get(&conf
->obs_file_hash
, new_file
);
164 "%s: new_pkg=%s wants file %s, from owner=%s\n",
165 __func__
, new_pkg
->name
, new_file
,
166 owner
? owner
->name
: "<NULL>");
168 if (!owner
|| (owner
== old_pkg
) || obs
)
169 file_hash_set_file_owner(new_file
, new_pkg
);
173 old_list
= pkg_get_installed_files(old_pkg
);
174 if (old_list
== NULL
) {
175 pkg_free_installed_files(new_pkg
);
179 for (iter
= str_list_first(old_list
), niter
=
180 str_list_next(old_list
, iter
); iter
;
181 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
182 char *old_file
= (char *)iter
->data
;
183 pkg_t
*owner
= file_hash_get_file_owner(old_file
);
184 if (!owner
|| (owner
== old_pkg
)) {
186 hash_table_insert(&conf
->obs_file_hash
,
190 pkg_free_installed_files(old_pkg
);
192 pkg_free_installed_files(new_pkg
);
196 static int verify_pkg_installable(pkg_t
* pkg
)
198 unsigned long kbs_available
, pkg_size_kbs
;
199 unsigned long installed_size
;
200 char *root_dir
= NULL
;
203 installed_size
= (unsigned long) pkg_get_int(pkg
, PKG_INSTALLED_SIZE
);
205 if (conf
->force_space
|| installed_size
== 0)
209 if (!strcmp(pkg
->dest
->name
, "root") && conf
->overlay_root
210 && !stat(conf
->overlay_root
, &s
) && (s
.st_mode
& S_IFDIR
))
211 root_dir
= conf
->overlay_root
;
213 root_dir
= pkg
->dest
->root_dir
;
217 root_dir
= conf
->default_dest
->root_dir
;
219 kbs_available
= get_available_kbytes(root_dir
);
221 pkg_size_kbs
= (installed_size
+ 1023) / 1024;
223 if (pkg_size_kbs
>= kbs_available
) {
224 opkg_msg(ERROR
, "Only have %ldkb available on filesystem %s, "
225 "pkg %s needs %ld\n",
226 kbs_available
, root_dir
, pkg
->name
, pkg_size_kbs
);
233 static int unpack_pkg_control_files(pkg_t
* pkg
)
236 char *conffiles_file_name
;
238 char *tmp_unpack_dir
;
239 FILE *conffiles_file
;
242 sprintf_alloc(&tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
,
245 tmp_unpack_dir
= mkdtemp(tmp_unpack_dir
);
246 if (tmp_unpack_dir
== NULL
) {
247 opkg_perror(ERROR
, "Failed to create temporary directory");
251 pkg_set_string(pkg
, PKG_TMP_UNPACK_DIR
, tmp_unpack_dir
);
253 err
= pkg_extract_control_files_to_dir(pkg
, tmp_unpack_dir
);
258 /* XXX: CLEANUP: There might be a cleaner place to read in the
259 conffiles. Seems like I should be able to get everything to go
260 through pkg_init_from_file. If so, maybe it would make sense to
261 move all of unpack_pkg_control_files to that function. */
263 /* Don't need to re-read conffiles if we already have it */
264 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
265 if (cl
&& !nv_pair_list_empty(cl
)) {
269 sprintf_alloc(&conffiles_file_name
, "%s/conffiles",
271 if (!file_exists(conffiles_file_name
)) {
272 free(conffiles_file_name
);
276 conffiles_file
= fopen(conffiles_file_name
, "r");
277 if (conffiles_file
== NULL
) {
278 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
279 free(conffiles_file_name
);
282 free(conffiles_file_name
);
284 cl
= xcalloc(1, sizeof(*cl
));
285 conffile_list_init(cl
);
289 char *cf_name_in_dest
;
292 cf_name
= file_read_line_alloc(conffiles_file
);
293 if (cf_name
== NULL
) {
296 if (cf_name
[0] == '\0') {
299 for (i
= strlen(cf_name
) - 1;
300 (i
>= 0) && (cf_name
[i
] == ' ' || cf_name
[i
] == '\t');
305 /* Prepend dest->root_dir to conffile name.
306 Take pains to avoid multiple slashes. */
307 root_dir
= pkg
->dest
->root_dir
;
308 if (conf
->offline_root
)
309 /* skip the offline_root prefix */
311 pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
312 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
313 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
315 /* Can't get an md5sum now, (file isn't extracted yet).
316 We'll wait until resolve_conffiles */
317 conffile_list_append(cl
, cf_name_in_dest
, NULL
);
320 free(cf_name_in_dest
);
323 pkg_set_ptr(pkg
, PKG_CONFFILES
, cl
);
325 fclose(conffiles_file
);
331 * Remove packages which were auto_installed due to a dependency by old_pkg,
332 * which are no longer a dependency in the new (upgraded) pkg.
334 static int pkg_remove_orphan_dependent(pkg_t
* pkg
, pkg_t
* old_pkg
)
336 int j
, l
, found
, r
, err
= 0;
339 struct compound_depend
*cd0
, *cd1
;
340 abstract_pkg_t
**dependents
;
342 for (cd0
= pkg_get_ptr(old_pkg
, PKG_DEPENDS
); cd0
&& cd0
->type
; cd0
++) {
343 if (cd0
->type
!= DEPEND
)
345 for (j
= 0; j
< cd0
->possibility_count
; j
++) {
349 for (cd1
= pkg_get_ptr(pkg
, PKG_DEPENDS
); cd1
&& cd1
->type
; cd1
++) {
350 if (cd1
->type
!= DEPEND
)
352 for (l
= 0; l
< cd1
->possibility_count
; l
++) {
353 if (cd0
->possibilities
[j
]
354 == cd1
->possibilities
[l
]) {
367 * old_pkg has a dependency that pkg does not.
369 p
= pkg_hash_fetch_installed_by_name(cd0
->
376 if (!p
->auto_installed
)
379 n_deps
= pkg_has_installed_dependents(p
, &dependents
);
380 n_deps
--; /* don't count old_pkg */
383 opkg_msg(NOTICE
, "%s was autoinstalled and is "
384 "now orphaned, removing.\n", p
->name
);
386 /* p has one installed dependency (old_pkg),
387 * which we need to ignore during removal. */
388 p
->state_flag
|= SF_REPLACE
;
390 r
= opkg_remove_pkg(p
, 0);
394 opkg_msg(INFO
, "%s was autoinstalled and is "
395 "still required by %d "
396 "installed packages.\n",
405 /* returns number of installed replacees */
407 pkg_get_installed_replacees(pkg_t
* pkg
, pkg_vec_t
* installed_replacees
)
409 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
412 while (replaces
&& *replaces
) {
413 abstract_pkg_t
*ab_pkg
= *replaces
++;
414 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
416 for (j
= 0; j
< pkg_vec
->len
; j
++) {
417 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
418 if (!pkg_conflicts(pkg
, replacee
))
420 if (replacee
->state_status
== SS_INSTALLED
) {
421 pkg_vec_insert(installed_replacees
,
428 return installed_replacees
->len
;
431 static int pkg_remove_installed_replacees(pkg_vec_t
* replacees
)
434 int replaces_count
= replacees
->len
;
435 for (i
= 0; i
< replaces_count
; i
++) {
436 pkg_t
*replacee
= replacees
->pkgs
[i
];
438 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
439 err
= opkg_remove_pkg(replacee
, 0);
446 /* to unwind the removal: make sure they are installed */
447 static int pkg_remove_installed_replacees_unwind(pkg_vec_t
* replacees
)
450 int replaces_count
= replacees
->len
;
451 for (i
= 0; i
< replaces_count
; i
++) {
452 pkg_t
*replacee
= replacees
->pkgs
[i
];
453 if (replacee
->state_status
!= SS_INSTALLED
) {
454 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
455 err
= opkg_install_pkg(replacee
, 0);
463 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
465 opkg_install_check_downgrade(pkg_t
* pkg
, pkg_t
* old_pkg
, int message
)
468 char message_out
[15];
469 char *old_version
= pkg_version_str_alloc(old_pkg
);
470 char *new_version
= pkg_version_str_alloc(pkg
);
471 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
474 memset(message_out
, '\x0', 15);
475 strncpy(message_out
, "Upgrading ", strlen("Upgrading "));
476 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
477 cmp
= -1; /* then we force opkg to downgrade */
478 strncpy(message_out
, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
479 /* reinstall, and some check could fail asking the "force-reinstall" option */
483 if (!conf
->download_only
)
485 "Not downgrading package %s on %s from %s to %s.\n",
486 old_pkg
->name
, old_pkg
->dest
->name
,
487 old_version
, new_version
);
489 } else if (cmp
< 0) {
490 if (!conf
->download_only
)
492 "%s%s on %s from %s to %s...\n",
493 message_out
, pkg
->name
,
494 old_pkg
->dest
->name
, old_version
,
496 pkg
->dest
= old_pkg
->dest
;
498 } else { /* cmp == 0 */
500 if (!conf
->download_only
)
502 "%s (%s) already install on %s.\n",
503 pkg
->name
, new_version
,
504 old_pkg
->dest
->name
);
511 char message_out
[15];
512 memset(message_out
, '\x0', 15);
514 strncpy(message_out
, "Upgrading ",
515 strlen("Upgrading "));
517 strncpy(message_out
, "Installing ",
518 strlen("Installing "));
519 char *version
= pkg_version_str_alloc(pkg
);
521 if (!conf
->download_only
)
522 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
523 pkg
->name
, version
, pkg
->dest
->name
);
529 static int prerm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
531 /* DPKG_INCOMPATIBILITY:
532 dpkg does some things here that we don't do yet. Do we care?
534 1. If a version of the package is already installed, call
535 old-prerm upgrade new-version
536 2. If the script runs but exits with a non-zero exit status
537 new-prerm failed-upgrade old-version
538 Error unwind, for both the above cases:
539 old-postinst abort-upgrade new-version
545 if (!old_pkg
|| !pkg
)
548 new_version
= pkg_version_str_alloc(pkg
);
550 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
552 err
= pkg_run_script(old_pkg
, "prerm", script_args
);
555 opkg_msg(ERROR
, "prerm script for package \"%s\" failed\n",
562 static int prerm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
564 /* DPKG_INCOMPATIBILITY:
565 dpkg does some things here that we don't do yet. Do we care?
566 (See prerm_upgrade_old_package for details)
571 static int prerm_deconfigure_conflictors(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
573 /* DPKG_INCOMPATIBILITY:
574 dpkg does some things here that we don't do yet. Do we care?
575 2. If a 'conflicting' package is being removed at the same time:
576 1. If any packages depended on that conflicting package and
577 --auto-deconfigure is specified, call, for each such package:
578 deconfigured's-prerm deconfigure \
579 in-favour package-being-installed version \
580 removing conflicting-package version
582 deconfigured's-postinst abort-deconfigure \
583 in-favour package-being-installed-but-failed version \
584 removing conflicting-package version
586 The deconfigured packages are marked as requiring
587 configuration, so that if --install is used they will be
588 configured again if possible.
589 2. To prepare for removal of the conflicting package, call:
590 conflictor's-prerm remove in-favour package new-version
592 conflictor's-postinst abort-remove in-favour package new-version
598 prerm_deconfigure_conflictors_unwind(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
600 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
601 do yet. Do we care? (See prerm_deconfigure_conflictors for
606 static int preinst_configure(pkg_t
* pkg
, pkg_t
* old_pkg
)
612 char *old_version
= pkg_version_str_alloc(old_pkg
);
613 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
615 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
616 char *pkg_version
= pkg_version_str_alloc(pkg
);
617 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
620 preinst_args
= xstrdup("install");
623 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
625 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
634 static int preinst_configure_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
636 /* DPKG_INCOMPATIBILITY:
637 dpkg does the following error unwind, should we?
638 pkg->postrm abort-upgrade old-version
639 OR pkg->postrm abort-install old-version
640 OR pkg->postrm abort-install
645 static char *backup_filename_alloc(const char *file_name
)
649 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
654 static int backup_make_backup(const char *file_name
)
659 backup
= backup_filename_alloc(file_name
);
660 err
= file_copy(file_name
, backup
);
662 opkg_msg(ERROR
, "Failed to copy %s to %s\n", file_name
, backup
);
670 static int backup_exists_for(const char *file_name
)
675 backup
= backup_filename_alloc(file_name
);
677 ret
= file_exists(backup
);
684 static int backup_remove(const char *file_name
)
688 backup
= backup_filename_alloc(file_name
);
695 static int backup_modified_conffiles(pkg_t
* pkg
, pkg_t
* old_pkg
)
698 conffile_list_elt_t
*iter
;
705 /* Backup all modified conffiles */
707 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
709 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
710 iter
= nv_pair_list_next(cl
, iter
)) {
714 cf_name
= root_filename_alloc(cf
->name
);
716 /* Don't worry if the conffile is just plain gone */
717 if (file_exists(cf_name
)
718 && conffile_has_been_modified(cf
)) {
719 err
= backup_make_backup(cf_name
);
728 /* Backup all conffiles that were not conffiles in old_pkg */
729 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
731 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
732 iter
= nv_pair_list_next(cl
, iter
)) {
734 cf
= (conffile_t
*) iter
->data
;
735 cf_name
= root_filename_alloc(cf
->name
);
736 /* Ignore if this was a conffile in old_pkg as well */
737 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
741 if (file_exists(cf_name
) && (!backup_exists_for(cf_name
))) {
742 err
= backup_make_backup(cf_name
);
753 static int backup_modified_conffiles_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
756 conffile_list_elt_t
*iter
;
759 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
761 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
762 iter
= nv_pair_list_next(cl
, iter
)) {
763 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
767 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
769 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
770 iter
= nv_pair_list_next(cl
, iter
)) {
771 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
777 static int check_data_file_clashes(pkg_t
* pkg
, pkg_t
* old_pkg
)
779 /* DPKG_INCOMPATIBILITY:
780 opkg takes a slightly different approach than dpkg at this
781 point. dpkg installs each file in the new package while
782 creating a backup for any file that is replaced, (so that it
783 can unwind if necessary). To avoid complexity and redundant
784 storage, opkg doesn't do any installation until later, (at the
785 point at which dpkg removes the backups.
787 But, we do have to check for data file clashes, since after
788 installing a package with a file clash, removing either of the
789 packages involved in the clash has the potential to break the
792 str_list_t
*files_list
;
793 str_list_elt_t
*iter
, *niter
;
797 files_list
= pkg_get_installed_files(pkg
);
798 if (files_list
== NULL
)
801 for (iter
= str_list_first(files_list
), niter
=
802 str_list_next(files_list
, iter
); iter
;
803 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
804 filename
= (char *)iter
->data
;
805 if (file_exists(filename
) && (!file_is_dir(filename
))) {
809 if (backup_exists_for(filename
)) {
813 /* Pre-existing files are OK if force-overwrite was asserted. */
814 if (conf
->force_overwrite
) {
815 /* but we need to change who owns this file */
816 file_hash_set_file_owner(filename
, pkg
);
820 owner
= file_hash_get_file_owner(filename
);
822 /* Pre-existing files are OK if owned by the pkg being upgraded. */
823 if (owner
&& old_pkg
) {
824 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
829 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
832 "Checking replaces for %s in package %s\n",
833 filename
, owner
->name
);
834 if (pkg_replaces(pkg
, owner
)) {
837 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
838 then it's ok to overwrite. */
839 if (strcmp(owner
->name
, pkg
->name
) == 0) {
841 "Replacing pre-existing file %s"
842 " owned by package %s\n",
843 filename
, owner
->name
);
848 /* Pre-existing files are OK if they are obsolete */
849 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
852 "Pre-exiting file %s is obsolete."
853 " obs_pkg=%s\n", filename
, obs
->name
);
857 /* We have found a clash. */
858 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
859 "\tBut that file is already provided by package ",
860 pkg
->name
, filename
);
862 opkg_message(ERROR
, "%s\n", owner
->name
);
864 opkg_message(ERROR
, "<no package>\n"
865 "Please move this file out of the way and try again.\n");
870 pkg_free_installed_files(pkg
);
876 * XXX: This function sucks, as does the below comment.
878 static int check_data_file_clashes_change(pkg_t
* pkg
, pkg_t
* old_pkg
)
880 /* Basically that's the worst hack I could do to be able to change ownership of
881 file list, but, being that we have no way to unwind the mods, due to structure
882 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
883 What we do here is change the ownership of file in hash if a replace ( or similar events
885 Only the action that are needed to change name should be considered.
886 @@@ To change after 1.0 release.
888 str_list_t
*files_list
;
889 str_list_elt_t
*iter
, *niter
;
891 files_list
= pkg_get_installed_files(pkg
);
892 if (files_list
== NULL
)
895 for (iter
= str_list_first(files_list
), niter
=
896 str_list_next(files_list
, iter
); iter
;
897 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
898 char *filename
= (char *)iter
->data
;
899 if (file_exists(filename
) && (!file_is_dir(filename
))) {
902 owner
= file_hash_get_file_owner(filename
);
904 if (conf
->force_overwrite
) {
905 /* but we need to change who owns this file */
906 file_hash_set_file_owner(filename
, pkg
);
910 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
912 if (pkg_replaces(pkg
, owner
)) {
913 /* It's now time to change the owner of that file.
914 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
916 "Replacing pre-existing file %s "
917 "owned by package %s\n",
918 filename
, owner
->name
);
919 file_hash_set_file_owner(filename
, pkg
);
926 pkg_free_installed_files(pkg
);
931 static int check_data_file_clashes_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
933 /* Nothing to do since check_data_file_clashes doesn't change state */
937 static int postrm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
939 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
940 1. If the package is being upgraded, call
941 old-postrm upgrade new-version
942 2. If this fails, attempt:
943 new-postrm failed-upgrade old-version
944 Error unwind, for both cases:
945 old-preinst abort-upgrade new-version */
950 if (!old_pkg
|| !pkg
)
953 new_version
= pkg_version_str_alloc(pkg
);
955 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
957 err
= pkg_run_script(old_pkg
, "postrm", script_args
);
960 opkg_msg(ERROR
, "postrm script for package \"%s\" failed\n",
967 static int postrm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
969 /* DPKG_INCOMPATIBILITY:
970 dpkg does some things here that we don't do yet. Do we care?
971 (See postrm_upgrade_old_pkg for details)
976 static int remove_obsolesced_files(pkg_t
* pkg
, pkg_t
* old_pkg
)
979 str_list_t
*old_files
;
981 str_list_t
*new_files
;
983 hash_table_t new_files_table
;
985 old_files
= pkg_get_installed_files(old_pkg
);
986 if (old_files
== NULL
)
989 new_files
= pkg_get_installed_files(pkg
);
990 if (new_files
== NULL
) {
991 pkg_free_installed_files(old_pkg
);
995 new_files_table
.entries
= NULL
;
996 hash_table_init("new_files", &new_files_table
, 20);
997 for (nf
= str_list_first(new_files
); nf
;
998 nf
= str_list_next(new_files
, nf
)) {
1000 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
1003 for (of
= str_list_first(old_files
); of
;
1004 of
= str_list_next(old_files
, of
)) {
1007 old
= (char *)of
->data
;
1008 new = (char *)hash_table_get(&new_files_table
, old
);
1012 if (file_is_dir(old
)) {
1015 owner
= file_hash_get_file_owner(old
);
1016 if (owner
!= old_pkg
) {
1017 /* in case obsolete file no longer belongs to old_pkg */
1021 /* old file is obsolete */
1022 opkg_msg(NOTICE
, "Removing obsolete file %s.\n", old
);
1023 if (!conf
->noaction
) {
1026 opkg_perror(ERROR
, "unlinking %s failed", old
);
1031 hash_table_deinit(&new_files_table
);
1032 pkg_free_installed_files(old_pkg
);
1033 pkg_free_installed_files(pkg
);
1038 static int install_maintainer_scripts(pkg_t
* pkg
, pkg_t
* old_pkg
)
1043 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1044 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1045 pkg
->dest
->info_dir
,
1051 static int remove_disappeared(pkg_t
* pkg
)
1053 /* DPKG_INCOMPATIBILITY:
1054 This is a fairly sophisticated dpkg operation. Shall we
1057 /* Any packages all of whose files have been overwritten during the
1058 installation, and which aren't required for dependencies, are
1059 considered to have been removed. For each such package
1060 1. disappearer's-postrm disappear overwriter overwriter-version
1061 2. The package's maintainer scripts are removed
1062 3. It is noted in the status database as being in a sane state,
1063 namely not installed (any conffiles it may have are ignored,
1064 rather than being removed by dpkg). Note that disappearing
1065 packages do not have their prerm called, because dpkg doesn't
1066 know in advance that the package is going to vanish.
1071 static int install_data_files(pkg_t
* pkg
)
1075 /* opkg takes a slightly different approach to data file backups
1076 than dpkg. Rather than removing backups at this point, we
1077 actually do the data file installation now. See comments in
1078 check_data_file_clashes() for more details. */
1080 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1081 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1086 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1087 err
= pkg_write_filelist(pkg
);
1091 /* XXX: FEATURE: opkg should identify any files which existed
1092 before installation and which were overwritten, (see
1093 check_data_file_clashes()). What it must do is remove any such
1094 files from the filelist of the old package which provided the
1095 file. Otherwise, if the old package were removed at some point
1096 it would break the new package. Removing the new package will
1097 also break the old one, but this cannot be helped since the old
1098 package's file has already been deleted. This is the importance
1099 of check_data_file_clashes(), and only allowing opkg to install
1100 a clashing package with a user force. */
1105 static int resolve_conffiles(pkg_t
* pkg
)
1107 conffile_list_elt_t
*iter
;
1108 conffile_list_t
*cl
;
1116 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
1118 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
1119 iter
= nv_pair_list_next(cl
, iter
)) {
1120 char *root_filename
;
1121 cf
= (conffile_t
*) iter
->data
;
1122 root_filename
= root_filename_alloc(cf
->name
);
1124 /* Might need to initialize the md5sum for each conffile */
1125 if (cf
->value
== NULL
) {
1126 cf
->value
= file_sha256sum_alloc(root_filename
);
1129 if (!file_exists(root_filename
)) {
1130 free(root_filename
);
1134 cf_backup
= backup_filename_alloc(root_filename
);
1136 if (file_exists(cf_backup
)) {
1137 /* Let's compute md5 to test if files are changed */
1138 if (cf
->value
&& strlen(cf
->value
) > 33) {
1139 chksum
= file_sha256sum_alloc(cf_backup
);
1141 chksum
= file_md5sum_alloc(cf_backup
);
1144 if (chksum
&& cf
->value
1145 && strcmp(cf
->value
, chksum
) != 0) {
1146 if (conf
->force_maintainer
) {
1148 "Conffile %s using maintainer's setting.\n",
1152 sprintf_alloc(&new_conffile
, "%s-opkg",
1155 "Existing conffile %s "
1156 "is different from the conffile in the new package."
1157 " The new conffile will be placed at %s.\n",
1158 root_filename
, new_conffile
);
1159 rename(root_filename
, new_conffile
);
1160 rename(cf_backup
, root_filename
);
1170 free(root_filename
);
1176 int opkg_install_by_name(const char *pkg_name
)
1180 char *old_version
, *new_version
;
1182 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1184 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1185 pkg_get_string(old
, PKG_VERSION
));
1187 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1189 opkg_msg(NOTICE
, "Unknown package '%s'.\n", pkg_name
);
1193 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1195 opkg_message(DEBUG2
, " old %s ", pkg_get_string(old
, PKG_VERSION
));
1196 opkg_message(DEBUG2
, " new %s\n", pkg_get_string(new, PKG_VERSION
));
1198 new->state_flag
|= SF_USER
;
1200 old_version
= pkg_version_str_alloc(old
);
1201 new_version
= pkg_version_str_alloc(new);
1203 cmp
= pkg_compare_versions(old
, new);
1204 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
1205 opkg_msg(DEBUG
, "Forcing downgrade\n");
1206 cmp
= -1; /* then we force opkg to downgrade */
1207 /* We need to use a value < 0 because in the 0 case we are asking to */
1208 /* reinstall, and some check could fail asking the "force-reinstall" option */
1210 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1211 "\n\t%s is installed "
1212 "\n\t%s is available "
1213 "\n\t%d was comparison result\n",
1214 pkg_name
, old_version
, new_version
, cmp
);
1217 "Package %s (%s) installed in %s is up to date.\n",
1218 old
->name
, old_version
, old
->dest
->name
);
1222 } else if (cmp
> 0) {
1224 "Not downgrading package %s on %s from %s to %s.\n",
1225 old
->name
, old
->dest
->name
, old_version
,
1230 } else if (cmp
< 0) {
1231 new->dest
= old
->dest
;
1232 old
->state_want
= SW_DEINSTALL
;
1238 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
1239 return opkg_install_pkg(new, 0);
1243 * @brief Really install a pkg_t
1245 int opkg_install_pkg(pkg_t
* pkg
, int from_upgrade
)
1249 pkg_t
*old_pkg
= NULL
;
1250 pkg_vec_t
*replacees
;
1251 abstract_pkg_t
*ab_pkg
= NULL
;
1253 char *file_md5
, *pkg_md5
;
1254 char *file_sha256
, *pkg_sha256
;
1255 sigset_t newset
, oldset
;
1256 const char *local_filename
;
1257 long long int pkg_expected_size
;
1258 struct stat pkg_stat
;
1262 message
= 1; /* Coming from an upgrade, and should change the output message */
1264 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1266 if (!pkg_arch_supported(pkg
)) {
1268 "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1269 pkg_get_architecture(pkg
), pkg
->name
);
1272 if (pkg
->state_status
== SS_INSTALLED
&& conf
->nodeps
== 0) {
1273 err
= satisfy_dependencies_for(pkg
);
1277 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1278 pkg
->name
, pkg
->dest
->name
);
1282 if (pkg
->dest
== NULL
) {
1283 pkg
->dest
= conf
->default_dest
;
1286 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1288 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1292 pkg
->state_want
= SW_INSTALL
;
1294 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependencies */
1297 err
= check_conflicts_for(pkg
);
1301 /* this setup is to remove the upgrade scenario in the end when
1302 installing pkg A, A deps B & B deps on A. So both B and A are
1303 installed. Then A's installation is started resulting in an
1304 uncecessary upgrade */
1305 if (pkg
->state_status
== SS_INSTALLED
)
1308 err
= verify_pkg_installable(pkg
);
1312 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1314 if (local_filename
== NULL
) {
1315 if (!conf
->cache
&& conf
->download_only
) {
1317 if (getcwd(cwd
, sizeof(cwd
)) != NULL
)
1318 err
= opkg_download_pkg(pkg
, cwd
);
1322 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1325 opkg_msg(ERROR
, "Failed to download %s. "
1326 "Perhaps you need to run 'opkg update'?\n",
1331 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1334 /* check that the repository is valid */
1335 #if defined(HAVE_USIGN)
1336 char *list_file_name
, *sig_file_name
, *lists_dir
;
1338 /* check to ensure the package has come from a repository */
1339 if (conf
->check_signature
&& pkg
->src
) {
1340 sprintf_alloc(&lists_dir
, "%s", (conf
->restrict_to_default_dest
)
1341 ? conf
->default_dest
->lists_dir
1343 sprintf_alloc(&list_file_name
, "%s/%s", lists_dir
,
1345 sprintf_alloc(&sig_file_name
, "%s/%s.sig", lists_dir
,
1348 if (file_exists(sig_file_name
)) {
1349 if (opkg_verify_file(list_file_name
, sig_file_name
)) {
1351 "Failed to verify the signature of %s.\n",
1353 if (!conf
->force_signature
)
1357 opkg_msg(ERROR
, "Signature file is missing for %s. "
1358 "Perhaps you need to run 'opkg update'?\n",
1360 if (!conf
->force_signature
)
1365 free(list_file_name
);
1366 free(sig_file_name
);
1370 /* Check file size */
1371 err
= lstat(local_filename
, &pkg_stat
);
1374 opkg_msg(ERROR
, "Failed to stat %s: %s\n",
1375 local_filename
, strerror(errno
));
1379 pkg_expected_size
= pkg_get_int(pkg
, PKG_SIZE
);
1381 if (pkg_expected_size
> 0 && pkg_stat
.st_size
!= pkg_expected_size
) {
1382 if (!conf
->force_checksum
) {
1384 "Package size mismatch: %s is %lld bytes, expecting %lld bytes\n",
1385 pkg
->name
, (long long int)pkg_stat
.st_size
, pkg_expected_size
);
1389 "Ignored %s size mismatch.\n",
1394 /* Check for md5 values */
1395 pkg_md5
= pkg_get_md5(pkg
);
1397 file_md5
= file_md5sum_alloc(local_filename
);
1398 if (file_md5
&& strcmp(file_md5
, pkg_md5
)) {
1399 if (!conf
->force_checksum
) {
1400 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1401 "Either the opkg or the package index are corrupt. "
1402 "Try 'opkg update'.\n", pkg
->name
);
1407 "Ignored %s md5sum mismatch.\n",
1415 /* Check for sha256 value */
1416 pkg_sha256
= pkg_get_sha256(pkg
);
1418 file_sha256
= file_sha256sum_alloc(local_filename
);
1419 if (file_sha256
&& strcmp(file_sha256
, pkg_sha256
)) {
1420 if (!conf
->force_checksum
) {
1422 "Package %s sha256sum mismatch. "
1423 "Either the opkg or the package index are corrupt. "
1424 "Try 'opkg update'.\n", pkg
->name
);
1429 "Ignored %s sha256sum mismatch.\n",
1437 if (conf
->download_only
) {
1438 if (conf
->nodeps
== 0) {
1439 err
= satisfy_dependencies_for(pkg
);
1446 if (!pkg_get_string(pkg
, PKG_TMP_UNPACK_DIR
)) {
1447 if (unpack_pkg_control_files(pkg
) == -1) {
1449 "Failed to unpack control files from %s.\n",
1455 err
= update_file_ownership(pkg
, old_pkg
);
1459 if (conf
->nodeps
== 0) {
1460 err
= satisfy_dependencies_for(pkg
);
1463 if (pkg
->state_status
== SS_UNPACKED
)
1464 /* Circular dependency has installed it for us. */
1468 replacees
= pkg_vec_alloc();
1469 pkg_get_installed_replacees(pkg
, replacees
);
1471 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1473 sigemptyset(&newset
);
1474 sigaddset(&newset
, SIGINT
);
1475 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1477 opkg_state_changed
++;
1478 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1481 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1482 old_pkg
->is_upgrade
= 1;
1483 pkg
->is_upgrade
= 1;
1485 /* XXX: BUG: we really should treat replacement more like an upgrade
1486 * Instead, we're going to remove the replacees
1488 err
= pkg_remove_installed_replacees(replacees
);
1490 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1492 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1494 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1496 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1498 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1500 err
= preinst_configure(pkg
, old_pkg
);
1502 goto UNWIND_PREINST_CONFIGURE
;
1504 err
= backup_modified_conffiles(pkg
, old_pkg
);
1506 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1508 err
= check_data_file_clashes(pkg
, old_pkg
);
1510 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1512 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1514 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1519 /* point of no return: no unwinding after this */
1521 old_pkg
->state_want
= SW_DEINSTALL
;
1523 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1524 opkg_msg(INFO
, "Not removing obsolesced files because "
1525 "package %s marked noprune.\n", old_pkg
->name
);
1527 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1529 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1530 opkg_msg(ERROR
, "Failed to determine "
1531 "obsolete files from previously "
1532 "installed %s\n", old_pkg
->name
);
1536 /* removing files from old package, to avoid ghost files */
1537 remove_data_files_and_list(old_pkg
);
1538 remove_maintainer_scripts(old_pkg
);
1541 opkg_msg(INFO
, "%s maintainer scripts.\n",
1542 (pkg
->is_upgrade
) ? ("Upgrading") : ("Installing"));
1543 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1544 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1545 " Package debris may remain!\n", pkg
->name
);
1549 /* the following just returns 0 */
1550 remove_disappeared(pkg
);
1552 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1554 if (install_data_files(pkg
)) {
1555 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1556 "Package debris may remain!\n", pkg
->name
);
1560 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1562 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1563 "for files belonging to %s.\n", pkg
->name
);
1566 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1567 resolve_conffiles(pkg
);
1569 pkg
->state_status
= SS_UNPACKED
;
1570 old_state_flag
= pkg
->state_flag
;
1571 pkg
->state_flag
&= ~SF_PREFER
;
1572 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1573 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1576 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1579 pkg_set_int(pkg
, PKG_INSTALLED_TIME
, now
);
1581 ab_pkg
= pkg
->parent
;
1583 ab_pkg
->state_status
= pkg
->state_status
;
1585 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1586 pkg_vec_free(replacees
);
1589 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1590 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1591 UNWIND_CHECK_DATA_FILE_CLASHES
:
1592 check_data_file_clashes_unwind(pkg
, old_pkg
);
1593 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1594 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1595 UNWIND_PREINST_CONFIGURE
:
1596 preinst_configure_unwind(pkg
, old_pkg
);
1597 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1598 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1599 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1600 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1601 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1602 pkg_remove_installed_replacees_unwind(replacees
);
1605 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1607 pkg_vec_free(replacees
);