remove.c 21 KB

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