remove.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but 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 License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <dirent.h>
  29. #include <unistd.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <dpkg/i18n.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include <dpkg/dir.h>
  36. #include <dpkg/myopt.h>
  37. #include <dpkg/triglib.h>
  38. #include "filesdb.h"
  39. #include "main.h"
  40. /*
  41. * pkgdepcheck may be a virtual pkg.
  42. */
  43. static void checkforremoval(struct pkginfo *pkgtoremove,
  44. struct pkginfo *pkgdepcheck,
  45. int *rokp, struct varbuf *raemsgs) {
  46. struct deppossi *possi;
  47. struct pkginfo *depender;
  48. int before, ok;
  49. for (possi = pkgdepcheck->installed.depended; possi; possi = possi->rev_next) {
  50. if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
  51. depender= possi->up->up;
  52. debug(dbg_depcon,"checking depending package `%s'",depender->name);
  53. if (!(depender->status == stat_installed ||
  54. depender->status == stat_triggerspending ||
  55. depender->status == stat_triggersawaited))
  56. continue;
  57. if (ignore_depends(depender)) {
  58. debug(dbg_depcon, "ignoring depending package '%s'", depender->name);
  59. continue;
  60. }
  61. if (dependtry > 1) { if (findbreakcycle(pkgtoremove)) sincenothing= 0; }
  62. before= raemsgs->used;
  63. ok= dependencies_ok(depender,pkgtoremove,raemsgs);
  64. if (ok == 0 && depender->clientdata->istobe == itb_remove) ok= 1;
  65. if (ok == 1)
  66. /* Don't burble about reasons for deferral. */
  67. varbuf_trunc(raemsgs, before);
  68. if (ok < *rokp) *rokp= ok;
  69. }
  70. }
  71. void deferred_remove(struct pkginfo *pkg) {
  72. struct varbuf raemsgs = VARBUF_INIT;
  73. int rok;
  74. struct dependency *dep;
  75. debug(dbg_general,"deferred_remove package %s",pkg->name);
  76. if (pkg->status == stat_notinstalled) {
  77. warning(_("ignoring request to remove %.250s which isn't installed."),
  78. pkg->name);
  79. pkg->clientdata->istobe= itb_normal;
  80. return;
  81. } else if (!f_pending &&
  82. pkg->status == stat_configfiles &&
  83. cipaction->arg != act_purge) {
  84. warning(_("ignoring request to remove %.250s, only the config\n"
  85. " files of which are on the system. Use --purge to remove them too."),
  86. pkg->name);
  87. pkg->clientdata->istobe= itb_normal;
  88. return;
  89. }
  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. varbuf_destroy(&raemsgs);
  106. pkg->clientdata->istobe= itb_remove;
  107. add_to_queue(pkg);
  108. return;
  109. } else if (rok == 0) {
  110. sincenothing= 0;
  111. varbuf_add_char(&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. varbuf_add_char(&raemsgs, '\0');
  118. fprintf(stderr,
  119. _("dpkg: %s: dependency problems, but removing anyway as you requested:\n%s"),
  120. pkg->name, raemsgs.buf);
  121. }
  122. varbuf_destroy(&raemsgs);
  123. sincenothing= 0;
  124. if (pkg->eflag & eflag_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. /* Will turn into ‘half-installed’ soon ... */
  150. pkg->status = stat_unpacked;
  151. }
  152. removal_bulk(pkg);
  153. }
  154. static void push_leftover(struct fileinlist **leftoverp,
  155. struct filenamenode *namenode) {
  156. struct fileinlist *newentry;
  157. newentry= nfmalloc(sizeof(struct fileinlist));
  158. newentry->next= *leftoverp;
  159. newentry->namenode= namenode;
  160. *leftoverp= newentry;
  161. }
  162. static void removal_bulk_remove_files(
  163. struct pkginfo *pkg,
  164. bool *out_foundpostrm)
  165. {
  166. int before;
  167. int infodirbaseused;
  168. struct reversefilelistiter rlistit;
  169. struct fileinlist *leftover;
  170. struct filenamenode *namenode;
  171. static struct varbuf fnvb;
  172. DIR *dsd;
  173. struct dirent *de;
  174. struct stat stab;
  175. pkg->status= stat_halfinstalled;
  176. modstatdb_note(pkg);
  177. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  178. reversefilelist_init(&rlistit,pkg->clientdata->files);
  179. leftover = NULL;
  180. while ((namenode= reversefilelist_next(&rlistit))) {
  181. struct filenamenode *usenode;
  182. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  183. namenode->name, namenode->flags);
  184. if (namenode->flags & fnnf_old_conff) {
  185. push_leftover(&leftover,namenode);
  186. continue;
  187. }
  188. usenode = namenodetouse(namenode, pkg);
  189. trig_file_activate(usenode, pkg);
  190. varbuf_reset(&fnvb);
  191. varbuf_add_str(&fnvb, instdir);
  192. varbuf_add_str(&fnvb, usenode->name);
  193. before= fnvb.used;
  194. varbuf_add_str(&fnvb, DPKGTEMPEXT);
  195. varbuf_add_char(&fnvb, '\0');
  196. debug(dbg_eachfiledetail, "removal_bulk cleaning temp `%s'", fnvb.buf);
  197. ensure_pathname_nonexisting(fnvb.buf);
  198. varbuf_trunc(&fnvb, before);
  199. varbuf_add_str(&fnvb, DPKGNEWEXT);
  200. varbuf_add_char(&fnvb, '\0');
  201. debug(dbg_eachfiledetail, "removal_bulk cleaning new `%s'", fnvb.buf);
  202. ensure_pathname_nonexisting(fnvb.buf);
  203. varbuf_trunc(&fnvb, before);
  204. varbuf_add_char(&fnvb, '\0');
  205. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  206. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  207. /* Only delete a directory or a link to one if we're the only
  208. * package which uses it. Other files should only be listed
  209. * in this package (but we don't check). */
  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. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  225. "%s - directory may be a mount point?"),
  226. pkg->name, namenode->name, strerror(errno));
  227. push_leftover(&leftover,namenode);
  228. continue;
  229. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  230. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  231. fnvb.buf);
  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. if (secure_unlink(fnvb.buf))
  238. ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
  239. }
  240. write_filelist_except(pkg,leftover,0);
  241. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  242. "remove", NULL);
  243. varbuf_reset(&fnvb);
  244. varbuf_add_str(&fnvb, pkgadmindir());
  245. infodirbaseused= fnvb.used;
  246. varbuf_add_char(&fnvb, '\0');
  247. dsd= opendir(fnvb.buf); if (!dsd) ohshite(_("cannot read info directory"));
  248. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  249. *out_foundpostrm = false;
  250. debug(dbg_general, "removal_bulk cleaning info directory");
  251. while ((de = readdir(dsd)) != NULL) {
  252. char *p;
  253. debug(dbg_veryverbose, "removal_bulk info file `%s'", de->d_name);
  254. if (de->d_name[0] == '.') continue;
  255. p= strrchr(de->d_name,'.'); if (!p) continue;
  256. if (strlen(pkg->name) != (size_t)(p-de->d_name) ||
  257. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  258. debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
  259. /* We need the postrm and list files for --purge. */
  260. if (!strcmp(p+1,LISTFILE)) continue;
  261. if (!strcmp(p + 1, POSTRMFILE)) {
  262. *out_foundpostrm = true;
  263. continue;
  264. }
  265. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  266. varbuf_trunc(&fnvb, infodirbaseused);
  267. varbuf_add_str(&fnvb, de->d_name);
  268. varbuf_add_char(&fnvb, '\0');
  269. if (unlink(fnvb.buf))
  270. ohshite(_("unable to delete control info file `%.250s'"),fnvb.buf);
  271. debug(dbg_scripts, "removal_bulk info unlinked %s",fnvb.buf);
  272. }
  273. /* Sync the info database directory. */
  274. dir_sync(dsd, fnvb.buf);
  275. pop_cleanup(ehflag_normaltidy); /* closedir */
  276. pkg->status= stat_configfiles;
  277. pkg->installed.essential = false;
  278. modstatdb_note(pkg);
  279. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  280. }
  281. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  282. struct reversefilelistiter rlistit;
  283. struct fileinlist *leftover;
  284. struct filenamenode *namenode;
  285. static struct varbuf fnvb;
  286. struct stat stab;
  287. modstatdb_note(pkg);
  288. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  289. reversefilelist_init(&rlistit,pkg->clientdata->files);
  290. leftover = NULL;
  291. while ((namenode= reversefilelist_next(&rlistit))) {
  292. struct filenamenode *usenode;
  293. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  294. namenode->name, namenode->flags);
  295. if (namenode->flags & fnnf_old_conff) {
  296. push_leftover(&leftover,namenode);
  297. continue;
  298. }
  299. usenode = namenodetouse(namenode, pkg);
  300. trig_file_activate(usenode, pkg);
  301. varbuf_reset(&fnvb);
  302. varbuf_add_str(&fnvb, instdir);
  303. varbuf_add_str(&fnvb, usenode->name);
  304. varbuf_add_char(&fnvb, '\0');
  305. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  306. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  307. /* Only delete a directory or a link to one if we're the only
  308. * package which uses it. Other files should only be listed
  309. * in this package (but we don't check). */
  310. if (hasdirectoryconffiles(namenode,pkg)) {
  311. push_leftover(&leftover,namenode);
  312. continue;
  313. }
  314. if (isdirectoryinuse(namenode,pkg)) continue;
  315. }
  316. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  317. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  318. if (errno == ENOTEMPTY || errno == EEXIST) {
  319. warning(_("while removing %.250s, directory '%.250s' not empty so not removed."),
  320. pkg->name, namenode->name);
  321. push_leftover(&leftover,namenode);
  322. continue;
  323. } else if (errno == EBUSY || errno == EPERM) {
  324. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  325. "%s - directory may be a mount point?"),
  326. pkg->name, namenode->name, strerror(errno));
  327. push_leftover(&leftover,namenode);
  328. continue;
  329. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  330. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  331. fnvb.buf);
  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. /* We may have modified this above. */
  358. ensure_packagefiles_available(pkg);
  359. /* We're about to remove the configuration, so remove the note
  360. * about which version it was ... */
  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. for (lconffp = &pkg->installed.conffiles; (conff = *lconffp) != NULL; ) {
  367. for (searchfile= pkg->clientdata->files;
  368. searchfile && strcmp(searchfile->namenode->name,conff->name);
  369. searchfile= searchfile->next);
  370. if (!searchfile) {
  371. debug(dbg_conff,"removal_bulk conffile not ours any more `%s'",conff->name);
  372. *lconffp= conff->next;
  373. } else if (searchfile->namenode->divert &&
  374. (searchfile->namenode->divert->camefrom ||
  375. (searchfile->namenode->divert->useinstead &&
  376. searchfile->namenode->divert->pkg != pkg))) {
  377. debug(dbg_conff, "removal_bulk conffile '%s' ignored due to diversion",
  378. conff->name);
  379. *lconffp= conff->next;
  380. } else {
  381. debug(dbg_conffdetail,"removal_bulk set to new conffile `%s'",conff->name);
  382. conff->hash = NEWCONFFILEFLAG;
  383. lconffp= &conff->next;
  384. }
  385. }
  386. modstatdb_note(pkg);
  387. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  388. static struct varbuf fnvb, removevb;
  389. if (conff->obsolete) {
  390. debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
  391. conff->name);
  392. }
  393. varbuf_reset(&fnvb);
  394. r= conffderef(pkg, &fnvb, conff->name);
  395. debug(dbg_conffdetail, "removal_bulk conffile `%s' (= `%s')",
  396. conff->name, r == -1 ? "<r==-1>" : fnvb.buf);
  397. if (r == -1) continue;
  398. conffnameused= fnvb.used-1;
  399. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  400. ohshite(_("cannot remove old config file `%.250s' (= `%.250s')"),
  401. conff->name, fnvb.buf);
  402. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  403. *p = '\0';
  404. varbuf_reset(&removevb);
  405. varbuf_add_str(&removevb, fnvb.buf);
  406. varbuf_add_char(&removevb, '/');
  407. removevbbase= removevb.used;
  408. varbuf_add_char(&removevb, '\0');
  409. dsd= opendir(removevb.buf);
  410. if (!dsd) {
  411. int e=errno;
  412. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  413. fnvb.buf, strerror(e)); errno= e;
  414. if (errno == ENOENT || errno == ENOTDIR) continue;
  415. ohshite(_("cannot read config file dir `%.250s' (from `%.250s')"),
  416. fnvb.buf, conff->name);
  417. }
  418. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  419. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  420. *p= '/';
  421. conffbasenamelen= strlen(++p);
  422. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  423. while ((de = readdir(dsd)) != NULL) {
  424. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry=`%s'"
  425. " conffbasename=`%s' conffnameused=%d conffbasenamelen=%d",
  426. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  427. if (!strncmp(de->d_name,conffbasename,conffbasenamelen)) {
  428. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  429. for (ext= removeconffexts; *ext; ext++)
  430. if (!strcmp(*ext,de->d_name+conffbasenamelen)) goto yes_remove;
  431. p= de->d_name+conffbasenamelen;
  432. if (*p++ == '~') {
  433. while (*p && cisdigit(*p)) p++;
  434. if (*p == '~' && !*++p) goto yes_remove;
  435. }
  436. }
  437. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  438. if (de->d_name[0] == '#' &&
  439. !strncmp(de->d_name+1,conffbasename,conffbasenamelen) &&
  440. !strcmp(de->d_name+1+conffbasenamelen,"#"))
  441. goto yes_remove;
  442. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  443. continue;
  444. yes_remove:
  445. varbuf_trunc(&removevb, removevbbase);
  446. varbuf_add_str(&removevb, de->d_name);
  447. varbuf_add_char(&removevb, '\0');
  448. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing `%s'",
  449. removevb.buf);
  450. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  451. ohshite(_("cannot remove old backup config file `%.250s' (of `%.250s')"),
  452. removevb.buf, conff->name);
  453. }
  454. pop_cleanup(ehflag_normaltidy); /* closedir */
  455. }
  456. pkg->installed.conffiles = NULL;
  457. modstatdb_note(pkg);
  458. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  459. "purge", NULL);
  460. }
  461. static bool
  462. pkg_has_postrm_script(struct pkginfo *pkg)
  463. {
  464. const char *postrmfilename;
  465. struct stat stab;
  466. postrmfilename = pkgadminfile(pkg, POSTRMFILE);
  467. if (!lstat(postrmfilename, &stab))
  468. return true;
  469. else if (errno == ENOENT)
  470. return false;
  471. else
  472. ohshite(_("unable to check existence of `%.250s'"), postrmfilename);
  473. }
  474. /*
  475. * This is used both by deferred_remove() in this file, and at the end of
  476. * process_archive() in archives.c if it needs to finish removing a
  477. * conflicting package.
  478. */
  479. void removal_bulk(struct pkginfo *pkg) {
  480. bool foundpostrm = false;
  481. debug(dbg_general,"removal_bulk package %s",pkg->name);
  482. if (pkg->status == stat_halfinstalled || pkg->status == stat_unpacked) {
  483. removal_bulk_remove_files(pkg, &foundpostrm);
  484. } else {
  485. foundpostrm = pkg_has_postrm_script(pkg);
  486. }
  487. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  488. if (!foundpostrm && !pkg->installed.conffiles) {
  489. /* If there are no config files and no postrm script then we
  490. * go straight into ‘purge’. */
  491. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  492. pkg->want= want_purge;
  493. blankversion(&pkg->configversion);
  494. } else if (pkg->want == want_purge) {
  495. removal_bulk_remove_configfiles(pkg);
  496. }
  497. /* I.e., either of the two branches above. */
  498. if (pkg->want == want_purge) {
  499. static struct varbuf fnvb;
  500. /* Retry empty directories, and warn on any leftovers that aren't. */
  501. removal_bulk_remove_leftover_dirs(pkg);
  502. varbuf_pkgadminfile(&fnvb, pkg, LISTFILE);
  503. debug(dbg_general, "removal_bulk purge done, removing list `%s'",fnvb.buf);
  504. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("cannot remove old files list"));
  505. varbuf_pkgadminfile(&fnvb, pkg, POSTRMFILE);
  506. debug(dbg_general, "removal_bulk purge done, removing postrm `%s'",fnvb.buf);
  507. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("can't remove old postrm script"));
  508. pkg->status= stat_notinstalled;
  509. pkg->want = want_unknown;
  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. pkgbin_blank(&pkg->installed);
  513. }
  514. pkg->eflag = eflag_ok;
  515. modstatdb_note(pkg);
  516. debug(dbg_general, "removal done");
  517. }