remove.c 21 KB

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