5d9cec99d25a2ccb917fea356d1dc7f7a2d3ff78
[project/opkg-lede.git] / ipkg_remove.c
1 /* ipkg_remove.c - the itsy package management system
2
3 Carl D. Worth
4
5 Copyright (C) 2001 University of Southern California
6
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.
11
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.
16 */
17
18 #include "ipkg.h"
19 #include "ipkg_message.h"
20
21 #include <glob.h>
22
23 #include "ipkg_remove.h"
24
25 #include "file_util.h"
26 #include "sprintf_alloc.h"
27 #include "str_util.h"
28
29 #include "ipkg_cmd.h"
30
31 /*
32 * Returns number of the number of packages depending on the packages provided by this package.
33 * Every package implicitly provides itself.
34 */
35 int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
36 {
37 int nprovides = pkg->provides_count;
38 abstract_pkg_t **provides = pkg->provides;
39 int n_installed_dependents = 0;
40 int i;
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)
46 continue;
47 while ((dep_ab_pkg = *dependers++) != NULL) {
48 if (dep_ab_pkg->state_status == SS_INSTALLED){
49 n_installed_dependents++;
50 }
51 }
52
53 }
54 /* if caller requested the set of installed dependents */
55 if (pdependents) {
56 int p = 0;
57 abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
58
59 if ( dependents == NULL ){
60 fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
61 return -1;
62 }
63
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)
70 continue;
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;
75 }
76 }
77 }
78 dependents[p] = NULL;
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;
83 }
84 }
85 return n_installed_dependents;
86 }
87
88 int ipkg_remove_dependent_pkgs (ipkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
89 {
90 int i;
91 int a;
92 int count;
93 pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
94 abstract_pkg_t * ab_pkg;
95
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);
99 return 0;
100 }
101
102 if (dependents == NULL)
103 return 0;
104
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;
110
111 i = 0;
112 count = 1;
113 while (dependents [i] != NULL) {
114 abstract_pkg_t *dep_ab_pkg = dependents[i];
115
116 if (dep_ab_pkg->dependencies_checked == 2){
117 i++;
118 continue;
119 }
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);
125 count++;
126 }
127 }
128 }
129 i++;
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 */
132 }
133
134 if (count == 1)
135 return 0;
136
137
138 for (i = 0; i < dependent_pkgs->len; i++) {
139 int err = ipkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
140 if (err)
141 return err;
142 }
143 return 0;
144 }
145
146 static int user_prefers_removing_dependents(ipkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
147 {
148 abstract_pkg_t *dep_ab_pkg;
149 ipkg_message(conf, IPKG_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 ipkg_message(conf, IPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
153 }
154 ipkg_message(conf, IPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
155 ipkg_message(conf, IPKG_ERROR, "");
156 ipkg_message(conf, IPKG_ERROR, "You can force removal of this package with -force-depends.\n");
157 ipkg_message(conf, IPKG_ERROR, "You can force removal of this package and its dependents\n");
158 ipkg_message(conf, IPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
159 ipkg_message(conf, IPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
160 ipkg_message(conf, IPKG_ERROR, "in ipkg.conf.\n");
161 return 0;
162 }
163
164 int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message)
165 {
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 :)
169 */
170 int err;
171 abstract_pkg_t *parent_pkg = NULL;
172
173 if (pkg->essential && !message) {
174 if (conf->force_removal_of_essential_packages) {
175 fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
176 "\tIf your system breaks, you get to keep both pieces\n",
177 pkg->name);
178 } else {
179 fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
180 "\tRemoving an essential package may lead to an unusable system, but if\n"
181 "\tyou enjoy that kind of pain, you can force ipkg to proceed against\n"
182 "\tits will with the option: -force-removal-of-essential-packages\n",
183 pkg->name);
184 return IPKG_PKG_IS_ESSENTIAL;
185 }
186 }
187
188 if ((parent_pkg = pkg->parent) == NULL)
189 return 0;
190
191 /* only attempt to remove dependent installed packages if
192 * force_depends is not specified or the package is being
193 * replaced.
194 */
195 if (!conf->force_depends
196 && !(pkg->state_flag & SF_REPLACE)) {
197 abstract_pkg_t **dependents;
198 int has_installed_dependents =
199 pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
200
201 if (has_installed_dependents) {
202 /*
203 * if this package is depended up by others, then either we should
204 * not remove it or we should remove it and all of its dependents
205 */
206
207 if (!conf->force_removal_of_dependent_packages
208 && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
209 return IPKG_PKG_HAS_DEPENDENTS;
210 }
211
212 /* remove packages depending on this package - Karthik */
213 err = ipkg_remove_dependent_pkgs (conf, pkg, dependents);
214 free(dependents);
215 if (err) return err;
216 }
217 }
218
219 if ( message==0 ){
220 printf("Removing package %s from %s...\n", pkg->name, pkg->dest->name);
221 fflush(stdout);
222 }
223 pkg->state_flag |= SF_FILELIST_CHANGED;
224
225 pkg->state_want = SW_DEINSTALL;
226 ipkg_state_changed++;
227
228 pkg_run_script(conf, pkg, "prerm", "remove");
229
230 /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
231 maintains an empty filelist rather than deleting it. That seems
232 like a big pain, and I don't see that that should make a big
233 difference, but for anyone who wants tighter compatibility,
234 feel free to fix this. */
235 remove_data_files_and_list(conf, pkg);
236
237 pkg_run_script(conf, pkg, "postrm", "remove");
238
239 remove_maintainer_scripts_except_postrm(conf, pkg);
240
241 /* Aman Gupta - Since ipkg is made for handheld devices with limited
242 * space, it doesn't make sense to leave extra configurations, files,
243 * and maintainer scripts left around. So, we make remove like purge,
244 * and take out all the crap :) */
245
246 remove_postrm(conf, pkg);
247 pkg->state_status = SS_NOT_INSTALLED;
248
249 if (parent_pkg)
250 parent_pkg->state_status = SS_NOT_INSTALLED;
251
252 return 0;
253 }
254
255 int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg)
256 {
257 ipkg_remove_pkg(conf, pkg,0);
258 return 0;
259 }
260
261 int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg)
262 {
263 str_list_t installed_dirs;
264 str_list_t *installed_files;
265 str_list_elt_t *iter;
266 char *file_name;
267 conffile_t *conffile;
268 int removed_a_dir;
269 pkg_t *owner;
270
271 str_list_init(&installed_dirs);
272 installed_files = pkg_get_installed_files(pkg);
273
274 for (iter = installed_files->head; iter; iter = iter->next) {
275 file_name = iter->data;
276
277 if (file_is_dir(file_name)) {
278 str_list_append(&installed_dirs, strdup(file_name));
279 continue;
280 }
281
282 conffile = pkg_get_conffile(pkg, file_name);
283 if (conffile) {
284 /* XXX: QUESTION: Is this right? I figure we only need to
285 save the conffile if it has been modified. Is that what
286 dpkg does? Or does dpkg preserve all conffiles? If so,
287 this seems like a better thing to do to conserve
288 space. */
289 if (conffile_has_been_modified(conf, conffile)) {
290 printf(" not deleting modified conffile %s\n", file_name);
291 fflush(stdout);
292 continue;
293 }
294 }
295
296 ipkg_message(conf, IPKG_INFO, " deleting %s (noaction=%d)\n", file_name, conf->noaction);
297 if (!conf->noaction)
298 unlink(file_name);
299 }
300
301 if (!conf->noaction) {
302 do {
303 removed_a_dir = 0;
304 for (iter = installed_dirs.head; iter; iter = iter->next) {
305 file_name = iter->data;
306
307 if (rmdir(file_name) == 0) {
308 ipkg_message(conf, IPKG_INFO, " deleting %s\n", file_name);
309 removed_a_dir = 1;
310 str_list_remove(&installed_dirs, &iter);
311 }
312 }
313 } while (removed_a_dir);
314 }
315
316 pkg_free_installed_files(pkg);
317 /* We have to remove the file list now, so that
318 find_pkg_owning_file does not always just report this package */
319 pkg_remove_installed_files_list(conf, pkg);
320
321 /* Don't print warning for dirs that are provided by other packages */
322 for (iter = installed_dirs.head; iter; iter = iter->next) {
323 file_name = iter->data;
324
325 owner = file_hash_get_file_owner(conf, file_name);
326 if (owner) {
327 free(iter->data);
328 iter->data = NULL;
329 str_list_remove(&installed_dirs, &iter);
330 }
331 }
332
333 /* cleanup */
334 for (iter = installed_dirs.head; iter; iter = iter->next) {
335 free(iter->data);
336 iter->data = NULL;
337 }
338 str_list_deinit(&installed_dirs);
339
340 return 0;
341 }
342
343 int remove_maintainer_scripts_except_postrm(ipkg_conf_t *conf, pkg_t *pkg)
344 {
345 int i, err;
346 char *globpattern;
347 glob_t globbuf;
348
349 if (conf->noaction) return 0;
350
351 sprintf_alloc(&globpattern, "%s/%s.*",
352 pkg->dest->info_dir, pkg->name);
353 err = glob(globpattern, 0, NULL, &globbuf);
354 free(globpattern);
355 if (err) {
356 return 0;
357 }
358
359 for (i = 0; i < globbuf.gl_pathc; i++) {
360 if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
361 continue;
362 }
363 ipkg_message(conf, IPKG_INFO, " deleting %s\n", globbuf.gl_pathv[i]);
364 unlink(globbuf.gl_pathv[i]);
365 }
366 globfree(&globbuf);
367
368 return 0;
369 }
370
371 int remove_postrm(ipkg_conf_t *conf, pkg_t *pkg)
372 {
373 char *postrm_file_name;
374
375 if (conf->noaction) return 0;
376
377 sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
378 pkg->dest->info_dir, pkg->name);
379 unlink(postrm_file_name);
380 free(postrm_file_name);
381
382 return 0;
383 }