Plug leaks found when using --force-removal-of-dependent-packages.
[oweals/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
37 pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
38 {
39      int nprovides = pkg->provides_count;
40      abstract_pkg_t **provides = pkg->provides;
41      int n_installed_dependents = 0;
42      int i;
43      for (i = 0; i < nprovides; i++) {
44           abstract_pkg_t *providee = provides[i];
45           abstract_pkg_t **dependers = providee->depended_upon_by;
46           abstract_pkg_t *dep_ab_pkg;
47           if (dependers == NULL)
48                continue;
49           while ((dep_ab_pkg = *dependers++) != NULL) {
50                if (dep_ab_pkg->state_status == SS_INSTALLED){
51                     n_installed_dependents++;
52                }
53           }
54
55      }
56      /* if caller requested the set of installed dependents */
57      if (pdependents) {
58           int p = 0;
59           abstract_pkg_t **dependents = xcalloc((n_installed_dependents+1), sizeof(abstract_pkg_t *));
60
61           *pdependents = dependents;
62           for (i = 0; i < nprovides; i++) {
63                abstract_pkg_t *providee = provides[i];
64                abstract_pkg_t **dependers = providee->depended_upon_by;
65                abstract_pkg_t *dep_ab_pkg;
66                if (dependers == NULL)
67                     continue;
68                while ((dep_ab_pkg = *dependers++) != NULL) {
69                     if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
70                          dependents[p++] = dep_ab_pkg;
71                          dep_ab_pkg->state_flag |= SF_MARKED;
72                     }
73                }
74           }
75           dependents[p] = NULL;
76           /* now clear the marks */
77           for (i = 0; i < p; i++) {
78                abstract_pkg_t *dep_ab_pkg = dependents[i];
79                dep_ab_pkg->state_flag &= ~SF_MARKED;
80           }
81      }
82      return n_installed_dependents;
83 }
84
85 static int
86 opkg_remove_dependent_pkgs (opkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
87 {
88     int i;
89     int a;
90     int count;
91     pkg_vec_t *dependent_pkgs;
92     abstract_pkg_t * ab_pkg;
93
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);
97         return 0;
98     }
99     
100     if (dependents == NULL)
101             return 0;
102
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;
108
109     i = 0;
110     count = 1;
111     dependent_pkgs = pkg_vec_alloc();
112
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         pkg_vec_free(dependent_pkgs);  
136         return 0;
137     }
138     
139     
140     int err=0;
141     for (i = 0; i < dependent_pkgs->len; i++) {
142         err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
143         if (err) {
144             pkg_vec_free(dependent_pkgs);
145             break;
146         }
147     }
148     pkg_vec_free(dependent_pkgs);
149     return err;
150 }
151
152 static void
153 print_dependents_warning(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
154 {
155     abstract_pkg_t *dep_ab_pkg;
156     opkg_message(conf, OPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
157     while ((dep_ab_pkg = *dependents++) != NULL) {
158          if (dep_ab_pkg->state_status == SS_INSTALLED)
159               opkg_message(conf, OPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
160     }
161     opkg_message(conf, OPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
162     opkg_message(conf, OPKG_ERROR, "");
163     opkg_message(conf, OPKG_ERROR, "You can force removal of this package with --force-depends.\n");
164     opkg_message(conf, OPKG_ERROR, "You can force removal of this package and its dependents\n");
165     opkg_message(conf, OPKG_ERROR, "with --force-removal-of-dependent-packages\n");
166 }
167
168 /*
169  * Find and remove packages that were autoinstalled and are orphaned
170  * by the removal of pkg.
171  */
172 static int
173 remove_autoinstalled(opkg_conf_t *conf, pkg_t *pkg)
174 {
175         int i, j;
176         int n_deps;
177         pkg_t *p;
178         struct compound_depend *cdep;
179         abstract_pkg_t **dependents;
180
181         int count = pkg->pre_depends_count +
182                                 pkg->depends_count +
183                                 pkg->recommends_count +
184                                 pkg->suggests_count;
185
186         for (i=0; i<count; i++) {
187                 cdep = &pkg->depends[i];
188                 if (cdep->type != DEPEND)
189                         continue;
190                 for (j=0; j<cdep->possibility_count; j++) {
191                         p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash,
192                                         cdep->possibilities[j]->pkg->name);
193
194                         /* If the package is not installed, this could have
195                          * been a circular dependency and the package has
196                          * already been removed.
197                          */
198                         if (!p)
199                                 return -1;
200
201                         if (!p->auto_installed)
202                                 continue;
203
204                         n_deps = pkg_has_installed_dependents(conf, NULL, p,
205                                         &dependents);
206                         if (n_deps == 0) {
207                                  opkg_message(conf, OPKG_NOTICE,
208                                                "%s was autoinstalled and is "
209                                                "now orphaned, removing\n",
210                                                p->name);
211                                  opkg_remove_pkg(conf, p, 0);
212                         } else
213                                 opkg_message(conf, OPKG_INFO,
214                                                 "%s was autoinstalled and is "
215                                                 "still required by %d "
216                                                 "installed packages.\n",
217                                                 p->name, n_deps);
218
219                         if (dependents)
220                                 free(dependents);
221                 }
222         }
223
224         return 0;
225 }
226
227 int
228 opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
229 {
230      int err;
231      abstract_pkg_t *parent_pkg = NULL;
232
233 /*
234  * If called from an upgrade and not from a normal remove,
235  * ignore the essential flag.
236  */
237      if (pkg->essential && !from_upgrade) {
238           if (conf->force_removal_of_essential_packages) {
239                fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
240                        "\tIf your system breaks, you get to keep both pieces\n",
241                        pkg->name);
242           } else {
243                fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
244                        "\tRemoving an essential package may lead to an unusable system, but if\n"
245                        "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
246                        "\tits will with the option: --force-removal-of-essential-packages\n",
247                        pkg->name);
248                return OPKG_PKG_IS_ESSENTIAL;
249           }
250      }
251
252      if ((parent_pkg = pkg->parent) == NULL)
253           return 0;
254
255      /* only attempt to remove dependent installed packages if
256       * force_depends is not specified or the package is being
257       * replaced.
258       */
259      if (!conf->force_depends
260          && !(pkg->state_flag & SF_REPLACE)) {
261           abstract_pkg_t **dependents;
262           int has_installed_dependents = 
263                pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
264
265           if (has_installed_dependents) {
266                /*
267                 * if this package is depended upon by others, then either we should
268                 * not remove it or we should remove it and all of its dependents 
269                 */
270
271                if (!conf->force_removal_of_dependent_packages) {
272                     print_dependents_warning(conf, parent_pkg, pkg, dependents);
273                     free(dependents);
274                     return OPKG_PKG_HAS_DEPENDENTS;
275                }
276
277                /* remove packages depending on this package - Karthik */
278                err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
279                if (err) {
280                  free(dependents);
281                  return err;
282                }
283           }
284           if (dependents)
285               free(dependents);
286      }
287
288      if (from_upgrade == 0) {
289          opkg_message (conf, OPKG_NOTICE,
290                        "Removing package %s from %s...\n", pkg->name, pkg->dest->name);
291      }
292      pkg->state_flag |= SF_FILELIST_CHANGED;
293
294      pkg->state_want = SW_DEINSTALL;
295      opkg_state_changed++;
296
297      pkg_run_script(conf, pkg, "prerm", "remove");
298
299      /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
300         maintains an empty filelist rather than deleting it. That seems
301         like a big pain, and I don't see that that should make a big
302         difference, but for anyone who wants tighter compatibility,
303         feel free to fix this. */
304      remove_data_files_and_list(conf, pkg);
305
306      pkg_run_script(conf, pkg, "postrm", "remove");
307
308      remove_maintainer_scripts(conf, pkg);
309      pkg->state_status = SS_NOT_INSTALLED;
310
311      if (parent_pkg) 
312           parent_pkg->state_status = SS_NOT_INSTALLED;
313
314      /* remove autoinstalled packages that are orphaned by the removal of this one */
315      if (conf->autoremove)
316        remove_autoinstalled (conf, pkg);
317
318      return 0;
319 }
320
321 void
322 remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
323 {
324      str_list_t installed_dirs;
325      str_list_t *installed_files;
326      str_list_elt_t *iter;
327      char *file_name;
328      conffile_t *conffile;
329      int removed_a_dir;
330      pkg_t *owner;
331      int rootdirlen = 0;
332
333      str_list_init(&installed_dirs);
334      installed_files = pkg_get_installed_files(conf, pkg);
335
336      /* don't include trailing slash */
337      if (conf->offline_root)
338           rootdirlen = strlen(conf->offline_root);
339
340      for (iter = str_list_first(installed_files); iter; iter = str_list_next(installed_files, iter)) {
341           file_name = (char *)iter->data;
342
343           if (file_is_dir(file_name)) {
344                str_list_append(&installed_dirs, file_name);
345                continue;
346           }
347
348           conffile = pkg_get_conffile(pkg, file_name+rootdirlen);
349           if (conffile) {
350                if (conffile_has_been_modified(conf, conffile)) {
351                     opkg_message (conf, OPKG_NOTICE,
352                                   "  not deleting modified conffile %s\n", file_name);
353                     continue;
354                }
355           }
356
357           opkg_message(conf, OPKG_INFO, "  deleting %s (noaction=%d)\n", file_name, conf->noaction);
358           if (!conf->noaction)
359                unlink(file_name);
360      }
361
362      /* Remove empty directories */
363      if (!conf->noaction) {
364           do {
365                removed_a_dir = 0;
366                for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
367                     file_name = (char *)iter->data;
368             
369                     if (rmdir(file_name) == 0) {
370                          opkg_message(conf, OPKG_INFO, "  deleting %s\n", file_name);
371                          removed_a_dir = 1;
372                          str_list_remove(&installed_dirs, &iter);
373                     }
374                }
375           } while (removed_a_dir);
376      }
377
378      pkg_free_installed_files(pkg);
379      pkg_remove_installed_files_list(conf, pkg);
380
381      /* Don't print warning for dirs that are provided by other packages */
382      for (iter = str_list_first(&installed_dirs); iter; iter = str_list_next(&installed_dirs, iter)) {
383           file_name = (char *)iter->data;
384
385           owner = file_hash_get_file_owner(conf, file_name);
386           if (owner) {
387                free(iter->data);
388                iter->data = NULL;
389                str_list_remove(&installed_dirs, &iter);
390           }
391      }
392
393      /* cleanup */
394      while (!void_list_empty(&installed_dirs)) {
395         iter = str_list_pop(&installed_dirs);
396         free(iter->data);
397         free(iter);
398      }
399      str_list_deinit(&installed_dirs);
400 }
401
402 void
403 remove_maintainer_scripts(opkg_conf_t *conf, pkg_t *pkg)
404 {
405         int i, err;
406         char *globpattern;
407         glob_t globbuf;
408
409         if (conf->noaction)
410                 return;
411
412         sprintf_alloc(&globpattern, "%s/%s.*",
413                         pkg->dest->info_dir, pkg->name);
414
415         err = glob(globpattern, 0, NULL, &globbuf);
416         free(globpattern);
417         if (err)
418                 return;
419
420         for (i = 0; i < globbuf.gl_pathc; i++) {
421                 opkg_message(conf, OPKG_INFO, "deleting %s\n",
422                                 globbuf.gl_pathv[i]);
423                 unlink(globbuf.gl_pathv[i]);
424         }
425         globfree(&globbuf);
426 }