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 **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
);
63 opkg_message(conf
, OPKG_ERROR
, " %s", *unresolved
);
66 opkg_message(conf
, OPKG_ERROR
, "\n");
67 if (! conf
->force_depends
) {
68 opkg_message(conf
, OPKG_INFO
,
69 "This could mean that your package list is out of date or that the packages\n"
70 "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
71 "of this problem try again with the '-force-depends' option.\n");
72 pkg_vec_free(depends
);
78 pkg_vec_free(depends
);
82 /* Mark packages as to-be-installed */
83 for (i
=0; i
< depends
->len
; i
++) {
84 /* Dependencies should be installed the same place as pkg */
85 if (depends
->pkgs
[i
]->dest
== NULL
) {
86 depends
->pkgs
[i
]->dest
= pkg
->dest
;
88 depends
->pkgs
[i
]->state_want
= SW_INSTALL
;
91 for (i
= 0; i
< depends
->len
; i
++) {
92 dep
= depends
->pkgs
[i
];
93 /* The package was uninstalled when we started, but another
94 dep earlier in this loop may have depended on it and pulled
95 it in, so check first. */
96 if ((dep
->state_status
!= SS_INSTALLED
)
97 && (dep
->state_status
!= SS_UNPACKED
)) {
98 opkg_message(conf
, OPKG_DEBUG2
,"Function: %s calling opkg_install_pkg \n",__FUNCTION__
);
99 err
= opkg_install_pkg(conf
, dep
,0);
100 /* mark this package as having been automatically installed to
101 * satisfy a dependancy */
102 dep
->auto_installed
= 1;
104 pkg_vec_free(depends
);
110 pkg_vec_free(depends
);
116 check_conflicts_for(opkg_conf_t
*conf
, pkg_t
*pkg
)
119 pkg_vec_t
*conflicts
= NULL
;
122 if (conf
->force_depends
) {
130 if (!conf
->force_depends
)
131 conflicts
= (pkg_vec_t
*)pkg_hash_fetch_conflicts(&conf
->pkg_hash
, pkg
);
134 opkg_message(conf
, level
,
135 "%s: The following packages conflict with %s:\n\t", prefix
, pkg
->name
);
137 while (i
< conflicts
->len
)
138 opkg_message(conf
, level
, " %s", conflicts
->pkgs
[i
++]->name
);
139 opkg_message(conf
, level
, "\n");
140 pkg_vec_free(conflicts
);
147 update_file_ownership(opkg_conf_t
*conf
, 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(conf
, new_pkg
);
153 if (new_list
== NULL
)
156 for (iter
= str_list_first(new_list
), niter
= str_list_next(new_list
, 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(conf
, new_file
);
162 opkg_message(conf
, OPKG_ERROR
, "Null new_file for new_pkg=%s\n", new_pkg
->name
);
163 if (!owner
|| (owner
== old_pkg
))
164 file_hash_set_file_owner(conf
, new_file
, new_pkg
);
168 old_list
= pkg_get_installed_files(conf
, old_pkg
);
169 if (old_list
== NULL
) {
170 pkg_free_installed_files(new_pkg
);
174 for (iter
= str_list_first(old_list
), niter
= str_list_next(old_list
, iter
);
176 iter
= niter
, niter
= str_list_next(old_list
, niter
)) {
177 char *old_file
= (char *)iter
->data
;
178 pkg_t
*owner
= file_hash_get_file_owner(conf
, old_file
);
179 if (owner
== old_pkg
) {
181 hash_table_insert(&conf
->obs_file_hash
, old_file
, old_pkg
);
184 pkg_free_installed_files(old_pkg
);
186 pkg_free_installed_files(new_pkg
);
191 verify_pkg_installable(opkg_conf_t
*conf
, pkg_t
*pkg
)
193 /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
195 /* sma 6.20.02: yup; here's the first bit */
197 * XXX: BUG easy for cworth
198 * 1) please point the call below to the correct current root destination
199 * 2) we need to resolve how to check the required space for a pending pkg,
200 * my diddling with the .opk file size below isn't going to cut it.
201 * 3) return a proper error code instead of 1
203 int comp_size
, blocks_available
;
206 if (!conf
->force_space
&& pkg
->installed_size
!= NULL
) {
207 root_dir
= pkg
->dest
? pkg
->dest
->root_dir
: conf
->default_dest
->root_dir
;
208 blocks_available
= get_available_blocks(root_dir
);
210 comp_size
= strtoul(pkg
->installed_size
, NULL
, 0);
211 /* round up a blocks count without doing fancy-but-slow casting jazz */
212 comp_size
= (int)((comp_size
+ 1023) / 1024);
214 if (comp_size
>= blocks_available
) {
215 opkg_message(conf
, OPKG_ERROR
,
216 "Only have %d available blocks on filesystem %s, pkg %s needs %d\n",
217 blocks_available
, root_dir
, pkg
->name
, comp_size
);
225 unpack_pkg_control_files(opkg_conf_t
*conf
, pkg_t
*pkg
)
228 char *conffiles_file_name
;
230 FILE *conffiles_file
;
232 sprintf_alloc(&pkg
->tmp_unpack_dir
, "%s/%s-XXXXXX", conf
->tmp_dir
, pkg
->name
);
234 pkg
->tmp_unpack_dir
= mkdtemp(pkg
->tmp_unpack_dir
);
235 if (pkg
->tmp_unpack_dir
== NULL
) {
236 opkg_message(conf
, OPKG_ERROR
,
237 "%s: Failed to create temporary directory '%s': %s\n",
238 __FUNCTION__
, pkg
->tmp_unpack_dir
, strerror(errno
));
242 err
= pkg_extract_control_files_to_dir(pkg
, pkg
->tmp_unpack_dir
);
247 /* XXX: CLEANUP: There might be a cleaner place to read in the
248 conffiles. Seems like I should be able to get everything to go
249 through pkg_init_from_file. If so, maybe it would make sense to
250 move all of unpack_pkg_control_files to that function. */
252 /* Don't need to re-read conffiles if we already have it */
253 if (!nv_pair_list_empty(&pkg
->conffiles
)) {
257 sprintf_alloc(&conffiles_file_name
, "%s/conffiles", pkg
->tmp_unpack_dir
);
258 if (! file_exists(conffiles_file_name
)) {
259 free(conffiles_file_name
);
263 conffiles_file
= fopen(conffiles_file_name
, "r");
264 if (conffiles_file
== NULL
) {
265 fprintf(stderr
, "%s: failed to open %s: %s\n",
266 __FUNCTION__
, conffiles_file_name
, strerror(errno
));
267 free(conffiles_file_name
);
270 free(conffiles_file_name
);
274 char *cf_name_in_dest
;
276 cf_name
= file_read_line_alloc(conffiles_file
);
277 if (cf_name
== NULL
) {
280 if (cf_name
[0] == '\0') {
284 /* Prepend dest->root_dir to conffile name.
285 Take pains to avoid multiple slashes. */
286 root_dir
= pkg
->dest
->root_dir
;
287 if (conf
->offline_root
)
288 /* skip the offline_root prefix */
289 root_dir
= pkg
->dest
->root_dir
+ strlen(conf
->offline_root
);
290 sprintf_alloc(&cf_name_in_dest
, "%s%s", root_dir
,
291 cf_name
[0] == '/' ? (cf_name
+ 1) : cf_name
);
293 /* Can't get an md5sum now, (file isn't extracted yet).
294 We'll wait until resolve_conffiles */
295 conffile_list_append(&pkg
->conffiles
, cf_name_in_dest
, NULL
);
298 free(cf_name_in_dest
);
301 fclose(conffiles_file
);
307 * Remove packages which were auto_installed due to a dependency by old_pkg,
308 * which are no longer a dependency in the new (upgraded) pkg.
311 pkg_remove_orphan_dependent(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
313 int i
, j
, k
, l
, found
;
316 struct compound_depend
*cd0
, *cd1
;
317 abstract_pkg_t
**dependents
;
319 int count0
= old_pkg
->pre_depends_count
+
320 old_pkg
->depends_count
+
321 old_pkg
->recommends_count
+
322 old_pkg
->suggests_count
;
323 int count1
= pkg
->pre_depends_count
+
325 pkg
->recommends_count
+
328 for (i
=0; i
<count0
; i
++) {
329 cd0
= &old_pkg
->depends
[i
];
330 if (cd0
->type
!= DEPEND
)
332 for (j
=0; j
<cd0
->possibility_count
; j
++) {
336 for (k
=0; k
<count1
; k
++) {
337 cd1
= &pkg
->depends
[i
];
338 if (cd1
->type
!= DEPEND
)
340 for (l
=0; l
<cd1
->possibility_count
; l
++) {
341 if (cd0
->possibilities
[j
]
342 == cd1
->possibilities
[l
]) {
355 * old_pkg has a dependency that pkg does not.
357 p
= pkg_hash_fetch_installed_by_name (&conf
->pkg_hash
,
358 cd0
->possibilities
[j
]->pkg
->name
);
363 if (!p
->auto_installed
)
366 n_deps
= pkg_has_installed_dependents(conf
, NULL
, p
,
368 n_deps
--; /* don't count old_pkg */
371 opkg_message (conf
, OPKG_NOTICE
,
372 "%s was autoinstalled and is "
373 "now orphaned, removing.\n",
376 /* p has one installed dependency (old_pkg),
377 * which we need to ignore during removal. */
378 p
->state_flag
|= SF_REPLACE
;
380 opkg_remove_pkg(conf
, p
, 0);
382 opkg_message(conf
, OPKG_INFO
,
383 "%s was autoinstalled and is "
384 "still required by %d "
385 "installed packages.\n",
394 /* returns number of installed replacees */
396 pkg_get_installed_replacees(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_vec_t
*installed_replacees
)
398 abstract_pkg_t
**replaces
= pkg
->replaces
;
399 int replaces_count
= pkg
->replaces_count
;
401 for (i
= 0; i
< replaces_count
; i
++) {
402 abstract_pkg_t
*ab_pkg
= replaces
[i
];
403 pkg_vec_t
*pkg_vec
= ab_pkg
->pkgs
;
405 for (j
= 0; j
< pkg_vec
->len
; j
++) {
406 pkg_t
*replacee
= pkg_vec
->pkgs
[j
];
407 if (!pkg_conflicts(pkg
, replacee
))
409 if (replacee
->state_status
== SS_INSTALLED
) {
410 pkg_vec_insert(installed_replacees
, replacee
);
415 return installed_replacees
->len
;
419 pkg_remove_installed_replacees(opkg_conf_t
*conf
, pkg_vec_t
*replacees
)
422 int replaces_count
= replacees
->len
;
423 for (i
= 0; i
< replaces_count
; i
++) {
424 pkg_t
*replacee
= replacees
->pkgs
[i
];
426 replacee
->state_flag
|= SF_REPLACE
; /* flag it so remove won't complain */
427 err
= opkg_remove_pkg(conf
, replacee
,0);
434 /* to unwind the removal: make sure they are installed */
436 pkg_remove_installed_replacees_unwind(opkg_conf_t
*conf
, pkg_vec_t
*replacees
)
439 int replaces_count
= replacees
->len
;
440 for (i
= 0; i
< replaces_count
; i
++) {
441 pkg_t
*replacee
= replacees
->pkgs
[i
];
442 if (replacee
->state_status
!= SS_INSTALLED
) {
443 opkg_message(conf
, OPKG_DEBUG2
,"Function: %s calling opkg_install_pkg \n",__FUNCTION__
);
444 err
= opkg_install_pkg(conf
, replacee
,0);
452 /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
454 opkg_install_check_downgrade(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
, int message
)
457 char message_out
[15];
458 char *old_version
= pkg_version_str_alloc(old_pkg
);
459 char *new_version
= pkg_version_str_alloc(pkg
);
460 int cmp
= pkg_compare_versions(old_pkg
, pkg
);
463 memset(message_out
,'\x0',15);
464 strncpy (message_out
,"Upgrading ",strlen("Upgrading "));
465 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
466 cmp
= -1 ; /* then we force opkg to downgrade */
467 strncpy (message_out
,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
468 /* reinstall, and some check could fail asking the "force-reinstall" option */
472 opkg_message(conf
, OPKG_NOTICE
,
473 "Not downgrading package %s on %s from %s to %s.\n",
474 old_pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
476 } else if (cmp
< 0) {
477 opkg_message(conf
, OPKG_NOTICE
,
478 "%s%s on %s from %s to %s...\n",
479 message_out
, pkg
->name
, old_pkg
->dest
->name
, old_version
, new_version
);
480 pkg
->dest
= old_pkg
->dest
;
482 } else /* cmp == 0 */ {
483 if (conf
->force_reinstall
) {
484 opkg_message(conf
, OPKG_NOTICE
,
485 "Reinstalling %s (%s) on %s...\n",
486 pkg
->name
, new_version
, old_pkg
->dest
->name
);
487 pkg
->dest
= old_pkg
->dest
;
490 opkg_message(conf
, OPKG_NOTICE
,
491 "Not installing %s (%s) on %s -- already installed.\n",
492 pkg
->name
, new_version
, old_pkg
->dest
->name
);
500 char message_out
[15] ;
501 memset(message_out
,'\x0',15);
503 strncpy( message_out
,"Upgrading ",strlen("Upgrading ") );
505 strncpy( message_out
,"Installing ",strlen("Installing ") );
506 char *version
= pkg_version_str_alloc(pkg
);
508 opkg_message(conf
, OPKG_NOTICE
,
509 "%s%s (%s) to %s...\n", message_out
,
510 pkg
->name
, version
, pkg
->dest
->name
);
518 prerm_upgrade_old_pkg(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
520 /* DPKG_INCOMPATIBILITY:
521 dpkg does some things here that we don't do yet. Do we care?
523 1. If a version of the package is already installed, call
524 old-prerm upgrade new-version
525 2. If the script runs but exits with a non-zero exit status
526 new-prerm failed-upgrade old-version
527 Error unwind, for both the above cases:
528 old-postinst abort-upgrade new-version
534 prerm_upgrade_old_pkg_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
536 /* DPKG_INCOMPATIBILITY:
537 dpkg does some things here that we don't do yet. Do we care?
538 (See prerm_upgrade_old_package for details)
544 prerm_deconfigure_conflictors(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_vec_t
*conflictors
)
546 /* DPKG_INCOMPATIBILITY:
547 dpkg does some things here that we don't do yet. Do we care?
548 2. If a 'conflicting' package is being removed at the same time:
549 1. If any packages depended on that conflicting package and
550 --auto-deconfigure is specified, call, for each such package:
551 deconfigured's-prerm deconfigure \
552 in-favour package-being-installed version \
553 removing conflicting-package version
555 deconfigured's-postinst abort-deconfigure \
556 in-favour package-being-installed-but-failed version \
557 removing conflicting-package version
559 The deconfigured packages are marked as requiring
560 configuration, so that if --install is used they will be
561 configured again if possible.
562 2. To prepare for removal of the conflicting package, call:
563 conflictor's-prerm remove in-favour package new-version
565 conflictor's-postinst abort-remove in-favour package new-version
571 prerm_deconfigure_conflictors_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_vec_t
*conflictors
)
573 /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
574 do yet. Do we care? (See prerm_deconfigure_conflictors for
580 preinst_configure(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
586 char *old_version
= pkg_version_str_alloc(old_pkg
);
587 sprintf_alloc(&preinst_args
, "upgrade %s", old_version
);
589 } else if (pkg
->state_status
== SS_CONFIG_FILES
) {
590 char *pkg_version
= pkg_version_str_alloc(pkg
);
591 sprintf_alloc(&preinst_args
, "install %s", pkg_version
);
594 preinst_args
= xstrdup("install");
597 err
= pkg_run_script(conf
, pkg
, "preinst", preinst_args
);
599 opkg_message(conf
, OPKG_ERROR
,
600 "Aborting installation of %s\n", pkg
->name
);
610 preinst_configure_unwind(opkg_conf_t
*conf
, 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(opkg_conf_t
*conf
, const char *file_name
)
638 backup
= backup_filename_alloc(file_name
);
639 err
= file_copy(file_name
, backup
);
641 opkg_message(conf
, OPKG_ERROR
,
642 "%s: Failed to copy %s to %s\n",
643 __FUNCTION__
, file_name
, backup
);
652 backup_exists_for(const char *file_name
)
657 backup
= backup_filename_alloc(file_name
);
659 ret
= file_exists(backup
);
667 backup_remove(const char *file_name
)
671 backup
= backup_filename_alloc(file_name
);
679 backup_modified_conffiles(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
682 conffile_list_elt_t
*iter
;
685 if (conf
->noaction
) return 0;
687 /* Backup all modified conffiles */
689 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
693 cf_name
= root_filename_alloc(conf
, cf
->name
);
695 /* Don't worry if the conffile is just plain gone */
696 if (file_exists(cf_name
) && conffile_has_been_modified(conf
, cf
)) {
697 err
= backup_make_backup(conf
, cf_name
);
706 /* Backup all conffiles that were not conffiles in old_pkg */
707 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
709 cf
= (conffile_t
*)iter
->data
;
710 cf_name
= root_filename_alloc(conf
, cf
->name
);
711 /* Ignore if this was a conffile in old_pkg as well */
712 if (pkg_get_conffile(old_pkg
, cf
->name
)) {
716 if (file_exists(cf_name
) && (! backup_exists_for(cf_name
))) {
717 err
= backup_make_backup(conf
, cf_name
);
729 backup_modified_conffiles_unwind(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
731 conffile_list_elt_t
*iter
;
734 for (iter
= nv_pair_list_first(&old_pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&old_pkg
->conffiles
, iter
)) {
735 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
739 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
740 backup_remove(((nv_pair_t
*)iter
->data
)->name
);
748 check_data_file_clashes(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
750 /* DPKG_INCOMPATIBILITY:
751 opkg takes a slightly different approach than dpkg at this
752 point. dpkg installs each file in the new package while
753 creating a backup for any file that is replaced, (so that it
754 can unwind if necessary). To avoid complexity and redundant
755 storage, opkg doesn't do any installation until later, (at the
756 point at which dpkg removes the backups.
758 But, we do have to check for data file clashes, since after
759 installing a package with a file clash, removing either of the
760 packages involved in the clash has the potential to break the
763 str_list_t
*files_list
;
764 str_list_elt_t
*iter
, *niter
;
768 files_list
= pkg_get_installed_files(conf
, pkg
);
769 if (files_list
== NULL
)
772 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
774 iter
= niter
, niter
= str_list_next(files_list
, iter
)) {
776 char *filename
= (char *) iter
->data
;
777 root_filename
= root_filename_alloc(conf
, filename
);
778 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
781 /* Pre-existing conffiles are OK */
782 /* @@@@ should have way to check that it is a conffile -Jamey */
783 if (backup_exists_for(root_filename
)) {
787 /* Pre-existing files are OK if force-overwrite was asserted. */
788 if (conf
->force_overwrite
) {
789 /* but we need to change who owns this file */
790 file_hash_set_file_owner(conf
, filename
, pkg
);
794 owner
= file_hash_get_file_owner(conf
, filename
);
796 /* Pre-existing files are OK if owned by the pkg being upgraded. */
797 if (owner
&& old_pkg
) {
798 if (strcmp(owner
->name
, old_pkg
->name
) == 0) {
803 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
805 opkg_message(conf
, OPKG_DEBUG2
, "Checking for replaces for %s in package %s\n", filename
, owner
->name
);
806 if (pkg_replaces(pkg
, owner
)) {
809 /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
810 then it's ok to overwrite. */
811 if (strcmp(owner
->name
,pkg
->name
)==0){
812 opkg_message(conf
, OPKG_INFO
, "Replacing pre-existing file %s owned by package %s\n", filename
, owner
->name
);
817 /* Pre-existing files are OK if they are obsolete */
818 obs
= hash_table_get(&conf
->obs_file_hash
, filename
);
820 opkg_message(conf
, OPKG_INFO
, "Pre-exiting file %s is obsolete. obs_pkg=%s\n", filename
, obs
->name
);
824 /* We have found a clash. */
825 opkg_message(conf
, OPKG_ERROR
,
826 "Package %s wants to install file %s\n"
827 "\tBut that file is already provided by package ",
828 pkg
->name
, filename
);
830 opkg_message(conf
, OPKG_ERROR
,
831 "%s\n", owner
->name
);
833 opkg_message(conf
, OPKG_ERROR
,
834 "<no package>\nPlease move this file out of the way and try again.\n");
840 pkg_free_installed_files(pkg
);
846 * XXX: This function sucks, as does the below comment.
849 check_data_file_clashes_change(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
851 /* Basically that's the worst hack I could do to be able to change ownership of
852 file list, but, being that we have no way to unwind the mods, due to structure
853 of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
854 What we do here is change the ownership of file in hash if a replace ( or similar events
856 Only the action that are needed to change name should be considered.
857 @@@ To change after 1.0 release.
859 str_list_t
*files_list
;
860 str_list_elt_t
*iter
, *niter
;
862 char *root_filename
= NULL
;
864 files_list
= pkg_get_installed_files(conf
, pkg
);
865 if (files_list
== NULL
)
868 for (iter
= str_list_first(files_list
), niter
= str_list_next(files_list
, iter
);
870 iter
= niter
, niter
= str_list_next(files_list
, niter
)) {
871 char *filename
= (char *) iter
->data
;
874 root_filename
= NULL
;
876 root_filename
= root_filename_alloc(conf
, filename
);
877 if (file_exists(root_filename
) && (! file_is_dir(root_filename
))) {
880 owner
= file_hash_get_file_owner(conf
, filename
);
882 if (conf
->force_overwrite
) {
883 /* but we need to change who owns this file */
884 file_hash_set_file_owner(conf
, filename
, pkg
);
889 /* Pre-existing files are OK if owned by a package replaced by new pkg. */
891 if (pkg_replaces(pkg
, owner
)) {
892 /* It's now time to change the owner of that file.
893 It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
894 opkg_message(conf
, OPKG_INFO
, "Replacing pre-existing file %s owned by package %s\n", filename
, owner
->name
);
895 file_hash_set_file_owner(conf
, filename
, pkg
);
904 root_filename
= NULL
;
906 pkg_free_installed_files(pkg
);
912 check_data_file_clashes_unwind(opkg_conf_t
*conf
, 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(opkg_conf_t
*conf
, 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(opkg_conf_t
*conf
, 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(opkg_conf_t
*conf
, 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(conf
, old_pkg
);
952 if (old_files
== NULL
)
955 new_files
= pkg_get_installed_files(conf
, 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(conf
, old
);
980 if (owner
!= old_pkg
) {
981 /* in case obsolete file no longer belongs to old_pkg */
985 /* old file is obsolete */
986 opkg_message(conf
, OPKG_INFO
,
987 " removing obsolete file %s\n", old
);
988 if (!conf
->noaction
) {
991 opkg_message(conf
, OPKG_ERROR
, " Warning: remove %s failed: %s\n", old
,
997 hash_table_deinit(&new_files_table
);
998 pkg_free_installed_files(old_pkg
);
999 pkg_free_installed_files(pkg
);
1005 install_maintainer_scripts(opkg_conf_t
*conf
, pkg_t
*pkg
, pkg_t
*old_pkg
)
1010 sprintf_alloc(&prefix
, "%s.", pkg
->name
);
1011 ret
= pkg_extract_control_files_to_dir_with_prefix(pkg
,
1012 pkg
->dest
->info_dir
,
1019 remove_disappeared(opkg_conf_t
*conf
, pkg_t
*pkg
)
1021 /* DPKG_INCOMPATIBILITY:
1022 This is a fairly sophisticated dpkg operation. Shall we
1025 /* Any packages all of whose files have been overwritten during the
1026 installation, and which aren't required for dependencies, are
1027 considered to have been removed. For each such package
1028 1. disappearer's-postrm disappear overwriter overwriter-version
1029 2. The package's maintainer scripts are removed
1030 3. It is noted in the status database as being in a sane state,
1031 namely not installed (any conffiles it may have are ignored,
1032 rather than being removed by dpkg). Note that disappearing
1033 packages do not have their prerm called, because dpkg doesn't
1034 know in advance that the package is going to vanish.
1040 install_data_files(opkg_conf_t
*conf
, pkg_t
*pkg
)
1044 /* opkg takes a slightly different approach to data file backups
1045 than dpkg. Rather than removing backups at this point, we
1046 actually do the data file installation now. See comments in
1047 check_data_file_clashes() for more details. */
1049 opkg_message(conf
, OPKG_INFO
,
1050 " extracting data files to %s\n", pkg
->dest
->root_dir
);
1051 err
= pkg_extract_data_files_to_dir(pkg
, pkg
->dest
->root_dir
);
1056 /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
1057 so we can't save ourself from removing important packages
1058 At this point we (should) have extracted the .control file, so it
1059 would be a good idea to reload the data in it, and set the Essential
1060 state in *pkg. From now on the Essential is back in status file and
1061 we can protect again.
1062 We should operate this way:
1063 fopen the file ( pkg->dest->root_dir/pkg->name.control )
1064 check for "Essential" in it
1065 set the value in pkg->essential.
1066 This new routine could be useful also for every other flag
1068 set_flags_from_control(conf
, pkg
) ;
1070 opkg_message(conf
, OPKG_DEBUG
, " Calling pkg_write_filelist from %s\n", __FUNCTION__
);
1071 err
= pkg_write_filelist(conf
, pkg
);
1075 /* XXX: FEATURE: opkg should identify any files which existed
1076 before installation and which were overwritten, (see
1077 check_data_file_clashes()). What it must do is remove any such
1078 files from the filelist of the old package which provided the
1079 file. Otherwise, if the old package were removed at some point
1080 it would break the new package. Removing the new package will
1081 also break the old one, but this cannot be helped since the old
1082 package's file has already been deleted. This is the importance
1083 of check_data_file_clashes(), and only allowing opkg to install
1084 a clashing package with a user force. */
1090 user_prefers_old_conffile(const char *file_name
, const char *backup
)
1093 const char *short_file_name
;
1095 short_file_name
= strrchr(file_name
, '/');
1096 if (short_file_name
) {
1099 short_file_name
= file_name
;
1103 response
= get_user_response(" Configuration file '%s'\n"
1104 " ==> File on system created by you or by a script.\n"
1105 " ==> File also in package provided by package maintainer.\n"
1106 " What would you like to do about it ? Your options are:\n"
1107 " Y or I : install the package maintainer's version\n"
1108 " N or O : keep your currently-installed version\n"
1109 " D : show the differences between the versions (if diff is installed)\n"
1110 " The default action is to keep your current version.\n"
1111 " *** %s (Y/I/N/O/D) [default=N] ? ", file_name
, short_file_name
);
1113 if (response
== NULL
)
1116 if (strcmp(response
, "y") == 0
1117 || strcmp(response
, "i") == 0
1118 || strcmp(response
, "yes") == 0) {
1123 if (strcmp(response
, "d") == 0) {
1124 const char *argv
[] = {"diff", "-u", backup
, file_name
, NULL
};
1126 printf(" [Press ENTER to continue]\n");
1127 response
= file_read_line_alloc(stdin
);
1138 resolve_conffiles(opkg_conf_t
*conf
, pkg_t
*pkg
)
1140 conffile_list_elt_t
*iter
;
1145 if (conf
->noaction
) return 0;
1147 for (iter
= nv_pair_list_first(&pkg
->conffiles
); iter
; iter
= nv_pair_list_next(&pkg
->conffiles
, iter
)) {
1148 char *root_filename
;
1149 cf
= (conffile_t
*)iter
->data
;
1150 root_filename
= root_filename_alloc(conf
, cf
->name
);
1152 /* Might need to initialize the md5sum for each conffile */
1153 if (cf
->value
== NULL
) {
1154 cf
->value
= file_md5sum_alloc(root_filename
);
1157 if (!file_exists(root_filename
)) {
1158 free(root_filename
);
1162 cf_backup
= backup_filename_alloc(root_filename
);
1165 if (file_exists(cf_backup
)) {
1166 /* Let's compute md5 to test if files are changed */
1167 md5sum
= file_md5sum_alloc(cf_backup
);
1168 if (md5sum
&& cf
->value
&& strcmp(cf
->value
,md5sum
) != 0 ) {
1169 if (conf
->force_maintainer
) {
1170 opkg_message(conf
, OPKG_NOTICE
, "Conffile %s using maintainer's setting.\n", cf_backup
);
1171 } else if (conf
->force_defaults
1172 || user_prefers_old_conffile(root_filename
, cf_backup
) ) {
1173 rename(cf_backup
, root_filename
);
1182 free(root_filename
);
1190 opkg_install_by_name(opkg_conf_t
*conf
, const char *pkg_name
)
1194 char *old_version
, *new_version
;
1196 old
= pkg_hash_fetch_installed_by_name(&conf
->pkg_hash
, pkg_name
);
1198 opkg_message(conf
, OPKG_DEBUG2
, "Old versions from pkg_hash_fetch %s \n", old
->version
);
1200 new = pkg_hash_fetch_best_installation_candidate_by_name(conf
, pkg_name
, &err
);
1201 if (new == NULL
|| err
)
1204 opkg_message(conf
, OPKG_DEBUG2
, "Versions from pkg_hash_fetch in %s ", __FUNCTION__
);
1206 opkg_message(conf
, OPKG_DEBUG2
, " old %s ", old
->version
);
1207 opkg_message(conf
, OPKG_DEBUG2
, " new %s\n", new->version
);
1209 new->state_flag
|= SF_USER
;
1211 old_version
= pkg_version_str_alloc(old
);
1212 new_version
= pkg_version_str_alloc(new);
1214 cmp
= pkg_compare_versions(old
, new);
1215 if ( (conf
->force_downgrade
==1) && (cmp
> 0) ){ /* We've been asked to allow downgrade and version is precedent */
1216 opkg_message(conf
, OPKG_DEBUG
, " Forcing downgrade \n");
1217 cmp
= -1 ; /* then we force opkg to downgrade */
1218 /* We need to use a value < 0 because in the 0 case we are asking to */
1219 /* reinstall, and some check could fail asking the "force-reinstall" option */
1221 opkg_message(conf
, OPKG_DEBUG
,
1222 "Comparing visible versions of pkg %s:"
1223 "\n\t%s is installed "
1224 "\n\t%s is available "
1225 "\n\t%d was comparison result\n",
1226 pkg_name
, old_version
, new_version
, cmp
);
1227 if (cmp
== 0 && !conf
->force_reinstall
) {
1228 opkg_message(conf
, OPKG_NOTICE
,
1229 "Package %s (%s) installed in %s is up to date.\n",
1230 old
->name
, old_version
, old
->dest
->name
);
1234 } else if (cmp
> 0) {
1235 opkg_message(conf
, OPKG_NOTICE
,
1236 "Not downgrading package %s on %s from %s to %s.\n",
1237 old
->name
, old
->dest
->name
, old_version
, new_version
);
1241 } else if (cmp
< 0) {
1242 new->dest
= old
->dest
;
1243 old
->state_want
= SW_DEINSTALL
; /* Here probably the problem for bug 1277 */
1249 opkg_message(conf
, OPKG_DEBUG2
,"%s: calling opkg_install_pkg \n",__FUNCTION__
);
1250 return opkg_install_pkg(conf
, new,0);
1254 * @brief Really install a pkg_t
1257 opkg_install_pkg(opkg_conf_t
*conf
, pkg_t
*pkg
, int from_upgrade
)
1261 pkg_t
*old_pkg
= NULL
;
1262 pkg_vec_t
*replacees
;
1263 abstract_pkg_t
*ab_pkg
= NULL
;
1269 sigset_t newset
, oldset
;
1272 message
= 1; /* Coming from an upgrade, and should change the output message */
1275 opkg_message(conf
, OPKG_ERROR
,
1276 "INTERNAL ERROR: null pkg passed to opkg_install_pkg\n");
1280 opkg_message(conf
, OPKG_DEBUG2
, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__
, __FUNCTION__
);
1282 if (!pkg_arch_supported(conf
, pkg
)) {
1283 opkg_message(conf
, OPKG_ERROR
, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
1284 pkg
->architecture
, pkg
->name
);
1287 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0 && conf
->nodeps
== 0) {
1288 err
= satisfy_dependencies_for(conf
, pkg
);
1292 opkg_message(conf
, OPKG_NOTICE
,
1293 "Package %s is already installed in %s.\n",
1294 pkg
->name
, pkg
->dest
->name
);
1298 if (pkg
->dest
== NULL
) {
1299 pkg
->dest
= conf
->default_dest
;
1302 old_pkg
= pkg_hash_fetch_installed_by_name(&conf
->pkg_hash
, pkg
->name
);
1304 err
= opkg_install_check_downgrade(conf
, pkg
, old_pkg
, message
);
1308 pkg
->state_want
= SW_INSTALL
;
1310 old_pkg
->state_want
= SW_DEINSTALL
; /* needed for check_data_file_clashes of dependences */
1313 err
= check_conflicts_for(conf
, pkg
);
1317 /* this setup is to remove the upgrade scenario in the end when
1318 installing pkg A, A deps B & B deps on A. So both B and A are
1319 installed. Then A's installation is started resulting in an
1320 uncecessary upgrade */
1321 if (pkg
->state_status
== SS_INSTALLED
&& conf
->force_reinstall
== 0)
1324 err
= verify_pkg_installable(conf
, pkg
);
1328 if (pkg
->local_filename
== NULL
) {
1329 err
= opkg_download_pkg(conf
, pkg
, conf
->tmp_dir
);
1331 opkg_message(conf
, OPKG_ERROR
,
1332 "Failed to download %s. Perhaps you need to run 'opkg update'?\n",
1338 /* check that the repository is valid */
1339 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
1340 char *list_file_name
, *sig_file_name
, *lists_dir
;
1342 /* check to ensure the package has come from a repository */
1343 if (conf
->check_signature
&& pkg
->src
)
1345 sprintf_alloc (&lists_dir
, "%s",
1346 (conf
->restrict_to_default_dest
)
1347 ? conf
->default_dest
->lists_dir
1349 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, pkg
->src
->name
);
1350 sprintf_alloc (&sig_file_name
, "%s/%s.sig", lists_dir
, pkg
->src
->name
);
1352 if (file_exists (sig_file_name
))
1354 if (opkg_verify_file (conf
, list_file_name
, sig_file_name
)){
1355 opkg_message(conf
, OPKG_ERROR
, "Failed to verify the signature of: %s\n",
1360 opkg_message(conf
, OPKG_ERROR
, "Signature file is missing. "
1361 "Perhaps you need to run 'opkg update'?\n");
1366 free (list_file_name
);
1367 free (sig_file_name
);
1371 /* Check for md5 values */
1374 file_md5
= file_md5sum_alloc(pkg
->local_filename
);
1375 if (file_md5
&& strcmp(file_md5
, pkg
->md5sum
))
1377 opkg_message(conf
, OPKG_ERROR
,
1378 "Package %s md5sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1388 /* Check for sha256 value */
1391 file_sha256
= file_sha256sum_alloc(pkg
->local_filename
);
1392 if (file_sha256
&& strcmp(file_sha256
, pkg
->sha256sum
))
1394 opkg_message(conf
, OPKG_ERROR
,
1395 "Package %s sha256sum mismatch. Either the opkg or the package index are corrupt. Try 'opkg update'.\n",
1405 if (pkg
->tmp_unpack_dir
== NULL
) {
1406 if (unpack_pkg_control_files(conf
, pkg
) == -1) {
1407 opkg_message(conf
, OPKG_ERROR
, "Failed to unpack control"
1408 " files from %s.\n", pkg
->local_filename
);
1413 /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
1414 /* Pigi: check if it will pass from here when replacing. It seems to fail */
1415 /* That's rather strange that files don't change owner. Investigate !!!!!!*/
1416 err
= update_file_ownership(conf
, pkg
, old_pkg
);
1420 if (conf
->nodeps
== 0) {
1421 err
= satisfy_dependencies_for(conf
, pkg
);
1424 if (pkg
->state_status
== SS_UNPACKED
)
1425 /* Circular dependency has installed it for us. */
1429 replacees
= pkg_vec_alloc();
1430 pkg_get_installed_replacees(conf
, pkg
, replacees
);
1432 /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
1434 sigemptyset(&newset
);
1435 sigaddset(&newset
, SIGINT
);
1436 sigprocmask(SIG_BLOCK
, &newset
, &oldset
);
1438 opkg_state_changed
++;
1439 pkg
->state_flag
|= SF_FILELIST_CHANGED
;
1442 pkg_remove_orphan_dependent(conf
, pkg
, old_pkg
);
1444 /* XXX: BUG: we really should treat replacement more like an upgrade
1445 * Instead, we're going to remove the replacees
1447 err
= pkg_remove_installed_replacees(conf
, replacees
);
1449 goto UNWIND_REMOVE_INSTALLED_REPLACEES
;
1451 err
= prerm_upgrade_old_pkg(conf
, pkg
, old_pkg
);
1453 goto UNWIND_PRERM_UPGRADE_OLD_PKG
;
1455 err
= prerm_deconfigure_conflictors(conf
, pkg
, replacees
);
1457 goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS
;
1459 err
= preinst_configure(conf
, pkg
, old_pkg
);
1461 goto UNWIND_PREINST_CONFIGURE
;
1463 err
= backup_modified_conffiles(conf
, pkg
, old_pkg
);
1465 goto UNWIND_BACKUP_MODIFIED_CONFFILES
;
1467 err
= check_data_file_clashes(conf
, pkg
, old_pkg
);
1469 goto UNWIND_CHECK_DATA_FILE_CLASHES
;
1471 err
= postrm_upgrade_old_pkg(conf
, pkg
, old_pkg
);
1473 goto UNWIND_POSTRM_UPGRADE_OLD_PKG
;
1478 /* point of no return: no unwinding after this */
1479 if (old_pkg
&& !conf
->force_reinstall
) {
1480 old_pkg
->state_want
= SW_DEINSTALL
;
1482 if (old_pkg
->state_flag
& SF_NOPRUNE
) {
1483 opkg_message(conf
, OPKG_INFO
,
1484 " not removing obsolesced files because package marked noprune\n");
1486 opkg_message(conf
, OPKG_INFO
,
1487 " removing obsolesced files\n");
1488 if (remove_obsolesced_files(conf
, pkg
, old_pkg
)) {
1489 opkg_message(conf
, OPKG_ERROR
, "Failed to determine "
1490 "obsolete files from previously "
1491 "installed %s\n", old_pkg
->name
);
1495 /* removing files from old package, to avoid ghost files */
1496 remove_data_files_and_list(conf
, old_pkg
);
1497 remove_maintainer_scripts(conf
, old_pkg
);
1501 opkg_message(conf
, OPKG_INFO
,
1502 " installing maintainer scripts\n");
1503 if (install_maintainer_scripts(conf
, pkg
, old_pkg
)) {
1504 opkg_message(conf
, OPKG_ERROR
, "Failed to extract maintainer"
1505 " scripts for %s. Package debris may remain!\n",
1510 /* the following just returns 0 */
1511 remove_disappeared(conf
, pkg
);
1513 opkg_message(conf
, OPKG_INFO
,
1514 " installing data files\n");
1516 if (install_data_files(conf
, pkg
)) {
1517 opkg_message(conf
, OPKG_ERROR
, "Failed to extract data files "
1518 "for %s. Package debris may remain!\n",
1523 err
= check_data_file_clashes_change(conf
, pkg
, old_pkg
);
1525 opkg_message(conf
, OPKG_ERROR
,
1526 "check_data_file_clashes_change() failed for "
1527 "for files belonging to %s.\n",
1531 opkg_message(conf
, OPKG_INFO
,
1532 " resolving conf files\n");
1533 resolve_conffiles(conf
, pkg
);
1535 pkg
->state_status
= SS_UNPACKED
;
1536 old_state_flag
= pkg
->state_flag
;
1537 pkg
->state_flag
&= ~SF_PREFER
;
1538 opkg_message(conf
, OPKG_DEBUG
, " pkg=%s old_state_flag=%x state_flag=%x\n", pkg
->name
, old_state_flag
, pkg
->state_flag
);
1540 if (old_pkg
&& !conf
->force_reinstall
) {
1541 old_pkg
->state_status
= SS_NOT_INSTALLED
;
1544 time(&pkg
->installed_time
);
1546 ab_pkg
= pkg
->parent
;
1548 ab_pkg
->state_status
= pkg
->state_status
;
1550 opkg_message(conf
, OPKG_INFO
, "Done.\n");
1552 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1553 pkg_vec_free (replacees
);
1557 UNWIND_POSTRM_UPGRADE_OLD_PKG
:
1558 postrm_upgrade_old_pkg_unwind(conf
, pkg
, old_pkg
);
1559 UNWIND_CHECK_DATA_FILE_CLASHES
:
1560 check_data_file_clashes_unwind(conf
, pkg
, old_pkg
);
1561 UNWIND_BACKUP_MODIFIED_CONFFILES
:
1562 backup_modified_conffiles_unwind(conf
, pkg
, old_pkg
);
1563 UNWIND_PREINST_CONFIGURE
:
1564 preinst_configure_unwind(conf
, pkg
, old_pkg
);
1565 UNWIND_PRERM_DECONFIGURE_CONFLICTORS
:
1566 prerm_deconfigure_conflictors_unwind(conf
, pkg
, replacees
);
1567 UNWIND_PRERM_UPGRADE_OLD_PKG
:
1568 prerm_upgrade_old_pkg_unwind(conf
, pkg
, old_pkg
);
1569 UNWIND_REMOVE_INSTALLED_REPLACEES
:
1570 pkg_remove_installed_replacees_unwind(conf
, replacees
);
1573 opkg_message(conf
, OPKG_INFO
,
1576 sigprocmask(SIG_UNBLOCK
, &newset
, &oldset
);
1578 pkg_vec_free (replacees
);