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.
19 #include "opkg_message.h"
23 #include "opkg_remove.h"
24 #include "opkg_error.h"
27 #include "file_util.h"
28 #include "sprintf_alloc.h"
30 #include "libbb/libbb.h"
33 * Returns number of the number of packages depending on the packages provided by this package.
34 * Every package implicitly provides itself.
37 pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
39 int nprovides = pkg->provides_count;
40 abstract_pkg_t **provides = pkg->provides;
41 int n_installed_dependents = 0;
43 for (i = 0; i < nprovides; i++) {
44 abstract_pkg_t *providee = provides[i];
45 abstract_pkg_t **dependers = providee->depended_upon_by;
46 abstract_pkg_t *dep_ab_pkg;
47 if (dependers == NULL)
49 while ((dep_ab_pkg = *dependers++) != NULL) {
50 if (dep_ab_pkg->state_status == SS_INSTALLED){
51 n_installed_dependents++;
56 /* if caller requested the set of installed dependents */
59 abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *));
61 *pdependents = dependents;
62 for (i = 0; i < nprovides; i++) {
63 abstract_pkg_t *providee = provides[i];
64 abstract_pkg_t **dependers = providee->depended_upon_by;
65 abstract_pkg_t *dep_ab_pkg;
66 if (dependers == NULL)
68 while ((dep_ab_pkg = *dependers++) != NULL) {
69 if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
70 dependents[p++] = dep_ab_pkg;
71 dep_ab_pkg->state_flag |= SF_MARKED;
76 /* now clear the marks */
77 for (i = 0; i < p; i++) {
78 abstract_pkg_t *dep_ab_pkg = dependents[i];
79 dep_ab_pkg->state_flag &= ~SF_MARKED;
82 return n_installed_dependents;
86 opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
91 pkg_vec_t *dependent_pkgs;
92 abstract_pkg_t * ab_pkg;
94 if((ab_pkg = pkg->parent) == NULL){
95 fprintf(stderr, "%s: unable to get dependent pkgs. pkg %s isn't in hash table\n",
96 __FUNCTION__, pkg->name);
100 if (dependents == NULL)
103 // here i am using the dependencies_checked
104 if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
105 return 0; // has already been encountered in the process
106 // of marking packages for removal - Karthik
107 ab_pkg->dependencies_checked = 2;
111 dependent_pkgs = pkg_vec_alloc();
113 while (dependents [i] != NULL) {
114 abstract_pkg_t *dep_ab_pkg = dependents[i];
116 if (dep_ab_pkg->dependencies_checked == 2){
120 if (dep_ab_pkg->state_status == SS_INSTALLED) {
121 for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
122 pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
123 if (dep_pkg->state_status == SS_INSTALLED) {
124 pkg_vec_insert(dependent_pkgs, dep_pkg);
130 /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
131 * 2 - to keep track of pkgs whose deps have been checked alrdy - Karthik */
135 free(dependent_pkgs);
141 for (i = 0; i < dependent_pkgs->len; i++) {
142 err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
146 free(dependent_pkgs);
151 print_dependents_warning(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
153 abstract_pkg_t *dep_ab_pkg;
154 opkg_message(conf, OPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
155 while ((dep_ab_pkg = *dependents++) != NULL) {
156 if (dep_ab_pkg->state_status == SS_INSTALLED)
157 opkg_message(conf, OPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
159 opkg_message(conf, OPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
160 opkg_message(conf, OPKG_ERROR, "");
161 opkg_message(conf, OPKG_ERROR, "You can force removal of this package with --force-depends.\n");
162 opkg_message(conf, OPKG_ERROR, "You can force removal of this package and its dependents\n");
163 opkg_message(conf, OPKG_ERROR, "with --force-removal-of-dependent-packages\n");
167 * Find and remove packages that were autoinstalled and are orphaned
168 * by the removal of pkg.
171 remove_autoinstalled(opkg_conf_t *conf, pkg_t *pkg)
176 struct compound_depend *cdep;
177 abstract_pkg_t **dependents;
179 int count = pkg->pre_depends_count +
181 pkg->recommends_count +
184 for (i=0; i<count; i++) {
185 cdep = &pkg->depends[i];
186 if (cdep->type != DEPEND)
188 for (j=0; j<cdep->possibility_count; j++) {
189 p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash,
190 cdep->possibilities[j]->pkg->name);
192 /* If the package is not installed, this could have
193 * been a circular dependency and the package has
194 * already been removed.
199 if (!p->auto_installed)
202 n_deps = pkg_has_installed_dependents(conf, NULL, p,
205 opkg_message(conf, OPKG_NOTICE,
206 "%s was autoinstalled and is "
207 "now orphaned, removing\n",
209 opkg_remove_pkg(conf, p, 0);
211 opkg_message(conf, OPKG_INFO,
212 "%s was autoinstalled and is "
213 "still required by %d "
214 "installed packages.\n",
226 opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
229 abstract_pkg_t *parent_pkg = NULL;
232 * If called from an upgrade and not from a normal remove,
233 * ignore the essential flag.
235 if (pkg->essential && !from_upgrade) {
236 if (conf->force_removal_of_essential_packages) {
237 fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
238 "\tIf your system breaks, you get to keep both pieces\n",
241 fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
242 "\tRemoving an essential package may lead to an unusable system, but if\n"
243 "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
244 "\tits will with the option: --force-removal-of-essential-packages\n",
246 return OPKG_PKG_IS_ESSENTIAL;
250 if ((parent_pkg = pkg->parent) == NULL)
253 /* only attempt to remove dependent installed packages if
254 * force_depends is not specified or the package is being
257 if (!conf->force_depends
258 && !(pkg->state_flag & SF_REPLACE)) {
259 abstract_pkg_t **dependents;
260 int has_installed_dependents =
261 pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
263 if (has_installed_dependents) {
265 * if this package is depended upon by others, then either we should
266 * not remove it or we should remove it and all of its dependents
269 if (!conf->force_removal_of_dependent_packages) {
270 print_dependents_warning(conf, parent_pkg, pkg, dependents);
272 return OPKG_PKG_HAS_DEPENDENTS;
275 /* remove packages depending on this package - Karthik */
276 err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
286 if (from_upgrade == 0) {
287 opkg_message (conf, OPKG_NOTICE,
288 "Removing package %s from %s...\n", pkg->name, pkg->dest->name);
290 pkg->state_flag |= SF_FILELIST_CHANGED;
292 pkg->state_want = SW_DEINSTALL;
293 opkg_state_changed++;
295 pkg_run_script(conf, pkg, "prerm", "remove");
297 /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
298 maintains an empty filelist rather than deleting it. That seems
299 like a big pain, and I don't see that that should make a big
300 difference, but for anyone who wants tighter compatibility,
301 feel free to fix this. */
302 remove_data_files_and_list(conf, pkg);
304 pkg_run_script(conf, pkg, "postrm", "remove");
306 remove_maintainer_scripts(conf, pkg);
307 pkg->state_status = SS_NOT_INSTALLED;
310 parent_pkg->state_status = SS_NOT_INSTALLED;
312 /* remove autoinstalled packages that are orphaned by the removal of this one */
313 if (conf->autoremove)
314 remove_autoinstalled (conf, pkg);
320 remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
322 str_list_t installed_dirs;
323 str_list_t *installed_files;
324 str_list_elt_t *iter;
326 conffile_t *conffile;
331 str_list_init(&installed_dirs);
332 installed_files = pkg_get_installed_files(conf, pkg);
334 /* don't include trailing slash */
335 if (conf->offline_root)
336 rootdirlen = strlen(conf->offline_root);
338 for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
339 file_name = (char *)iter->data;
341 if (file_is_dir(file_name)) {
342 str_list_append(&installed_dirs, file_name);
346 conffile = pkg_get_conffile(pkg, file_name+rootdirlen);
348 if (conffile_has_been_modified(conf, conffile)) {
349 opkg_message (conf, OPKG_NOTICE,
350 " not deleting modified conffile %s\n", file_name);
355 opkg_message(conf, OPKG_INFO, " deleting %s (noaction=%d)\n", file_name, conf->noaction);
360 /* Remove empty directories */
361 if (!conf->noaction) {
364 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
365 file_name = (char *)iter->data;
367 if (rmdir(file_name) == 0) {
368 opkg_message(conf, OPKG_INFO, " deleting %s\n", file_name);
370 str_list_remove(&installed_dirs, &iter);
373 } while (removed_a_dir);
376 pkg_free_installed_files(pkg);
377 pkg_remove_installed_files_list(conf, pkg);
379 /* Don't print warning for dirs that are provided by other packages */
380 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
381 file_name = (char *)iter->data;
383 owner = file_hash_get_file_owner(conf, file_name);
387 str_list_remove(&installed_dirs, &iter);
392 while (!void_list_empty(&installed_dirs)) {
393 iter = str_list_pop(&installed_dirs);
397 str_list_deinit(&installed_dirs);
401 remove_maintainer_scripts(opkg_conf_t *conf, pkg_t *pkg)
410 sprintf_alloc(&globpattern, "%s/%s.*",
411 pkg->dest->info_dir, pkg->name);
413 err = glob(globpattern, 0, NULL, &globbuf);
418 for (i = 0; i < globbuf.gl_pathc; i++) {
419 opkg_message(conf, OPKG_INFO, "deleting %s\n",
420 globbuf.gl_pathv[i]);
421 unlink(globbuf.gl_pathv[i]);