Iterate the whole depends array, in the unlikely event that pre_depends_count!=0
[project/opkg-lede.git] / libopkg / opkg_remove.c
1 /* opkg_remove.c - the opkg 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 "includes.h"
19 #include "opkg_message.h"
20
21 #include <glob.h>
22
23 #include "opkg_remove.h"
24 #include "opkg_error.h"
25 #include "opkg_cmd.h"
26
27 #include "file_util.h"
28 #include "sprintf_alloc.h"
29 #include "str_util.h"
30 #include "libbb/libbb.h"
31
32 /*
33 * Returns number of the number of packages depending on the packages provided by this package.
34 * Every package implicitly provides itself.
35 */
36 int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
37 {
38 int nprovides = pkg->provides_count;
39 abstract_pkg_t **provides = pkg->provides;
40 int n_installed_dependents = 0;
41 int i;
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)
47 continue;
48 while ((dep_ab_pkg = *dependers++) != NULL) {
49 if (dep_ab_pkg->state_status == SS_INSTALLED){
50 n_installed_dependents++;
51 }
52 }
53
54 }
55 /* if caller requested the set of installed dependents */
56 if (pdependents) {
57 int p = 0;
58 abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *));
59
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)
66 continue;
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;
71 }
72 }
73 }
74 dependents[p] = NULL;
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;
79 }
80 }
81 return n_installed_dependents;
82 }
83
84 int opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
85 {
86 int i;
87 int a;
88 int count;
89 pkg_vec_t *dependent_pkgs;
90 abstract_pkg_t * ab_pkg;
91
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);
95 return 0;
96 }
97
98 if (dependents == NULL)
99 return 0;
100
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;
106
107 i = 0;
108 count = 1;
109 dependent_pkgs = pkg_vec_alloc();
110
111 while (dependents [i] != NULL) {
112 abstract_pkg_t *dep_ab_pkg = dependents[i];
113
114 if (dep_ab_pkg->dependencies_checked == 2){
115 i++;
116 continue;
117 }
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);
123 count++;
124 }
125 }
126 }
127 i++;
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 */
130 }
131
132 if (count == 1) {
133 free(dependent_pkgs);
134 return 0;
135 }
136
137
138 int err=0;
139 for (i = 0; i < dependent_pkgs->len; i++) {
140 err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
141 if (err)
142 break;
143 }
144 free(dependent_pkgs);
145 return err;
146 }
147
148 static int user_prefers_removing_dependents(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
149 {
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);
155 }
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");
163 return 0;
164 }
165
166 /*
167 * Find and remove packages that were autoinstalled and are orphaned
168 * by the removal of pkg.
169 */
170 static int
171 remove_autoinstalled(opkg_conf_t *conf, pkg_t *pkg)
172 {
173 int i, j;
174 int n_deps;
175 pkg_t *p;
176 struct compound_depend *cdep;
177 abstract_pkg_t **dependents;
178
179 int count = pkg->pre_depends_count +
180 pkg->depends_count +
181 pkg->recommends_count +
182 pkg->suggests_count;
183
184 for (i=0; i<count; i++) {
185 cdep = &pkg->depends[i];
186 if (cdep->type != DEPEND)
187 continue;
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);
191
192 /* If the package is not installed, this could have
193 * been a circular dependency and the package has
194 * already been removed.
195 */
196 if (!p)
197 return -1;
198
199 if (!p->auto_installed)
200 continue;
201
202 n_deps = pkg_has_installed_dependents(conf, NULL, p,
203 &dependents);
204 if (n_deps == 0) {
205 opkg_message(conf, OPKG_NOTICE,
206 "%s was autoinstalled and is "
207 "now orphaned, removing\n",
208 p->name);
209 opkg_remove_pkg(conf, p, 0);
210 } else
211 opkg_message(conf, OPKG_INFO,
212 "%s was autoinstalled and is "
213 "still required by %d "
214 "installed packages.\n",
215 p->name, n_deps);
216
217 if (dependents)
218 free(dependents);
219 }
220 }
221
222 return 0;
223 }
224
225 int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
226 {
227 /* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
228 thus I wan't check for essential, as I'm upgrading.
229 I hope it won't break anything :)
230 */
231 int err;
232 abstract_pkg_t *parent_pkg = NULL;
233
234 if (pkg->essential && !message) {
235 if (conf->force_removal_of_essential_packages) {
236 fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
237 "\tIf your system breaks, you get to keep both pieces\n",
238 pkg->name);
239 } else {
240 fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
241 "\tRemoving an essential package may lead to an unusable system, but if\n"
242 "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
243 "\tits will with the option: -force-removal-of-essential-packages\n",
244 pkg->name);
245 return OPKG_PKG_IS_ESSENTIAL;
246 }
247 }
248
249 if ((parent_pkg = pkg->parent) == NULL)
250 return 0;
251
252 /* only attempt to remove dependent installed packages if
253 * force_depends is not specified or the package is being
254 * replaced.
255 */
256 if (!conf->force_depends
257 && !(pkg->state_flag & SF_REPLACE)) {
258 abstract_pkg_t **dependents;
259 int has_installed_dependents =
260 pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
261
262 if (has_installed_dependents) {
263 /*
264 * if this package is depended up by others, then either we should
265 * not remove it or we should remove it and all of its dependents
266 */
267
268 if (!conf->force_removal_of_dependent_packages
269 && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
270 free(dependents);
271 return OPKG_PKG_HAS_DEPENDENTS;
272 }
273
274 /* remove packages depending on this package - Karthik */
275 err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
276 if (err) {
277 free(dependents);
278 return err;
279 }
280 }
281 if (dependents)
282 free(dependents);
283 }
284
285 if ( message==0 ){
286 opkg_message (conf, OPKG_NOTICE,
287 "Removing package %s from %s...\n", pkg->name, pkg->dest->name);
288 fflush(stdout);
289 }
290 pkg->state_flag |= SF_FILELIST_CHANGED;
291
292 pkg->state_want = SW_DEINSTALL;
293 opkg_state_changed++;
294
295 pkg_run_script(conf, pkg, "prerm", "remove");
296
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);
303
304 pkg_run_script(conf, pkg, "postrm", "remove");
305
306 remove_maintainer_scripts_except_postrm(conf, pkg);
307
308 /* Aman Gupta - Since opkg is made for handheld devices with limited
309 * space, it doesn't make sense to leave extra configurations, files,
310 * and maintainer scripts left around. So, we make remove like purge,
311 * and take out all the crap :) */
312
313 remove_postrm(conf, pkg);
314 pkg->state_status = SS_NOT_INSTALLED;
315
316 if (parent_pkg)
317 parent_pkg->state_status = SS_NOT_INSTALLED;
318
319
320 /* remove autoinstalled packages that are orphaned by the removal of this one */
321 if (conf->autoremove)
322 remove_autoinstalled (conf, pkg);
323
324
325
326 return 0;
327 }
328
329 int opkg_purge_pkg(opkg_conf_t *conf, pkg_t *pkg)
330 {
331 opkg_remove_pkg(conf, pkg,0);
332 return 0;
333 }
334
335 int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
336 {
337 str_list_t installed_dirs;
338 str_list_t *installed_files;
339 str_list_elt_t *iter;
340 char *file_name;
341 conffile_t *conffile;
342 int removed_a_dir;
343 pkg_t *owner;
344 int rootdirlen = 0;
345
346 str_list_init(&installed_dirs);
347 installed_files = pkg_get_installed_files(conf, pkg);
348
349 /* don't include trailing slash */
350 if (conf->offline_root)
351 rootdirlen = strlen(conf->offline_root);
352
353 for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
354 file_name = (char *)iter->data;
355
356 if (file_is_dir(file_name)) {
357 str_list_append(&installed_dirs, file_name);
358 continue;
359 }
360
361 conffile = pkg_get_conffile(pkg, file_name+rootdirlen);
362 if (conffile) {
363 /* XXX: QUESTION: Is this right? I figure we only need to
364 save the conffile if it has been modified. Is that what
365 dpkg does? Or does dpkg preserve all conffiles? If so,
366 this seems like a better thing to do to conserve
367 space. */
368 if (conffile_has_been_modified(conf, conffile)) {
369 opkg_message (conf, OPKG_NOTICE,
370 " not deleting modified conffile %s\n", file_name);
371 fflush(stdout);
372 continue;
373 }
374 }
375
376 opkg_message(conf, OPKG_INFO, " deleting %s (noaction=%d)\n", file_name, conf->noaction);
377 if (!conf->noaction)
378 unlink(file_name);
379 }
380
381 if (!conf->noaction) {
382 do {
383 removed_a_dir = 0;
384 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
385 file_name = (char *)iter->data;
386
387 if (rmdir(file_name) == 0) {
388 opkg_message(conf, OPKG_INFO, " deleting %s\n", file_name);
389 removed_a_dir = 1;
390 str_list_remove(&installed_dirs, &iter);
391 }
392 }
393 } while (removed_a_dir);
394 }
395
396 pkg_free_installed_files(pkg);
397 /* We have to remove the file list now, so that
398 find_pkg_owning_file does not always just report this package */
399 pkg_remove_installed_files_list(conf, pkg);
400
401 /* Don't print warning for dirs that are provided by other packages */
402 for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
403 file_name = (char *)iter->data;
404
405 owner = file_hash_get_file_owner(conf, file_name);
406 if (owner) {
407 free(iter->data);
408 iter->data = NULL;
409 str_list_remove(&installed_dirs, &iter);
410 }
411 }
412
413 /* cleanup */
414 while (!void_list_empty(&installed_dirs)) {
415 iter = str_list_pop(&installed_dirs);
416 free(iter->data);
417 free(iter);
418 }
419 str_list_deinit(&installed_dirs);
420
421 return 0;
422 }
423
424 int remove_maintainer_scripts_except_postrm(opkg_conf_t *conf, pkg_t *pkg)
425 {
426 int i, err;
427 char *globpattern;
428 glob_t globbuf;
429
430 if (conf->noaction) return 0;
431
432 sprintf_alloc(&globpattern, "%s/%s.*",
433 pkg->dest->info_dir, pkg->name);
434 err = glob(globpattern, 0, NULL, &globbuf);
435 free(globpattern);
436 if (err) {
437 return 0;
438 }
439
440 for (i = 0; i < globbuf.gl_pathc; i++) {
441 if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
442 continue;
443 }
444 opkg_message(conf, OPKG_INFO, " deleting %s\n", globbuf.gl_pathv[i]);
445 unlink(globbuf.gl_pathv[i]);
446 }
447 globfree(&globbuf);
448
449 return 0;
450 }
451
452 int remove_postrm(opkg_conf_t *conf, pkg_t *pkg)
453 {
454 char *postrm_file_name;
455
456 if (conf->noaction) return 0;
457
458 sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
459 pkg->dest->info_dir, pkg->name);
460 unlink(postrm_file_name);
461 free(postrm_file_name);
462
463 return 0;
464 }