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.
36 int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
38 int nprovides = pkg->provides_count;
39 abstract_pkg_t **provides = pkg->provides;
40 int n_installed_dependents = 0;
42 for (i = 0; i < nprovides; i++) {
43 abstract_pkg_t *providee = provides[i];
44 abstract_pkg_t **dependers = providee->depended_upon_by;
45 abstract_pkg_t *dep_ab_pkg;
46 if (dependers == NULL)
48 while ((dep_ab_pkg = *dependers++) != NULL) {
49 if (dep_ab_pkg->state_status == SS_INSTALLED){
50 n_installed_dependents++;
55 /* if caller requested the set of installed dependents */
58 abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *));
60 *pdependents = dependents;
61 for (i = 0; i < nprovides; i++) {
62 abstract_pkg_t *providee = provides[i];
63 abstract_pkg_t **dependers = providee->depended_upon_by;
64 abstract_pkg_t *dep_ab_pkg;
65 if (dependers == NULL)
67 while ((dep_ab_pkg = *dependers++) != NULL) {
68 if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
69 dependents[p++] = dep_ab_pkg;
70 dep_ab_pkg->state_flag |= SF_MARKED;
75 /* now clear the marks */
76 for (i = 0; i < p; i++) {
77 abstract_pkg_t *dep_ab_pkg = dependents[i];
78 dep_ab_pkg->state_flag &= ~SF_MARKED;
81 return n_installed_dependents;
84 int opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
89 pkg_vec_t *dependent_pkgs;
90 abstract_pkg_t * ab_pkg;
92 if((ab_pkg = pkg->parent) == NULL){
93 fprintf(stderr, "%s: unable to get dependent pkgs. pkg %s isn't in hash table\n",
94 __FUNCTION__, pkg->name);
98 if (dependents == NULL)
101 // here i am using the dependencies_checked
102 if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
103 return 0; // has already been encountered in the process
104 // of marking packages for removal - Karthik
105 ab_pkg->dependencies_checked = 2;
109 dependent_pkgs = pkg_vec_alloc();
111 while (dependents [i] != NULL) {
112 abstract_pkg_t *dep_ab_pkg = dependents[i];
114 if (dep_ab_pkg->dependencies_checked == 2){
118 if (dep_ab_pkg->state_status == SS_INSTALLED) {
119 for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
120 pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
121 if (dep_pkg->state_status == SS_INSTALLED) {
122 pkg_vec_insert(dependent_pkgs, dep_pkg);
128 /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
129 * 2 - to keep track of pkgs whose deps have been checked alrdy - Karthik */
133 free(dependent_pkgs);
139 for (i = 0; i < dependent_pkgs->len; i++) {
140 err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
144 free(dependent_pkgs);
148 static int user_prefers_removing_dependents(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
150 abstract_pkg_t *dep_ab_pkg;
151 opkg_message(conf, OPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
152 while ((dep_ab_pkg = *dependents++) != NULL) {
153 if (dep_ab_pkg->state_status == SS_INSTALLED)
154 opkg_message(conf, OPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
156 opkg_message(conf, OPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
157 opkg_message(conf, OPKG_ERROR, "");
158 opkg_message(conf, OPKG_ERROR, "You can force removal of this package with -force-depends.\n");
159 opkg_message(conf, OPKG_ERROR, "You can force removal of this package and its dependents\n");
160 opkg_message(conf, OPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
161 opkg_message(conf, OPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
162 opkg_message(conf, OPKG_ERROR, "in opkg.conf.\n");
166 static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg)
169 * find and remove packages that were autoinstalled and are orphaned by the removal of pkg
172 char *buffer, *d_str;
175 for (i = 0; i < pkg->depends_count; ++i)
179 d_str = pkg->depends_str[i];
180 buffer = xcalloc(1, strlen (d_str) + 1);
182 while (d_str[x] != '\0' && d_str[x] != ' ')
184 buffer[x] = d_str[x];
188 buffer = xrealloc (buffer, strlen (buffer) + 1);
189 p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash, buffer);
191 /* if the package is not installed, this could have been a circular
192 * depenancy and the package has already been removed */
196 if (p->auto_installed)
199 abstract_pkg_t **dependents;
201 deps = pkg_has_installed_dependents(conf, NULL, p, &dependents);
204 opkg_message (conf, OPKG_INFO,
205 "%s was autoinstalled but is now orphaned\n", buffer);
206 opkg_remove_pkg(conf, p,0);
209 opkg_message (conf, OPKG_INFO, "%s was autoinstalled and is still required by "
210 "%d installed packages\n", buffer, deps);
220 int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
222 /* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
223 thus I wan't check for essential, as I'm upgrading.
224 I hope it won't break anything :)
227 abstract_pkg_t *parent_pkg = NULL;
229 if (pkg->essential && !message) {
230 if (conf->force_removal_of_essential_packages) {
231 fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
232 "\tIf your system breaks, you get to keep both pieces\n",
235 fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
236 "\tRemoving an essential package may lead to an unusable system, but if\n"
237 "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
238 "\tits will with the option: -force-removal-of-essential-packages\n",
240 return OPKG_PKG_IS_ESSENTIAL;
244 if ((parent_pkg = pkg->parent) == NULL)
247 /* only attempt to remove dependent installed packages if
248 * force_depends is not specified or the package is being
251 if (!conf->force_depends
252 && !(pkg->state_flag & SF_REPLACE)) {
253 abstract_pkg_t **dependents;
254 int has_installed_dependents =
255 pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
257 if (has_installed_dependents) {
259 * if this package is depended up by others, then either we should
260 * not remove it or we should remove it and all of its dependents
263 if (!conf->force_removal_of_dependent_packages
264 && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
266 return OPKG_PKG_HAS_DEPENDENTS;
269 /* remove packages depending on this package - Karthik */
270 err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
281 opkg_message (conf, OPKG_NOTICE,
282 "Removing package %s from %s...\n", pkg->name, pkg->dest->name);
285 pkg->state_flag |= SF_FILELIST_CHANGED;
287 pkg->state_want = SW_DEINSTALL;
288 opkg_state_changed++;
290 pkg_run_script(conf, pkg, "prerm", "remove");
292 /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
293 maintains an empty filelist rather than deleting it. That seems
294 like a big pain, and I don't see that that should make a big
295 difference, but for anyone who wants tighter compatibility,
296 feel free to fix this. */
297 remove_data_files_and_list(conf, pkg);
299 pkg_run_script(conf, pkg, "postrm", "remove");
301 remove_maintainer_scripts_except_postrm(conf, pkg);
303 /* Aman Gupta - Since opkg is made for handheld devices with limited
304 * space, it doesn't make sense to leave extra configurations, files,
305 * and maintainer scripts left around. So, we make remove like purge,
306 * and take out all the crap :) */
308 remove_postrm(conf, pkg);
309 pkg->state_status = SS_NOT_INSTALLED;
312 parent_pkg->state_status = SS_NOT_INSTALLED;
315 /* remove autoinstalled packages that are orphaned by the removal of this one */
316 if (conf->autoremove)
317 remove_autoinstalled (conf, pkg);
324 int opkg_purge_pkg(opkg_conf_t *conf, pkg_t *pkg)
326 opkg_remove_pkg(conf, pkg,0);
330 int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
332 str_list_t installed_dirs;
333 str_list_t *installed_files;
334 str_list_elt_t *iter;
336 conffile_t *conffile;
341 str_list_init(&installed_dirs);
342 installed_files = pkg_get_installed_files(conf, pkg);
344 /* don't include trailing slash */
345 if (conf->offline_root)
346 rootdirlen = strlen(conf->offline_root);
348 for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
349 file_name = (char *)iter->data;
351 if (file_is_dir(file_name)) {
352 str_list_append(&installed_dirs, file_name);
356 conffile = pkg_get_conffile(pkg, file_name+rootdirlen);
358 /* XXX: QUESTION: Is this right? I figure we only need to
359 save the conffile if it has been modified. Is that what
360 dpkg does? Or does dpkg preserve all conffiles? If so,
361 this seems like a better thing to do to conserve
363 if (conffile_has_been_modified(conf, conffile)) {
364 opkg_message (conf, OPKG_NOTICE,
365 " not deleting modified conffile %s\n", file_name);
371 opkg_message(conf, OPKG_INFO, " deleting %s (noaction=%d)\n", file_name, conf->noaction);
376 if (!conf->noaction) {
379 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
380 file_name = (char *)iter->data;
382 if (rmdir(file_name) == 0) {
383 opkg_message(conf, OPKG_INFO, " deleting %s\n", file_name);
385 str_list_remove(&installed_dirs, &iter);
388 } while (removed_a_dir);
391 pkg_free_installed_files(pkg);
392 /* We have to remove the file list now, so that
393 find_pkg_owning_file does not always just report this package */
394 pkg_remove_installed_files_list(conf, pkg);
396 /* Don't print warning for dirs that are provided by other packages */
397 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
398 file_name = (char *)iter->data;
400 owner = file_hash_get_file_owner(conf, file_name);
404 str_list_remove(&installed_dirs, &iter);
409 while (!void_list_empty(&installed_dirs)) {
410 iter = str_list_pop(&installed_dirs);
414 str_list_deinit(&installed_dirs);
419 int remove_maintainer_scripts_except_postrm(opkg_conf_t *conf, pkg_t *pkg)
425 if (conf->noaction) return 0;
427 sprintf_alloc(&globpattern, "%s/%s.*",
428 pkg->dest->info_dir, pkg->name);
429 err = glob(globpattern, 0, NULL, &globbuf);
435 for (i = 0; i < globbuf.gl_pathc; i++) {
436 if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
439 opkg_message(conf, OPKG_INFO, " deleting %s\n", globbuf.gl_pathv[i]);
440 unlink(globbuf.gl_pathv[i]);
447 int remove_postrm(opkg_conf_t *conf, pkg_t *pkg)
449 char *postrm_file_name;
451 if (conf->noaction) return 0;
453 sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
454 pkg->dest->info_dir, pkg->name);
455 unlink(postrm_file_name);
456 free(postrm_file_name);