1 /* opkg_remove.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.
22 #include "opkg_message.h"
23 #include "opkg_remove.h"
25 #include "file_util.h"
26 #include "sprintf_alloc.h"
27 #include "libbb/libbb.h"
30 * Returns number of the number of packages depending on the packages provided by this package.
31 * Every package implicitly provides itself.
34 pkg_has_installed_dependents(pkg_t *pkg, abstract_pkg_t *** pdependents)
36 int nprovides = pkg->provides_count;
37 abstract_pkg_t **provides = pkg->provides;
38 unsigned int n_installed_dependents = 0;
40 for (i = 0; i < nprovides; i++) {
41 abstract_pkg_t *providee = provides[i];
42 abstract_pkg_t **dependers = providee->depended_upon_by;
43 abstract_pkg_t *dep_ab_pkg;
44 if (dependers == NULL)
46 while ((dep_ab_pkg = *dependers++) != NULL) {
47 if (dep_ab_pkg->state_status == SS_INSTALLED){
48 n_installed_dependents++;
53 /* if caller requested the set of installed dependents */
56 abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *));
58 *pdependents = dependents;
59 for (i = 0; i < nprovides; i++) {
60 abstract_pkg_t *providee = provides[i];
61 abstract_pkg_t **dependers = providee->depended_upon_by;
62 abstract_pkg_t *dep_ab_pkg;
63 if (dependers == NULL)
65 while ((dep_ab_pkg = *dependers++) != NULL) {
66 if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
67 dependents[p++] = dep_ab_pkg;
68 dep_ab_pkg->state_flag |= SF_MARKED;
73 /* now clear the marks */
74 for (i = 0; i < p; i++) {
75 abstract_pkg_t *dep_ab_pkg = dependents[i];
76 dep_ab_pkg->state_flag &= ~SF_MARKED;
79 return n_installed_dependents;
83 opkg_remove_dependent_pkgs(pkg_t *pkg, abstract_pkg_t **dependents)
88 pkg_vec_t *dependent_pkgs;
89 abstract_pkg_t * ab_pkg;
91 if((ab_pkg = pkg->parent) == NULL){
92 opkg_msg(ERROR, "Internal error: pkg %s isn't in hash table\n",
97 if (dependents == NULL)
100 // here i am using the dependencies_checked
101 if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
102 return 0; // has already been encountered in the process
103 // of marking packages for removal - Karthik
104 ab_pkg->dependencies_checked = 2;
108 dependent_pkgs = pkg_vec_alloc();
110 while (dependents [i] != NULL) {
111 abstract_pkg_t *dep_ab_pkg = dependents[i];
113 if (dep_ab_pkg->dependencies_checked == 2){
117 if (dep_ab_pkg->state_status == SS_INSTALLED) {
118 for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
119 pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
120 if (dep_pkg->state_status == SS_INSTALLED) {
121 pkg_vec_insert(dependent_pkgs, dep_pkg);
127 /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
128 * 2 - to keep track of pkgs whose deps have been checked alrdy - Karthik */
132 pkg_vec_free(dependent_pkgs);
138 for (i = 0; i < dependent_pkgs->len; i++) {
139 err = opkg_remove_pkg(dependent_pkgs->pkgs[i],0);
141 pkg_vec_free(dependent_pkgs);
145 pkg_vec_free(dependent_pkgs);
150 print_dependents_warning(pkg_t *pkg, abstract_pkg_t **dependents)
152 abstract_pkg_t *dep_ab_pkg;
153 opkg_msg(ERROR, "Package %s is depended upon by packages:\n", pkg->name);
154 while ((dep_ab_pkg = *dependents++) != NULL) {
155 if (dep_ab_pkg->state_status == SS_INSTALLED)
156 opkg_msg(ERROR, "\t%s\n", dep_ab_pkg->name);
158 opkg_msg(ERROR, "These might cease to work if package %s is removed.\n\n",
160 opkg_msg(ERROR, "Force removal of this package with --force-depends.\n");
161 opkg_msg(ERROR, "Force removal of this package and its dependents\n");
162 opkg_msg(ERROR, "with --force-removal-of-dependent-packages.\n");
166 * Find and remove packages that were autoinstalled and are orphaned
167 * by the removal of pkg.
170 remove_autoinstalled(pkg_t *pkg)
175 struct compound_depend *cdep;
176 abstract_pkg_t **dependents;
178 int count = pkg->pre_depends_count +
180 pkg->recommends_count +
183 for (i=0; i<count; i++) {
184 cdep = &pkg->depends[i];
185 if (cdep->type != DEPEND)
187 for (j=0; j<cdep->possibility_count; j++) {
188 p = pkg_hash_fetch_installed_by_name(
189 cdep->possibilities[j]->pkg->name);
191 /* If the package is not installed, this could have
192 * been a circular dependency and the package has
193 * already been removed.
198 if (!p->auto_installed)
201 n_deps = pkg_has_installed_dependents(p, &dependents);
203 opkg_msg(NOTICE, "%s was autoinstalled and is "
204 "now orphaned, removing.\n",
206 opkg_remove_pkg(p, 0);
208 opkg_msg(INFO, "%s was autoinstalled and is "
209 "still required by %d "
210 "installed packages.\n",
222 opkg_remove_pkg(pkg_t *pkg, int from_upgrade)
225 abstract_pkg_t *parent_pkg = NULL;
228 * If called from an upgrade and not from a normal remove,
229 * ignore the essential flag.
231 if (pkg->essential && !from_upgrade) {
232 if (conf->force_removal_of_essential_packages) {
234 "Removing essential package %s under your coercion.\n"
235 "\tIf your system breaks, you get to keep both pieces\n",
238 opkg_msg(NOTICE, "Refusing to remove essential package %s.\n"
239 "\tRemoving an essential package may lead to an unusable system, but if\n"
240 "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
241 "\tits will with the option: --force-removal-of-essential-packages\n",
247 if ((parent_pkg = pkg->parent) == NULL)
250 /* only attempt to remove dependent installed packages if
251 * force_depends is not specified or the package is being
254 if (!conf->force_depends
255 && !(pkg->state_flag & SF_REPLACE)) {
256 abstract_pkg_t **dependents;
257 int has_installed_dependents =
258 pkg_has_installed_dependents(pkg, &dependents);
260 if (has_installed_dependents) {
262 * if this package is depended upon by others, then either we should
263 * not remove it or we should remove it and all of its dependents
266 if (!conf->force_removal_of_dependent_packages) {
267 print_dependents_warning(pkg, dependents);
272 /* remove packages depending on this package - Karthik */
273 err = opkg_remove_dependent_pkgs(pkg, dependents);
283 if (from_upgrade == 0) {
284 opkg_msg(NOTICE, "Removing package %s from %s...\n",
285 pkg->name, pkg->dest->name);
287 pkg->state_flag |= SF_FILELIST_CHANGED;
289 pkg->state_want = SW_DEINSTALL;
290 opkg_state_changed++;
292 pkg_run_script(pkg, "prerm", "remove");
294 /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
295 maintains an empty filelist rather than deleting it. That seems
296 like a big pain, and I don't see that that should make a big
297 difference, but for anyone who wants tighter compatibility,
298 feel free to fix this. */
299 remove_data_files_and_list(pkg);
301 pkg_run_script(pkg, "postrm", "remove");
303 remove_maintainer_scripts(pkg);
304 pkg->state_status = SS_NOT_INSTALLED;
307 parent_pkg->state_status = SS_NOT_INSTALLED;
309 /* remove autoinstalled packages that are orphaned by the removal of this one */
310 if (conf->autoremove)
311 remove_autoinstalled(pkg);
317 remove_data_files_and_list(pkg_t *pkg)
319 str_list_t installed_dirs;
320 str_list_t *installed_files;
321 str_list_elt_t *iter;
323 conffile_t *conffile;
328 installed_files = pkg_get_installed_files(pkg);
329 if (installed_files == NULL) {
330 opkg_msg(ERROR, "Failed to determine installed "
331 "files for %s. None removed.\n", pkg->name);
335 str_list_init(&installed_dirs);
337 /* don't include trailing slash */
338 if (conf->offline_root)
339 rootdirlen = strlen(conf->offline_root);
341 for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
342 file_name = (char *)iter->data;
344 if (file_is_dir(file_name)) {
345 str_list_append(&installed_dirs, file_name);
349 conffile = pkg_get_conffile(pkg, file_name+rootdirlen);
351 if (conffile_has_been_modified(conffile)) {
352 opkg_msg(NOTICE, "Not deleting modified conffile %s.\n",
358 if (!conf->noaction) {
359 opkg_msg(INFO, "Deleting %s.\n", file_name, conf->noaction);
362 opkg_msg(INFO, "Not deleting %s. (noaction)\n",
367 /* Remove empty directories */
368 if (!conf->noaction) {
371 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
372 file_name = (char *)iter->data;
374 if (rmdir(file_name) == 0) {
375 opkg_msg(INFO, "Deleting %s.\n", file_name);
377 str_list_remove(&installed_dirs, &iter);
380 } while (removed_a_dir);
383 pkg_free_installed_files(pkg);
384 pkg_remove_installed_files_list(pkg);
386 /* Don't print warning for dirs that are provided by other packages */
387 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
388 file_name = (char *)iter->data;
390 owner = file_hash_get_file_owner(file_name);
394 str_list_remove(&installed_dirs, &iter);
399 while (!void_list_empty(&installed_dirs)) {
400 iter = str_list_pop(&installed_dirs);
404 str_list_deinit(&installed_dirs);
408 remove_maintainer_scripts(pkg_t *pkg)
417 sprintf_alloc(&globpattern, "%s/%s.*",
418 pkg->dest->info_dir, pkg->name);
420 err = glob(globpattern, 0, NULL, &globbuf);
425 for (i = 0; i < globbuf.gl_pathc; i++) {
426 opkg_msg(INFO, "Deleting %s.\n", globbuf.gl_pathv[i]);
427 unlink(globbuf.gl_pathv[i]);