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.
27 #include "pkg_extract.h"
29 #include "opkg_install.h"
30 #include "opkg_configure.h"
31 #include "opkg_download.h"
32 #include "opkg_remove.h"
34 #include "opkg_utils.h"
35 #include "opkg_message.h"
37 #include "opkg_defines.h"
39 #include "sprintf_alloc.h"
40 #include "file_util.h"
43 #include "libbb/libbb.h"
46 satisfy_dependencies_for(opkg_conf_t
*conf
, pkg_t
*pkg
)
49 pkg_vec_t
*depends
= pkg_vec_alloc();
51 char **tmp
, **unresolved
= NULL
;
54 ndepends
= pkg_hash_fetch_unsatisfied_dependencies(conf
,
59 opkg_message(conf
, OPKG_ERROR
,
60 "%s: Cannot satisfy the following dependencies for %s:\n\t",
61 conf
->force_depends
? "Warning" : "ERROR", pkg
->name
);
64 opkg_message(conf
, OPKG_ERROR
, " %s", *unresolved
);
69 opkg_message(conf
, OPKG_ERROR
, "\n");
70 if (! conf
->force_depends
) {
71 opkg_message(conf
, OPKG_INFO
,
72 "This could mean that your package list is out of date or that the packages\n"
73 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
74 "of this problem try again with the '-force-depends' option.\n");
75 pkg_vec_free(depends
);
81 pkg_vec_free(depends
);
85 /* Mark packages as to-be-installed */
86 for (i
=0; i
< depends
->len
; i
++) {
87 /* Dependencies should be installed the same place as pkg */
88 if (depends
->pkgs
[i
]->dest
== NULL
) {
89 depends
->pkgs
[i
]->dest
= pkg
->dest
;
91 depends
->pkgs
[i
]->state_want
= SW_INSTALL
;
94 for (i
= 0; i
< depends
->len
; i
++) {
95 dep
= depends
->pkgs
[i
];
96 /* The package was uninstalled when we started, but another
97 dep earlier in this loop may have depended on it and pulled
98 it in, so check first. */
99 if ((dep
->state_status
!= SS_INSTALLED
)
100 && (dep
->state_status
!= SS_UNPACKED
)) {
101 opkg_message(conf
, OPKG_DEBUG2
,"Function: %s calling opkg_install_pkg \n",__FUNCTION__
);
102 err
= opkg_install_pkg(conf
, dep
,0);
103 /* mark this package as having been automatically installed to
104 * satisfy a dependancy */
105 dep
->auto_installed
= 1;
107 pkg_vec_free(depends
);
113 pkg_vec_free(depends
);
119 check_conflicts_for(opkg_conf_t
*conf
, pkg_t
*pkg
)
122 pkg_vec_t
*conflicts
= NULL
;
125 if (conf
->force_depends
) {
133 if (!conf
->force_depends
)
134 conflicts
= (pkg_vec_t
*)pkg_hash_fetch_conflicts(&conf
->pkg_hash
, pkg
);
137 opkg_message(conf
, level
,
138 "%s: The following packages conflict with %s:\n\t", prefix
, pkg
->name
);
140 while (i
< conflicts
->len
)
141 opkg_message(conf
, level
, " %s", conflicts
->pkgs
[i
++]->name
);
142 opkg_message(conf
, level
, "\n");
143 pkg_vec_free(conflicts
);
150 update_file_ownership(opkg_conf_t
*conf
, pkg_t
*new_pkg
, pkg_t
*old_pkg
)
152 str_list_t
*new_list
, *old_list
;
153 str_list_elt_t
*iter
, *niter
;
155 new_list
= pkg_get_installed_files(conf
, new_pkg
);
156 if (new_list
== NULL
)
159 for (iter
= str_list_first(new_list
), niter
= str_list_next(new_list
, iter
);
161 iter
= niter
, niter
= str_list_next(new_list
, niter
)) {
162 char *new_file
= (char *)iter
->data
;
163 pkg_t
*owner
= file_hash_get_file_owner(conf
, new_file
);
165 opkg_message(conf
, OPKG_ERROR
, "Null new_file for new_pkg=%s\n", new_pkg
->name
);
166 if (!owner
|| (owner
== old_pkg
))
167 file_hash_set_file_owner(conf
, new_file
, new_pkg
);
171 old_list
= pkg_get_installed_files(conf
, old_pkg
);
172 if (old_list
== NULL
) {
173 pkg_free_installed_files(new_pkg
);
177 for (iter
= str_list_first(old_list
), niter
= str_list_next(old_list
, iter
);
179 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
180 char *old_file
= (char *)iter
->data
;
181 pkg_t
*owner
= file_hash_get_file_owner(conf
, old_file
);
182 if (owner
== old_pkg
) {
184 hash_table_insert(&conf
->obs_file_hash
, old_file
, old_pkg
);
187 pkg_free_installed_files(old_pkg
);
189 pkg_free_installed_files(new_pkg
);
194 verify_pkg_installable(opkg_conf_t
*conf
, pkg_t
*pkg
)
196 /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
198 /* sma 6.20.02: yup; here's the first bit */
200 * XXX: BUG easy for cworth
201 * 1) please point the call below to the correct current root destination
202 * 2) we need to resolve how to check the required space for a pending pkg,
203 * my diddling with the .opk file size below isn't going to cut it.
204 * 3) return a proper error code instead of 1
206 int comp_size
, blocks_available
;
209 if (!conf
->force_space
&& pkg
->installed_size
!= NULL
) {
210 root_dir
= pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
;
211 blocks_available
= get_available_blocks(root_dir
);
213 comp_size
= strtoul(pkg
->installed_size
, NULL
, 0);
214 /* round up a blocks count without doing fancy-but-slow casting jazz */
215 comp_size
= (int)((comp_size
+ 1023) / 1024);
217 if (comp_size
>= blocks_available
) {
218 opkg_message(conf
, OPKG_ERROR
,
219 "Only have %d available blocks on filesystem %s, pkg %s needs %d\n",
220 blocks_available
, root_dir
, pkg
->name
, comp_size
);
228 unpack_pkg_control_files(opkg_conf_t
*conf
, pkg_t
*pkg
)
231 char *conffiles_file_name
;
233 FILE *conffiles_file
;
235 sprintf_alloc(&pkg
->tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
, pkg
->name
);
237 pkg
->tmp_unpack_dir
= mkdtemp(pkg
->tmp_unpack_dir
);
238 if (pkg
->tmp_unpack_dir
== NULL
) {
239 opkg_message(conf
, OPKG_ERROR
,
240 "%s: Failed to create temporary directory '%s': %s\n",
241 __FUNCTION__
, pkg
->tmp_unpack_dir
, strerror(errno
));
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 fprintf(stderr
, "%s: failed to open %s: %s\n",
269 __FUNCTION__
, conffiles_file_name
, strerror(errno
));
270 free(conffiles_file_name
);
273 free(conffiles_file_name
);
277 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') {
287 /* Prepend dest->root_dir to conffile name.
288 Take pains to avoid multiple slashes. */
289 root_dir
= pkg
->dest
->root_dir
;
290 if (conf
->offline_root
)
291 /* skip the offline_root prefix */
292 root_dir
= pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
293 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
294 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
296 /* Can't get an md5sum now, (file isn't extracted yet).
297 We'll wait until resolve_conffiles */
298 conffile_list_append(&pkg
->conffiles
, cf_name_in_dest
, NULL
);
301 free(cf_name_in_dest
);
304 fclose(conffiles_file
);
310 * Remove packages which were auto_installed due to a dependency by old_pkg,
311 * which are no longer a dependency in the new (upgraded) pkg.
314 pkg_remove_orphan_dependent(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
316 int i
, j
, k
, l
, found
;
319 struct compound_depend
*cd0
, *cd1
;
320 abstract_pkg_t
**dependents
;
322 int count0
= old_pkg
->pre_depends_count
+
323 old_pkg
->depends_count
+
324 old_pkg
->recommends_count
+
325 old_pkg
->suggests_count
;
326 int count1
= pkg
->pre_depends_count
+
328 pkg
->recommends_count
+
331 for (i
=0; i
<count0
; i
++) {
332 cd0
= &old_pkg
->depends
[i
];
333 if (cd0
->type
!= DEPEND
)
335 for (j
=0; j
<cd0
->possibility_count
; j
++) {
339 for (k
=0; k
<count1
; k
++) {
340 cd1
= &pkg
->depends
[i
];
341 if (cd1
->type
!= DEPEND
)
343 for (l
=0; l
<cd1
->possibility_count
; l
++) {
344 if (cd0
->possibilities
[j
]
345 == cd1
->possibilities
[l
]) {
358 * old_pkg has a dependency that pkg does not.
360 p
= pkg_hash_fetch_installed_by_name (&conf
->pkg_hash
,
361 cd0
->possibilities
[j
]->pkg
->name
);
366 if (!p
->auto_installed
)
369 n_deps
= pkg_has_installed_dependents(conf
, NULL
, p
,
371 n_deps
--; /* don't count old_pkg */
374 opkg_message (conf
, OPKG_NOTICE
,
375 "%s was autoinstalled and is "
376 "now orphaned, removing.\n",
379 /* p has one installed dependency (old_pkg),
380 * which we need to ignore during removal. */
381 p
->state_flag
|= SF_REPLACE
;
383 opkg_remove_pkg(conf
, p
, 0);
385 opkg_message(conf
, OPKG_INFO
,
386 "%s was autoinstalled and is "
387 "still required by %d "
388 "installed packages.\n",
397 /* returns number of installed replacees */
399 pkg_get_installed_replacees(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_vec_t
*installed_replacees
)
401 abstract_pkg_t
**replaces
= pkg
->replaces
;
402 int replaces_count
= pkg
->replaces_count
;
404 for (i
= 0; i
< replaces_count
; i
++) {
405 abstract_pkg_t
*ab_pkg
= replaces
[i
];
406 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
408 for (j
= 0; j
< pkg_vec
->len
; j
++) {
409 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
410 if (!pkg_conflicts(pkg
, replacee
))
412 if (replacee
->state_status
== SS_INSTALLED
) {
413 pkg_vec_insert(installed_replacees
, replacee
);
418 return installed_replacees
->len
;
422 pkg_remove_installed_replacees(opkg_conf_t
*conf
, pkg_vec_t
*replacees
)
425 int replaces_count
= replacees
->len
;
426 for (i
= 0; i
< replaces_count
; i
++) {
427 pkg_t
*replacee
= replacees
->pkgs
[i
];
429 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
430 err
= opkg_remove_pkg(conf
, replacee
,0);
437 /* to unwind the removal: make sure they are installed */
439 pkg_remove_installed_replacees_unwind(opkg_conf_t
*conf
, pkg_vec_t
*replacees
)
442 int replaces_count
= replacees
->len
;
443 for (i
= 0; i
< replaces_count
; i
++) {
444 pkg_t
*replacee
= replacees
->pkgs
[i
];
445 if (replacee
->state_status
!= SS_INSTALLED
) {
446 opkg_message(conf
, OPKG_DEBUG2
,"Function: %s calling opkg_install_pkg \n",__FUNCTION__
);
447 err
= opkg_install_pkg(conf
, replacee
,0);
455 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
457 opkg_install_check_downgrade(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
, int message
)
460 char message_out
[15];
461 char *old_version
= pkg_version_str_alloc(old_pkg
);
462 char *new_version
= pkg_version_str_alloc(pkg
);
463 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
466 memset(message_out
,'\x0',15);
467 strncpy (message_out
,"Upgrading ",strlen("Upgrading "));
468 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
469 cmp
= -1 ; /* then we force opkg to downgrade */
470 strncpy (message_out
,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
471 /* reinstall, and some check could fail asking the "force-reinstall" option */
475 opkg_message(conf
, OPKG_NOTICE
,
476 "Not downgrading package %s on %s from %s to %s.\n",
477 old_pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
479 } else if (cmp
< 0) {
480 opkg_message(conf
, OPKG_NOTICE
,
481 "%s%s on %s from %s to %s...\n",
482 message_out
, pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
483 pkg
->dest
= old_pkg
->dest
;
485 } else /* cmp == 0 */ {
486 if (conf
->force_reinstall
) {
487 opkg_message(conf
, OPKG_NOTICE
,
488 "Reinstalling %s (%s) on %s...\n",
489 pkg
->name
, new_version
, old_pkg
->dest
->name
);
490 pkg
->dest
= old_pkg
->dest
;
493 opkg_message(conf
, OPKG_NOTICE
,
494 "Not installing %s (%s) on %s -- already installed.\n",
495 pkg
->name
, new_version
, old_pkg
->dest
->name
);
503 char message_out
[15] ;
504 memset(message_out
,'\x0',15);
506 strncpy( message_out
,"Upgrading ",strlen("Upgrading ") );
508 strncpy( message_out
,"Installing ",strlen("Installing ") );
509 char *version
= pkg_version_str_alloc(pkg
);
511 opkg_message(conf
, OPKG_NOTICE
,
512 "%s%s (%s) to %s...\n", message_out
,
513 pkg
->name
, version
, pkg
->dest
->name
);
521 prerm_upgrade_old_pkg(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
523 /* DPKG_INCOMPATIBILITY:
524 dpkg does some things here that we don't do yet. Do we care?
526 1. If a version of the package is already installed, call
527 old-prerm upgrade new-version
528 2. If the script runs but exits with a non-zero exit status
529 new-prerm failed-upgrade old-version
530 Error unwind, for both the above cases:
531 old-postinst abort-upgrade new-version
537 prerm_upgrade_old_pkg_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
539 /* DPKG_INCOMPATIBILITY:
540 dpkg does some things here that we don't do yet. Do we care?
541 (See prerm_upgrade_old_package for details)
547 prerm_deconfigure_conflictors(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_vec_t
*conflictors
)
549 /* DPKG_INCOMPATIBILITY:
550 dpkg does some things here that we don't do yet. Do we care?
551 2. If a 'conflicting' package is being removed at the same time:
552 1. If any packages depended on that conflicting package and
553 --auto-deconfigure is specified, call, for each such package:
554 deconfigured's-prerm deconfigure \
555 in-favour package-being-installed version \
556 removing conflicting-package version
558 deconfigured's-postinst abort-deconfigure \
559 in-favour package-being-installed-but-failed version \
560 removing conflicting-package version
562 The deconfigured packages are marked as requiring
563 configuration, so that if --install is used they will be
564 configured again if possible.
565 2. To prepare for removal of the conflicting package, call:
566 conflictor's-prerm remove in-favour package new-version
568 conflictor's-postinst abort-remove in-favour package new-version
574 prerm_deconfigure_conflictors_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_vec_t
*conflictors
)
576 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
577 do yet. Do we care? (See prerm_deconfigure_conflictors for
583 preinst_configure(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
589 char *old_version
= pkg_version_str_alloc(old_pkg
);
590 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
592 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
593 char *pkg_version
= pkg_version_str_alloc(pkg
);
594 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
597 preinst_args
= xstrdup("install");
600 err
= pkg_run_script(conf
, pkg
, "preinst", preinst_args
);
602 opkg_message(conf
, OPKG_ERROR
,
603 "Aborting installation of %s\n", pkg
->name
);
613 preinst_configure_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
615 /* DPKG_INCOMPATIBILITY:
616 dpkg does the following error unwind, should we?
617 pkg->postrm abort-upgrade old-version
618 OR pkg->postrm abort-install old-version
619 OR pkg->postrm abort-install
625 backup_filename_alloc(const char *file_name
)
629 sprintf_alloc(&backup
, "%s%s", file_name
, OPKG_BACKUP_SUFFIX
);
636 backup_make_backup(opkg_conf_t
*conf
, const char *file_name
)
641 backup
= backup_filename_alloc(file_name
);
642 err
= file_copy(file_name
, backup
);
644 opkg_message(conf
, OPKG_ERROR
,
645 "%s: Failed to copy %s to %s\n",
646 __FUNCTION__
, file_name
, backup
);
655 backup_exists_for(const char *file_name
)
660 backup
= backup_filename_alloc(file_name
);
662 ret
= file_exists(backup
);
670 backup_remove(const char *file_name
)
674 backup
= backup_filename_alloc(file_name
);
682 backup_modified_conffiles(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
685 conffile_list_elt_t
*iter
;
688 if (conf
->noaction
) return 0;
690 /* Backup all modified conffiles */
692 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
696 cf_name
= root_filename_alloc(conf
, cf
->name
);
698 /* Don't worry if the conffile is just plain gone */
699 if (file_exists(cf_name
) && conffile_has_been_modified(conf
, cf
)) {
700 err
= backup_make_backup(conf
, cf_name
);
709 /* Backup all conffiles that were not conffiles in old_pkg */
710 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
712 cf
= (conffile_t
*)iter
->data
;
713 cf_name
= root_filename_alloc(conf
, cf
->name
);
714 /* Ignore if this was a conffile in old_pkg as well */
715 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
719 if (file_exists(cf_name
) && (! backup_exists_for(cf_name
))) {
720 err
= backup_make_backup(conf
, cf_name
);
732 backup_modified_conffiles_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
734 conffile_list_elt_t
*iter
;
737 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
738 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
742 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
743 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
751 check_data_file_clashes(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
753 /* DPKG_INCOMPATIBILITY:
754 opkg takes a slightly different approach than dpkg at this
755 point. dpkg installs each file in the new package while
756 creating a backup for any file that is replaced, (so that it
757 can unwind if necessary). To avoid complexity and redundant
758 storage, opkg doesn't do any installation until later, (at the
759 point at which dpkg removes the backups.
761 But, we do have to check for data file clashes, since after
762 installing a package with a file clash, removing either of the
763 packages involved in the clash has the potential to break the
766 str_list_t
*files_list
;
767 str_list_elt_t
*iter
, *niter
;
771 files_list
= pkg_get_installed_files(conf
, pkg
);
772 if (files_list
== NULL
)
775 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
777 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
779 char *filename
= (char *) iter
->data
;
780 root_filename
= root_filename_alloc(conf
, filename
);
781 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
784 /* Pre-existing conffiles are OK */
785 /* @@@@ should have way to check that it is a conffile -Jamey */
786 if (backup_exists_for(root_filename
)) {
790 /* Pre-existing files are OK if force-overwrite was asserted. */
791 if (conf
->force_overwrite
) {
792 /* but we need to change who owns this file */
793 file_hash_set_file_owner(conf
, filename
, pkg
);
797 owner
= file_hash_get_file_owner(conf
, filename
);
799 /* Pre-existing files are OK if owned by the pkg being upgraded. */
800 if (owner
&& old_pkg
) {
801 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
806 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
808 opkg_message(conf
, OPKG_DEBUG2
, "Checking for replaces for %s in package %s\n", filename
, owner
->name
);
809 if (pkg_replaces(pkg
, owner
)) {
812 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
813 then it's ok to overwrite. */
814 if (strcmp(owner
->name
,pkg
->name
)==0){
815 opkg_message(conf
, OPKG_INFO
, "Replacing pre-existing file %s owned by package %s\n", filename
, owner
->name
);
820 /* Pre-existing files are OK if they are obsolete */
821 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
823 opkg_message(conf
, OPKG_INFO
, "Pre-exiting file %s is obsolete. obs_pkg=%s\n", filename
, obs
->name
);
827 /* We have found a clash. */
828 opkg_message(conf
, OPKG_ERROR
,
829 "Package %s wants to install file %s\n"
830 "\tBut that file is already provided by package ",
831 pkg
->name
, filename
);
833 opkg_message(conf
, OPKG_ERROR
,
834 "%s\n", owner
->name
);
836 opkg_message(conf
, OPKG_ERROR
,
837 "<no package>\nPlease move this file out of the way and try again.\n");
843 pkg_free_installed_files(pkg
);
849 * XXX: This function sucks, as does the below comment.
852 check_data_file_clashes_change(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
854 /* Basically that's the worst hack I could do to be able to change ownership of
855 file list, but, being that we have no way to unwind the mods, due to structure
856 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
857 What we do here is change the ownership of file in hash if a replace ( or similar events
859 Only the action that are needed to change name should be considered.
860 @@@ To change after 1.0 release.
862 str_list_t
*files_list
;
863 str_list_elt_t
*iter
, *niter
;
865 char *root_filename
= NULL
;
867 files_list
= pkg_get_installed_files(conf
, pkg
);
868 if (files_list
== NULL
)
871 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
873 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
874 char *filename
= (char *) iter
->data
;
877 root_filename
= NULL
;
879 root_filename
= root_filename_alloc(conf
, filename
);
880 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
883 owner
= file_hash_get_file_owner(conf
, filename
);
885 if (conf
->force_overwrite
) {
886 /* but we need to change who owns this file */
887 file_hash_set_file_owner(conf
, filename
, pkg
);
892 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
894 if (pkg_replaces(pkg
, owner
)) {
895 /* It's now time to change the owner of that file.
896 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
897 opkg_message(conf
, OPKG_INFO
, "Replacing pre-existing file %s owned by package %s\n", filename
, owner
->name
);
898 file_hash_set_file_owner(conf
, filename
, pkg
);
907 root_filename
= NULL
;
909 pkg_free_installed_files(pkg
);
915 check_data_file_clashes_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
917 /* Nothing to do since check_data_file_clashes doesn't change state */
922 postrm_upgrade_old_pkg(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
924 /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
925 1. If the package is being upgraded, call
926 old-postrm upgrade new-version
927 2. If this fails, attempt:
928 new-postrm failed-upgrade old-version
929 Error unwind, for both cases:
930 old-preinst abort-upgrade new-version */
935 postrm_upgrade_old_pkg_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
937 /* DPKG_INCOMPATIBILITY:
938 dpkg does some things here that we don't do yet. Do we care?
939 (See postrm_upgrade_old_pkg for details)
945 remove_obsolesced_files(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
948 str_list_t
*old_files
;
950 str_list_t
*new_files
;
952 hash_table_t new_files_table
;
954 old_files
= pkg_get_installed_files(conf
, old_pkg
);
955 if (old_files
== NULL
)
958 new_files
= pkg_get_installed_files(conf
, pkg
);
959 if (new_files
== NULL
) {
960 pkg_free_installed_files(old_pkg
);
964 new_files_table
.entries
= NULL
;
965 hash_table_init("new_files" , &new_files_table
, 20);
966 for (nf
= str_list_first(new_files
); nf
; nf
= str_list_next(new_files
, nf
)) {
968 hash_table_insert(&new_files_table
, nf
->data
, nf
->data
);
971 for (of
= str_list_first(old_files
); of
; of
= str_list_next(old_files
, of
)) {
974 old
= (char *)of
->data
;
975 new = (char *) hash_table_get (&new_files_table
, old
);
979 if (file_is_dir(old
)) {
982 owner
= file_hash_get_file_owner(conf
, old
);
983 if (owner
!= old_pkg
) {
984 /* in case obsolete file no longer belongs to old_pkg */
988 /* old file is obsolete */
989 opkg_message(conf
, OPKG_INFO
,
990 " removing obsolete file %s\n", old
);
991 if (!conf
->noaction
) {
994 opkg_message(conf
, OPKG_ERROR
, " Warning: remove %s failed: %s\n", old
,
1000 hash_table_deinit(&new_files_table
);
1001 pkg_free_installed_files(old_pkg
);
1002 pkg_free_installed_files(pkg
);
1008 install_maintainer_scripts(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
1013 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1014 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1015 pkg
->dest
->info_dir
,
1022 remove_disappeared(opkg_conf_t
*conf
, pkg_t
*pkg
)
1024 /* DPKG_INCOMPATIBILITY:
1025 This is a fairly sophisticated dpkg operation. Shall we
1028 /* Any packages all of whose files have been overwritten during the
1029 installation, and which aren't required for dependencies, are
1030 considered to have been removed. For each such package
1031 1. disappearer's-postrm disappear overwriter overwriter-version
1032 2. The package's maintainer scripts are removed
1033 3. It is noted in the status database as being in a sane state,
1034 namely not installed (any conffiles it may have are ignored,
1035 rather than being removed by dpkg). Note that disappearing
1036 packages do not have their prerm called, because dpkg doesn't
1037 know in advance that the package is going to vanish.
1043 install_data_files(opkg_conf_t
*conf
, pkg_t
*pkg
)
1047 /* opkg takes a slightly different approach to data file backups
1048 than dpkg. Rather than removing backups at this point, we
1049 actually do the data file installation now. See comments in
1050 check_data_file_clashes() for more details. */
1052 opkg_message(conf
, OPKG_INFO
,
1053 " extracting data files to %s\n", pkg
->dest
->root_dir
);
1054 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1059 /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
1060 so we can't save ourself from removing important packages
1061 At this point we (should) have extracted the .control file, so it
1062 would be a good idea to reload the data in it, and set the Essential
1063 state in *pkg. From now on the Essential is back in status file and
1064 we can protect again.
1065 We should operate this way:
1066 fopen the file ( pkg->dest->root_dir/pkg->name.control )
1067 check for "Essential" in it
1068 set the value in pkg->essential.
1069 This new routine could be useful also for every other flag
1071 set_flags_from_control(conf
, pkg
) ;
1073 opkg_message(conf
, OPKG_DEBUG
, " Calling pkg_write_filelist from %s\n", __FUNCTION__
);
1074 err
= pkg_write_filelist(conf
, pkg
);
1078 /* XXX: FEATURE: opkg should identify any files which existed
1079 before installation and which were overwritten, (see
1080 check_data_file_clashes()). What it must do is remove any such
1081 files from the filelist of the old package which provided the
1082 file. Otherwise, if the old package were removed at some point
1083 it would break the new package. Removing the new package will
1084 also break the old one, but this cannot be helped since the old
1085 package's file has already been deleted. This is the importance
1086 of check_data_file_clashes(), and only allowing opkg to install
1087 a clashing package with a user force. */
1093 user_prefers_old_conffile(const char *file_name
, const char *backup
)
1096 const char *short_file_name
;
1098 short_file_name
= strrchr(file_name
, '/');
1099 if (short_file_name
) {
1102 short_file_name
= file_name
;
1106 response
= get_user_response(" Configuration file '%s'\n"
1107 " ==> File on system created by you or by a script.\n"
1108 " ==> File also in package provided by package maintainer.\n"
1109 " What would you like to do about it ? Your options are:\n"
1110 " Y or I : install the package maintainer's version\n"
1111 " N or O : keep your currently-installed version\n"
1112 " D : show the differences between the versions (if diff is installed)\n"
1113 " The default action is to keep your current version.\n"
1114 " *** %s (Y/I/N/O/D) [default=N] ? ", file_name
, short_file_name
);
1116 if (response
== NULL
)
1119 if (strcmp(response
, "y") == 0
1120 || strcmp(response
, "i") == 0
1121 || strcmp(response
, "yes") == 0) {
1126 if (strcmp(response
, "d") == 0) {
1127 const char *argv
[] = {"diff", "-u", backup
, file_name
, NULL
};
1129 printf(" [Press ENTER to continue]\n");
1130 response
= file_read_line_alloc(stdin
);
1141 resolve_conffiles(opkg_conf_t
*conf
, pkg_t
*pkg
)
1143 conffile_list_elt_t
*iter
;
1148 if (conf
->noaction
) return 0;
1150 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1151 char *root_filename
;
1152 cf
= (conffile_t
*)iter
->data
;
1153 root_filename
= root_filename_alloc(conf
, cf
->name
);
1155 /* Might need to initialize the md5sum for each conffile */
1156 if (cf
->value
== NULL
) {
1157 cf
->value
= file_md5sum_alloc(root_filename
);
1160 if (!file_exists(root_filename
)) {
1161 free(root_filename
);
1165 cf_backup
= backup_filename_alloc(root_filename
);
1168 if (file_exists(cf_backup
)) {
1169 /* Let's compute md5 to test if files are changed */
1170 md5sum
= file_md5sum_alloc(cf_backup
);
1171 if (md5sum
&& cf
->value
&& strcmp(cf
->value
,md5sum
) != 0 ) {
1172 if (conf
->force_maintainer
) {
1173 opkg_message(conf
, OPKG_NOTICE
, "Conffile %s using maintainer's setting.\n", cf_backup
);
1174 } else if (conf
->force_defaults
1175 || user_prefers_old_conffile(root_filename
, cf_backup
) ) {
1176 rename(cf_backup
, root_filename
);
1185 free(root_filename
);
1193 opkg_install_by_name(opkg_conf_t
*conf
, const char *pkg_name
)
1197 char *old_version
, *new_version
;
1199 old
= pkg_hash_fetch_installed_by_name(&conf
->pkg_hash
, pkg_name
);
1201 opkg_message(conf
, OPKG_DEBUG2
, "Old versions from pkg_hash_fetch %s \n", old
->version
);
1203 new = pkg_hash_fetch_best_installation_candidate_by_name(conf
, pkg_name
);
1207 opkg_message(conf
, OPKG_DEBUG2
, "Versions from pkg_hash_fetch in %s ", __FUNCTION__
);
1209 opkg_message(conf
, OPKG_DEBUG2
, " old %s ", old
->version
);
1210 opkg_message(conf
, OPKG_DEBUG2
, " new %s\n", new->version
);
1212 new->state_flag
|= SF_USER
;
1214 old_version
= pkg_version_str_alloc(old
);
1215 new_version
= pkg_version_str_alloc(new);
1217 cmp
= pkg_compare_versions(old
, new);
1218 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
1219 opkg_message(conf
, OPKG_DEBUG
, " Forcing downgrade \n");
1220 cmp
= -1 ; /* then we force opkg to downgrade */
1221 /* We need to use a value < 0 because in the 0 case we are asking to */
1222 /* reinstall, and some check could fail asking the "force-reinstall" option */
1224 opkg_message(conf
, OPKG_DEBUG
,
1225 "Comparing visible versions of pkg %s:"
1226 "\n\t%s is installed "
1227 "\n\t%s is available "
1228 "\n\t%d was comparison result\n",
1229 pkg_name
, old_version
, new_version
, cmp
);
1230 if (cmp
== 0 && !conf
->force_reinstall
) {
1231 opkg_message(conf
, OPKG_NOTICE
,
1232 "Package %s (%s) installed in %s is up to date.\n",
1233 old
->name
, old_version
, old
->dest
->name
);
1237 } else if (cmp
> 0) {
1238 opkg_message(conf
, OPKG_NOTICE
,
1239 "Not downgrading package %s on %s from %s to %s.\n",
1240 old
->name
, old
->dest
->name
, old_version
, new_version
);
1244 } else if (cmp
< 0) {
1245 new->dest
= old
->dest
;
1246 old
->state_want
= SW_DEINSTALL
; /* Here probably the problem for bug 1277 */
1252 opkg_message(conf
, OPKG_DEBUG2
,"%s: calling opkg_install_pkg \n",__FUNCTION__
);
1253 return opkg_install_pkg(conf
, new,0);
1257 * @brief Really install a pkg_t
1260 opkg_install_pkg(opkg_conf_t
*conf
, pkg_t
*pkg
, int from_upgrade
)
1264 pkg_t
*old_pkg
= NULL
;
1265 pkg_vec_t
*replacees
;
1266 abstract_pkg_t
*ab_pkg
= NULL
;
1272 sigset_t newset
, oldset
;
1275 message
= 1; /* Coming from an upgrade, and should change the output message */
1278 opkg_message(conf
, OPKG_ERROR
,
1279 "INTERNAL ERROR: null pkg passed to opkg_install_pkg\n");
1283 opkg_message(conf
, OPKG_DEBUG2
, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__
, __FUNCTION__
);
1285 if (!pkg_arch_supported(conf
, pkg
)) {
1286 opkg_message(conf
, OPKG_ERROR
, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1287 pkg
->architecture
, pkg
->name
);
1290 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0 && conf
->nodeps
== 0) {
1291 err
= satisfy_dependencies_for(conf
, pkg
);
1295 opkg_message(conf
, OPKG_NOTICE
,
1296 "Package %s is already installed in %s.\n",
1297 pkg
->name
, pkg
->dest
->name
);
1301 if (pkg
->dest
== NULL
) {
1302 pkg
->dest
= conf
->default_dest
;
1305 old_pkg
= pkg_hash_fetch_installed_by_name(&conf
->pkg_hash
, pkg
->name
);
1307 err
= opkg_install_check_downgrade(conf
, pkg
, old_pkg
, message
);
1311 pkg
->state_want
= SW_INSTALL
;
1313 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependences */
1316 err
= check_conflicts_for(conf
, pkg
);
1320 /* this setup is to remove the upgrade scenario in the end when
1321 installing pkg A, A deps B & B deps on A. So both B and A are
1322 installed. Then A's installation is started resulting in an
1323 uncecessary upgrade */
1324 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0)
1327 err
= verify_pkg_installable(conf
, pkg
);
1331 if (pkg
->local_filename
== NULL
) {
1332 err
= opkg_download_pkg(conf
, pkg
, conf
->tmp_dir
);
1334 opkg_message(conf
, OPKG_ERROR
,
1335 "Failed to download %s. Perhaps you need to run 'opkg update'?\n",
1341 /* check that the repository is valid */
1342 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1343 char *list_file_name
, *sig_file_name
, *lists_dir
;
1345 /* check to ensure the package has come from a repository */
1346 if (conf
->check_signature
&& pkg
->src
)
1348 sprintf_alloc (&lists_dir
, "%s",
1349 (conf
->restrict_to_default_dest
)
1350 ? conf
->default_dest
->lists_dir
1352 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, pkg
->src
->name
);
1353 sprintf_alloc (&sig_file_name
, "%s/%s.sig", lists_dir
, pkg
->src
->name
);
1355 if (file_exists (sig_file_name
))
1357 if (opkg_verify_file (conf
, list_file_name
, sig_file_name
)){
1358 opkg_message(conf
, OPKG_ERROR
, "Failed to verify the signature of: %s\n",
1363 opkg_message(conf
, OPKG_ERROR
, "Signature file is missing. "
1364 "Perhaps you need to run 'opkg update'?\n");
1369 free (list_file_name
);
1370 free (sig_file_name
);
1374 /* Check for md5 values */
1377 file_md5
= file_md5sum_alloc(pkg
->local_filename
);
1378 if (file_md5
&& strcmp(file_md5
, pkg
->md5sum
))
1380 opkg_message(conf
, OPKG_ERROR
,
1381 "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1391 /* Check for sha256 value */
1394 file_sha256
= file_sha256sum_alloc(pkg
->local_filename
);
1395 if (file_sha256
&& strcmp(file_sha256
, pkg
->sha256sum
))
1397 opkg_message(conf
, OPKG_ERROR
,
1398 "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1408 if (pkg
->tmp_unpack_dir
== NULL
) {
1409 if (unpack_pkg_control_files(conf
, pkg
) == -1) {
1410 opkg_message(conf
, OPKG_ERROR
, "Failed to unpack control"
1411 " files from %s.\n", pkg
->local_filename
);
1416 /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
1417 /* Pigi: check if it will pass from here when replacing. It seems to fail */
1418 /* That's rather strange that files don't change owner. Investigate !!!!!!*/
1419 err
= update_file_ownership(conf
, pkg
, old_pkg
);
1423 if (conf
->nodeps
== 0) {
1424 err
= satisfy_dependencies_for(conf
, pkg
);
1427 if (pkg
->state_status
== SS_UNPACKED
)
1428 /* Circular dependency has installed it for us. */
1432 replacees
= pkg_vec_alloc();
1433 pkg_get_installed_replacees(conf
, pkg
, replacees
);
1435 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1437 sigemptyset(&newset
);
1438 sigaddset(&newset
, SIGINT
);
1439 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1441 opkg_state_changed
++;
1442 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1445 pkg_remove_orphan_dependent(conf
, pkg
, old_pkg
);
1447 /* XXX: BUG: we really should treat replacement more like an upgrade
1448 * Instead, we're going to remove the replacees
1450 err
= pkg_remove_installed_replacees(conf
, replacees
);
1452 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1454 err
= prerm_upgrade_old_pkg(conf
, pkg
, old_pkg
);
1456 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1458 err
= prerm_deconfigure_conflictors(conf
, pkg
, replacees
);
1460 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1462 err
= preinst_configure(conf
, pkg
, old_pkg
);
1464 goto UNWIND_PREINST_CONFIGURE
;
1466 err
= backup_modified_conffiles(conf
, pkg
, old_pkg
);
1468 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1470 err
= check_data_file_clashes(conf
, pkg
, old_pkg
);
1472 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1474 err
= postrm_upgrade_old_pkg(conf
, pkg
, old_pkg
);
1476 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1481 /* point of no return: no unwinding after this */
1482 if (old_pkg
&& !conf
->force_reinstall
) {
1483 old_pkg
->state_want
= SW_DEINSTALL
;
1485 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1486 opkg_message(conf
, OPKG_INFO
,
1487 " not removing obsolesced files because package marked noprune\n");
1489 opkg_message(conf
, OPKG_INFO
,
1490 " removing obsolesced files\n");
1491 if (remove_obsolesced_files(conf
, pkg
, old_pkg
)) {
1492 opkg_message(conf
, OPKG_ERROR
, "Failed to determine "
1493 "obsolete files from previously "
1494 "installed %s\n", old_pkg
->name
);
1498 /* removing files from old package, to avoid ghost files */
1499 remove_data_files_and_list(conf
, old_pkg
);
1500 remove_maintainer_scripts(conf
, old_pkg
);
1504 opkg_message(conf
, OPKG_INFO
,
1505 " installing maintainer scripts\n");
1506 if (install_maintainer_scripts(conf
, pkg
, old_pkg
)) {
1507 opkg_message(conf
, OPKG_ERROR
, "Failed to extract maintainer"
1508 " scripts for %s. Package debris may remain!\n",
1513 /* the following just returns 0 */
1514 remove_disappeared(conf
, pkg
);
1516 opkg_message(conf
, OPKG_INFO
,
1517 " installing data files\n");
1519 if (install_data_files(conf
, pkg
)) {
1520 opkg_message(conf
, OPKG_ERROR
, "Failed to extract data files "
1521 "for %s. Package debris may remain!\n",
1526 err
= check_data_file_clashes_change(conf
, pkg
, old_pkg
);
1528 opkg_message(conf
, OPKG_ERROR
,
1529 "check_data_file_clashes_change() failed for "
1530 "for files belonging to %s.\n",
1534 opkg_message(conf
, OPKG_INFO
,
1535 " resolving conf files\n");
1536 resolve_conffiles(conf
, pkg
);
1538 pkg
->state_status
= SS_UNPACKED
;
1539 old_state_flag
= pkg
->state_flag
;
1540 pkg
->state_flag
&= ~SF_PREFER
;
1541 opkg_message(conf
, OPKG_DEBUG
, " pkg=%s old_state_flag=%x state_flag=%x\n", pkg
->name
, old_state_flag
, pkg
->state_flag
);
1543 if (old_pkg
&& !conf
->force_reinstall
) {
1544 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1547 time(&pkg
->installed_time
);
1549 ab_pkg
= pkg
->parent
;
1551 ab_pkg
->state_status
= pkg
->state_status
;
1553 opkg_message(conf
, OPKG_INFO
, "Done.\n");
1555 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1556 pkg_vec_free (replacees
);
1560 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1561 postrm_upgrade_old_pkg_unwind(conf
, pkg
, old_pkg
);
1562 UNWIND_CHECK_DATA_FILE_CLASHES
:
1563 check_data_file_clashes_unwind(conf
, pkg
, old_pkg
);
1564 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1565 backup_modified_conffiles_unwind(conf
, pkg
, old_pkg
);
1566 UNWIND_PREINST_CONFIGURE
:
1567 preinst_configure_unwind(conf
, pkg
, old_pkg
);
1568 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1569 prerm_deconfigure_conflictors_unwind(conf
, pkg
, replacees
);
1570 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1571 prerm_upgrade_old_pkg_unwind(conf
, pkg
, old_pkg
);
1572 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1573 pkg_remove_installed_replacees_unwind(conf
, replacees
);
1576 opkg_message(conf
, OPKG_INFO
,
1579 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1581 pkg_vec_free (replacees
);