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.
28 #include "pkg_extract.h"
30 #include "opkg_install.h"
31 #include "opkg_configure.h"
32 #include "opkg_download.h"
33 #include "opkg_remove.h"
35 #include "opkg_utils.h"
36 #include "opkg_message.h"
38 #include "opkg_defines.h"
40 #include "sprintf_alloc.h"
41 #include "file_util.h"
43 #include "libbb/libbb.h"
46 satisfy_dependencies_for(pkg_t
*pkg
)
49 pkg_vec_t
*depends
= pkg_vec_alloc();
51 char **tmp
, **unresolved
= NULL
;
54 ndepends
= pkg_hash_fetch_unsatisfied_dependencies(pkg
, depends
,
58 opkg_msg(ERROR
, "Cannot satisfy the following dependencies for %s:\n",
62 opkg_message(ERROR
, "\t%s", *unresolved
);
67 opkg_message(ERROR
, "\n");
68 if (! conf
->force_depends
) {
70 "This could mean that your package list is out of date or that the packages\n"
71 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
72 "of this problem try again with the '-force-depends' option.\n");
73 pkg_vec_free(depends
);
79 pkg_vec_free(depends
);
83 /* Mark packages as to-be-installed */
84 for (i
=0; i
< depends
->len
; i
++) {
85 /* Dependencies should be installed the same place as pkg */
86 if (depends
->pkgs
[i
]->dest
== NULL
) {
87 depends
->pkgs
[i
]->dest
= pkg
->dest
;
89 depends
->pkgs
[i
]->state_want
= SW_INSTALL
;
92 for (i
= 0; i
< depends
->len
; i
++) {
93 dep
= depends
->pkgs
[i
];
94 /* The package was uninstalled when we started, but another
95 dep earlier in this loop may have depended on it and pulled
96 it in, so check first. */
97 if ((dep
->state_status
!= SS_INSTALLED
)
98 && (dep
->state_status
!= SS_UNPACKED
)) {
99 opkg_msg(DEBUG2
,"Calling opkg_install_pkg.\n");
100 err
= opkg_install_pkg(dep
, 0);
101 /* mark this package as having been automatically installed to
102 * satisfy a dependancy */
103 dep
->auto_installed
= 1;
105 pkg_vec_free(depends
);
111 pkg_vec_free(depends
);
117 check_conflicts_for(pkg_t
*pkg
)
120 pkg_vec_t
*conflicts
= NULL
;
121 message_level_t level
;
123 if (conf
->force_depends
) {
129 if (!conf
->force_depends
)
130 conflicts
= pkg_hash_fetch_conflicts(pkg
);
133 opkg_msg(level
, "The following packages conflict with %s:\n",
136 while (i
< conflicts
->len
)
137 opkg_msg(level
, "\t%s", conflicts
->pkgs
[i
++]->name
);
138 opkg_message(level
, "\n");
139 pkg_vec_free(conflicts
);
146 update_file_ownership(pkg_t
*new_pkg
, pkg_t
*old_pkg
)
148 str_list_t
*new_list
, *old_list
;
149 str_list_elt_t
*iter
, *niter
;
151 new_list
= pkg_get_installed_files(new_pkg
);
152 if (new_list
== NULL
)
155 for (iter
= str_list_first(new_list
), niter
= str_list_next(new_list
, iter
);
157 iter
= niter
, niter
= str_list_next(new_list
, niter
)) {
158 char *new_file
= (char *)iter
->data
;
159 pkg_t
*owner
= file_hash_get_file_owner(new_file
);
160 pkg_t
*obs
= hash_table_get(&conf
->obs_file_hash
, new_file
);
162 opkg_msg(DEBUG2
, "%s: new_pkg=%s wants file %s, from owner=%s\n",
163 __func__
, new_pkg
->name
, new_file
, owner
?owner
->name
:"<NULL>");
165 if (!owner
|| (owner
== old_pkg
) || obs
)
166 file_hash_set_file_owner(new_file
, new_pkg
);
170 old_list
= pkg_get_installed_files(old_pkg
);
171 if (old_list
== NULL
) {
172 pkg_free_installed_files(new_pkg
);
176 for (iter
= str_list_first(old_list
), niter
= str_list_next(old_list
, iter
);
178 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
179 char *old_file
= (char *)iter
->data
;
180 pkg_t
*owner
= file_hash_get_file_owner(old_file
);
181 if (!owner
|| (owner
== old_pkg
)) {
183 hash_table_insert(&conf
->obs_file_hash
, old_file
, old_pkg
);
186 pkg_free_installed_files(old_pkg
);
188 pkg_free_installed_files(new_pkg
);
193 verify_pkg_installable(pkg_t
*pkg
)
195 unsigned long kbs_available
, pkg_size_kbs
;
196 char *root_dir
= NULL
;
199 if (conf
->force_space
|| pkg
->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
= (pkg
->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
);
229 unpack_pkg_control_files(pkg_t
*pkg
)
232 char *conffiles_file_name
;
234 FILE *conffiles_file
;
236 sprintf_alloc(&pkg
->tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
, pkg
->name
);
238 pkg
->tmp_unpack_dir
= mkdtemp(pkg
->tmp_unpack_dir
);
239 if (pkg
->tmp_unpack_dir
== NULL
) {
240 opkg_perror(ERROR
, "Failed to create temporary directory '%s'",
241 pkg
->tmp_unpack_dir
);
245 err
= pkg_extract_control_files_to_dir(pkg
, pkg
->tmp_unpack_dir
);
250 /* XXX: CLEANUP: There might be a cleaner place to read in the
251 conffiles. Seems like I should be able to get everything to go
252 through pkg_init_from_file. If so, maybe it would make sense to
253 move all of unpack_pkg_control_files to that function. */
255 /* Don't need to re-read conffiles if we already have it */
256 if (!nv_pair_list_empty(&pkg
->conffiles
)) {
260 sprintf_alloc(&conffiles_file_name
, "%s/conffiles", pkg
->tmp_unpack_dir
);
261 if (! file_exists(conffiles_file_name
)) {
262 free(conffiles_file_name
);
266 conffiles_file
= fopen(conffiles_file_name
, "r");
267 if (conffiles_file
== NULL
) {
268 opkg_perror(ERROR
, "Failed to open %s", conffiles_file_name
);
269 free(conffiles_file_name
);
272 free(conffiles_file_name
);
276 char *cf_name_in_dest
;
279 cf_name
= file_read_line_alloc(conffiles_file
);
280 if (cf_name
== NULL
) {
283 if (cf_name
[0] == '\0') {
286 for (i
= strlen(cf_name
) - 1;
287 (i
>= 0) && (cf_name
[i
] == ' ' || cf_name
[i
] == '\t');
293 /* Prepend dest->root_dir to conffile name.
294 Take pains to avoid multiple slashes. */
295 root_dir
= pkg
->dest
->root_dir
;
296 if (conf
->offline_root
)
297 /* skip the offline_root prefix */
298 root_dir
= pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
299 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
300 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
302 /* Can't get an md5sum now, (file isn't extracted yet).
303 We'll wait until resolve_conffiles */
304 conffile_list_append(&pkg
->conffiles
, cf_name_in_dest
, NULL
);
307 free(cf_name_in_dest
);
310 fclose(conffiles_file
);
316 * Remove packages which were auto_installed due to a dependency by old_pkg,
317 * which are no longer a dependency in the new (upgraded) pkg.
320 pkg_remove_orphan_dependent(pkg_t
*pkg
, pkg_t
*old_pkg
)
322 int i
, j
, k
, l
, found
,r
, err
= 0;
325 struct compound_depend
*cd0
, *cd1
;
326 abstract_pkg_t
**dependents
;
328 int count0
= old_pkg
->pre_depends_count
+
329 old_pkg
->depends_count
+
330 old_pkg
->recommends_count
+
331 old_pkg
->suggests_count
;
332 int count1
= pkg
->pre_depends_count
+
334 pkg
->recommends_count
+
337 for (i
=0; i
<count0
; i
++) {
338 cd0
= &old_pkg
->depends
[i
];
339 if (cd0
->type
!= DEPEND
)
341 for (j
=0; j
<cd0
->possibility_count
; j
++) {
345 for (k
=0; k
<count1
; k
++) {
346 cd1
= &pkg
->depends
[k
];
347 if (cd1
->type
!= DEPEND
)
349 for (l
=0; l
<cd1
->possibility_count
; l
++) {
350 if (cd0
->possibilities
[j
]
351 == cd1
->possibilities
[l
]) {
364 * old_pkg has a dependency that pkg does not.
366 p
= pkg_hash_fetch_installed_by_name(
367 cd0
->possibilities
[j
]->pkg
->name
);
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",
383 /* p has one installed dependency (old_pkg),
384 * which we need to ignore during removal. */
385 p
->state_flag
|= SF_REPLACE
;
387 r
= opkg_remove_pkg(p
, 0);
391 opkg_msg(INFO
, "%s was autoinstalled and is "
392 "still required by %d "
393 "installed packages.\n",
402 /* returns number of installed replacees */
404 pkg_get_installed_replacees(pkg_t
*pkg
, pkg_vec_t
*installed_replacees
)
406 abstract_pkg_t
**replaces
= pkg
->replaces
;
407 int replaces_count
= pkg
->replaces_count
;
409 for (i
= 0; i
< replaces_count
; i
++) {
410 abstract_pkg_t
*ab_pkg
= replaces
[i
];
411 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
413 for (j
= 0; j
< pkg_vec
->len
; j
++) {
414 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
415 if (!pkg_conflicts(pkg
, replacee
))
417 if (replacee
->state_status
== SS_INSTALLED
) {
418 pkg_vec_insert(installed_replacees
, replacee
);
423 return installed_replacees
->len
;
427 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 */
444 pkg_remove_installed_replacees_unwind(pkg_vec_t
*replacees
)
447 int replaces_count
= replacees
->len
;
448 for (i
= 0; i
< replaces_count
; i
++) {
449 pkg_t
*replacee
= replacees
->pkgs
[i
];
450 if (replacee
->state_status
!= SS_INSTALLED
) {
451 opkg_msg(DEBUG2
, "Calling opkg_install_pkg.\n");
452 err
= opkg_install_pkg(replacee
, 0);
460 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
462 opkg_install_check_downgrade(pkg_t
*pkg
, pkg_t
*old_pkg
, int message
)
465 char message_out
[15];
466 char *old_version
= pkg_version_str_alloc(old_pkg
);
467 char *new_version
= pkg_version_str_alloc(pkg
);
468 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
471 memset(message_out
,'\x0',15);
472 strncpy (message_out
,"Upgrading ",strlen("Upgrading "));
473 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
474 cmp
= -1 ; /* then we force opkg to downgrade */
475 strncpy (message_out
,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
476 /* reinstall, and some check could fail asking the "force-reinstall" option */
480 if(!conf
->download_only
)
482 "Not downgrading package %s on %s from %s to %s.\n",
483 old_pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
485 } else if (cmp
< 0) {
486 if(!conf
->download_only
)
487 opkg_msg(NOTICE
, "%s%s on %s from %s to %s...\n",
488 message_out
, pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
489 pkg
->dest
= old_pkg
->dest
;
491 } else /* cmp == 0 */ {
492 if(!conf
->download_only
)
493 opkg_msg(NOTICE
, "%s (%s) already install on %s.\n",
494 pkg
->name
, new_version
, old_pkg
->dest
->name
);
501 char message_out
[15] ;
502 memset(message_out
,'\x0',15);
504 strncpy( message_out
,"Upgrading ",strlen("Upgrading ") );
506 strncpy( message_out
,"Installing ",strlen("Installing ") );
507 char *version
= pkg_version_str_alloc(pkg
);
509 if(!conf
->download_only
)
510 opkg_msg(NOTICE
, "%s%s (%s) to %s...\n", message_out
,
511 pkg
->name
, version
, pkg
->dest
->name
);
519 prerm_upgrade_old_pkg(pkg_t
*pkg
, pkg_t
*old_pkg
)
521 /* DPKG_INCOMPATIBILITY:
522 dpkg does some things here that we don't do yet. Do we care?
524 1. If a version of the package is already installed, call
525 old-prerm upgrade new-version
526 2. If the script runs but exits with a non-zero exit status
527 new-prerm failed-upgrade old-version
528 Error unwind, for both the above cases:
529 old-postinst abort-upgrade new-version
535 prerm_upgrade_old_pkg_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
537 /* DPKG_INCOMPATIBILITY:
538 dpkg does some things here that we don't do yet. Do we care?
539 (See prerm_upgrade_old_package for details)
545 prerm_deconfigure_conflictors(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
547 /* DPKG_INCOMPATIBILITY:
548 dpkg does some things here that we don't do yet. Do we care?
549 2. If a 'conflicting' package is being removed at the same time:
550 1. If any packages depended on that conflicting package and
551 --auto-deconfigure is specified, call, for each such package:
552 deconfigured's-prerm deconfigure \
553 in-favour package-being-installed version \
554 removing conflicting-package version
556 deconfigured's-postinst abort-deconfigure \
557 in-favour package-being-installed-but-failed version \
558 removing conflicting-package version
560 The deconfigured packages are marked as requiring
561 configuration, so that if --install is used they will be
562 configured again if possible.
563 2. To prepare for removal of the conflicting package, call:
564 conflictor's-prerm remove in-favour package new-version
566 conflictor's-postinst abort-remove in-favour package new-version
572 prerm_deconfigure_conflictors_unwind(pkg_t
*pkg
, pkg_vec_t
*conflictors
)
574 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
575 do yet. Do we care? (See prerm_deconfigure_conflictors for
581 preinst_configure(pkg_t
*pkg
, pkg_t
*old_pkg
)
587 char *old_version
= pkg_version_str_alloc(old_pkg
);
588 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
590 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
591 char *pkg_version
= pkg_version_str_alloc(pkg
);
592 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
595 preinst_args
= xstrdup("install");
598 err
= pkg_run_script(pkg
, "preinst", preinst_args
);
600 opkg_msg(ERROR
, "Aborting installation of %s.\n", pkg
->name
);
610 preinst_configure_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
612 /* DPKG_INCOMPATIBILITY:
613 dpkg does the following error unwind, should we?
614 pkg->postrm abort-upgrade old-version
615 OR pkg->postrm abort-install old-version
616 OR pkg->postrm abort-install
622 backup_filename_alloc(const char *file_name
)
626 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
633 backup_make_backup(const char *file_name
)
638 backup
= backup_filename_alloc(file_name
);
639 err
= file_copy(file_name
, backup
);
641 opkg_msg(ERROR
, "Failed to copy %s to %s\n",
651 backup_exists_for(const char *file_name
)
656 backup
= backup_filename_alloc(file_name
);
658 ret
= file_exists(backup
);
666 backup_remove(const char *file_name
)
670 backup
= backup_filename_alloc(file_name
);
678 backup_modified_conffiles(pkg_t
*pkg
, pkg_t
*old_pkg
)
681 conffile_list_elt_t
*iter
;
684 if (conf
->noaction
) return 0;
686 /* Backup all modified conffiles */
688 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
692 cf_name
= root_filename_alloc(cf
->name
);
694 /* Don't worry if the conffile is just plain gone */
695 if (file_exists(cf_name
) && conffile_has_been_modified(cf
)) {
696 err
= backup_make_backup(cf_name
);
705 /* Backup all conffiles that were not conffiles in old_pkg */
706 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
708 cf
= (conffile_t
*)iter
->data
;
709 cf_name
= root_filename_alloc(cf
->name
);
710 /* Ignore if this was a conffile in old_pkg as well */
711 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
715 if (file_exists(cf_name
) && (! backup_exists_for(cf_name
))) {
716 err
= backup_make_backup(cf_name
);
728 backup_modified_conffiles_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
730 conffile_list_elt_t
*iter
;
733 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
734 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
738 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
739 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
747 check_data_file_clashes(pkg_t
*pkg
, pkg_t
*old_pkg
)
749 /* DPKG_INCOMPATIBILITY:
750 opkg takes a slightly different approach than dpkg at this
751 point. dpkg installs each file in the new package while
752 creating a backup for any file that is replaced, (so that it
753 can unwind if necessary). To avoid complexity and redundant
754 storage, opkg doesn't do any installation until later, (at the
755 point at which dpkg removes the backups.
757 But, we do have to check for data file clashes, since after
758 installing a package with a file clash, removing either of the
759 packages involved in the clash has the potential to break the
762 str_list_t
*files_list
;
763 str_list_elt_t
*iter
, *niter
;
767 files_list
= pkg_get_installed_files(pkg
);
768 if (files_list
== NULL
)
771 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
773 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
774 filename
= (char *) iter
->data
;
775 if (file_exists(filename
) && (! file_is_dir(filename
))) {
779 if (backup_exists_for(filename
)) {
783 /* Pre-existing files are OK if force-overwrite was asserted. */
784 if (conf
->force_overwrite
) {
785 /* but we need to change who owns this file */
786 file_hash_set_file_owner(filename
, pkg
);
790 owner
= file_hash_get_file_owner(filename
);
792 /* Pre-existing files are OK if owned by the pkg being upgraded. */
793 if (owner
&& old_pkg
) {
794 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
799 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
801 opkg_msg(DEBUG2
, "Checking replaces for %s in package %s\n",
802 filename
, owner
->name
);
803 if (pkg_replaces(pkg
, owner
)) {
806 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
807 then it's ok to overwrite. */
808 if (strcmp(owner
->name
,pkg
->name
)==0){
809 opkg_msg(INFO
, "Replacing pre-existing file %s"
810 " owned by package %s\n",
811 filename
, owner
->name
);
816 /* Pre-existing files are OK if they are obsolete */
817 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
819 opkg_msg(INFO
, "Pre-exiting file %s is obsolete."
821 filename
, obs
->name
);
825 /* We have found a clash. */
826 opkg_msg(ERROR
, "Package %s wants to install file %s\n"
827 "\tBut that file is already provided by package ",
828 pkg
->name
, filename
);
830 opkg_message(ERROR
, "%s\n", owner
->name
);
832 opkg_message(ERROR
, "<no package>\n"
833 "Please move this file out of the way and try again.\n");
838 pkg_free_installed_files(pkg
);
844 * XXX: This function sucks, as does the below comment.
847 check_data_file_clashes_change(pkg_t
*pkg
, pkg_t
*old_pkg
)
849 /* Basically that's the worst hack I could do to be able to change ownership of
850 file list, but, being that we have no way to unwind the mods, due to structure
851 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
852 What we do here is change the ownership of file in hash if a replace ( or similar events
854 Only the action that are needed to change name should be considered.
855 @@@ To change after 1.0 release.
857 str_list_t
*files_list
;
858 str_list_elt_t
*iter
, *niter
;
860 char *root_filename
= NULL
;
862 files_list
= pkg_get_installed_files(pkg
);
863 if (files_list
== NULL
)
866 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
868 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
869 char *filename
= (char *) iter
->data
;
872 root_filename
= NULL
;
874 root_filename
= root_filename_alloc(filename
);
875 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
878 owner
= file_hash_get_file_owner(filename
);
880 if (conf
->force_overwrite
) {
881 /* but we need to change who owns this file */
882 file_hash_set_file_owner(filename
, pkg
);
887 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
889 if (pkg_replaces(pkg
, owner
)) {
890 /* It's now time to change the owner of that file.
891 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
892 opkg_msg(INFO
, "Replacing pre-existing file %s "
893 "owned by package %s\n",
894 filename
, owner
->name
);
895 file_hash_set_file_owner(filename
, pkg
);
904 root_filename
= NULL
;
906 pkg_free_installed_files(pkg
);
912 check_data_file_clashes_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
914 /* Nothing to do since check_data_file_clashes doesn't change state */
919 postrm_upgrade_old_pkg(pkg_t
*pkg
, pkg_t
*old_pkg
)
921 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
922 1. If the package is being upgraded, call
923 old-postrm upgrade new-version
924 2. If this fails, attempt:
925 new-postrm failed-upgrade old-version
926 Error unwind, for both cases:
927 old-preinst abort-upgrade new-version */
932 postrm_upgrade_old_pkg_unwind(pkg_t
*pkg
, pkg_t
*old_pkg
)
934 /* DPKG_INCOMPATIBILITY:
935 dpkg does some things here that we don't do yet. Do we care?
936 (See postrm_upgrade_old_pkg for details)
942 remove_obsolesced_files(pkg_t
*pkg
, pkg_t
*old_pkg
)
945 str_list_t
*old_files
;
947 str_list_t
*new_files
;
949 hash_table_t new_files_table
;
951 old_files
= pkg_get_installed_files(old_pkg
);
952 if (old_files
== NULL
)
955 new_files
= pkg_get_installed_files(pkg
);
956 if (new_files
== NULL
) {
957 pkg_free_installed_files(old_pkg
);
961 new_files_table
.entries
= NULL
;
962 hash_table_init("new_files" , &new_files_table
, 20);
963 for (nf
= str_list_first(new_files
); nf
; nf
= str_list_next(new_files
, nf
)) {
965 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
968 for (of
= str_list_first(old_files
); of
; of
= str_list_next(old_files
, of
)) {
971 old
= (char *)of
->data
;
972 new = (char *) hash_table_get (&new_files_table
, old
);
976 if (file_is_dir(old
)) {
979 owner
= file_hash_get_file_owner(old
);
980 if (owner
!= old_pkg
) {
981 /* in case obsolete file no longer belongs to old_pkg */
985 /* old file is obsolete */
986 opkg_msg(NOTICE
, "Removing obsolete file %s.\n", old
);
987 if (!conf
->noaction
) {
990 opkg_perror(ERROR
, "unlinking %s failed", old
);
995 hash_table_deinit(&new_files_table
);
996 pkg_free_installed_files(old_pkg
);
997 pkg_free_installed_files(pkg
);
1003 install_maintainer_scripts(pkg_t
*pkg
, pkg_t
*old_pkg
)
1008 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1009 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1010 pkg
->dest
->info_dir
,
1017 remove_disappeared(pkg_t
*pkg
)
1019 /* DPKG_INCOMPATIBILITY:
1020 This is a fairly sophisticated dpkg operation. Shall we
1023 /* Any packages all of whose files have been overwritten during the
1024 installation, and which aren't required for dependencies, are
1025 considered to have been removed. For each such package
1026 1. disappearer's-postrm disappear overwriter overwriter-version
1027 2. The package's maintainer scripts are removed
1028 3. It is noted in the status database as being in a sane state,
1029 namely not installed (any conffiles it may have are ignored,
1030 rather than being removed by dpkg). Note that disappearing
1031 packages do not have their prerm called, because dpkg doesn't
1032 know in advance that the package is going to vanish.
1038 install_data_files(pkg_t
*pkg
)
1042 /* opkg takes a slightly different approach to data file backups
1043 than dpkg. Rather than removing backups at this point, we
1044 actually do the data file installation now. See comments in
1045 check_data_file_clashes() for more details. */
1047 opkg_msg(INFO
, "Extracting data files to %s.\n", pkg
->dest
->root_dir
);
1048 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1053 /* The "Essential" control field may only be present in the control
1054 * file and not in the Packages list. Ensure we capture it regardless.
1056 * XXX: This should be fixed outside of opkg, in the Package list.
1058 set_flags_from_control(pkg
) ;
1060 opkg_msg(DEBUG
, "Calling pkg_write_filelist.\n");
1061 err
= pkg_write_filelist(pkg
);
1065 /* XXX: FEATURE: opkg should identify any files which existed
1066 before installation and which were overwritten, (see
1067 check_data_file_clashes()). What it must do is remove any such
1068 files from the filelist of the old package which provided the
1069 file. Otherwise, if the old package were removed at some point
1070 it would break the new package. Removing the new package will
1071 also break the old one, but this cannot be helped since the old
1072 package's file has already been deleted. This is the importance
1073 of check_data_file_clashes(), and only allowing opkg to install
1074 a clashing package with a user force. */
1080 resolve_conffiles(pkg_t
*pkg
)
1082 conffile_list_elt_t
*iter
;
1087 if (conf
->noaction
) return 0;
1089 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1090 char *root_filename
;
1091 cf
= (conffile_t
*)iter
->data
;
1092 root_filename
= root_filename_alloc(cf
->name
);
1094 /* Might need to initialize the md5sum for each conffile */
1095 if (cf
->value
== NULL
) {
1096 cf
->value
= file_md5sum_alloc(root_filename
);
1099 if (!file_exists(root_filename
)) {
1100 free(root_filename
);
1104 cf_backup
= backup_filename_alloc(root_filename
);
1106 if (file_exists(cf_backup
)) {
1107 /* Let's compute md5 to test if files are changed */
1108 md5sum
= file_md5sum_alloc(cf_backup
);
1109 if (md5sum
&& cf
->value
&& strcmp(cf
->value
,md5sum
) != 0 ) {
1110 if (conf
->force_maintainer
) {
1111 opkg_msg(NOTICE
, "Conffile %s using maintainer's setting.\n",
1115 sprintf_alloc(&new_conffile
, "%s-opkg", root_filename
);
1116 opkg_msg(ERROR
, "Existing conffile %s "
1117 "is different from the conffile in the new package."
1118 " The new conffile will be placed at %s.\n",
1119 root_filename
, new_conffile
);
1120 rename(root_filename
, new_conffile
);
1121 rename(cf_backup
, root_filename
);
1131 free(root_filename
);
1139 opkg_install_by_name(const char *pkg_name
)
1143 char *old_version
, *new_version
;
1145 old
= pkg_hash_fetch_installed_by_name(pkg_name
);
1147 opkg_msg(DEBUG2
, "Old versions from pkg_hash_fetch %s.\n",
1150 new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name
);
1152 opkg_msg(NOTICE
, "Unknown package '%s'.\n", pkg_name
);
1156 opkg_msg(DEBUG2
, "Versions from pkg_hash_fetch:");
1158 opkg_message(DEBUG2
, " old %s ", old
->version
);
1159 opkg_message(DEBUG2
, " new %s\n", new->version
);
1161 new->state_flag
|= SF_USER
;
1163 old_version
= pkg_version_str_alloc(old
);
1164 new_version
= pkg_version_str_alloc(new);
1166 cmp
= pkg_compare_versions(old
, new);
1167 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
1168 opkg_msg(DEBUG
, "Forcing downgrade\n");
1169 cmp
= -1 ; /* then we force opkg to downgrade */
1170 /* We need to use a value < 0 because in the 0 case we are asking to */
1171 /* reinstall, and some check could fail asking the "force-reinstall" option */
1173 opkg_msg(DEBUG
, "Comparing visible versions of pkg %s:"
1174 "\n\t%s is installed "
1175 "\n\t%s is available "
1176 "\n\t%d was comparison result\n",
1177 pkg_name
, old_version
, new_version
, cmp
);
1180 "Package %s (%s) installed in %s is up to date.\n",
1181 old
->name
, old_version
, old
->dest
->name
);
1185 } else if (cmp
> 0) {
1187 "Not downgrading package %s on %s from %s to %s.\n",
1188 old
->name
, old
->dest
->name
, old_version
, new_version
);
1192 } else if (cmp
< 0) {
1193 new->dest
= old
->dest
;
1194 old
->state_want
= SW_DEINSTALL
;
1200 opkg_msg(DEBUG2
,"Calling opkg_install_pkg.\n");
1201 return opkg_install_pkg(new, 0);
1205 * @brief Really install a pkg_t
1208 opkg_install_pkg(pkg_t
*pkg
, int from_upgrade
)
1212 pkg_t
*old_pkg
= NULL
;
1213 pkg_vec_t
*replacees
;
1214 abstract_pkg_t
*ab_pkg
= NULL
;
1220 sigset_t newset
, oldset
;
1223 message
= 1; /* Coming from an upgrade, and should change the output message */
1225 opkg_msg(DEBUG2
, "Calling pkg_arch_supported.\n");
1227 if (!pkg_arch_supported(pkg
)) {
1228 opkg_msg(ERROR
, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1229 pkg
->architecture
, pkg
->name
);
1232 if (pkg
->state_status
== SS_INSTALLED
&& conf
->nodeps
== 0) {
1233 err
= satisfy_dependencies_for(pkg
);
1237 opkg_msg(NOTICE
, "Package %s is already installed on %s.\n",
1238 pkg
->name
, pkg
->dest
->name
);
1242 if (pkg
->dest
== NULL
) {
1243 pkg
->dest
= conf
->default_dest
;
1246 old_pkg
= pkg_hash_fetch_installed_by_name(pkg
->name
);
1248 err
= opkg_install_check_downgrade(pkg
, old_pkg
, message
);
1252 pkg
->state_want
= SW_INSTALL
;
1254 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependencies */
1257 err
= check_conflicts_for(pkg
);
1261 /* this setup is to remove the upgrade scenario in the end when
1262 installing pkg A, A deps B & B deps on A. So both B and A are
1263 installed. Then A's installation is started resulting in an
1264 uncecessary upgrade */
1265 if (pkg
->state_status
== SS_INSTALLED
)
1268 err
= verify_pkg_installable(pkg
);
1272 if (pkg
->local_filename
== NULL
) {
1273 if(!conf
->cache
&& conf
->download_only
){
1275 if(getcwd(cwd
, sizeof(cwd
)) != NULL
)
1276 err
= opkg_download_pkg(pkg
, cwd
);
1280 err
= opkg_download_pkg(pkg
, conf
->tmp_dir
);
1283 opkg_msg(ERROR
, "Failed to download %s. "
1284 "Perhaps you need to run 'opkg update'?\n",
1290 /* check that the repository is valid */
1291 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1292 char *list_file_name
, *sig_file_name
, *lists_dir
;
1294 /* check to ensure the package has come from a repository */
1295 if (conf
->check_signature
&& pkg
->src
)
1297 sprintf_alloc (&lists_dir
, "%s",
1298 (conf
->restrict_to_default_dest
)
1299 ? conf
->default_dest
->lists_dir
1301 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, pkg
->src
->name
);
1302 sprintf_alloc (&sig_file_name
, "%s/%s.sig", lists_dir
, pkg
->src
->name
);
1304 if (file_exists (sig_file_name
))
1306 if (opkg_verify_file (list_file_name
, sig_file_name
)){
1307 opkg_msg(ERROR
, "Failed to verify the signature of %s.\n",
1312 opkg_msg(ERROR
, "Signature file is missing for %s. "
1313 "Perhaps you need to run 'opkg update'?\n",
1319 free (list_file_name
);
1320 free (sig_file_name
);
1324 /* Check for md5 values */
1327 file_md5
= file_md5sum_alloc(pkg
->local_filename
);
1328 if (file_md5
&& strcmp(file_md5
, pkg
->md5sum
))
1330 opkg_msg(ERROR
, "Package %s md5sum mismatch. "
1331 "Either the opkg or the package index are corrupt. "
1332 "Try 'opkg update'.\n",
1342 /* Check for sha256 value */
1345 file_sha256
= file_sha256sum_alloc(pkg
->local_filename
);
1346 if (file_sha256
&& strcmp(file_sha256
, pkg
->sha256sum
))
1348 opkg_msg(ERROR
, "Package %s sha256sum mismatch. "
1349 "Either the opkg or the package index are corrupt. "
1350 "Try 'opkg update'.\n",
1359 if(conf
->download_only
) {
1360 if (conf
->nodeps
== 0) {
1361 err
= satisfy_dependencies_for(pkg
);
1368 if (pkg
->tmp_unpack_dir
== NULL
) {
1369 if (unpack_pkg_control_files(pkg
) == -1) {
1370 opkg_msg(ERROR
, "Failed to unpack control files from %s.\n",
1371 pkg
->local_filename
);
1376 err
= update_file_ownership(pkg
, old_pkg
);
1380 if (conf
->nodeps
== 0) {
1381 err
= satisfy_dependencies_for(pkg
);
1384 if (pkg
->state_status
== SS_UNPACKED
)
1385 /* Circular dependency has installed it for us. */
1389 replacees
= pkg_vec_alloc();
1390 pkg_get_installed_replacees(pkg
, replacees
);
1392 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1394 sigemptyset(&newset
);
1395 sigaddset(&newset
, SIGINT
);
1396 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1398 opkg_state_changed
++;
1399 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1402 pkg_remove_orphan_dependent(pkg
, old_pkg
);
1404 /* XXX: BUG: we really should treat replacement more like an upgrade
1405 * Instead, we're going to remove the replacees
1407 err
= pkg_remove_installed_replacees(replacees
);
1409 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1411 err
= prerm_upgrade_old_pkg(pkg
, old_pkg
);
1413 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1415 err
= prerm_deconfigure_conflictors(pkg
, replacees
);
1417 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1419 err
= preinst_configure(pkg
, old_pkg
);
1421 goto UNWIND_PREINST_CONFIGURE
;
1423 err
= backup_modified_conffiles(pkg
, old_pkg
);
1425 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1427 err
= check_data_file_clashes(pkg
, old_pkg
);
1429 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1431 err
= postrm_upgrade_old_pkg(pkg
, old_pkg
);
1433 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1438 /* point of no return: no unwinding after this */
1440 old_pkg
->state_want
= SW_DEINSTALL
;
1442 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1443 opkg_msg(INFO
, "Not removing obsolesced files because "
1444 "package %s marked noprune.\n",
1447 opkg_msg(INFO
, "Removing obsolesced files for %s\n",
1449 if (remove_obsolesced_files(pkg
, old_pkg
)) {
1450 opkg_msg(ERROR
, "Failed to determine "
1451 "obsolete files from previously "
1452 "installed %s\n", old_pkg
->name
);
1456 /* removing files from old package, to avoid ghost files */
1457 remove_data_files_and_list(old_pkg
);
1458 remove_maintainer_scripts(old_pkg
);
1462 opkg_msg(INFO
, "Installing maintainer scripts.\n");
1463 if (install_maintainer_scripts(pkg
, old_pkg
)) {
1464 opkg_msg(ERROR
, "Failed to extract maintainer scripts for %s."
1465 " Package debris may remain!\n",
1470 /* the following just returns 0 */
1471 remove_disappeared(pkg
);
1473 opkg_msg(INFO
, "Installing data files for %s.\n", pkg
->name
);
1475 if (install_data_files(pkg
)) {
1476 opkg_msg(ERROR
, "Failed to extract data files for %s. "
1477 "Package debris may remain!\n",
1482 err
= check_data_file_clashes_change(pkg
, old_pkg
);
1484 opkg_msg(ERROR
, "check_data_file_clashes_change() failed for "
1485 "for files belonging to %s.\n",
1489 opkg_msg(INFO
, "Resolving conf files for %s\n", pkg
->name
);
1490 resolve_conffiles(pkg
);
1492 pkg
->state_status
= SS_UNPACKED
;
1493 old_state_flag
= pkg
->state_flag
;
1494 pkg
->state_flag
&= ~SF_PREFER
;
1495 opkg_msg(DEBUG
, "pkg=%s old_state_flag=%x state_flag=%x\n",
1496 pkg
->name
, old_state_flag
, pkg
->state_flag
);
1499 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1501 time(&pkg
->installed_time
);
1503 ab_pkg
= pkg
->parent
;
1505 ab_pkg
->state_status
= pkg
->state_status
;
1507 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1508 pkg_vec_free (replacees
);
1512 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1513 postrm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1514 UNWIND_CHECK_DATA_FILE_CLASHES
:
1515 check_data_file_clashes_unwind(pkg
, old_pkg
);
1516 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1517 backup_modified_conffiles_unwind(pkg
, old_pkg
);
1518 UNWIND_PREINST_CONFIGURE
:
1519 preinst_configure_unwind(pkg
, old_pkg
);
1520 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1521 prerm_deconfigure_conflictors_unwind(pkg
, replacees
);
1522 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1523 prerm_upgrade_old_pkg_unwind(pkg
, old_pkg
);
1524 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1525 pkg_remove_installed_replacees_unwind(replacees
);
1528 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1530 pkg_vec_free (replacees
);