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"
32 * Returns number of the number of packages depending on the packages provided by this package.
33 * Every package implicitly provides itself.
35 int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
37 int nprovides = pkg->provides_count;
38 abstract_pkg_t **provides = pkg->provides;
39 int n_installed_dependents = 0;
41 for (i = 0; i <= nprovides; i++) {
42 abstract_pkg_t *providee = provides[i];
43 abstract_pkg_t **dependers = providee->depended_upon_by;
44 abstract_pkg_t *dep_ab_pkg;
45 if (dependers == NULL)
47 while ((dep_ab_pkg = *dependers++) != NULL) {
48 if (dep_ab_pkg->state_status == SS_INSTALLED){
49 n_installed_dependents++;
54 /* if caller requested the set of installed dependents */
57 abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
59 if ( dependents == NULL ){
60 fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
64 *pdependents = dependents;
65 for (i = 0; i <= nprovides; i++) {
66 abstract_pkg_t *providee = provides[i];
67 abstract_pkg_t **dependers = providee->depended_upon_by;
68 abstract_pkg_t *dep_ab_pkg;
69 if (dependers == NULL)
71 while ((dep_ab_pkg = *dependers++) != NULL) {
72 if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
73 dependents[p++] = dep_ab_pkg;
74 dep_ab_pkg->state_flag |= SF_MARKED;
79 /* now clear the marks */
80 for (i = 0; i < p; i++) {
81 abstract_pkg_t *dep_ab_pkg = dependents[i];
82 dep_ab_pkg->state_flag &= ~SF_MARKED;
85 return n_installed_dependents;
88 int opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
93 pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
94 abstract_pkg_t * ab_pkg;
96 if((ab_pkg = pkg->parent) == NULL){
97 fprintf(stderr, "%s: unable to get dependent pkgs. pkg %s isn't in hash table\n",
98 __FUNCTION__, pkg->name);
102 if (dependents == NULL)
105 // here i am using the dependencies_checked
106 if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
107 return 0; // has already been encountered in the process
108 // of marking packages for removal - Karthik
109 ab_pkg->dependencies_checked = 2;
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 */
138 for (i = 0; i < dependent_pkgs->len; i++) {
139 int err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
146 static int user_prefers_removing_dependents(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
148 abstract_pkg_t *dep_ab_pkg;
149 opkg_message(conf, OPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
150 while ((dep_ab_pkg = *dependents++) != NULL) {
151 if (dep_ab_pkg->state_status == SS_INSTALLED)
152 opkg_message(conf, OPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
154 opkg_message(conf, OPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
155 opkg_message(conf, OPKG_ERROR, "");
156 opkg_message(conf, OPKG_ERROR, "You can force removal of this package with -force-depends.\n");
157 opkg_message(conf, OPKG_ERROR, "You can force removal of this package and its dependents\n");
158 opkg_message(conf, OPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
159 opkg_message(conf, OPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
160 opkg_message(conf, OPKG_ERROR, "in opkg.conf.\n");
164 int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
166 /* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
167 thus I wan't check for essential, as I'm upgrading.
168 I hope it won't break anything :)
171 abstract_pkg_t *parent_pkg = NULL;
173 if (conf->autoremove)
174 printf ("autoremove is enabled, but not yet implemented\n");
176 if (pkg->essential && !message) {
177 if (conf->force_removal_of_essential_packages) {
178 fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
179 "\tIf your system breaks, you get to keep both pieces\n",
182 fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
183 "\tRemoving an essential package may lead to an unusable system, but if\n"
184 "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
185 "\tits will with the option: -force-removal-of-essential-packages\n",
187 return OPKG_PKG_IS_ESSENTIAL;
191 if ((parent_pkg = pkg->parent) == NULL)
194 /* only attempt to remove dependent installed packages if
195 * force_depends is not specified or the package is being
198 if (!conf->force_depends
199 && !(pkg->state_flag & SF_REPLACE)) {
200 abstract_pkg_t **dependents;
201 int has_installed_dependents =
202 pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
204 if (has_installed_dependents) {
206 * if this package is depended up by others, then either we should
207 * not remove it or we should remove it and all of its dependents
210 if (!conf->force_removal_of_dependent_packages
211 && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
212 return OPKG_PKG_HAS_DEPENDENTS;
215 /* remove packages depending on this package - Karthik */
216 err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
223 printf("Removing package %s from %s...\n", pkg->name, pkg->dest->name);
226 pkg->state_flag |= SF_FILELIST_CHANGED;
228 pkg->state_want = SW_DEINSTALL;
229 opkg_state_changed++;
231 pkg_run_script(conf, pkg, "prerm", "remove");
233 /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
234 maintains an empty filelist rather than deleting it. That seems
235 like a big pain, and I don't see that that should make a big
236 difference, but for anyone who wants tighter compatibility,
237 feel free to fix this. */
238 remove_data_files_and_list(conf, pkg);
240 pkg_run_script(conf, pkg, "postrm", "remove");
242 remove_maintainer_scripts_except_postrm(conf, pkg);
244 /* Aman Gupta - Since opkg is made for handheld devices with limited
245 * space, it doesn't make sense to leave extra configurations, files,
246 * and maintainer scripts left around. So, we make remove like purge,
247 * and take out all the crap :) */
249 remove_postrm(conf, pkg);
250 pkg->state_status = SS_NOT_INSTALLED;
253 parent_pkg->state_status = SS_NOT_INSTALLED;
258 int opkg_purge_pkg(opkg_conf_t *conf, pkg_t *pkg)
260 opkg_remove_pkg(conf, pkg,0);
264 int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
266 str_list_t installed_dirs;
267 str_list_t *installed_files;
268 str_list_elt_t *iter;
270 conffile_t *conffile;
274 str_list_init(&installed_dirs);
275 installed_files = pkg_get_installed_files(pkg);
277 for (iter = installed_files->head; iter; iter = iter->next) {
278 file_name = iter->data;
280 if (file_is_dir(file_name)) {
281 str_list_append(&installed_dirs, strdup(file_name));
285 conffile = pkg_get_conffile(pkg, file_name);
287 /* XXX: QUESTION: Is this right? I figure we only need to
288 save the conffile if it has been modified. Is that what
289 dpkg does? Or does dpkg preserve all conffiles? If so,
290 this seems like a better thing to do to conserve
292 if (conffile_has_been_modified(conf, conffile)) {
293 printf(" not deleting modified conffile %s\n", file_name);
299 opkg_message(conf, OPKG_INFO, " deleting %s (noaction=%d)\n", file_name, conf->noaction);
304 if (!conf->noaction) {
307 for (iter = installed_dirs.head; iter; iter = iter->next) {
308 file_name = iter->data;
310 if (rmdir(file_name) == 0) {
311 opkg_message(conf, OPKG_INFO, " deleting %s\n", file_name);
313 str_list_remove(&installed_dirs, &iter);
316 } while (removed_a_dir);
319 pkg_free_installed_files(pkg);
320 /* We have to remove the file list now, so that
321 find_pkg_owning_file does not always just report this package */
322 pkg_remove_installed_files_list(conf, pkg);
324 /* Don't print warning for dirs that are provided by other packages */
325 for (iter = installed_dirs.head; iter; iter = iter->next) {
326 file_name = iter->data;
328 owner = file_hash_get_file_owner(conf, file_name);
332 str_list_remove(&installed_dirs, &iter);
337 for (iter = installed_dirs.head; iter; iter = iter->next) {
341 str_list_deinit(&installed_dirs);
346 int remove_maintainer_scripts_except_postrm(opkg_conf_t *conf, pkg_t *pkg)
352 if (conf->noaction) return 0;
354 sprintf_alloc(&globpattern, "%s/%s.*",
355 pkg->dest->info_dir, pkg->name);
356 err = glob(globpattern, 0, NULL, &globbuf);
362 for (i = 0; i < globbuf.gl_pathc; i++) {
363 if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
366 opkg_message(conf, OPKG_INFO, " deleting %s\n", globbuf.gl_pathv[i]);
367 unlink(globbuf.gl_pathv[i]);
374 int remove_postrm(opkg_conf_t *conf, pkg_t *pkg)
376 char *postrm_file_name;
378 if (conf->noaction) return 0;
380 sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
381 pkg->dest->info_dir, pkg->name);
382 unlink(postrm_file_name);
383 free(postrm_file_name);