opkg_remove.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* opkg_remove.c - the opkg package management system
  2. Carl D. Worth
  3. Copyright (C) 2001 University of Southern California
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. */
  13. #include <stdio.h>
  14. #include <glob.h>
  15. #include <unistd.h>
  16. #include "opkg_message.h"
  17. #include "opkg_remove.h"
  18. #include "opkg_cmd.h"
  19. #include "file_util.h"
  20. #include "sprintf_alloc.h"
  21. #include "libbb/libbb.h"
  22. /*
  23. * Returns number of the number of packages depending on the packages provided by this package.
  24. * Every package implicitly provides itself.
  25. */
  26. int pkg_has_installed_dependents(pkg_t * pkg, abstract_pkg_t *** pdependents)
  27. {
  28. abstract_pkg_t **provider, **provides = pkg_get_ptr(pkg, PKG_PROVIDES);
  29. unsigned int i, n_installed_dependents = 0;
  30. provider = provides;
  31. while (provider && *provider) {
  32. abstract_pkg_t *providee = *provider++;
  33. abstract_pkg_t **dependers = providee->depended_upon_by;
  34. abstract_pkg_t *dep_ab_pkg;
  35. if (dependers == NULL)
  36. continue;
  37. while ((dep_ab_pkg = *dependers++) != NULL) {
  38. if (dep_ab_pkg->state_status == SS_INSTALLED) {
  39. n_installed_dependents++;
  40. }
  41. }
  42. }
  43. /* if caller requested the set of installed dependents */
  44. if (pdependents) {
  45. int p = 0;
  46. abstract_pkg_t **dependents =
  47. xcalloc((n_installed_dependents + 1),
  48. sizeof(abstract_pkg_t *));
  49. provider = provides;
  50. *pdependents = dependents;
  51. while (provider && *provider) {
  52. abstract_pkg_t *providee = *provider++;
  53. abstract_pkg_t **dependers = providee->depended_upon_by;
  54. abstract_pkg_t *dep_ab_pkg;
  55. if (dependers == NULL)
  56. continue;
  57. while ((dep_ab_pkg = *dependers++) != NULL) {
  58. if (dep_ab_pkg->state_status == SS_INSTALLED
  59. && !(dep_ab_pkg->state_flag & SF_MARKED)) {
  60. dependents[p++] = dep_ab_pkg;
  61. dep_ab_pkg->state_flag |= SF_MARKED;
  62. }
  63. }
  64. }
  65. dependents[p] = NULL;
  66. /* now clear the marks */
  67. for (i = 0; i < p; i++) {
  68. abstract_pkg_t *dep_ab_pkg = dependents[i];
  69. dep_ab_pkg->state_flag &= ~SF_MARKED;
  70. }
  71. }
  72. return n_installed_dependents;
  73. }
  74. static int opkg_remove_dependent_pkgs(pkg_t * pkg, abstract_pkg_t ** dependents)
  75. {
  76. int i;
  77. int a;
  78. int count;
  79. pkg_vec_t *dependent_pkgs;
  80. abstract_pkg_t *ab_pkg;
  81. if ((ab_pkg = pkg->parent) == NULL) {
  82. opkg_msg(ERROR, "Internal error: pkg %s isn't in hash table\n",
  83. pkg->name);
  84. return 0;
  85. }
  86. if (dependents == NULL)
  87. return 0;
  88. // here i am using the dependencies_checked
  89. if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
  90. return 0; // has already been encountered in the process
  91. // of marking packages for removal - Karthik
  92. ab_pkg->dependencies_checked = 2;
  93. i = 0;
  94. count = 1;
  95. dependent_pkgs = pkg_vec_alloc();
  96. while (dependents[i] != NULL) {
  97. abstract_pkg_t *dep_ab_pkg = dependents[i];
  98. if (dep_ab_pkg->dependencies_checked == 2) {
  99. i++;
  100. continue;
  101. }
  102. if (dep_ab_pkg->state_status == SS_INSTALLED) {
  103. for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
  104. pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
  105. if (dep_pkg->state_status == SS_INSTALLED) {
  106. pkg_vec_insert(dependent_pkgs, dep_pkg);
  107. count++;
  108. }
  109. }
  110. }
  111. i++;
  112. /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
  113. * 2 - to keep track of pkgs whose deps have been checked alrdy - Karthik */
  114. }
  115. if (count == 1) {
  116. pkg_vec_free(dependent_pkgs);
  117. return 0;
  118. }
  119. int err = 0;
  120. for (i = 0; i < dependent_pkgs->len; i++) {
  121. err = opkg_remove_pkg(dependent_pkgs->pkgs[i], 0);
  122. if (err) {
  123. break;
  124. }
  125. }
  126. pkg_vec_free(dependent_pkgs);
  127. return err;
  128. }
  129. static void print_dependents_warning(pkg_t * pkg, abstract_pkg_t ** dependents)
  130. {
  131. abstract_pkg_t *dep_ab_pkg;
  132. opkg_msg(ERROR, "Package %s is depended upon by packages:\n",
  133. pkg->name);
  134. while ((dep_ab_pkg = *dependents++) != NULL) {
  135. if (dep_ab_pkg->state_status == SS_INSTALLED)
  136. opkg_msg(ERROR, "\t%s\n", dep_ab_pkg->name);
  137. }
  138. opkg_msg(ERROR,
  139. "These might cease to work if package %s is removed.\n\n",
  140. pkg->name);
  141. opkg_msg(ERROR,
  142. "Force removal of this package with --force-depends.\n");
  143. opkg_msg(ERROR, "Force removal of this package and its dependents\n");
  144. opkg_msg(ERROR, "with --force-removal-of-dependent-packages.\n");
  145. }
  146. /*
  147. * Find and remove packages that were autoinstalled and are orphaned
  148. * by the removal of pkg.
  149. */
  150. static int remove_autoinstalled(pkg_t * pkg)
  151. {
  152. int j;
  153. int err = 0;
  154. int n_deps;
  155. pkg_t *p;
  156. struct compound_depend *cdep;
  157. abstract_pkg_t **dependents;
  158. for (cdep = pkg_get_ptr(pkg, PKG_DEPENDS); cdep && cdep->type; cdep++) {
  159. if (cdep->type != PREDEPEND
  160. && cdep->type != DEPEND && cdep->type != RECOMMEND)
  161. continue;
  162. for (j = 0; j < cdep->possibility_count; j++) {
  163. p = pkg_hash_fetch_installed_by_name(cdep->
  164. possibilities[j]->
  165. pkg->name);
  166. /* If the package is not installed, this could have
  167. * been a circular dependency and the package has
  168. * already been removed.
  169. */
  170. if (!p)
  171. return -1;
  172. if (!p->auto_installed)
  173. continue;
  174. n_deps = pkg_has_installed_dependents(p, &dependents);
  175. if (n_deps == 0) {
  176. opkg_msg(NOTICE, "%s was autoinstalled and is "
  177. "now orphaned, removing.\n", p->name);
  178. if (opkg_remove_pkg(p, 0) != 0) {
  179. err = -1;
  180. }
  181. } else
  182. opkg_msg(INFO, "%s was autoinstalled and is "
  183. "still required by %d "
  184. "installed packages.\n",
  185. p->name, n_deps);
  186. if (dependents)
  187. free(dependents);
  188. }
  189. }
  190. return err;
  191. }
  192. int opkg_remove_pkg(pkg_t * pkg, int from_upgrade)
  193. {
  194. int err;
  195. abstract_pkg_t *parent_pkg = NULL;
  196. /*
  197. * If called from an upgrade and not from a normal remove,
  198. * ignore the essential flag.
  199. */
  200. if (pkg->essential && !from_upgrade) {
  201. if (conf->force_removal_of_essential_packages) {
  202. opkg_msg(NOTICE,
  203. "Removing essential package %s under your coercion.\n"
  204. "\tIf your system breaks, you get to keep both pieces\n",
  205. pkg->name);
  206. } else {
  207. opkg_msg(NOTICE,
  208. "Refusing to remove essential package %s.\n"
  209. "\tRemoving an essential package may lead to an unusable system, but if\n"
  210. "\tyou enjoy that kind of pain, you can force opkg to proceed against\n"
  211. "\tits will with the option: --force-removal-of-essential-packages\n",
  212. pkg->name);
  213. return -1;
  214. }
  215. }
  216. if ((parent_pkg = pkg->parent) == NULL)
  217. return 0;
  218. /* only attempt to remove dependent installed packages if
  219. * force_depends is not specified or the package is being
  220. * replaced.
  221. */
  222. if (!conf->force_depends && !(pkg->state_flag & SF_REPLACE)) {
  223. abstract_pkg_t **dependents;
  224. int has_installed_dependents =
  225. pkg_has_installed_dependents(pkg, &dependents);
  226. if (has_installed_dependents) {
  227. /*
  228. * if this package is depended upon by others, then either we should
  229. * not remove it or we should remove it and all of its dependents
  230. */
  231. if (!conf->force_removal_of_dependent_packages) {
  232. print_dependents_warning(pkg, dependents);
  233. free(dependents);
  234. return -1;
  235. }
  236. /* remove packages depending on this package - Karthik */
  237. err = opkg_remove_dependent_pkgs(pkg, dependents);
  238. if (err) {
  239. free(dependents);
  240. return err;
  241. }
  242. }
  243. if (dependents)
  244. free(dependents);
  245. }
  246. if (from_upgrade == 0) {
  247. opkg_msg(NOTICE, "Removing package %s from %s...\n",
  248. pkg->name, pkg->dest->name);
  249. }
  250. pkg->state_flag |= SF_FILELIST_CHANGED;
  251. pkg->state_want = SW_DEINSTALL;
  252. opkg_state_changed++;
  253. if (pkg_run_script(pkg, "prerm", "remove") != 0) {
  254. if (!conf->force_remove) {
  255. opkg_msg(ERROR, "not removing package \"%s\", "
  256. "prerm script failed\n", pkg->name);
  257. opkg_msg(NOTICE,
  258. "You can force removal of packages with failed "
  259. "prerm scripts with the option: \n"
  260. "\t--force-remove\n");
  261. return -1;
  262. }
  263. }
  264. /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
  265. maintains an empty filelist rather than deleting it. That seems
  266. like a big pain, and I don't see that that should make a big
  267. difference, but for anyone who wants tighter compatibility,
  268. feel free to fix this. */
  269. remove_data_files_and_list(pkg);
  270. err = pkg_run_script(pkg, "postrm", "remove");
  271. remove_maintainer_scripts(pkg);
  272. pkg->state_status = SS_NOT_INSTALLED;
  273. if (parent_pkg)
  274. parent_pkg->state_status = SS_NOT_INSTALLED;
  275. /* remove autoinstalled packages that are orphaned by the removal of this one */
  276. if (conf->autoremove) {
  277. if (remove_autoinstalled(pkg) != 0) {
  278. err = -1;
  279. }
  280. }
  281. return err;
  282. }
  283. void remove_data_files_and_list(pkg_t * pkg)
  284. {
  285. str_list_t installed_dirs;
  286. str_list_t *installed_files;
  287. str_list_elt_t *iter;
  288. char *file_name;
  289. conffile_t *conffile;
  290. int removed_a_dir;
  291. pkg_t *owner;
  292. int rootdirlen = 0;
  293. installed_files = pkg_get_installed_files(pkg);
  294. if (installed_files == NULL) {
  295. opkg_msg(ERROR, "Failed to determine installed "
  296. "files for %s. None removed.\n", pkg->name);
  297. return;
  298. }
  299. str_list_init(&installed_dirs);
  300. /* don't include trailing slash */
  301. if (conf->offline_root)
  302. rootdirlen = strlen(conf->offline_root);
  303. for (iter = str_list_first(installed_files); iter;
  304. iter = str_list_next(installed_files, iter)) {
  305. file_name = (char *)iter->data;
  306. owner = file_hash_get_file_owner(file_name);
  307. if (owner != pkg)
  308. /* File may have been claimed by another package. */
  309. continue;
  310. if (file_is_dir(file_name)) {
  311. str_list_append(&installed_dirs, file_name);
  312. continue;
  313. }
  314. conffile = pkg_get_conffile(pkg, file_name + rootdirlen);
  315. if (conffile) {
  316. if (conffile_has_been_modified(conffile)) {
  317. opkg_msg(NOTICE,
  318. "Not deleting modified conffile %s.\n",
  319. file_name);
  320. continue;
  321. }
  322. }
  323. if (!conf->noaction) {
  324. opkg_msg(INFO, "Deleting %s.\n", file_name);
  325. unlink(file_name);
  326. } else
  327. opkg_msg(INFO, "Not deleting %s. (noaction)\n",
  328. file_name);
  329. file_hash_remove(file_name);
  330. }
  331. /* Remove empty directories */
  332. if (!conf->noaction) {
  333. do {
  334. removed_a_dir = 0;
  335. for (iter = str_list_first(&installed_dirs); iter;
  336. iter = str_list_next(&installed_dirs, iter)) {
  337. file_name = (char *)iter->data;
  338. if (rmdir(file_name) == 0) {
  339. opkg_msg(INFO, "Deleting %s.\n",
  340. file_name);
  341. removed_a_dir = 1;
  342. str_list_remove(&installed_dirs, &iter);
  343. }
  344. }
  345. } while (removed_a_dir);
  346. }
  347. pkg_free_installed_files(pkg);
  348. pkg_remove_installed_files_list(pkg);
  349. /* Don't print warning for dirs that are provided by other packages */
  350. for (iter = str_list_first(&installed_dirs); iter;
  351. iter = str_list_next(&installed_dirs, iter)) {
  352. file_name = (char *)iter->data;
  353. owner = file_hash_get_file_owner(file_name);
  354. if (owner) {
  355. free(iter->data);
  356. iter->data = NULL;
  357. str_list_remove(&installed_dirs, &iter);
  358. }
  359. }
  360. /* cleanup */
  361. while (!void_list_empty(&installed_dirs)) {
  362. iter = str_list_pop(&installed_dirs);
  363. free(iter->data);
  364. free(iter);
  365. }
  366. str_list_deinit(&installed_dirs);
  367. }
  368. void remove_maintainer_scripts(pkg_t * pkg)
  369. {
  370. int i, err;
  371. char *globpattern;
  372. glob_t globbuf;
  373. if (conf->noaction)
  374. return;
  375. sprintf_alloc(&globpattern, "%s/%s.*", pkg->dest->info_dir, pkg->name);
  376. err = glob(globpattern, 0, NULL, &globbuf);
  377. free(globpattern);
  378. if (err)
  379. return;
  380. for (i = 0; i < globbuf.gl_pathc; i++) {
  381. opkg_msg(INFO, "Deleting %s.\n", globbuf.gl_pathv[i]);
  382. unlink(globbuf.gl_pathv[i]);
  383. }
  384. globfree(&globbuf);
  385. }