remove.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * dpkg - main program for package management
  3. * remove.c - functionality for removing packages
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This 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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <dirent.h>
  29. #include <ctype.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <assert.h>
  33. #include "config.h"
  34. #include "dpkg.h"
  35. #include "dpkg-db.h"
  36. #include "myopt.h"
  37. #include "filesdb.h"
  38. #include "main.h"
  39. static void checkforremoval(struct pkginfo *pkgtoremove,
  40. struct pkginfo *pkgdepcheck, /* may be virtual pkg */
  41. int *rokp, struct varbuf *raemsgs) {
  42. struct deppossi *possi;
  43. struct pkginfo *depender;
  44. int before, ok;
  45. for (possi= pkgdepcheck->installed.depended; possi; possi= possi->nextrev) {
  46. if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
  47. depender= possi->up->up;
  48. debug(dbg_depcon,"checking depending package `%s'",depender->name);
  49. if (depender->status != stat_installed) continue;
  50. if (ignore_depends(depender)) {
  51. debug(dbg_depcon,"ignoring depending package `%s'\n",depender->name);
  52. continue;
  53. }
  54. if (dependtry > 1) { if (findbreakcycle(pkgdepcheck,0)) sincenothing= 0; }
  55. before= raemsgs->used;
  56. ok= dependencies_ok(depender,pkgtoremove,raemsgs);
  57. if (ok == 0 && depender->clientdata->istobe == itb_remove) ok= 1;
  58. if (ok == 1) raemsgs->used= before; /* Don't burble about reasons for deferral */
  59. if (ok < *rokp) *rokp= ok;
  60. }
  61. }
  62. void deferred_remove(struct pkginfo *pkg) {
  63. struct varbuf raemsgs;
  64. int rok;
  65. struct dependency *dep;
  66. debug(dbg_general,"deferred_remove package %s",pkg->name);
  67. if (pkg->status == stat_notinstalled) {
  68. fprintf(stderr, DPKG
  69. " - warning: ignoring request to remove %.250s which isn't installed.\n",
  70. pkg->name);
  71. pkg->clientdata->istobe= itb_normal;
  72. return;
  73. } else if (!f_pending &&
  74. pkg->status == stat_configfiles &&
  75. cipaction->arg != act_purge) {
  76. fprintf(stderr, DPKG
  77. " - warning: ignoring request to remove %.250s, only the config\n"
  78. " files of which are on the system. Use --purge to remove them too.\n",
  79. pkg->name);
  80. pkg->clientdata->istobe= itb_normal;
  81. return;
  82. }
  83. assert(pkg->installed.valid);
  84. if (pkg->installed.essential && pkg->status != stat_configfiles)
  85. forcibleerr(fc_removeessential, "This is an essential package -"
  86. " it should not be removed.");
  87. if (!f_pending)
  88. pkg->want= (cipaction->arg == act_purge) ? want_purge : want_deinstall;
  89. if (!f_noact) modstatdb_note(pkg);
  90. debug(dbg_general,"checking dependencies for remove `%s'",pkg->name);
  91. varbufinit(&raemsgs);
  92. rok= 2;
  93. checkforremoval(pkg,pkg,&rok,&raemsgs);
  94. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  95. if (dep->type != dep_provides) continue;
  96. debug(dbg_depcon,"checking virtual package `%s'",dep->list->ed->name);
  97. checkforremoval(pkg,dep->list->ed,&rok,&raemsgs);
  98. }
  99. if (rok == 1) {
  100. varbuffree(&raemsgs);
  101. pkg->clientdata->istobe= itb_remove;
  102. add_to_queue(pkg);
  103. return;
  104. } else if (rok == 0) {
  105. sincenothing= 0;
  106. varbufaddc(&raemsgs,0);
  107. fprintf(stderr,
  108. DPKG ": dependency problems prevent removal of %s:\n%s",
  109. pkg->name, raemsgs.buf);
  110. ohshit("dependency problems - not removing");
  111. } else if (raemsgs.used) {
  112. varbufaddc(&raemsgs,0);
  113. fprintf(stderr,
  114. DPKG ": %s: dependency problems, but removing anyway as you request:\n%s",
  115. pkg->name, raemsgs.buf);
  116. }
  117. varbuffree(&raemsgs);
  118. sincenothing= 0;
  119. if (pkg->eflag & eflagf_reinstreq)
  120. forcibleerr(fc_removereinstreq,
  121. "Package is in a very bad inconsistent state - you should\n"
  122. " reinstall it before attempting a removal.");
  123. ensure_allinstfiles_available();
  124. filesdbinit();
  125. if (f_noact) {
  126. printf("Would remove or purge %s ...\n",pkg->name);
  127. pkg->status= stat_notinstalled;
  128. pkg->clientdata->istobe= itb_normal;
  129. return;
  130. }
  131. oldconffsetflags(pkg->installed.conffiles);
  132. printf("Removing %s ...\n",pkg->name);
  133. if (pkg->status == stat_halfconfigured || pkg->status == stat_installed) {
  134. if (pkg->status == stat_installed || pkg->status == stat_halfconfigured) {
  135. pkg->status= stat_halfconfigured;
  136. modstatdb_note(pkg);
  137. push_cleanup(cu_prermremove,~ehflag_normaltidy, 0,0, 1,(void*)pkg);
  138. maintainer_script_installed(pkg, PRERMFILE, "pre-removal",
  139. "remove", (char*)0);
  140. }
  141. pkg->status= stat_unpacked; /* Will turn into halfinstalled soon ... */
  142. }
  143. removal_bulk(pkg);
  144. }
  145. static void push_leftover(struct fileinlist **leftoverp,
  146. struct filenamenode *namenode) {
  147. struct fileinlist *newentry;
  148. newentry= nfmalloc(sizeof(struct fileinlist));
  149. newentry->next= *leftoverp;
  150. newentry->namenode= namenode;
  151. *leftoverp= newentry;
  152. }
  153. void removal_bulk(struct pkginfo *pkg) {
  154. /* This is used both by deferred_remove in this file, and at
  155. * the end of process_archive in archives.c if it needs to finish
  156. * removing a conflicting package.
  157. */
  158. static const char *const removeconffexts[]= { REMOVECONFFEXTS, 0 };
  159. static struct varbuf fnvb, removevb;
  160. int before, r, foundpostrm, removevbbase;
  161. int infodirbaseused, conffnameused, conffbasenamelen, pkgnameused;
  162. char *conffbasename;
  163. struct reversefilelistiter rlistit;
  164. struct conffile *conff, **lconffp;
  165. struct fileinlist *searchfile, *leftover;
  166. struct stat stab;
  167. DIR *dsd;
  168. struct dirent *de;
  169. char *p;
  170. const char *const *ext;
  171. const char *postrmfilename;
  172. struct filenamenode *namenode;
  173. debug(dbg_general,"removal_bulk package %s",pkg->name);
  174. if (pkg->status == stat_halfinstalled || pkg->status == stat_unpacked) {
  175. pkg->status= stat_halfinstalled;
  176. modstatdb_note(pkg);
  177. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  178. reversefilelist_init(&rlistit,pkg->clientdata->files);
  179. leftover= 0;
  180. while ((namenode= reversefilelist_next(&rlistit))) {
  181. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  182. namenode->name, namenode->flags);
  183. if (namenode->flags & fnnf_old_conff) {
  184. push_leftover(&leftover,namenode);
  185. continue;
  186. }
  187. varbufreset(&fnvb);
  188. varbufaddstr(&fnvb,instdir);
  189. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  190. before= fnvb.used;
  191. varbufaddstr(&fnvb,DPKGTEMPEXT);
  192. varbufaddc(&fnvb,0);
  193. debug(dbg_eachfiledetail, "removal_bulk cleaning temp `%s'", fnvb.buf);
  194. ensure_pathname_nonexisting(fnvb.buf);
  195. fnvb.used= before;
  196. varbufaddstr(&fnvb,DPKGNEWEXT);
  197. varbufaddc(&fnvb,0);
  198. debug(dbg_eachfiledetail, "removal_bulk cleaning new `%s'", fnvb.buf);
  199. ensure_pathname_nonexisting(fnvb.buf);
  200. fnvb.used= before;
  201. varbufaddc(&fnvb,0);
  202. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  203. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  204. /* Only delete a directory or a link to one if we're the only
  205. * package which uses it. Other files should only be listed
  206. * in this package (but we don't check).
  207. */
  208. if (isdirectoryinuse(namenode,pkg)) continue;
  209. }
  210. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  211. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  212. if (errno == ENOTEMPTY) {
  213. fprintf(stderr, DPKG
  214. " - warning: while removing %.250s, directory `%.250s' not empty "
  215. "so not removed.\n",
  216. pkg->name, namenode->name);
  217. push_leftover(&leftover,namenode);
  218. continue;
  219. } else if (errno == EPERM) {
  220. fprintf(stderr, DPKG " - warning: while removing %.250s,"
  221. " unable to remove directory `%.250s':"
  222. " Operation not permitted - directory may be a mount point ?\n",
  223. pkg->name, namenode->name);
  224. push_leftover(&leftover,namenode);
  225. continue;
  226. }
  227. if (errno != ENOTDIR) ohshite("cannot remove `%.250s'",fnvb.buf);
  228. debug(dbg_eachfiledetail, "removal_bulk unlinking `%s'", fnvb.buf);
  229. if (unlink(fnvb.buf)) ohshite("cannot remove file `%.250s'",fnvb.buf);
  230. }
  231. write_filelist_except(pkg,leftover,0);
  232. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  233. "remove", (char*)0);
  234. varbufreset(&fnvb);
  235. varbufaddstr(&fnvb,admindir);
  236. varbufaddstr(&fnvb,"/" INFODIR);
  237. infodirbaseused= fnvb.used;
  238. varbufaddc(&fnvb,0);
  239. dsd= opendir(fnvb.buf); if (!dsd) ohshite("cannot read info directory");
  240. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  241. foundpostrm= 0;
  242. debug(dbg_general, "removal_bulk cleaning info directory");
  243. while ((de= readdir(dsd)) != 0) {
  244. debug(dbg_veryverbose, "removal_bulk info file `%s'", de->d_name);
  245. if (de->d_name[0] == '.') continue;
  246. p= strrchr(de->d_name,'.'); if (!p) continue;
  247. if (strlen(pkg->name) != p-de->d_name ||
  248. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  249. debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
  250. /* We need the postrm and list files for --purge. */
  251. if (!strcmp(p+1,LISTFILE)) continue;
  252. if (!strcmp(p+1,POSTRMFILE)) { foundpostrm=1; continue; }
  253. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  254. fnvb.used= infodirbaseused;
  255. varbufaddstr(&fnvb,de->d_name);
  256. varbufaddc(&fnvb,0);
  257. if (unlink(fnvb.buf))
  258. ohshite("unable to delete control info file `%.250s'",fnvb.buf);
  259. debug(dbg_scripts, "removal_bulk info unlinked %s",fnvb.buf);
  260. }
  261. pop_cleanup(ehflag_normaltidy); /* closedir */
  262. pkg->status= stat_configfiles;
  263. pkg->installed.essential= 0;
  264. modstatdb_note(pkg);
  265. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  266. } else {
  267. postrmfilename= pkgadminfile(pkg,POSTRMFILE);
  268. if (!lstat(postrmfilename,&stab)) foundpostrm= 1;
  269. else if (errno == ENOENT) foundpostrm= 0;
  270. else ohshite("unable to check existence of `%.250s'",postrmfilename);
  271. }
  272. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  273. if (!foundpostrm && !pkg->installed.conffiles) {
  274. /* If there are no config files and no postrm script then we
  275. * go straight into `purge'.
  276. */
  277. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  278. pkg->want= want_purge;
  279. } else if (pkg->want == want_purge) {
  280. printf("Purging configuration files for %s ...\n",pkg->name);
  281. ensure_packagefiles_available(pkg); /* We may have modified this above. */
  282. /* We're about to remove the configuration, so remove the note
  283. * about which version it was ...
  284. */
  285. blankversion(&pkg->configversion);
  286. modstatdb_note(pkg);
  287. /* Remove from our list any conffiles that aren't ours any more or
  288. * are involved in diversions, except if we are the package doing the
  289. * diverting.
  290. */
  291. for (lconffp= &pkg->installed.conffiles;
  292. (conff= *lconffp) != 0;
  293. lconffp= &conff->next) {
  294. for (searchfile= pkg->clientdata->files;
  295. searchfile && strcmp(searchfile->namenode->name,conff->name);
  296. searchfile= searchfile->next);
  297. if (!searchfile) {
  298. debug(dbg_conff,"removal_bulk conffile not ours any more `%s'",conff->name);
  299. *lconffp= conff->next;
  300. } else if (searchfile->namenode->divert &&
  301. (searchfile->namenode->divert->camefrom ||
  302. (searchfile->namenode->divert->useinstead &&
  303. searchfile->namenode->divert->pkg != pkg))) {
  304. debug(dbg_conff,"removal_bulk conffile `%s' ignored due to diversion\n",
  305. conff->name);
  306. *lconffp= conff->next;
  307. } else {
  308. debug(dbg_conffdetail,"removal_bulk set to new conffile `%s'",conff->name);
  309. conff->hash= (char*)NEWCONFFILEFLAG; /* yes, cast away const */
  310. }
  311. }
  312. modstatdb_note(pkg);
  313. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  314. varbufreset(&fnvb);
  315. r= conffderef(pkg, &fnvb, conff->name);
  316. debug(dbg_conffdetail, "removal_bulk conffile `%s' (= `%s')",
  317. conff->name, r == -1 ? "<r==-1>" : fnvb.buf);
  318. if (r == -1) continue;
  319. conffnameused= fnvb.used-1;
  320. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  321. ohshite("cannot remove old config file `%.250s' (= `%.250s')",
  322. conff->name, fnvb.buf);
  323. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  324. *p= 0;
  325. varbufreset(&removevb);
  326. varbufaddstr(&removevb,fnvb.buf);
  327. varbufaddc(&removevb,'/');
  328. removevbbase= removevb.used;
  329. varbufaddc(&removevb,0);
  330. dsd= opendir(removevb.buf);
  331. if (!dsd) {
  332. int e=errno;
  333. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  334. fnvb.buf, strerror(e)); errno= e;
  335. if (errno == ENOENT || errno == ENOTDIR) continue;
  336. ohshite("cannot read config file dir `%.250s' (from `%.250s')",
  337. fnvb.buf, conff->name);
  338. }
  339. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  340. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  341. *p= '/';
  342. conffbasenamelen= strlen(++p);
  343. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  344. while ((de= readdir(dsd)) != 0) {
  345. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry=`%s'"
  346. " conffbasename=`%s' conffnameused=%d conffbasenamelen=%d",
  347. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  348. if (!strncmp(de->d_name,conffbasename,conffbasenamelen)) {
  349. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  350. for (ext= removeconffexts; *ext; ext++)
  351. if (!strcmp(*ext,de->d_name+conffbasenamelen)) goto yes_remove;
  352. p= de->d_name+conffbasenamelen;
  353. if (*p++ == '~') {
  354. while (*p && isdigit(*p)) p++;
  355. if (*p == '~' && !*++p) goto yes_remove;
  356. }
  357. }
  358. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  359. if (de->d_name[0] == '#' &&
  360. !strncmp(de->d_name+1,conffbasename,conffbasenamelen) &&
  361. !strcmp(de->d_name+1+conffbasenamelen,"#"))
  362. goto yes_remove;
  363. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  364. continue;
  365. yes_remove:
  366. removevb.used= removevbbase;
  367. varbufaddstr(&removevb,de->d_name); varbufaddc(&removevb,0);
  368. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing `%s'",
  369. removevb.buf);
  370. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  371. ohshite("cannot remove old backup config file `%.250s' (of `%.250s')",
  372. removevb.buf, conff->name);
  373. }
  374. pop_cleanup(ehflag_normaltidy); /* closedir */
  375. }
  376. pkg->installed.conffiles= 0;
  377. modstatdb_note(pkg);
  378. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  379. "purge", (char*)0);
  380. /* fixme: retry empty directories */
  381. }
  382. if (pkg->want == want_purge) {
  383. /* ie, either of the two branches above. */
  384. varbufreset(&fnvb);
  385. varbufaddstr(&fnvb,admindir);
  386. varbufaddstr(&fnvb,"/" INFODIR);
  387. varbufaddstr(&fnvb,pkg->name);
  388. pkgnameused= fnvb.used;
  389. varbufaddstr(&fnvb,"." LISTFILE);
  390. varbufaddc(&fnvb,0);
  391. debug(dbg_general, "removal_bulk purge done, removing list `%s'",fnvb.buf);
  392. if (unlink(fnvb.buf) && errno != ENOENT) ohshite("cannot remove old files list");
  393. fnvb.used= pkgnameused;
  394. varbufaddstr(&fnvb,"." POSTRMFILE);
  395. varbufaddc(&fnvb,0);
  396. debug(dbg_general, "removal_bulk purge done, removing postrm `%s'",fnvb.buf);
  397. if (unlink(fnvb.buf) && errno != ENOENT) ohshite("can't remove old postrm script");
  398. pkg->status= stat_notinstalled;
  399. /* This will mess up reverse links, but if we follow them
  400. * we won't go back because pkg->status is stat_notinstalled.
  401. */
  402. pkg->installed.depends= 0;
  403. pkg->installed.essential= 0;
  404. pkg->installed.description= pkg->installed.maintainer= 0;
  405. blankversion(&pkg->installed.version);
  406. pkg->installed.arbs= 0;
  407. }
  408. pkg->eflag= eflagv_ok;
  409. modstatdb_note(pkg);
  410. debug(dbg_general, "removal done");
  411. }