1 /* opkg_remove.c - the itsy 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"
25 #include "file_util.h"
26 #include "sprintf_alloc.h"
30 * Returns number of the number of packages depending on the packages provided by this package.
31 * Every package implicitly provides itself.
33 int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
35 int nprovides = pkg->provides_count;
36 abstract_pkg_t **provides = pkg->provides;
37 int n_installed_dependents = 0;
39 for (i = 0; i <= nprovides; i++) {
40 abstract_pkg_t *providee = provides[i];
41 abstract_pkg_t **dependers = providee->depended_upon_by;
42 abstract_pkg_t *dep_ab_pkg;
43 if (dependers == NULL)
45 while ((dep_ab_pkg = *dependers++) != NULL) {
46 if (dep_ab_pkg->state_status == SS_INSTALLED){
47 n_installed_dependents++;
52 /* if caller requested the set of installed dependents */
55 abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
57 if ( dependents == NULL ){
58 fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
62 *pdependents = dependents;
63 for (i = 0; i <= nprovides; i++) {
64 abstract_pkg_t *providee = provides[i];
65 abstract_pkg_t **dependers = providee->depended_upon_by;
66 abstract_pkg_t *dep_ab_pkg;
67 if (dependers == NULL)
69 while ((dep_ab_pkg = *dependers++) != NULL) {
70 if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
71 dependents[p++] = dep_ab_pkg;
72 dep_ab_pkg->state_flag |= SF_MARKED;
77 /* now clear the marks */
78 for (i = 0; i < p; i++) {
79 abstract_pkg_t *dep_ab_pkg = dependents[i];
80 dep_ab_pkg->state_flag &= ~SF_MARKED;
83 return n_installed_dependents;
86 int opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
91 pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
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 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 */
136 for (i = 0; i < dependent_pkgs->len; i++) {
137 int err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
144 static int user_prefers_removing_dependents(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
146 abstract_pkg_t *dep_ab_pkg;
147 opkg_message(conf, OPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
148 while ((dep_ab_pkg = *dependents++) != NULL) {
149 if (dep_ab_pkg->state_status == SS_INSTALLED)
150 opkg_message(conf, OPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
152 opkg_message(conf, OPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
153 opkg_message(conf, OPKG_ERROR, "");
154 opkg_message(conf, OPKG_ERROR, "You can force removal of this package with -force-depends.\n");
155 opkg_message(conf, OPKG_ERROR, "You can force removal of this package and its dependents\n");
156 opkg_message(conf, OPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
157 opkg_message(conf, OPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
158 opkg_message(conf, OPKG_ERROR, "in opkg.conf.\n");
162 static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg)
165 * find and remove packages that were autoinstalled and are orphaned by the removal of pkg
168 char *buffer, *d_str;
171 for (i = 0; i < pkg->depends_count; ++i)
175 d_str = pkg->depends_str[i];
176 buffer = malloc (strlen (d_str) + 1);
179 fprintf(stderr,"%s Unable to allocate memory.\n", __FUNCTION__);
183 while (d_str[x] != '\0' && d_str[x] != ' ')
185 buffer[x] = d_str[x];
189 buffer = realloc (buffer, strlen (buffer) + 1);
190 p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash, buffer);
192 /* if the package is not installed, this could have been a circular
193 * depenancy and the package has already been removed */
197 if (p->auto_installed)
200 abstract_pkg_t **dependents;
202 deps = pkg_has_installed_dependents(conf, NULL, p, &dependents);
205 opkg_message (conf, OPKG_INFO,
206 "%s was autoinstalled but is now orphaned\n", buffer);
207 opkg_remove_pkg(conf, p,0);
210 opkg_message (conf, OPKG_INFO, "%s was autoinstalled and is still required by "
211 "%d installed packages\n", buffer, deps);
219 int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
221 /* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
222 thus I wan't check for essential, as I'm upgrading.
223 I hope it won't break anything :)
226 abstract_pkg_t *parent_pkg = NULL;
228 if (pkg->essential && !message) {
229 if (conf->force_removal_of_essential_packages) {
230 fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
231 "\tIf your system breaks, you get to keep both pieces\n",
234 fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
235 "\tRemoving an essential package may lead to an unusable system, but if\n"
236 "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
237 "\tits will with the option: -force-removal-of-essential-packages\n",
239 return OPKG_PKG_IS_ESSENTIAL;
243 if ((parent_pkg = pkg->parent) == NULL)
246 /* only attempt to remove dependent installed packages if
247 * force_depends is not specified or the package is being
250 if (!conf->force_depends
251 && !(pkg->state_flag & SF_REPLACE)) {
252 abstract_pkg_t **dependents;
253 int has_installed_dependents =
254 pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
256 if (has_installed_dependents) {
258 * if this package is depended up by others, then either we should
259 * not remove it or we should remove it and all of its dependents
262 if (!conf->force_removal_of_dependent_packages
263 && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
264 return OPKG_PKG_HAS_DEPENDENTS;
267 /* remove packages depending on this package - Karthik */
268 err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
275 opkg_message (conf, OPKG_NOTICE,
276 "Removing package %s from %s...\n", pkg->name, pkg->dest->name);
279 pkg->state_flag |= SF_FILELIST_CHANGED;
281 pkg->state_want = SW_DEINSTALL;
282 opkg_state_changed++;
284 pkg_run_script(conf, pkg, "prerm", "remove");
286 /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
287 maintains an empty filelist rather than deleting it. That seems
288 like a big pain, and I don't see that that should make a big
289 difference, but for anyone who wants tighter compatibility,
290 feel free to fix this. */
291 remove_data_files_and_list(conf, pkg);
293 pkg_run_script(conf, pkg, "postrm", "remove");
295 remove_maintainer_scripts_except_postrm(conf, pkg);
297 /* Aman Gupta - Since opkg is made for handheld devices with limited
298 * space, it doesn't make sense to leave extra configurations, files,
299 * and maintainer scripts left around. So, we make remove like purge,
300 * and take out all the crap :) */
302 remove_postrm(conf, pkg);
303 pkg->state_status = SS_NOT_INSTALLED;
306 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 (conf, pkg);
318 int opkg_purge_pkg(opkg_conf_t *conf, pkg_t *pkg)
320 opkg_remove_pkg(conf, pkg,0);
324 int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
326 str_list_t installed_dirs;
327 str_list_t *installed_files;
328 str_list_elt_t *iter;
330 conffile_t *conffile;
334 str_list_init(&installed_dirs);
335 installed_files = pkg_get_installed_files(pkg);
337 for (iter = installed_files->head; iter; iter = iter->next) {
338 file_name = iter->data;
340 if (file_is_dir(file_name)) {
341 str_list_append(&installed_dirs, strdup(file_name));
345 conffile = pkg_get_conffile(pkg, file_name);
347 /* XXX: QUESTION: Is this right? I figure we only need to
348 save the conffile if it has been modified. Is that what
349 dpkg does? Or does dpkg preserve all conffiles? If so,
350 this seems like a better thing to do to conserve
352 if (conffile_has_been_modified(conf, conffile)) {
353 opkg_message (conf, OPKG_NOTICE,
354 " not deleting modified conffile %s\n", file_name);
360 opkg_message(conf, OPKG_INFO, " deleting %s (noaction=%d)\n", file_name, conf->noaction);
365 if (!conf->noaction) {
368 for (iter = installed_dirs.head; iter; iter = iter->next) {
369 file_name = iter->data;
371 if (rmdir(file_name) == 0) {
372 opkg_message(conf, OPKG_INFO, " deleting %s\n", file_name);
374 str_list_remove(&installed_dirs, &iter);
377 } while (removed_a_dir);
380 pkg_free_installed_files(pkg);
381 /* We have to remove the file list now, so that
382 find_pkg_owning_file does not always just report this package */
383 pkg_remove_installed_files_list(conf, pkg);
385 /* Don't print warning for dirs that are provided by other packages */
386 for (iter = installed_dirs.head; iter; iter = iter->next) {
387 file_name = iter->data;
389 owner = file_hash_get_file_owner(conf, file_name);
393 str_list_remove(&installed_dirs, &iter);
398 for (iter = installed_dirs.head; iter; iter = iter->next) {
402 str_list_deinit(&installed_dirs);
407 int remove_maintainer_scripts_except_postrm(opkg_conf_t *conf, pkg_t *pkg)
413 if (conf->noaction) return 0;
415 sprintf_alloc(&globpattern, "%s/%s.*",
416 pkg->dest->info_dir, pkg->name);
417 err = glob(globpattern, 0, NULL, &globbuf);
423 for (i = 0; i < globbuf.gl_pathc; i++) {
424 if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
427 opkg_message(conf, OPKG_INFO, " deleting %s\n", globbuf.gl_pathv[i]);
428 unlink(globbuf.gl_pathv[i]);
435 int remove_postrm(opkg_conf_t *conf, pkg_t *pkg)
437 char *postrm_file_name;
439 if (conf->noaction) return 0;
441 sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
442 pkg->dest->info_dir, pkg->name);
443 unlink(postrm_file_name);
444 free(postrm_file_name);