remove.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. * Copyright © 2007-2012 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #include <ctype.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <dirent.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/dpkg.h>
  35. #include <dpkg/dpkg-db.h>
  36. #include <dpkg/pkg.h>
  37. #include <dpkg/dir.h>
  38. #include <dpkg/options.h>
  39. #include <dpkg/triglib.h>
  40. #include "infodb.h"
  41. #include "filesdb.h"
  42. #include "main.h"
  43. /*
  44. * pkgdepcheck may be a virtual pkg.
  45. */
  46. static void checkforremoval(struct pkginfo *pkgtoremove,
  47. struct pkgset *pkgdepcheck,
  48. int *rokp, struct varbuf *raemsgs) {
  49. struct deppossi *possi;
  50. struct pkginfo *depender;
  51. int before, ok;
  52. for (possi = pkgdepcheck->depended.installed; possi; possi = possi->rev_next) {
  53. if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
  54. depender= possi->up->up;
  55. debug(dbg_depcon, "checking depending package '%s'",
  56. pkg_name(depender, pnaw_always));
  57. if (!(depender->status == stat_installed ||
  58. depender->status == stat_triggerspending ||
  59. depender->status == stat_triggersawaited))
  60. continue;
  61. if (ignore_depends(depender)) {
  62. debug(dbg_depcon, "ignoring depending package '%s'",
  63. pkg_name(depender, pnaw_always));
  64. continue;
  65. }
  66. if (dependtry > 1) { if (findbreakcycle(pkgtoremove)) sincenothing= 0; }
  67. before= raemsgs->used;
  68. ok= dependencies_ok(depender,pkgtoremove,raemsgs);
  69. if (ok == dep_check_halt && depender->clientdata->istobe == itb_remove)
  70. ok = dep_check_defer;
  71. if (ok == dep_check_defer)
  72. /* Don't burble about reasons for deferral. */
  73. varbuf_trunc(raemsgs, before);
  74. if (ok < *rokp) *rokp= ok;
  75. }
  76. }
  77. void deferred_remove(struct pkginfo *pkg) {
  78. struct varbuf raemsgs = VARBUF_INIT;
  79. int rok;
  80. struct dependency *dep;
  81. debug(dbg_general, "deferred_remove package %s",
  82. pkg_name(pkg, pnaw_always));
  83. if (pkg->status == stat_notinstalled) {
  84. warning(_("ignoring request to remove %.250s which isn't installed."),
  85. pkg_name(pkg, pnaw_nonambig));
  86. pkg->clientdata->istobe= itb_normal;
  87. return;
  88. } else if (!f_pending &&
  89. pkg->status == stat_configfiles &&
  90. cipaction->arg_int != act_purge) {
  91. warning(_("ignoring request to remove %.250s, only the config\n"
  92. " files of which are on the system. Use --purge to remove them too."),
  93. pkg_name(pkg, pnaw_nonambig));
  94. pkg->clientdata->istobe= itb_normal;
  95. return;
  96. }
  97. if (pkg->installed.essential && pkg->status != stat_configfiles)
  98. forcibleerr(fc_removeessential, _("This is an essential package -"
  99. " it should not be removed."));
  100. if (!f_pending) {
  101. if (cipaction->arg_int == act_purge)
  102. pkg_set_want(pkg, want_purge);
  103. else
  104. pkg_set_want(pkg, want_deinstall);
  105. }
  106. if (!f_noact) modstatdb_note(pkg);
  107. debug(dbg_general, "checking dependencies for remove '%s'",
  108. pkg_name(pkg, pnaw_always));
  109. rok= 2;
  110. checkforremoval(pkg, pkg->set, &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. varbuf_destroy(&raemsgs);
  118. pkg->clientdata->istobe= itb_remove;
  119. add_to_queue(pkg);
  120. return;
  121. } else if (rok == 0) {
  122. sincenothing= 0;
  123. varbuf_end_str(&raemsgs);
  124. fprintf(stderr,
  125. _("dpkg: dependency problems prevent removal of %s:\n%s"),
  126. pkg_name(pkg, pnaw_nonambig), raemsgs.buf);
  127. ohshit(_("dependency problems - not removing"));
  128. } else if (raemsgs.used) {
  129. varbuf_end_str(&raemsgs);
  130. fprintf(stderr,
  131. _("dpkg: %s: dependency problems, but removing anyway as you requested:\n%s"),
  132. pkg_name(pkg, pnaw_nonambig), raemsgs.buf);
  133. }
  134. varbuf_destroy(&raemsgs);
  135. sincenothing= 0;
  136. if (pkg->eflag & eflag_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(pkg, pnaw_nonambig));
  144. pkg_set_status(pkg, stat_notinstalled);
  145. pkg->clientdata->istobe= itb_normal;
  146. return;
  147. }
  148. oldconffsetflags(pkg->installed.conffiles);
  149. printf(_("Removing %s ...\n"), pkg_name(pkg, pnaw_nonambig));
  150. log_action("remove", pkg, &pkg->installed);
  151. trig_activate_packageprocessing(pkg);
  152. if (pkg->status >= stat_halfconfigured) {
  153. static enum pkgstatus oldpkgstatus;
  154. oldpkgstatus= pkg->status;
  155. pkg_set_status(pkg, 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. /* Will turn into ‘half-installed’ soon ... */
  162. pkg_set_status(pkg, stat_unpacked);
  163. }
  164. removal_bulk(pkg);
  165. }
  166. static void push_leftover(struct fileinlist **leftoverp,
  167. struct filenamenode *namenode) {
  168. struct fileinlist *newentry;
  169. newentry= nfmalloc(sizeof(struct fileinlist));
  170. newentry->next= *leftoverp;
  171. newentry->namenode= namenode;
  172. *leftoverp= newentry;
  173. }
  174. static void
  175. removal_bulk_remove_file(const char *filename, const char *filetype)
  176. {
  177. /* We need the postrm and list files for --purge. */
  178. if (strcmp(filetype, LISTFILE) == 0 ||
  179. strcmp(filetype, POSTRMFILE) == 0)
  180. return;
  181. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  182. if (unlink(filename))
  183. ohshite(_("unable to delete control info file `%.250s'"), filename);
  184. debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
  185. }
  186. static bool
  187. removal_bulk_file_is_shared(struct pkginfo *pkg, struct filenamenode *namenode)
  188. {
  189. struct filepackages_iterator *iter;
  190. struct pkginfo *otherpkg;
  191. bool shared = false;
  192. if (pkgset_installed_instances(pkg->set) <= 1)
  193. return false;
  194. iter = filepackages_iter_new(namenode);
  195. while ((otherpkg = filepackages_iter_next(iter))) {
  196. if (otherpkg == pkg)
  197. continue;
  198. if (otherpkg->set != pkg->set)
  199. continue;
  200. debug(dbg_eachfiledetail, "removal_bulk file shared with %s, skipping",
  201. pkg_name(otherpkg, pnaw_always));
  202. shared = true;
  203. break;
  204. }
  205. filepackages_iter_free(iter);
  206. return shared;
  207. }
  208. static void
  209. removal_bulk_remove_files(struct pkginfo *pkg)
  210. {
  211. int before;
  212. struct reversefilelistiter rlistit;
  213. struct fileinlist *leftover;
  214. struct filenamenode *namenode;
  215. static struct varbuf fnvb;
  216. struct stat stab;
  217. pkg_set_status(pkg, stat_halfinstalled);
  218. modstatdb_note(pkg);
  219. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  220. reversefilelist_init(&rlistit,pkg->clientdata->files);
  221. leftover = NULL;
  222. while ((namenode= reversefilelist_next(&rlistit))) {
  223. struct filenamenode *usenode;
  224. bool is_dir;
  225. debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
  226. namenode->name, namenode->flags);
  227. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  228. varbuf_reset(&fnvb);
  229. varbuf_add_str(&fnvb, instdir);
  230. varbuf_add_str(&fnvb, usenode->name);
  231. varbuf_end_str(&fnvb);
  232. before= fnvb.used;
  233. is_dir = stat(fnvb.buf, &stab) == 0 && S_ISDIR(stab.st_mode);
  234. /* A pkgset can share files between its instances that we
  235. * don't want to remove, we just want to forget them. This
  236. * applies to shared conffiles too. */
  237. if (!is_dir && removal_bulk_file_is_shared(pkg, namenode))
  238. continue;
  239. /* Non-shared conffiles are kept. */
  240. if (namenode->flags & fnnf_old_conff) {
  241. push_leftover(&leftover, namenode);
  242. continue;
  243. }
  244. trig_file_activate(usenode, pkg);
  245. varbuf_trunc(&fnvb, before);
  246. varbuf_add_str(&fnvb, DPKGTEMPEXT);
  247. varbuf_end_str(&fnvb);
  248. debug(dbg_eachfiledetail, "removal_bulk cleaning temp '%s'", fnvb.buf);
  249. ensure_pathname_nonexisting(fnvb.buf);
  250. varbuf_trunc(&fnvb, before);
  251. varbuf_add_str(&fnvb, DPKGNEWEXT);
  252. varbuf_end_str(&fnvb);
  253. debug(dbg_eachfiledetail, "removal_bulk cleaning new '%s'", fnvb.buf);
  254. ensure_pathname_nonexisting(fnvb.buf);
  255. varbuf_trunc(&fnvb, before);
  256. varbuf_end_str(&fnvb);
  257. if (is_dir) {
  258. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  259. /* Only delete a directory or a link to one if we're the only
  260. * package which uses it. Other files should only be listed
  261. * in this package (but we don't check). */
  262. if (dir_has_conffiles(namenode, pkg)) {
  263. push_leftover(&leftover,namenode);
  264. continue;
  265. }
  266. if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
  267. push_leftover(&leftover, namenode);
  268. continue;
  269. }
  270. if (dir_is_used_by_others(namenode, pkg))
  271. continue;
  272. }
  273. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  274. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  275. if (errno == ENOTEMPTY || errno == EEXIST) {
  276. debug(dbg_eachfiledetail,
  277. "removal_bulk '%s' was not empty, will try again later",
  278. fnvb.buf);
  279. push_leftover(&leftover,namenode);
  280. continue;
  281. } else if (errno == EBUSY || errno == EPERM) {
  282. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  283. "%s - directory may be a mount point?"),
  284. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  285. push_leftover(&leftover,namenode);
  286. continue;
  287. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  288. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  289. fnvb.buf);
  290. push_leftover(&leftover, namenode);
  291. continue;
  292. }
  293. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  294. debug(dbg_eachfiledetail, "removal_bulk unlinking '%s'", fnvb.buf);
  295. if (secure_unlink(fnvb.buf))
  296. ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
  297. }
  298. write_filelist_except(pkg, &pkg->installed, leftover, 0);
  299. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  300. "remove", NULL);
  301. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  302. trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
  303. trig_file_interests_save();
  304. debug(dbg_general, "removal_bulk cleaning info directory");
  305. pkg_infodb_foreach(pkg, &pkg->installed, removal_bulk_remove_file);
  306. dir_sync_path(pkg_infodb_get_dir());
  307. pkg_set_status(pkg, stat_configfiles);
  308. pkg->installed.essential = false;
  309. modstatdb_note(pkg);
  310. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  311. }
  312. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  313. struct reversefilelistiter rlistit;
  314. struct fileinlist *leftover;
  315. struct filenamenode *namenode;
  316. static struct varbuf fnvb;
  317. struct stat stab;
  318. /* We may have modified this previously. */
  319. ensure_packagefiles_available(pkg);
  320. modstatdb_note(pkg);
  321. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  322. reversefilelist_init(&rlistit,pkg->clientdata->files);
  323. leftover = NULL;
  324. while ((namenode= reversefilelist_next(&rlistit))) {
  325. struct filenamenode *usenode;
  326. debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
  327. namenode->name, namenode->flags);
  328. if (namenode->flags & fnnf_old_conff) {
  329. /* This can only happen if removal_bulk_remove_configfiles() got
  330. * interrupted half way. */
  331. debug(dbg_eachfiledetail, "removal_bulk expecting only left over dirs, "
  332. "ignoring conffile '%s'", namenode->name);
  333. continue;
  334. }
  335. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  336. trig_file_activate(usenode, pkg);
  337. varbuf_reset(&fnvb);
  338. varbuf_add_str(&fnvb, instdir);
  339. varbuf_add_str(&fnvb, usenode->name);
  340. varbuf_end_str(&fnvb);
  341. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  342. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  343. /* Only delete a directory or a link to one if we're the only
  344. * package which uses it. Other files should only be listed
  345. * in this package (but we don't check). */
  346. if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
  347. push_leftover(&leftover, namenode);
  348. continue;
  349. }
  350. if (dir_is_used_by_others(namenode, pkg))
  351. continue;
  352. }
  353. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  354. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  355. if (errno == ENOTEMPTY || errno == EEXIST) {
  356. warning(_("while removing %.250s, directory '%.250s' not empty so not removed."),
  357. pkg_name(pkg, pnaw_nonambig), namenode->name);
  358. push_leftover(&leftover,namenode);
  359. continue;
  360. } else if (errno == EBUSY || errno == EPERM) {
  361. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  362. "%s - directory may be a mount point?"),
  363. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  364. push_leftover(&leftover,namenode);
  365. continue;
  366. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  367. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  368. fnvb.buf);
  369. push_leftover(&leftover, namenode);
  370. continue;
  371. }
  372. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  373. if (lstat(fnvb.buf, &stab) == 0 && S_ISLNK(stab.st_mode)) {
  374. debug(dbg_eachfiledetail, "removal_bulk is a symlink to a directory");
  375. if (unlink(fnvb.buf))
  376. ohshite(_("cannot remove '%.250s'"), fnvb.buf);
  377. continue;
  378. }
  379. push_leftover(&leftover,namenode);
  380. continue;
  381. }
  382. write_filelist_except(pkg, &pkg->installed, leftover, 0);
  383. modstatdb_note(pkg);
  384. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  385. }
  386. static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
  387. static const char *const removeconffexts[] = { REMOVECONFFEXTS, NULL };
  388. int r, removevbbase;
  389. int conffnameused, conffbasenamelen;
  390. char *conffbasename;
  391. struct conffile *conff, **lconffp;
  392. struct fileinlist *searchfile;
  393. DIR *dsd;
  394. struct dirent *de;
  395. char *p;
  396. const char *const *ext;
  397. printf(_("Purging configuration files for %s ...\n"),
  398. pkg_name(pkg, pnaw_nonambig));
  399. log_action("purge", pkg, &pkg->installed);
  400. trig_activate_packageprocessing(pkg);
  401. /* We may have modified this above. */
  402. ensure_packagefiles_available(pkg);
  403. /* We're about to remove the configuration, so remove the note
  404. * about which version it was ... */
  405. dpkg_version_blank(&pkg->configversion);
  406. modstatdb_note(pkg);
  407. /* Remove from our list any conffiles that aren't ours any more or
  408. * are involved in diversions, except if we are the package doing the
  409. * diverting. */
  410. for (lconffp = &pkg->installed.conffiles; (conff = *lconffp) != NULL; ) {
  411. for (searchfile= pkg->clientdata->files;
  412. searchfile && strcmp(searchfile->namenode->name,conff->name);
  413. searchfile= searchfile->next);
  414. if (!searchfile) {
  415. debug(dbg_conff, "removal_bulk conffile not ours any more '%s'",
  416. conff->name);
  417. *lconffp= conff->next;
  418. } else if (searchfile->namenode->divert &&
  419. (searchfile->namenode->divert->camefrom ||
  420. (searchfile->namenode->divert->useinstead &&
  421. searchfile->namenode->divert->pkgset != pkg->set))) {
  422. debug(dbg_conff, "removal_bulk conffile '%s' ignored due to diversion",
  423. conff->name);
  424. *lconffp= conff->next;
  425. } else {
  426. debug(dbg_conffdetail, "removal_bulk set to new conffile '%s'",
  427. conff->name);
  428. conff->hash = NEWCONFFILEFLAG;
  429. lconffp= &conff->next;
  430. }
  431. }
  432. modstatdb_note(pkg);
  433. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  434. static struct varbuf fnvb, removevb;
  435. if (conff->obsolete) {
  436. debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
  437. conff->name);
  438. }
  439. varbuf_reset(&fnvb);
  440. r= conffderef(pkg, &fnvb, conff->name);
  441. debug(dbg_conffdetail, "removal_bulk conffile '%s' (= '%s')",
  442. conff->name, r == -1 ? "<r==-1>" : fnvb.buf);
  443. if (r == -1) continue;
  444. conffnameused = fnvb.used;
  445. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  446. ohshite(_("cannot remove old config file `%.250s' (= `%.250s')"),
  447. conff->name, fnvb.buf);
  448. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  449. *p = '\0';
  450. varbuf_reset(&removevb);
  451. varbuf_add_str(&removevb, fnvb.buf);
  452. varbuf_add_char(&removevb, '/');
  453. removevbbase= removevb.used;
  454. varbuf_end_str(&removevb);
  455. dsd= opendir(removevb.buf);
  456. if (!dsd) {
  457. int e=errno;
  458. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  459. fnvb.buf, strerror(e)); errno= e;
  460. if (errno == ENOENT || errno == ENOTDIR) continue;
  461. ohshite(_("cannot read config file dir `%.250s' (from `%.250s')"),
  462. fnvb.buf, conff->name);
  463. }
  464. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  465. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  466. *p= '/';
  467. conffbasenamelen= strlen(++p);
  468. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  469. while ((de = readdir(dsd)) != NULL) {
  470. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry='%s'"
  471. " conffbasename='%s' conffnameused=%d conffbasenamelen=%d",
  472. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  473. if (strncmp(de->d_name, conffbasename, conffbasenamelen) == 0) {
  474. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  475. for (ext= removeconffexts; *ext; ext++)
  476. if (strcmp(*ext, de->d_name + conffbasenamelen) == 0)
  477. goto yes_remove;
  478. p= de->d_name+conffbasenamelen;
  479. if (*p++ == '~') {
  480. while (*p && cisdigit(*p)) p++;
  481. if (*p == '~' && !*++p) goto yes_remove;
  482. }
  483. }
  484. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  485. if (de->d_name[0] == '#' &&
  486. strncmp(de->d_name + 1, conffbasename, conffbasenamelen) == 0 &&
  487. strcmp(de->d_name + 1 + conffbasenamelen, "#") == 0)
  488. goto yes_remove;
  489. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  490. continue;
  491. yes_remove:
  492. varbuf_trunc(&removevb, removevbbase);
  493. varbuf_add_str(&removevb, de->d_name);
  494. varbuf_end_str(&removevb);
  495. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing '%s'",
  496. removevb.buf);
  497. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  498. ohshite(_("cannot remove old backup config file `%.250s' (of `%.250s')"),
  499. removevb.buf, conff->name);
  500. }
  501. pop_cleanup(ehflag_normaltidy); /* closedir */
  502. }
  503. /* Remove the conffiles from the file list file. */
  504. write_filelist_except(pkg, &pkg->installed, pkg->clientdata->files,
  505. fnnf_old_conff);
  506. pkg->installed.conffiles = NULL;
  507. modstatdb_note(pkg);
  508. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  509. "purge", NULL);
  510. }
  511. /*
  512. * This is used both by deferred_remove() in this file, and at the end of
  513. * process_archive() in archives.c if it needs to finish removing a
  514. * conflicting package.
  515. */
  516. void removal_bulk(struct pkginfo *pkg) {
  517. bool foundpostrm;
  518. debug(dbg_general, "removal_bulk package %s", pkg_name(pkg, pnaw_always));
  519. if (pkg->status == stat_halfinstalled || pkg->status == stat_unpacked) {
  520. removal_bulk_remove_files(pkg);
  521. }
  522. foundpostrm = pkg_infodb_has_file(pkg, &pkg->installed, POSTRMFILE);
  523. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  524. if (!foundpostrm && !pkg->installed.conffiles) {
  525. /* If there are no config files and no postrm script then we
  526. * go straight into ‘purge’. */
  527. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  528. pkg_set_want(pkg, want_purge);
  529. dpkg_version_blank(&pkg->configversion);
  530. } else if (pkg->want == want_purge) {
  531. removal_bulk_remove_configfiles(pkg);
  532. }
  533. /* I.e., either of the two branches above. */
  534. if (pkg->want == want_purge) {
  535. const char *filename;
  536. /* Retry empty directories, and warn on any leftovers that aren't. */
  537. removal_bulk_remove_leftover_dirs(pkg);
  538. filename = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  539. debug(dbg_general, "removal_bulk purge done, removing list '%s'",
  540. filename);
  541. if (unlink(filename) && errno != ENOENT)
  542. ohshite(_("cannot remove old files list"));
  543. filename = pkg_infodb_get_file(pkg, &pkg->installed, POSTRMFILE);
  544. debug(dbg_general, "removal_bulk purge done, removing postrm '%s'",
  545. filename);
  546. if (unlink(filename) && errno != ENOENT)
  547. ohshite(_("can't remove old postrm script"));
  548. pkg_set_status(pkg, stat_notinstalled);
  549. pkg_set_want(pkg, want_unknown);
  550. /* This will mess up reverse links, but if we follow them
  551. * we won't go back because pkg->status is stat_notinstalled. */
  552. pkgbin_blank(&pkg->installed);
  553. }
  554. pkg_reset_eflags(pkg);
  555. modstatdb_note(pkg);
  556. debug(dbg_general, "removal done");
  557. }