libopkg: remove internal dependency on libopkg.h and opkg_cmd.h
[oweals/opkg-lede.git] / libopkg / opkg_remove.c
1 /* opkg_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 "opkg.h"
19 #include "opkg_message.h"
20
21 #include <glob.h>
22
23 #include "opkg_remove.h"
24
25 #include "file_util.h"
26 #include "sprintf_alloc.h"
27 #include "str_util.h"
28
29 /*
30  * Returns number of the number of packages depending on the packages provided by this package.
31  * Every package implicitly provides itself.
32  */
33 int pkg_has_installed_dependents(opkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
34 {
35      int nprovides = pkg->provides_count;
36      abstract_pkg_t **provides = pkg->provides;
37      int n_installed_dependents = 0;
38      int i;
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)
44                continue;
45           while ((dep_ab_pkg = *dependers++) != NULL) {
46                if (dep_ab_pkg->state_status == SS_INSTALLED){
47                     n_installed_dependents++;
48                }
49           }
50
51      }
52      /* if caller requested the set of installed dependents */
53      if (pdependents) {
54           int p = 0;
55           abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
56
57           if ( dependents == NULL ){
58               fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
59               return -1;  
60           }
61
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)
68                     continue;
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;
73                     }
74                }
75           }
76           dependents[p] = NULL;
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;
81           }
82      }
83      return n_installed_dependents;
84 }
85
86 int 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 = pkg_vec_alloc();
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     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             return 0;
134     
135     
136     for (i = 0; i < dependent_pkgs->len; i++) {
137         int err = opkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
138         if (err)
139             return err;
140     }
141     return 0;
142 }
143
144 static int user_prefers_removing_dependents(opkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
145 {
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);
151     }
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");
159     return 0;
160 }
161
162 static int remove_autoinstalled (opkg_conf_t *conf, pkg_t *pkg)
163 {
164   /*
165    * find and remove packages that were autoinstalled and are orphaned by the removal of pkg
166    */
167
168   char *buffer, *d_str;
169   int i;
170
171   for (i = 0; i < pkg->depends_count; ++i)
172   {
173     int x = 0;
174     pkg_t *p;
175     d_str = pkg->depends_str[i];
176     buffer = malloc (strlen (d_str) + 1);
177     if (!buffer)
178     {
179       fprintf(stderr,"%s Unable to allocate memory.\n", __FUNCTION__);
180       return -1;
181     }
182
183     while (d_str[x] != '\0' && d_str[x] != ' ')
184     {
185       buffer[x] = d_str[x];
186       ++x;
187     }
188     buffer[x] = '\0';
189     buffer = realloc (buffer, strlen (buffer) + 1);
190     p = pkg_hash_fetch_installed_by_name (&conf->pkg_hash, buffer);
191
192     /* if the package is not installed, this could have been a circular
193      * depenancy and the package has already been removed */
194     if (!p)
195       return -1;
196
197     if (p->auto_installed)
198     {
199       int deps;
200       abstract_pkg_t **dependents;
201
202       deps = pkg_has_installed_dependents(conf, NULL, p, &dependents);
203       if (deps == 0)
204       {
205          opkg_message (conf, OPKG_INFO,
206                        "%s was autoinstalled but is now orphaned\n", buffer);
207          opkg_remove_pkg(conf, p,0);
208       }
209         else
210            opkg_message (conf, OPKG_INFO, "%s was autoinstalled and is still required by "
211                          "%d installed packages\n", buffer, deps);
212     }
213     free (buffer);
214   }
215
216   return 0;
217 }
218
219 int opkg_remove_pkg(opkg_conf_t *conf, pkg_t *pkg,int message)
220 {
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 :) 
224 */
225      int err;
226      abstract_pkg_t *parent_pkg = NULL;
227
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",
232                        pkg->name);
233           } else {
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",
238                        pkg->name);
239                return OPKG_PKG_IS_ESSENTIAL;
240           }
241      }
242
243      if ((parent_pkg = pkg->parent) == NULL)
244           return 0;
245
246      /* only attempt to remove dependent installed packages if
247       * force_depends is not specified or the package is being
248       * replaced.
249       */
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);
255
256           if (has_installed_dependents) {
257                /*
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 
260                 */
261
262                if (!conf->force_removal_of_dependent_packages
263                    && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
264                     return OPKG_PKG_HAS_DEPENDENTS;
265                }
266
267                /* remove packages depending on this package - Karthik */
268                err = opkg_remove_dependent_pkgs (conf, pkg, dependents);
269                free(dependents);
270                if (err) return err;
271           }
272      }
273
274      if ( message==0 ){
275          opkg_message (conf, OPKG_NOTICE,
276                        "Removing package %s from %s...\n", pkg->name, pkg->dest->name);
277          fflush(stdout);
278      }
279      pkg->state_flag |= SF_FILELIST_CHANGED;
280
281      pkg->state_want = SW_DEINSTALL;
282      opkg_state_changed++;
283
284      pkg_run_script(conf, pkg, "prerm", "remove");
285
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);
292
293      pkg_run_script(conf, pkg, "postrm", "remove");
294
295      remove_maintainer_scripts_except_postrm(conf, pkg);
296
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 :) */
301
302      remove_postrm(conf, pkg);
303      pkg->state_status = SS_NOT_INSTALLED;
304
305      if (parent_pkg) 
306           parent_pkg->state_status = SS_NOT_INSTALLED;
307
308
309      /* remove autoinstalled packages that are orphaned by the removal of this one */
310      if (conf->autoremove)
311        remove_autoinstalled (conf, pkg);
312
313
314
315      return 0;
316 }
317
318 int opkg_purge_pkg(opkg_conf_t *conf, pkg_t *pkg)
319 {
320     opkg_remove_pkg(conf, pkg,0);
321     return 0;
322 }
323
324 int remove_data_files_and_list(opkg_conf_t *conf, pkg_t *pkg)
325 {
326      str_list_t installed_dirs;
327      str_list_t *installed_files;
328      str_list_elt_t *iter;
329      char *file_name;
330      conffile_t *conffile;
331      int removed_a_dir;
332      pkg_t *owner;
333
334      str_list_init(&installed_dirs);
335      installed_files = pkg_get_installed_files(pkg);
336
337      for (iter = installed_files->head; iter; iter = iter->next) {
338           file_name = iter->data;
339
340           if (file_is_dir(file_name)) {
341                str_list_append(&installed_dirs, strdup(file_name));
342                continue;
343           }
344
345           conffile = pkg_get_conffile(pkg, file_name);
346           if (conffile) {
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
351                   space. */
352                if (conffile_has_been_modified(conf, conffile)) {
353                     opkg_message (conf, OPKG_NOTICE,
354                                   "  not deleting modified conffile %s\n", file_name);
355                     fflush(stdout);
356                     continue;
357                }
358           }
359
360           opkg_message(conf, OPKG_INFO, "  deleting %s (noaction=%d)\n", file_name, conf->noaction);
361           if (!conf->noaction)
362                unlink(file_name);
363      }
364
365      if (!conf->noaction) {
366           do {
367                removed_a_dir = 0;
368                for (iter = installed_dirs.head; iter; iter = iter->next) {
369                     file_name = iter->data;
370             
371                     if (rmdir(file_name) == 0) {
372                          opkg_message(conf, OPKG_INFO, "  deleting %s\n", file_name);
373                          removed_a_dir = 1;
374                          str_list_remove(&installed_dirs, &iter);
375                     }
376                }
377           } while (removed_a_dir);
378      }
379
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);
384
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;
388
389           owner = file_hash_get_file_owner(conf, file_name);
390           if (owner) {
391                free(iter->data);
392                iter->data = NULL;
393                str_list_remove(&installed_dirs, &iter);
394           }
395      }
396
397      /* cleanup */
398      for (iter = installed_dirs.head; iter; iter = iter->next) {
399           free(iter->data);
400           iter->data = NULL;
401      }
402      str_list_deinit(&installed_dirs);
403
404      return 0;
405 }
406
407 int remove_maintainer_scripts_except_postrm(opkg_conf_t *conf, pkg_t *pkg)
408 {
409     int i, err;
410     char *globpattern;
411     glob_t globbuf;
412     
413     if (conf->noaction) return 0;
414
415     sprintf_alloc(&globpattern, "%s/%s.*",
416                   pkg->dest->info_dir, pkg->name);
417     err = glob(globpattern, 0, NULL, &globbuf);
418     free(globpattern);
419     if (err) {
420         return 0;
421     }
422
423     for (i = 0; i < globbuf.gl_pathc; i++) {
424         if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
425             continue;
426         }
427         opkg_message(conf, OPKG_INFO, "  deleting %s\n", globbuf.gl_pathv[i]);
428         unlink(globbuf.gl_pathv[i]);
429     }
430     globfree(&globbuf);
431
432     return 0;
433 }
434
435 int remove_postrm(opkg_conf_t *conf, pkg_t *pkg)
436 {
437     char *postrm_file_name;
438
439     if (conf->noaction) return 0;
440
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);
445
446     return 0;
447 }