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 '%s'",
252 pkg_set_string(pkg
, PKG_TMP_UNPACK_DIR
, tmp_unpack_dir
);
254 err
= pkg_extract_control_files_to_dir(pkg
, tmp_unpack_dir
);
259 /* XXX: CLEANUP: There might be a cleaner place to read in the
260 conffiles. Seems like I should be able to get everything to go
261 through pkg_init_from_file. If so, maybe it would make sense to
262 move all of unpack_pkg_control_files to that function. */
264 /* Don't need to re-read conffiles if we already have it */
265 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
266 if (cl
&& !nv_pair_list_empty(cl
)) {
270 sprintf_alloc(&conffiles_file_name
, "%s/conffiles",
272 if (!file_exists(conffiles_file_name
)) {
273 free(conffiles_file_name
);
277 conffiles_file
= fopen(conffiles_file_name
, "r");
278 if (conffiles_file
== NULL
) {
279 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
280 free(conffiles_file_name
);
283 free(conffiles_file_name
);
285 cl
= xcalloc(1, sizeof(*cl
));
286 conffile_list_init(cl
);
290 char *cf_name_in_dest
;
293 cf_name
= file_read_line_alloc(conffiles_file
);
294 if (cf_name
== NULL
) {
297 if (cf_name
[0] == '\0') {
300 for (i
= strlen(cf_name
) - 1;
301 (i
>= 0) && (cf_name
[i
] == ' ' || cf_name
[i
] == '\t');
306 /* Prepend dest->root_dir to conffile name.
307 Take pains to avoid multiple slashes. */
308 root_dir
= pkg
->dest
->root_dir
;
309 if (conf
->offline_root
)
310 /* skip the offline_root prefix */
312 pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
313 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
314 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
316 /* Can't get an md5sum now, (file isn't extracted yet).
317 We'll wait until resolve_conffiles */
318 conffile_list_append(cl
, cf_name_in_dest
, NULL
);
321 free(cf_name_in_dest
);
324 pkg_set_ptr(pkg
, PKG_CONFFILES
, cl
);
326 fclose(conffiles_file
);
332 * Remove packages which were auto_installed due to a dependency by old_pkg,
333 * which are no longer a dependency in the new (upgraded) pkg.
335 static int pkg_remove_orphan_dependent(pkg_t
* pkg
, pkg_t
* old_pkg
)
337 int j
, l
, found
, r
, err
= 0;
340 struct compound_depend
*cd0
, *cd1
;
341 abstract_pkg_t
**dependents
;
343 for (cd0
= pkg_get_ptr(old_pkg
, PKG_DEPENDS
); cd0
&& cd0
->type
; cd0
++) {
344 if (cd0
->type
!= DEPEND
)
346 for (j
= 0; j
< cd0
->possibility_count
; j
++) {
350 for (cd1
= pkg_get_ptr(pkg
, PKG_DEPENDS
); cd1
&& cd1
->type
; cd1
++) {
351 if (cd1
->type
!= DEPEND
)
353 for (l
= 0; l
< cd1
->possibility_count
; l
++) {
354 if (cd0
->possibilities
[j
]
355 == cd1
->possibilities
[l
]) {
368 * old_pkg has a dependency that pkg does not.
370 p
= pkg_hash_fetch_installed_by_name(cd0
->
377 if (!p
->auto_installed
)
380 n_deps
= pkg_has_installed_dependents(p
, &dependents
);
381 n_deps
--; /* don't count old_pkg */
384 opkg_msg(NOTICE
, "%s was autoinstalled and is "
385 "now orphaned, removing.\n", p
->name
);
387 /* p has one installed dependency (old_pkg),
388 * which we need to ignore during removal. */
389 p
->state_flag
|= SF_REPLACE
;
391 r
= opkg_remove_pkg(p
, 0);
395 opkg_msg(INFO
, "%s was autoinstalled and is "
396 "still required by %d "
397 "installed packages.\n",
406 /* returns number of installed replacees */
408 pkg_get_installed_replacees(pkg_t
* pkg
, pkg_vec_t
* installed_replacees
)
410 abstract_pkg_t
**replaces
= pkg_get_ptr(pkg
, PKG_REPLACES
);
413 while (replaces
&& *replaces
) {
414 abstract_pkg_t
*ab_pkg
= *replaces
++;
415 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
417 for (j
= 0; j
< pkg_vec
->len
; j
++) {
418 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
419 if (!pkg_conflicts(pkg
, replacee
))
421 if (replacee
->state_status
== SS_INSTALLED
) {
422 pkg_vec_insert(installed_replacees
,
429 return installed_replacees
->len
;
432 static int pkg_remove_installed_replacees(pkg_vec_t
* replacees
)
435 int replaces_count
= replacees
->len
;
436 for (i
= 0; i
< replaces_count
; i
++) {
437 pkg_t
*replacee
= replacees
->pkgs
[i
];
439 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
440 err
= opkg_remove_pkg(replacee
, 0);
447 /* to unwind the removal: make sure they are installed */
448 static int pkg_remove_installed_replacees_unwind(pkg_vec_t
* replacees
)
451 int replaces_count
= replacees
->len
;
452 for (i
= 0; i
< replaces_count
; i
++) {
453 pkg_t
*replacee
= replacees
->pkgs
[i
];
454 if (replacee
->state_status
!= SS_INSTALLED
) {
455 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
456 err
= opkg_install_pkg(replacee
, 0);
464 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
466 opkg_install_check_downgrade(pkg_t
* pkg
, pkg_t
* old_pkg
, int message
)
469 char message_out
[15];
470 char *old_version
= pkg_version_str_alloc(old_pkg
);
471 char *new_version
= pkg_version_str_alloc(pkg
);
472 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
475 memset(message_out
, '\x0', 15);
476 strncpy(message_out
, "Upgrading ", strlen("Upgrading "));
477 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
478 cmp
= -1; /* then we force opkg to downgrade */
479 strncpy(message_out
, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
480 /* reinstall, and some check could fail asking the "force-reinstall" option */
484 if (!conf
->download_only
)
486 "Not downgrading package %s on %s from %s to %s.\n",
487 old_pkg
->name
, old_pkg
->dest
->name
,
488 old_version
, new_version
);
490 } else if (cmp
< 0) {
491 if (!conf
->download_only
)
493 "%s%s on %s from %s to %s...\n",
494 message_out
, pkg
->name
,
495 old_pkg
->dest
->name
, old_version
,
497 pkg
->dest
= old_pkg
->dest
;
499 } else { /* cmp == 0 */
501 if (!conf
->download_only
)
503 "%s (%s) already install on %s.\n",
504 pkg
->name
, new_version
,
505 old_pkg
->dest
->name
);
512 char message_out
[15];
513 memset(message_out
, '\x0', 15);
515 strncpy(message_out
, "Upgrading ",
516 strlen("Upgrading "));
518 strncpy(message_out
, "Installing ",
519 strlen("Installing "));
520 char *version
= pkg_version_str_alloc(pkg
);
522 if (!conf
->download_only
)
523 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
524 pkg
->name
, version
, pkg
->dest
->name
);
530 static int prerm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
532 /* DPKG_INCOMPATIBILITY:
533 dpkg does some things here that we don't do yet. Do we care?
535 1. If a version of the package is already installed, call
536 old-prerm upgrade new-version
537 2. If the script runs but exits with a non-zero exit status
538 new-prerm failed-upgrade old-version
539 Error unwind, for both the above cases:
540 old-postinst abort-upgrade new-version
546 if (!old_pkg
|| !pkg
)
549 new_version
= pkg_version_str_alloc(pkg
);
551 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
553 err
= pkg_run_script(old_pkg
, "prerm", script_args
);
556 opkg_msg(ERROR
, "prerm script for package \"%s\" failed\n",
563 static int prerm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
565 /* DPKG_INCOMPATIBILITY:
566 dpkg does some things here that we don't do yet. Do we care?
567 (See prerm_upgrade_old_package for details)
572 static int prerm_deconfigure_conflictors(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
574 /* DPKG_INCOMPATIBILITY:
575 dpkg does some things here that we don't do yet. Do we care?
576 2. If a 'conflicting' package is being removed at the same time:
577 1. If any packages depended on that conflicting package and
578 --auto-deconfigure is specified, call, for each such package:
579 deconfigured's-prerm deconfigure \
580 in-favour package-being-installed version \
581 removing conflicting-package version
583 deconfigured's-postinst abort-deconfigure \
584 in-favour package-being-installed-but-failed version \
585 removing conflicting-package version
587 The deconfigured packages are marked as requiring
588 configuration, so that if --install is used they will be
589 configured again if possible.
590 2. To prepare for removal of the conflicting package, call:
591 conflictor's-prerm remove in-favour package new-version
593 conflictor's-postinst abort-remove in-favour package new-version
599 prerm_deconfigure_conflictors_unwind(pkg_t
* pkg
, pkg_vec_t
* conflictors
)
601 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
602 do yet. Do we care? (See prerm_deconfigure_conflictors for
607 static int preinst_configure(pkg_t
* pkg
, pkg_t
* old_pkg
)
613 char *old_version
= pkg_version_str_alloc(old_pkg
);
614 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
616 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
617 char *pkg_version
= pkg_version_str_alloc(pkg
);
618 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
621 preinst_args
= xstrdup("install");
624 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
626 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
635 static int preinst_configure_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
637 /* DPKG_INCOMPATIBILITY:
638 dpkg does the following error unwind, should we?
639 pkg->postrm abort-upgrade old-version
640 OR pkg->postrm abort-install old-version
641 OR pkg->postrm abort-install
646 static char *backup_filename_alloc(const char *file_name
)
650 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
655 static int backup_make_backup(const char *file_name
)
660 backup
= backup_filename_alloc(file_name
);
661 err
= file_copy(file_name
, backup
);
663 opkg_msg(ERROR
, "Failed to copy %s to %s\n", file_name
, backup
);
671 static int backup_exists_for(const char *file_name
)
676 backup
= backup_filename_alloc(file_name
);
678 ret
= file_exists(backup
);
685 static int backup_remove(const char *file_name
)
689 backup
= backup_filename_alloc(file_name
);
696 static int backup_modified_conffiles(pkg_t
* pkg
, pkg_t
* old_pkg
)
699 conffile_list_elt_t
*iter
;
706 /* Backup all modified conffiles */
708 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
710 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
711 iter
= nv_pair_list_next(cl
, iter
)) {
715 cf_name
= root_filename_alloc(cf
->name
);
717 /* Don't worry if the conffile is just plain gone */
718 if (file_exists(cf_name
)
719 && conffile_has_been_modified(cf
)) {
720 err
= backup_make_backup(cf_name
);
729 /* Backup all conffiles that were not conffiles in old_pkg */
730 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
732 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
733 iter
= nv_pair_list_next(cl
, iter
)) {
735 cf
= (conffile_t
*) iter
->data
;
736 cf_name
= root_filename_alloc(cf
->name
);
737 /* Ignore if this was a conffile in old_pkg as well */
738 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
742 if (file_exists(cf_name
) && (!backup_exists_for(cf_name
))) {
743 err
= backup_make_backup(cf_name
);
754 static int backup_modified_conffiles_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
757 conffile_list_elt_t
*iter
;
760 cl
= pkg_get_ptr(old_pkg
, PKG_CONFFILES
);
762 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
763 iter
= nv_pair_list_next(cl
, iter
)) {
764 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
768 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
770 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
771 iter
= nv_pair_list_next(cl
, iter
)) {
772 backup_remove(((nv_pair_t
*) iter
->data
)->name
);
778 static int check_data_file_clashes(pkg_t
* pkg
, pkg_t
* old_pkg
)
780 /* DPKG_INCOMPATIBILITY:
781 opkg takes a slightly different approach than dpkg at this
782 point. dpkg installs each file in the new package while
783 creating a backup for any file that is replaced, (so that it
784 can unwind if necessary). To avoid complexity and redundant
785 storage, opkg doesn't do any installation until later, (at the
786 point at which dpkg removes the backups.
788 But, we do have to check for data file clashes, since after
789 installing a package with a file clash, removing either of the
790 packages involved in the clash has the potential to break the
793 str_list_t
*files_list
;
794 str_list_elt_t
*iter
, *niter
;
798 files_list
= pkg_get_installed_files(pkg
);
799 if (files_list
== NULL
)
802 for (iter
= str_list_first(files_list
), niter
=
803 str_list_next(files_list
, iter
); iter
;
804 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
805 filename
= (char *)iter
->data
;
806 if (file_exists(filename
) && (!file_is_dir(filename
))) {
810 if (backup_exists_for(filename
)) {
814 /* Pre-existing files are OK if force-overwrite was asserted. */
815 if (conf
->force_overwrite
) {
816 /* but we need to change who owns this file */
817 file_hash_set_file_owner(filename
, pkg
);
821 owner
= file_hash_get_file_owner(filename
);
823 /* Pre-existing files are OK if owned by the pkg being upgraded. */
824 if (owner
&& old_pkg
) {
825 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
830 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
833 "Checking replaces for %s in package %s\n",
834 filename
, owner
->name
);
835 if (pkg_replaces(pkg
, owner
)) {
838 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
839 then it's ok to overwrite. */
840 if (strcmp(owner
->name
, pkg
->name
) == 0) {
842 "Replacing pre-existing file %s"
843 " owned by package %s\n",
844 filename
, owner
->name
);
849 /* Pre-existing files are OK if they are obsolete */
850 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
853 "Pre-exiting file %s is obsolete."
854 " obs_pkg=%s\n", filename
, obs
->name
);
858 /* We have found a clash. */
859 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
860 "\tBut that file is already provided by package ",
861 pkg
->name
, filename
);
863 opkg_message(ERROR
, "%s\n", owner
->name
);
865 opkg_message(ERROR
, "<no package>\n"
866 "Please move this file out of the way and try again.\n");
871 pkg_free_installed_files(pkg
);
877 * XXX: This function sucks, as does the below comment.
879 static int check_data_file_clashes_change(pkg_t
* pkg
, pkg_t
* old_pkg
)
881 /* Basically that's the worst hack I could do to be able to change ownership of
882 file list, but, being that we have no way to unwind the mods, due to structure
883 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
884 What we do here is change the ownership of file in hash if a replace ( or similar events
886 Only the action that are needed to change name should be considered.
887 @@@ To change after 1.0 release.
889 str_list_t
*files_list
;
890 str_list_elt_t
*iter
, *niter
;
892 files_list
= pkg_get_installed_files(pkg
);
893 if (files_list
== NULL
)
896 for (iter
= str_list_first(files_list
), niter
=
897 str_list_next(files_list
, iter
); iter
;
898 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
899 char *filename
= (char *)iter
->data
;
900 if (file_exists(filename
) && (!file_is_dir(filename
))) {
903 owner
= file_hash_get_file_owner(filename
);
905 if (conf
->force_overwrite
) {
906 /* but we need to change who owns this file */
907 file_hash_set_file_owner(filename
, pkg
);
911 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
913 if (pkg_replaces(pkg
, owner
)) {
914 /* It's now time to change the owner of that file.
915 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
917 "Replacing pre-existing file %s "
918 "owned by package %s\n",
919 filename
, owner
->name
);
920 file_hash_set_file_owner(filename
, pkg
);
927 pkg_free_installed_files(pkg
);
932 static int check_data_file_clashes_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
934 /* Nothing to do since check_data_file_clashes doesn't change state */
938 static int postrm_upgrade_old_pkg(pkg_t
* pkg
, pkg_t
* old_pkg
)
940 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
941 1. If the package is being upgraded, call
942 old-postrm upgrade new-version
943 2. If this fails, attempt:
944 new-postrm failed-upgrade old-version
945 Error unwind, for both cases:
946 old-preinst abort-upgrade new-version */
951 if (!old_pkg
|| !pkg
)
954 new_version
= pkg_version_str_alloc(pkg
);
956 sprintf_alloc(&script_args
, "upgrade %s", new_version
);
958 err
= pkg_run_script(old_pkg
, "postrm", script_args
);
961 opkg_msg(ERROR
, "postrm script for package \"%s\" failed\n",
968 static int postrm_upgrade_old_pkg_unwind(pkg_t
* pkg
, pkg_t
* old_pkg
)
970 /* DPKG_INCOMPATIBILITY:
971 dpkg does some things here that we don't do yet. Do we care?
972 (See postrm_upgrade_old_pkg for details)
977 static int remove_obsolesced_files(pkg_t
* pkg
, pkg_t
* old_pkg
)
980 str_list_t
*old_files
;
982 str_list_t
*new_files
;
984 hash_table_t new_files_table
;
986 old_files
= pkg_get_installed_files(old_pkg
);
987 if (old_files
== NULL
)
990 new_files
= pkg_get_installed_files(pkg
);
991 if (new_files
== NULL
) {
992 pkg_free_installed_files(old_pkg
);
996 new_files_table
.entries
= NULL
;
997 hash_table_init("new_files", &new_files_table
, 20);
998 for (nf
= str_list_first(new_files
); nf
;
999 nf
= str_list_next(new_files
, nf
)) {
1001 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
1004 for (of
= str_list_first(old_files
); of
;
1005 of
= str_list_next(old_files
, of
)) {
1008 old
= (char *)of
->data
;
1009 new = (char *)hash_table_get(&new_files_table
, old
);
1013 if (file_is_dir(old
)) {
1016 owner
= file_hash_get_file_owner(old
);
1017 if (owner
!= old_pkg
) {
1018 /* in case obsolete file no longer belongs to old_pkg */
1022 /* old file is obsolete */
1023 opkg_msg(NOTICE
, "Removing obsolete file %s.\n", old
);
1024 if (!conf
->noaction
) {
1027 opkg_perror(ERROR
, "unlinking %s failed", old
);
1032 hash_table_deinit(&new_files_table
);
1033 pkg_free_installed_files(old_pkg
);
1034 pkg_free_installed_files(pkg
);
1039 static int install_maintainer_scripts(pkg_t
* pkg
, pkg_t
* old_pkg
)
1044 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1045 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1046 pkg
->dest
->info_dir
,
1052 static int remove_disappeared(pkg_t
* pkg
)
1054 /* DPKG_INCOMPATIBILITY:
1055 This is a fairly sophisticated dpkg operation. Shall we
1058 /* Any packages all of whose files have been overwritten during the
1059 installation, and which aren't required for dependencies, are
1060 considered to have been removed. For each such package
1061 1. disappearer's-postrm disappear overwriter overwriter-version
1062 2. The package's maintainer scripts are removed
1063 3. It is noted in the status database as being in a sane state,
1064 namely not installed (any conffiles it may have are ignored,
1065 rather than being removed by dpkg). Note that disappearing
1066 packages do not have their prerm called, because dpkg doesn't
1067 know in advance that the package is going to vanish.
1072 static int install_data_files(pkg_t
* pkg
)
1076 /* opkg takes a slightly different approach to data file backups
1077 than dpkg. Rather than removing backups at this point, we
1078 actually do the data file installation now. See comments in
1079 check_data_file_clashes() for more details. */
1081 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1082 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1087 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1088 err
= pkg_write_filelist(pkg
);
1092 /* XXX: FEATURE: opkg should identify any files which existed
1093 before installation and which were overwritten, (see
1094 check_data_file_clashes()). What it must do is remove any such
1095 files from the filelist of the old package which provided the
1096 file. Otherwise, if the old package were removed at some point
1097 it would break the new package. Removing the new package will
1098 also break the old one, but this cannot be helped since the old
1099 package's file has already been deleted. This is the importance
1100 of check_data_file_clashes(), and only allowing opkg to install
1101 a clashing package with a user force. */
1106 static int resolve_conffiles(pkg_t
* pkg
)
1108 conffile_list_elt_t
*iter
;
1109 conffile_list_t
*cl
;
1117 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
1119 for (iter
= cl
? nv_pair_list_first(cl
) : NULL
; iter
;
1120 iter
= nv_pair_list_next(cl
, iter
)) {
1121 char *root_filename
;
1122 cf
= (conffile_t
*) iter
->data
;
1123 root_filename
= root_filename_alloc(cf
->name
);
1125 /* Might need to initialize the md5sum for each conffile */
1126 if (cf
->value
== NULL
) {
1127 cf
->value
= file_sha256sum_alloc(root_filename
);
1130 if (!file_exists(root_filename
)) {
1131 free(root_filename
);
1135 cf_backup
= backup_filename_alloc(root_filename
);
1137 if (file_exists(cf_backup
)) {
1138 /* Let's compute md5 to test if files are changed */
1139 if (cf
->value
&& strlen(cf
->value
) > 33) {
1140 chksum
= file_sha256sum_alloc(cf_backup
);
1142 chksum
= file_md5sum_alloc(cf_backup
);
1145 if (chksum
&& cf
->value
1146 && strcmp(cf
->value
, chksum
) != 0) {
1147 if (conf
->force_maintainer
) {
1149 "Conffile %s using maintainer's setting.\n",
1153 sprintf_alloc(&new_conffile
, "%s-opkg",
1156 "Existing conffile %s "
1157 "is different from the conffile in the new package."
1158 " The new conffile will be placed at %s.\n",
1159 root_filename
, new_conffile
);
1160 rename(root_filename
, new_conffile
);
1161 rename(cf_backup
, root_filename
);
1171 free(root_filename
);
1177 int opkg_install_by_name(const char *pkg_name
)
1181 char *old_version
, *new_version
;
1183 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1185 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1186 pkg_get_string(old
, PKG_VERSION
));
1188 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1190 opkg_msg(NOTICE
, "Unknown package '%s'.\n", pkg_name
);
1194 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1196 opkg_message(DEBUG2
, " old %s ", pkg_get_string(old
, PKG_VERSION
));
1197 opkg_message(DEBUG2
, " new %s\n", pkg_get_string(new, PKG_VERSION
));
1199 new->state_flag
|= SF_USER
;
1201 old_version
= pkg_version_str_alloc(old
);
1202 new_version
= pkg_version_str_alloc(new);
1204 cmp
= pkg_compare_versions(old
, new);
1205 if ((conf
->force_downgrade
== 1) && (cmp
> 0)) { /* We've been asked to allow downgrade and version is precedent */
1206 opkg_msg(DEBUG
, "Forcing downgrade\n");
1207 cmp
= -1; /* then we force opkg to downgrade */
1208 /* We need to use a value < 0 because in the 0 case we are asking to */
1209 /* reinstall, and some check could fail asking the "force-reinstall" option */
1211 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1212 "\n\t%s is installed "
1213 "\n\t%s is available "
1214 "\n\t%d was comparison result\n",
1215 pkg_name
, old_version
, new_version
, cmp
);
1218 "Package %s (%s) installed in %s is up to date.\n",
1219 old
->name
, old_version
, old
->dest
->name
);
1223 } else if (cmp
> 0) {
1225 "Not downgrading package %s on %s from %s to %s.\n",
1226 old
->name
, old
->dest
->name
, old_version
,
1231 } else if (cmp
< 0) {
1232 new->dest
= old
->dest
;
1233 old
->state_want
= SW_DEINSTALL
;
1239 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
1240 return opkg_install_pkg(new, 0);
1244 * @brief Really install a pkg_t
1246 int opkg_install_pkg(pkg_t
* pkg
, int from_upgrade
)
1250 pkg_t
*old_pkg
= NULL
;
1251 pkg_vec_t
*replacees
;
1252 abstract_pkg_t
*ab_pkg
= NULL
;
1254 char *file_md5
, *pkg_md5
;
1255 char *file_sha256
, *pkg_sha256
;
1256 sigset_t newset
, oldset
;
1257 const char *local_filename
;
1261 message
= 1; /* Coming from an upgrade, and should change the output message */
1263 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1265 if (!pkg_arch_supported(pkg
)) {
1267 "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1268 pkg_get_architecture(pkg
), pkg
->name
);
1271 if (pkg
->state_status
== SS_INSTALLED
&& conf
->nodeps
== 0) {
1272 err
= satisfy_dependencies_for(pkg
);
1276 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1277 pkg
->name
, pkg
->dest
->name
);
1281 if (pkg
->dest
== NULL
) {
1282 pkg
->dest
= conf
->default_dest
;
1285 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1287 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1291 pkg
->state_want
= SW_INSTALL
;
1293 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependencies */
1296 err
= check_conflicts_for(pkg
);
1300 /* this setup is to remove the upgrade scenario in the end when
1301 installing pkg A, A deps B & B deps on A. So both B and A are
1302 installed. Then A's installation is started resulting in an
1303 uncecessary upgrade */
1304 if (pkg
->state_status
== SS_INSTALLED
)
1307 err
= verify_pkg_installable(pkg
);
1311 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1313 if (local_filename
== NULL
) {
1314 if (!conf
->cache
&& conf
->download_only
) {
1316 if (getcwd(cwd
, sizeof(cwd
)) != NULL
)
1317 err
= opkg_download_pkg(pkg
, cwd
);
1321 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1324 opkg_msg(ERROR
, "Failed to download %s. "
1325 "Perhaps you need to run 'opkg update'?\n",
1330 local_filename
= pkg_get_string(pkg
, PKG_LOCAL_FILENAME
);
1333 /* check that the repository is valid */
1334 #if defined(HAVE_USIGN)
1335 char *list_file_name
, *sig_file_name
, *lists_dir
;
1337 /* check to ensure the package has come from a repository */
1338 if (conf
->check_signature
&& pkg
->src
) {
1339 sprintf_alloc(&lists_dir
, "%s", (conf
->restrict_to_default_dest
)
1340 ? conf
->default_dest
->lists_dir
1342 sprintf_alloc(&list_file_name
, "%s/%s", lists_dir
,
1344 sprintf_alloc(&sig_file_name
, "%s/%s.sig", lists_dir
,
1347 if (file_exists(sig_file_name
)) {
1348 if (opkg_verify_file(list_file_name
, sig_file_name
)) {
1350 "Failed to verify the signature of %s.\n",
1352 if (!conf
->force_signature
)
1356 opkg_msg(ERROR
, "Signature file is missing for %s. "
1357 "Perhaps you need to run 'opkg update'?\n",
1359 if (!conf
->force_signature
)
1364 free(list_file_name
);
1365 free(sig_file_name
);
1369 /* Check for md5 values */
1370 pkg_md5
= pkg_get_md5(pkg
);
1372 file_md5
= file_md5sum_alloc(local_filename
);
1373 if (file_md5
&& strcmp(file_md5
, pkg_md5
)) {
1374 if (!conf
->force_checksum
) {
1375 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1376 "Either the opkg or the package index are corrupt. "
1377 "Try 'opkg update'.\n", pkg
->name
);
1382 "Ignored %s md5sum mismatch.\n",
1390 /* Check for sha256 value */
1391 pkg_sha256
= pkg_get_sha256(pkg
);
1393 file_sha256
= file_sha256sum_alloc(local_filename
);
1394 if (file_sha256
&& strcmp(file_sha256
, pkg_sha256
)) {
1395 if (!conf
->force_checksum
) {
1397 "Package %s sha256sum mismatch. "
1398 "Either the opkg or the package index are corrupt. "
1399 "Try 'opkg update'.\n", pkg
->name
);
1404 "Ignored %s sha256sum mismatch.\n",
1412 if (conf
->download_only
) {
1413 if (conf
->nodeps
== 0) {
1414 err
= satisfy_dependencies_for(pkg
);
1421 if (!pkg_get_string(pkg
, PKG_TMP_UNPACK_DIR
)) {
1422 if (unpack_pkg_control_files(pkg
) == -1) {
1424 "Failed to unpack control files from %s.\n",
1430 err
= update_file_ownership(pkg
, old_pkg
);
1434 if (conf
->nodeps
== 0) {
1435 err
= satisfy_dependencies_for(pkg
);
1438 if (pkg
->state_status
== SS_UNPACKED
)
1439 /* Circular dependency has installed it for us. */
1443 replacees
= pkg_vec_alloc();
1444 pkg_get_installed_replacees(pkg
, replacees
);
1446 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1448 sigemptyset(&newset
);
1449 sigaddset(&newset
, SIGINT
);
1450 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1452 opkg_state_changed
++;
1453 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1456 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1457 old_pkg
->is_upgrade
= 1;
1458 pkg
->is_upgrade
= 1;
1460 /* XXX: BUG: we really should treat replacement more like an upgrade
1461 * Instead, we're going to remove the replacees
1463 err
= pkg_remove_installed_replacees(replacees
);
1465 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1467 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1469 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1471 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1473 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1475 err
= preinst_configure(pkg
, old_pkg
);
1477 goto UNWIND_PREINST_CONFIGURE
;
1479 err
= backup_modified_conffiles(pkg
, old_pkg
);
1481 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1483 err
= check_data_file_clashes(pkg
, old_pkg
);
1485 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1487 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1489 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1494 /* point of no return: no unwinding after this */
1496 old_pkg
->state_want
= SW_DEINSTALL
;
1498 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1499 opkg_msg(INFO
, "Not removing obsolesced files because "
1500 "package %s marked noprune.\n", old_pkg
->name
);
1502 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1504 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1505 opkg_msg(ERROR
, "Failed to determine "
1506 "obsolete files from previously "
1507 "installed %s\n", old_pkg
->name
);
1511 /* removing files from old package, to avoid ghost files */
1512 remove_data_files_and_list(old_pkg
);
1513 remove_maintainer_scripts(old_pkg
);
1516 opkg_msg(INFO
, "%s maintainer scripts.\n",
1517 (pkg
->is_upgrade
) ? ("Upgrading") : ("Installing"));
1518 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1519 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1520 " Package debris may remain!\n", pkg
->name
);
1524 /* the following just returns 0 */
1525 remove_disappeared(pkg
);
1527 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1529 if (install_data_files(pkg
)) {
1530 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1531 "Package debris may remain!\n", pkg
->name
);
1535 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1537 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1538 "for files belonging to %s.\n", pkg
->name
);
1541 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1542 resolve_conffiles(pkg
);
1544 pkg
->state_status
= SS_UNPACKED
;
1545 old_state_flag
= pkg
->state_flag
;
1546 pkg
->state_flag
&= ~SF_PREFER
;
1547 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1548 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1551 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1554 pkg_set_int(pkg
, PKG_INSTALLED_TIME
, now
);
1556 ab_pkg
= pkg
->parent
;
1558 ab_pkg
->state_status
= pkg
->state_status
;
1560 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1561 pkg_vec_free(replacees
);
1564 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1565 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1566 UNWIND_CHECK_DATA_FILE_CLASHES
:
1567 check_data_file_clashes_unwind(pkg
, old_pkg
);
1568 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1569 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1570 UNWIND_PREINST_CONFIGURE
:
1571 preinst_configure_unwind(pkg
, old_pkg
);
1572 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1573 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1574 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1575 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1576 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1577 pkg_remove_installed_replacees_unwind(replacees
);
1580 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1582 pkg_vec_free(replacees
);