remove.c 21 KB

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