remove.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* Change: separate removal_bulk handling of halfinstalled or unpacked pkgs
  2. * (ie, remove the real files in the .deb) into its own function.
  3. * Note that "installed" state is converted to unpacked by
  4. * deferred_remove() or halfinstalled by process_archive())
  5. *
  6. * Change: separate purging of configfiles and running of postrm to its
  7. * own function.
  8. *
  9. * Change: retry removing directories after postrm purge is finished, to
  10. * handle directories that had conffiles or config files and so
  11. * forth in them. also only warn about non-empty directories at
  12. * that point.
  13. */
  14. /*
  15. * dpkg - main program for package management
  16. * remove.c - functionality for removing packages
  17. *
  18. * Copyright (C) 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  19. *
  20. * This is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2,
  23. * or (at your option) any later version.
  24. *
  25. * This is distributed in the hope that it will be useful, but
  26. * WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public
  31. * License along with dpkg; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. */
  34. #include <config.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <fcntl.h>
  41. #include <sys/stat.h>
  42. #include <sys/types.h>
  43. #include <dirent.h>
  44. #include <ctype.h>
  45. #include <unistd.h>
  46. #include <string.h>
  47. #include <assert.h>
  48. #include <dpkg.h>
  49. #include <dpkg-db.h>
  50. #include <myopt.h>
  51. #include "filesdb.h"
  52. #include "main.h"
  53. static void checkforremoval(struct pkginfo *pkgtoremove,
  54. struct pkginfo *pkgdepcheck, /* may be virtual pkg */
  55. int *rokp, struct varbuf *raemsgs) {
  56. struct deppossi *possi;
  57. struct pkginfo *depender;
  58. int before, ok;
  59. for (possi= pkgdepcheck->installed.depended; possi; possi= possi->nextrev) {
  60. if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
  61. depender= possi->up->up;
  62. debug(dbg_depcon,"checking depending package `%s'",depender->name);
  63. if (!(depender->status == stat_installed ||
  64. depender->status == stat_triggerspending ||
  65. depender->status == stat_triggersawaited))
  66. continue;
  67. if (ignore_depends(depender)) {
  68. debug(dbg_depcon,"ignoring depending package `%s'\n",depender->name);
  69. continue;
  70. }
  71. if (dependtry > 1) { if (findbreakcycle(pkgtoremove)) sincenothing= 0; }
  72. before= raemsgs->used;
  73. ok= dependencies_ok(depender,pkgtoremove,raemsgs);
  74. if (ok == 0 && depender->clientdata->istobe == itb_remove) ok= 1;
  75. if (ok == 1) raemsgs->used= before; /* Don't burble about reasons for deferral */
  76. if (ok < *rokp) *rokp= ok;
  77. }
  78. }
  79. void deferred_remove(struct pkginfo *pkg) {
  80. struct varbuf raemsgs = VARBUF_INIT;
  81. int rok;
  82. struct dependency *dep;
  83. debug(dbg_general,"deferred_remove package %s",pkg->name);
  84. if (pkg->status == stat_notinstalled) {
  85. fprintf(stderr,
  86. _("dpkg - warning: ignoring request to remove %.250s which isn't installed.\n"),
  87. pkg->name);
  88. pkg->clientdata->istobe= itb_normal;
  89. return;
  90. } else if (!f_pending &&
  91. pkg->status == stat_configfiles &&
  92. cipaction->arg != act_purge) {
  93. fprintf(stderr,
  94. _("dpkg - warning: ignoring request to remove %.250s, only the config\n"
  95. " files of which are on the system. Use --purge to remove them too.\n"),
  96. pkg->name);
  97. pkg->clientdata->istobe= itb_normal;
  98. return;
  99. }
  100. assert(pkg->installed.valid);
  101. if (pkg->installed.essential && pkg->status != stat_configfiles)
  102. forcibleerr(fc_removeessential, _("This is an essential package -"
  103. " it should not be removed."));
  104. if (!f_pending)
  105. pkg->want= (cipaction->arg == act_purge) ? want_purge : want_deinstall;
  106. if (!f_noact) modstatdb_note(pkg);
  107. debug(dbg_general,"checking dependencies for remove `%s'",pkg->name);
  108. rok= 2;
  109. checkforremoval(pkg,pkg,&rok,&raemsgs);
  110. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  111. if (dep->type != dep_provides) continue;
  112. debug(dbg_depcon,"checking virtual package `%s'",dep->list->ed->name);
  113. checkforremoval(pkg,dep->list->ed,&rok,&raemsgs);
  114. }
  115. if (rok == 1) {
  116. varbuffree(&raemsgs);
  117. pkg->clientdata->istobe= itb_remove;
  118. add_to_queue(pkg);
  119. return;
  120. } else if (rok == 0) {
  121. sincenothing= 0;
  122. varbufaddc(&raemsgs,0);
  123. fprintf(stderr,
  124. _("dpkg: dependency problems prevent removal of %s:\n%s"),
  125. pkg->name, raemsgs.buf);
  126. ohshit(_("dependency problems - not removing"));
  127. } else if (raemsgs.used) {
  128. varbufaddc(&raemsgs,0);
  129. fprintf(stderr,
  130. _("dpkg: %s: dependency problems, but removing anyway as you request:\n%s"),
  131. pkg->name, raemsgs.buf);
  132. }
  133. varbuffree(&raemsgs);
  134. sincenothing= 0;
  135. if (pkg->eflag & eflagf_reinstreq)
  136. forcibleerr(fc_removereinstreq,
  137. _("Package is in a very bad inconsistent state - you should\n"
  138. " reinstall it before attempting a removal."));
  139. ensure_allinstfiles_available();
  140. filesdbinit();
  141. if (f_noact) {
  142. printf(_("Would remove or purge %s ...\n"),pkg->name);
  143. pkg->status= stat_notinstalled;
  144. pkg->clientdata->istobe= itb_normal;
  145. return;
  146. }
  147. oldconffsetflags(pkg->installed.conffiles);
  148. printf(_("Removing %s ...\n"),pkg->name);
  149. log_action("remove", pkg);
  150. trig_activate_packageprocessing(pkg);
  151. if (pkg->status >= stat_halfconfigured) {
  152. static enum pkgstatus oldpkgstatus;
  153. oldpkgstatus= pkg->status;
  154. pkg->status= stat_halfconfigured;
  155. modstatdb_note(pkg);
  156. push_cleanup(cu_prermremove, ~ehflag_normaltidy, NULL, 0, 2,
  157. (void *)pkg, (void *)&oldpkgstatus);
  158. maintainer_script_installed(pkg, PRERMFILE, "pre-removal",
  159. "remove", NULL);
  160. pkg->status= stat_unpacked; /* Will turn into halfinstalled soon ... */
  161. }
  162. removal_bulk(pkg);
  163. }
  164. static void push_leftover(struct fileinlist **leftoverp,
  165. struct filenamenode *namenode) {
  166. struct fileinlist *newentry;
  167. newentry= nfmalloc(sizeof(struct fileinlist));
  168. newentry->next= *leftoverp;
  169. newentry->namenode= namenode;
  170. *leftoverp= newentry;
  171. }
  172. static void removal_bulk_remove_files(
  173. struct pkginfo *pkg,
  174. int *out_foundpostrm)
  175. {
  176. int before;
  177. int infodirbaseused;
  178. struct reversefilelistiter rlistit;
  179. struct fileinlist *leftover;
  180. struct filenamenode *namenode;
  181. static struct varbuf fnvb;
  182. DIR *dsd;
  183. struct dirent *de;
  184. struct stat stab;
  185. pkg->status= stat_halfinstalled;
  186. modstatdb_note(pkg);
  187. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  188. reversefilelist_init(&rlistit,pkg->clientdata->files);
  189. leftover = NULL;
  190. while ((namenode= reversefilelist_next(&rlistit))) {
  191. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  192. namenode->name, namenode->flags);
  193. if (namenode->flags & fnnf_old_conff) {
  194. push_leftover(&leftover,namenode);
  195. continue;
  196. }
  197. varbufreset(&fnvb);
  198. varbufaddstr(&fnvb,instdir);
  199. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  200. before= fnvb.used;
  201. varbufaddstr(&fnvb,DPKGTEMPEXT);
  202. varbufaddc(&fnvb,0);
  203. debug(dbg_eachfiledetail, "removal_bulk cleaning temp `%s'", fnvb.buf);
  204. ensure_pathname_nonexisting(fnvb.buf);
  205. fnvb.used= before;
  206. varbufaddstr(&fnvb,DPKGNEWEXT);
  207. varbufaddc(&fnvb,0);
  208. debug(dbg_eachfiledetail, "removal_bulk cleaning new `%s'", fnvb.buf);
  209. ensure_pathname_nonexisting(fnvb.buf);
  210. fnvb.used= before;
  211. varbufaddc(&fnvb,0);
  212. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  213. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  214. /* Only delete a directory or a link to one if we're the only
  215. * package which uses it. Other files should only be listed
  216. * in this package (but we don't check).
  217. */
  218. if (hasdirectoryconffiles(namenode,pkg)) {
  219. push_leftover(&leftover,namenode);
  220. continue;
  221. }
  222. if (isdirectoryinuse(namenode,pkg)) continue;
  223. }
  224. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  225. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  226. if (errno == ENOTEMPTY || errno == EEXIST) {
  227. debug(dbg_eachfiledetail, "removal_bulk `%s' was not empty, will try again later",
  228. fnvb.buf);
  229. push_leftover(&leftover,namenode);
  230. continue;
  231. } else if (errno == EBUSY || errno == EPERM) {
  232. fprintf(stderr, _("dpkg - warning: while removing %.250s,"
  233. " unable to remove directory `%.250s':"
  234. " %s - directory may be a mount point ?\n"),
  235. pkg->name, namenode->name, strerror(errno));
  236. push_leftover(&leftover,namenode);
  237. continue;
  238. }
  239. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  240. debug(dbg_eachfiledetail, "removal_bulk unlinking `%s'", fnvb.buf);
  241. {
  242. /*
  243. * If file to remove is a device or s[gu]id, change its mode
  244. * so that a malicious user cannot use it even if it's linked
  245. * to another file
  246. */
  247. struct stat stat_buf;
  248. if (lstat(fnvb.buf,&stat_buf)==0) {
  249. if (S_ISCHR(stat_buf.st_mode) || S_ISBLK(stat_buf.st_mode)) {
  250. chmod(fnvb.buf,0);
  251. }
  252. if (stat_buf.st_mode & (S_ISUID|S_ISGID)) {
  253. chmod(fnvb.buf,stat_buf.st_mode & ~(S_ISUID|S_ISGID));
  254. }
  255. }
  256. }
  257. if (unlink(fnvb.buf)) ohshite(_("cannot remove file `%.250s'"),fnvb.buf);
  258. }
  259. write_filelist_except(pkg,leftover,0);
  260. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  261. "remove", NULL);
  262. varbufreset(&fnvb);
  263. varbufaddstr(&fnvb,admindir);
  264. varbufaddstr(&fnvb,"/" INFODIR);
  265. infodirbaseused= fnvb.used;
  266. varbufaddc(&fnvb,0);
  267. dsd= opendir(fnvb.buf); if (!dsd) ohshite(_("cannot read info directory"));
  268. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  269. *out_foundpostrm= 0;
  270. debug(dbg_general, "removal_bulk cleaning info directory");
  271. while ((de = readdir(dsd)) != NULL) {
  272. char *p;
  273. debug(dbg_veryverbose, "removal_bulk info file `%s'", de->d_name);
  274. if (de->d_name[0] == '.') continue;
  275. p= strrchr(de->d_name,'.'); if (!p) continue;
  276. if (strlen(pkg->name) != (size_t)(p-de->d_name) ||
  277. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  278. debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
  279. /* We need the postrm and list files for --purge. */
  280. if (!strcmp(p+1,LISTFILE)) continue;
  281. if (!strcmp(p+1,POSTRMFILE)) { *out_foundpostrm=1; continue; }
  282. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  283. fnvb.used= infodirbaseused;
  284. varbufaddstr(&fnvb,de->d_name);
  285. varbufaddc(&fnvb,0);
  286. if (unlink(fnvb.buf))
  287. ohshite(_("unable to delete control info file `%.250s'"),fnvb.buf);
  288. debug(dbg_scripts, "removal_bulk info unlinked %s",fnvb.buf);
  289. }
  290. pop_cleanup(ehflag_normaltidy); /* closedir */
  291. pkg->status= stat_configfiles;
  292. pkg->installed.essential= 0;
  293. modstatdb_note(pkg);
  294. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  295. }
  296. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  297. struct reversefilelistiter rlistit;
  298. struct fileinlist *leftover;
  299. struct filenamenode *namenode;
  300. static struct varbuf fnvb;
  301. struct stat stab;
  302. modstatdb_note(pkg);
  303. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  304. reversefilelist_init(&rlistit,pkg->clientdata->files);
  305. leftover = NULL;
  306. while ((namenode= reversefilelist_next(&rlistit))) {
  307. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  308. namenode->name, namenode->flags);
  309. if (namenode->flags & fnnf_old_conff) {
  310. push_leftover(&leftover,namenode);
  311. continue;
  312. }
  313. varbufreset(&fnvb);
  314. varbufaddstr(&fnvb,instdir);
  315. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  316. varbufaddc(&fnvb,0);
  317. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  318. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  319. /* Only delete a directory or a link to one if we're the only
  320. * package which uses it. Other files should only be listed
  321. * in this package (but we don't check).
  322. */
  323. if (hasdirectoryconffiles(namenode,pkg)) {
  324. push_leftover(&leftover,namenode);
  325. continue;
  326. }
  327. if (isdirectoryinuse(namenode,pkg)) continue;
  328. }
  329. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  330. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  331. if (errno == ENOTEMPTY || errno == EEXIST) {
  332. fprintf(stderr,
  333. _("dpkg - warning: while removing %.250s, directory `%.250s' not empty "
  334. "so not removed.\n"),
  335. pkg->name, namenode->name);
  336. push_leftover(&leftover,namenode);
  337. continue;
  338. } else if (errno == EBUSY || errno == EPERM) {
  339. fprintf(stderr, _("dpkg - warning: while removing %.250s,"
  340. " unable to remove directory `%.250s':"
  341. " %s - directory may be a mount point ?\n"),
  342. pkg->name, namenode->name, strerror(errno));
  343. push_leftover(&leftover,namenode);
  344. continue;
  345. }
  346. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  347. push_leftover(&leftover,namenode);
  348. continue;
  349. }
  350. write_filelist_except(pkg,leftover,0);
  351. modstatdb_note(pkg);
  352. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  353. }
  354. static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
  355. static const char *const removeconffexts[] = { REMOVECONFFEXTS, NULL };
  356. int r, removevbbase;
  357. int conffnameused, conffbasenamelen;
  358. char *conffbasename;
  359. struct conffile *conff, **lconffp;
  360. struct fileinlist *searchfile;
  361. DIR *dsd;
  362. struct dirent *de;
  363. char *p;
  364. const char *const *ext;
  365. printf(_("Purging configuration files for %s ...\n"),pkg->name);
  366. log_action("purge", pkg);
  367. trig_activate_packageprocessing(pkg);
  368. ensure_packagefiles_available(pkg); /* We may have modified this above. */
  369. /* We're about to remove the configuration, so remove the note
  370. * about which version it was ...
  371. */
  372. blankversion(&pkg->configversion);
  373. modstatdb_note(pkg);
  374. /* Remove from our list any conffiles that aren't ours any more or
  375. * are involved in diversions, except if we are the package doing the
  376. * diverting.
  377. */
  378. for (lconffp = &pkg->installed.conffiles; (conff = *lconffp) != NULL; ) {
  379. for (searchfile= pkg->clientdata->files;
  380. searchfile && strcmp(searchfile->namenode->name,conff->name);
  381. searchfile= searchfile->next);
  382. if (!searchfile) {
  383. debug(dbg_conff,"removal_bulk conffile not ours any more `%s'",conff->name);
  384. *lconffp= conff->next;
  385. } else if (searchfile->namenode->divert &&
  386. (searchfile->namenode->divert->camefrom ||
  387. (searchfile->namenode->divert->useinstead &&
  388. searchfile->namenode->divert->pkg != pkg))) {
  389. debug(dbg_conff,"removal_bulk conffile `%s' ignored due to diversion\n",
  390. conff->name);
  391. *lconffp= conff->next;
  392. } else {
  393. debug(dbg_conffdetail,"removal_bulk set to new conffile `%s'",conff->name);
  394. conff->hash= NEWCONFFILEFLAG; /* yes, cast away const */
  395. lconffp= &conff->next;
  396. }
  397. }
  398. modstatdb_note(pkg);
  399. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  400. static struct varbuf fnvb, removevb;
  401. if (conff->obsolete) {
  402. debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
  403. conff->name);
  404. continue;
  405. }
  406. varbufreset(&fnvb);
  407. r= conffderef(pkg, &fnvb, conff->name);
  408. debug(dbg_conffdetail, "removal_bulk conffile `%s' (= `%s')",
  409. conff->name, r == -1 ? "<r==-1>" : fnvb.buf);
  410. if (r == -1) continue;
  411. conffnameused= fnvb.used-1;
  412. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  413. ohshite(_("cannot remove old config file `%.250s' (= `%.250s')"),
  414. conff->name, fnvb.buf);
  415. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  416. *p= 0;
  417. varbufreset(&removevb);
  418. varbufaddstr(&removevb,fnvb.buf);
  419. varbufaddc(&removevb,'/');
  420. removevbbase= removevb.used;
  421. varbufaddc(&removevb,0);
  422. dsd= opendir(removevb.buf);
  423. if (!dsd) {
  424. int e=errno;
  425. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  426. fnvb.buf, strerror(e)); errno= e;
  427. if (errno == ENOENT || errno == ENOTDIR) continue;
  428. ohshite(_("cannot read config file dir `%.250s' (from `%.250s')"),
  429. fnvb.buf, conff->name);
  430. }
  431. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  432. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  433. *p= '/';
  434. conffbasenamelen= strlen(++p);
  435. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  436. while ((de = readdir(dsd)) != NULL) {
  437. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry=`%s'"
  438. " conffbasename=`%s' conffnameused=%d conffbasenamelen=%d",
  439. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  440. if (!strncmp(de->d_name,conffbasename,conffbasenamelen)) {
  441. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  442. for (ext= removeconffexts; *ext; ext++)
  443. if (!strcmp(*ext,de->d_name+conffbasenamelen)) goto yes_remove;
  444. p= de->d_name+conffbasenamelen;
  445. if (*p++ == '~') {
  446. while (*p && cisdigit(*p)) p++;
  447. if (*p == '~' && !*++p) goto yes_remove;
  448. }
  449. }
  450. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  451. if (de->d_name[0] == '#' &&
  452. !strncmp(de->d_name+1,conffbasename,conffbasenamelen) &&
  453. !strcmp(de->d_name+1+conffbasenamelen,"#"))
  454. goto yes_remove;
  455. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  456. continue;
  457. yes_remove:
  458. removevb.used= removevbbase;
  459. varbufaddstr(&removevb,de->d_name); varbufaddc(&removevb,0);
  460. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing `%s'",
  461. removevb.buf);
  462. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  463. ohshite(_("cannot remove old backup config file `%.250s' (of `%.250s')"),
  464. removevb.buf, conff->name);
  465. }
  466. pop_cleanup(ehflag_normaltidy); /* closedir */
  467. }
  468. pkg->installed.conffiles = NULL;
  469. modstatdb_note(pkg);
  470. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  471. "purge", NULL);
  472. }
  473. void removal_bulk(struct pkginfo *pkg) {
  474. /* This is used both by deferred_remove in this file, and at
  475. * the end of process_archive in archives.c if it needs to finish
  476. * removing a conflicting package.
  477. */
  478. int pkgnameused;
  479. int foundpostrm= 0;
  480. const char *postrmfilename;
  481. debug(dbg_general,"removal_bulk package %s",pkg->name);
  482. if (pkg->status == stat_halfinstalled || pkg->status == stat_unpacked) {
  483. removal_bulk_remove_files(pkg, &foundpostrm);
  484. } else {
  485. struct stat stab;
  486. postrmfilename= pkgadminfile(pkg,POSTRMFILE);
  487. if (!lstat(postrmfilename,&stab)) foundpostrm= 1;
  488. else if (errno == ENOENT) foundpostrm= 0;
  489. else ohshite(_("unable to check existence of `%.250s'"),postrmfilename);
  490. }
  491. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  492. if (!foundpostrm && !pkg->installed.conffiles) {
  493. /* If there are no config files and no postrm script then we
  494. * go straight into `purge'.
  495. */
  496. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  497. pkg->want= want_purge;
  498. } else if (pkg->want == want_purge) {
  499. removal_bulk_remove_configfiles(pkg);
  500. }
  501. if (pkg->want == want_purge) {
  502. /* ie, either of the two branches above. */
  503. static struct varbuf fnvb;
  504. /* retry empty directories, and warn on any leftovers that aren't */
  505. removal_bulk_remove_leftover_dirs(pkg);
  506. varbufreset(&fnvb);
  507. varbufaddstr(&fnvb,admindir);
  508. varbufaddstr(&fnvb,"/" INFODIR);
  509. varbufaddstr(&fnvb,pkg->name);
  510. pkgnameused= fnvb.used;
  511. varbufaddstr(&fnvb,"." LISTFILE);
  512. varbufaddc(&fnvb,0);
  513. debug(dbg_general, "removal_bulk purge done, removing list `%s'",fnvb.buf);
  514. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("cannot remove old files list"));
  515. fnvb.used= pkgnameused;
  516. varbufaddstr(&fnvb,"." POSTRMFILE);
  517. varbufaddc(&fnvb,0);
  518. debug(dbg_general, "removal_bulk purge done, removing postrm `%s'",fnvb.buf);
  519. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("can't remove old postrm script"));
  520. pkg->status= stat_notinstalled;
  521. /* This will mess up reverse links, but if we follow them
  522. * we won't go back because pkg->status is stat_notinstalled.
  523. */
  524. pkg->installed.depends = NULL;
  525. pkg->installed.essential= 0;
  526. pkg->installed.description = pkg->installed.maintainer = NULL;
  527. pkg->installed.source = pkg->installed.installedsize = NULL;
  528. pkg->installed.origin = pkg->installed.bugs = NULL;
  529. pkg->installed.architecture = NULL;
  530. blankversion(&pkg->installed.version);
  531. pkg->installed.arbs = NULL;
  532. }
  533. pkg->eflag= eflagv_ok;
  534. modstatdb_note(pkg);
  535. debug(dbg_general, "removal done");
  536. }