remove.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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) continue;
  64. if (ignore_depends(depender)) {
  65. debug(dbg_depcon,"ignoring depending package `%s'\n",depender->name);
  66. continue;
  67. }
  68. if (dependtry > 1) { if (findbreakcycle(pkgtoremove)) sincenothing= 0; }
  69. before= raemsgs->used;
  70. ok= dependencies_ok(depender,pkgtoremove,raemsgs);
  71. if (ok == 0 && depender->clientdata->istobe == itb_remove) ok= 1;
  72. if (ok == 1) raemsgs->used= before; /* Don't burble about reasons for deferral */
  73. if (ok < *rokp) *rokp= ok;
  74. }
  75. }
  76. void deferred_remove(struct pkginfo *pkg) {
  77. struct varbuf raemsgs;
  78. int rok;
  79. struct dependency *dep;
  80. debug(dbg_general,"deferred_remove package %s",pkg->name);
  81. if (pkg->status == stat_notinstalled) {
  82. fprintf(stderr,
  83. _("dpkg - warning: ignoring request to remove %.250s which isn't installed.\n"),
  84. pkg->name);
  85. pkg->clientdata->istobe= itb_normal;
  86. return;
  87. } else if (!f_pending &&
  88. pkg->status == stat_configfiles &&
  89. cipaction->arg != act_purge) {
  90. fprintf(stderr,
  91. _("dpkg - warning: ignoring request to remove %.250s, only the config\n"
  92. " files of which are on the system. Use --purge to remove them too.\n"),
  93. pkg->name);
  94. pkg->clientdata->istobe= itb_normal;
  95. return;
  96. }
  97. assert(pkg->installed.valid);
  98. if (pkg->installed.essential && pkg->status != stat_configfiles)
  99. forcibleerr(fc_removeessential, _("This is an essential package -"
  100. " it should not be removed."));
  101. if (!f_pending)
  102. pkg->want= (cipaction->arg == act_purge) ? want_purge : want_deinstall;
  103. if (!f_noact) modstatdb_note(pkg);
  104. debug(dbg_general,"checking dependencies for remove `%s'",pkg->name);
  105. varbufinit(&raemsgs);
  106. rok= 2;
  107. checkforremoval(pkg,pkg,&rok,&raemsgs);
  108. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  109. if (dep->type != dep_provides) continue;
  110. debug(dbg_depcon,"checking virtual package `%s'",dep->list->ed->name);
  111. checkforremoval(pkg,dep->list->ed,&rok,&raemsgs);
  112. }
  113. if (rok == 1) {
  114. varbuffree(&raemsgs);
  115. pkg->clientdata->istobe= itb_remove;
  116. add_to_queue(pkg);
  117. return;
  118. } else if (rok == 0) {
  119. sincenothing= 0;
  120. varbufaddc(&raemsgs,0);
  121. fprintf(stderr,
  122. _("dpkg: dependency problems prevent removal of %s:\n%s"),
  123. pkg->name, raemsgs.buf);
  124. ohshit(_("dependency problems - not removing"));
  125. } else if (raemsgs.used) {
  126. varbufaddc(&raemsgs,0);
  127. fprintf(stderr,
  128. _("dpkg: %s: dependency problems, but removing anyway as you request:\n%s"),
  129. pkg->name, raemsgs.buf);
  130. }
  131. varbuffree(&raemsgs);
  132. sincenothing= 0;
  133. if (pkg->eflag & eflagf_reinstreq)
  134. forcibleerr(fc_removereinstreq,
  135. _("Package is in a very bad inconsistent state - you should\n"
  136. " reinstall it before attempting a removal."));
  137. ensure_allinstfiles_available();
  138. filesdbinit();
  139. if (f_noact) {
  140. printf(_("Would remove or purge %s ...\n"),pkg->name);
  141. pkg->status= stat_notinstalled;
  142. pkg->clientdata->istobe= itb_normal;
  143. return;
  144. }
  145. oldconffsetflags(pkg->installed.conffiles);
  146. printf(_("Removing %s ...\n"),pkg->name);
  147. log_action("remove", pkg);
  148. if (pkg->status == stat_halfconfigured || pkg->status == stat_installed) {
  149. if (pkg->status == stat_installed || pkg->status == stat_halfconfigured) {
  150. pkg->status= stat_halfconfigured;
  151. modstatdb_note(pkg);
  152. push_cleanup(cu_prermremove,~ehflag_normaltidy, 0,0, 1,(void*)pkg);
  153. maintainer_script_installed(pkg, PRERMFILE, "pre-removal",
  154. "remove", (char*)0);
  155. }
  156. pkg->status= stat_unpacked; /* Will turn into halfinstalled soon ... */
  157. }
  158. removal_bulk(pkg);
  159. }
  160. static void push_leftover(struct fileinlist **leftoverp,
  161. struct filenamenode *namenode) {
  162. struct fileinlist *newentry;
  163. newentry= nfmalloc(sizeof(struct fileinlist));
  164. newentry->next= *leftoverp;
  165. newentry->namenode= namenode;
  166. *leftoverp= newentry;
  167. }
  168. static void removal_bulk_remove_files(
  169. struct pkginfo *pkg,
  170. int *out_foundpostrm)
  171. {
  172. int before;
  173. int infodirbaseused;
  174. struct reversefilelistiter rlistit;
  175. struct fileinlist *leftover;
  176. struct filenamenode *namenode;
  177. static struct varbuf fnvb;
  178. DIR *dsd;
  179. struct dirent *de;
  180. struct stat stab;
  181. pkg->status= stat_halfinstalled;
  182. modstatdb_note(pkg);
  183. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  184. reversefilelist_init(&rlistit,pkg->clientdata->files);
  185. leftover= 0;
  186. while ((namenode= reversefilelist_next(&rlistit))) {
  187. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  188. namenode->name, namenode->flags);
  189. if (namenode->flags & fnnf_old_conff) {
  190. push_leftover(&leftover,namenode);
  191. continue;
  192. }
  193. varbufreset(&fnvb);
  194. varbufaddstr(&fnvb,instdir);
  195. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  196. before= fnvb.used;
  197. varbufaddstr(&fnvb,DPKGTEMPEXT);
  198. varbufaddc(&fnvb,0);
  199. debug(dbg_eachfiledetail, "removal_bulk cleaning temp `%s'", fnvb.buf);
  200. ensure_pathname_nonexisting(fnvb.buf);
  201. fnvb.used= before;
  202. varbufaddstr(&fnvb,DPKGNEWEXT);
  203. varbufaddc(&fnvb,0);
  204. debug(dbg_eachfiledetail, "removal_bulk cleaning new `%s'", fnvb.buf);
  205. ensure_pathname_nonexisting(fnvb.buf);
  206. fnvb.used= before;
  207. varbufaddc(&fnvb,0);
  208. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  209. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  210. /* Only delete a directory or a link to one if we're the only
  211. * package which uses it. Other files should only be listed
  212. * in this package (but we don't check).
  213. */
  214. if (hasdirectoryconffiles(namenode,pkg)) {
  215. push_leftover(&leftover,namenode);
  216. continue;
  217. }
  218. if (isdirectoryinuse(namenode,pkg)) continue;
  219. }
  220. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  221. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  222. if (errno == ENOTEMPTY) {
  223. debug(dbg_eachfiledetail, "removal_bulk `%s' was not empty, will try again later",
  224. fnvb.buf);
  225. push_leftover(&leftover,namenode);
  226. continue;
  227. } else if (errno == EBUSY || errno == EPERM) {
  228. fprintf(stderr, _("dpkg - warning: while removing %.250s,"
  229. " unable to remove directory `%.250s':"
  230. " %s - directory may be a mount point ?\n"),
  231. pkg->name, namenode->name, strerror(errno));
  232. push_leftover(&leftover,namenode);
  233. continue;
  234. }
  235. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  236. debug(dbg_eachfiledetail, "removal_bulk unlinking `%s'", fnvb.buf);
  237. {
  238. /*
  239. * If file to remove is a device or s[gu]id, change its mode
  240. * so that a malicious user cannot use it even if it's linked
  241. * to another file
  242. */
  243. struct stat stat_buf;
  244. if (lstat(fnvb.buf,&stat_buf)==0) {
  245. if (S_ISCHR(stat_buf.st_mode) || S_ISBLK(stat_buf.st_mode)) {
  246. chmod(fnvb.buf,0);
  247. }
  248. if (stat_buf.st_mode & (S_ISUID|S_ISGID)) {
  249. chmod(fnvb.buf,stat_buf.st_mode & ~(S_ISUID|S_ISGID));
  250. }
  251. }
  252. }
  253. if (unlink(fnvb.buf)) ohshite(_("cannot remove file `%.250s'"),fnvb.buf);
  254. }
  255. write_filelist_except(pkg,leftover,0);
  256. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  257. "remove", (char*)0);
  258. varbufreset(&fnvb);
  259. varbufaddstr(&fnvb,admindir);
  260. varbufaddstr(&fnvb,"/" INFODIR);
  261. infodirbaseused= fnvb.used;
  262. varbufaddc(&fnvb,0);
  263. dsd= opendir(fnvb.buf); if (!dsd) ohshite(_("cannot read info directory"));
  264. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  265. *out_foundpostrm= 0;
  266. debug(dbg_general, "removal_bulk cleaning info directory");
  267. while ((de= readdir(dsd)) != 0) {
  268. char *p;
  269. debug(dbg_veryverbose, "removal_bulk info file `%s'", de->d_name);
  270. if (de->d_name[0] == '.') continue;
  271. p= strrchr(de->d_name,'.'); if (!p) continue;
  272. if (strlen(pkg->name) != (size_t)(p-de->d_name) ||
  273. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  274. debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
  275. /* We need the postrm and list files for --purge. */
  276. if (!strcmp(p+1,LISTFILE)) continue;
  277. if (!strcmp(p+1,POSTRMFILE)) { *out_foundpostrm=1; continue; }
  278. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  279. fnvb.used= infodirbaseused;
  280. varbufaddstr(&fnvb,de->d_name);
  281. varbufaddc(&fnvb,0);
  282. if (unlink(fnvb.buf))
  283. ohshite(_("unable to delete control info file `%.250s'"),fnvb.buf);
  284. debug(dbg_scripts, "removal_bulk info unlinked %s",fnvb.buf);
  285. }
  286. pop_cleanup(ehflag_normaltidy); /* closedir */
  287. pkg->status= stat_configfiles;
  288. pkg->installed.essential= 0;
  289. modstatdb_note(pkg);
  290. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  291. }
  292. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  293. struct reversefilelistiter rlistit;
  294. struct fileinlist *leftover;
  295. struct filenamenode *namenode;
  296. static struct varbuf fnvb;
  297. struct stat stab;
  298. modstatdb_note(pkg);
  299. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  300. reversefilelist_init(&rlistit,pkg->clientdata->files);
  301. leftover= 0;
  302. while ((namenode= reversefilelist_next(&rlistit))) {
  303. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  304. namenode->name, namenode->flags);
  305. if (namenode->flags & fnnf_old_conff) {
  306. push_leftover(&leftover,namenode);
  307. continue;
  308. }
  309. varbufreset(&fnvb);
  310. varbufaddstr(&fnvb,instdir);
  311. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  312. varbufaddc(&fnvb,0);
  313. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  314. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  315. /* Only delete a directory or a link to one if we're the only
  316. * package which uses it. Other files should only be listed
  317. * in this package (but we don't check).
  318. */
  319. if (hasdirectoryconffiles(namenode,pkg)) {
  320. push_leftover(&leftover,namenode);
  321. continue;
  322. }
  323. if (isdirectoryinuse(namenode,pkg)) continue;
  324. }
  325. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  326. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  327. if (errno == ENOTEMPTY) {
  328. fprintf(stderr,
  329. _("dpkg - warning: while removing %.250s, directory `%.250s' not empty "
  330. "so not removed.\n"),
  331. pkg->name, namenode->name);
  332. push_leftover(&leftover,namenode);
  333. continue;
  334. } else if (errno == EBUSY || errno == EPERM) {
  335. fprintf(stderr, _("dpkg - warning: while removing %.250s,"
  336. " unable to remove directory `%.250s':"
  337. " %s - directory may be a mount point ?\n"),
  338. pkg->name, namenode->name, strerror(errno));
  339. push_leftover(&leftover,namenode);
  340. continue;
  341. }
  342. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  343. push_leftover(&leftover,namenode);
  344. continue;
  345. }
  346. write_filelist_except(pkg,leftover,0);
  347. modstatdb_note(pkg);
  348. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  349. }
  350. static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
  351. static const char *const removeconffexts[]= { REMOVECONFFEXTS, 0 };
  352. int r, removevbbase;
  353. int conffnameused, conffbasenamelen;
  354. char *conffbasename;
  355. struct conffile *conff, **lconffp;
  356. struct fileinlist *searchfile;
  357. DIR *dsd;
  358. struct dirent *de;
  359. char *p;
  360. const char *const *ext;
  361. printf(_("Purging configuration files for %s ...\n"),pkg->name);
  362. log_action("purge", 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) != 0; ) {
  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; /* yes, cast away const */
  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, 0,0, 1,(void*)dsd);
  428. *p= '/';
  429. conffbasenamelen= strlen(++p);
  430. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  431. while ((de= readdir(dsd)) != 0) {
  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= 0;
  464. modstatdb_note(pkg);
  465. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  466. "purge", (char*)0);
  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. pkg->installed.depends= 0;
  520. pkg->installed.essential= 0;
  521. pkg->installed.description= pkg->installed.maintainer= 0;
  522. pkg->installed.source= pkg->installed.installedsize= 0;
  523. pkg->installed.origin= pkg->installed.bugs= 0;
  524. pkg->installed.architecture= 0;
  525. blankversion(&pkg->installed.version);
  526. pkg->installed.arbs= 0;
  527. }
  528. pkg->eflag= eflagv_ok;
  529. modstatdb_note(pkg);
  530. debug(dbg_general, "removal done");
  531. }