remove.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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-2013 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 (!f_pending && pkg->want != want_unknown) {
  84. if (cipaction->arg_int == act_purge)
  85. pkg_set_want(pkg, want_purge);
  86. else
  87. pkg_set_want(pkg, want_deinstall);
  88. if (!f_noact)
  89. modstatdb_note(pkg);
  90. }
  91. if (pkg->status == stat_notinstalled) {
  92. warning(_("ignoring request to remove %.250s which isn't installed"),
  93. pkg_name(pkg, pnaw_nonambig));
  94. pkg->clientdata->istobe= itb_normal;
  95. return;
  96. } else if (!f_pending &&
  97. pkg->status == stat_configfiles &&
  98. cipaction->arg_int != act_purge) {
  99. warning(_("ignoring request to remove %.250s, only the config\n"
  100. " files of which are on the system; use --purge to remove them too"),
  101. pkg_name(pkg, pnaw_nonambig));
  102. pkg->clientdata->istobe= itb_normal;
  103. return;
  104. }
  105. if (pkg->installed.essential && pkg->status != stat_configfiles)
  106. forcibleerr(fc_removeessential, _("This is an essential package -"
  107. " it should not be removed."));
  108. debug(dbg_general, "checking dependencies for remove '%s'",
  109. pkg_name(pkg, pnaw_always));
  110. rok= 2;
  111. checkforremoval(pkg, pkg->set, &rok, &raemsgs);
  112. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  113. if (dep->type != dep_provides) continue;
  114. debug(dbg_depcon, "checking virtual package '%s'", dep->list->ed->name);
  115. checkforremoval(pkg, dep->list->ed, &rok, &raemsgs);
  116. }
  117. if (rok == 1) {
  118. varbuf_destroy(&raemsgs);
  119. pkg->clientdata->istobe= itb_remove;
  120. add_to_queue(pkg);
  121. return;
  122. } else if (rok == 0) {
  123. sincenothing= 0;
  124. varbuf_end_str(&raemsgs);
  125. notice(_("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. notice(_("%s: dependency problems, but removing anyway as you requested:\n%s"),
  131. pkg_name(pkg, pnaw_nonambig), raemsgs.buf);
  132. }
  133. varbuf_destroy(&raemsgs);
  134. sincenothing= 0;
  135. if (pkg->eflag & eflag_reinstreq)
  136. forcibleerr(fc_removereinstreq,
  137. _("Package is in a very bad inconsistent state - you should\n"
  138. " reinstall it before attempting a removal."));
  139. ensure_allinstfiles_available();
  140. filesdbinit();
  141. if (f_noact) {
  142. printf(_("Would remove or purge %s ...\n"), pkg_name(pkg, pnaw_nonambig));
  143. pkg_set_status(pkg, stat_notinstalled);
  144. pkg->clientdata->istobe= itb_normal;
  145. return;
  146. }
  147. oldconffsetflags(pkg->installed.conffiles);
  148. printf(_("Removing %s ...\n"), pkg_name(pkg, pnaw_nonambig));
  149. log_action("remove", pkg, &pkg->installed);
  150. trig_activate_packageprocessing(pkg);
  151. if (pkg->status >= stat_halfconfigured) {
  152. static enum pkgstatus oldpkgstatus;
  153. oldpkgstatus= pkg->status;
  154. pkg_set_status(pkg, stat_halfconfigured);
  155. modstatdb_note(pkg);
  156. push_cleanup(cu_prermremove, ~ehflag_normaltidy, NULL, 0, 2,
  157. (void *)pkg, (void *)&oldpkgstatus);
  158. maintainer_script_installed(pkg, PRERMFILE, "pre-removal",
  159. "remove", NULL);
  160. /* Will turn into ‘half-installed’ soon ... */
  161. pkg_set_status(pkg, stat_unpacked);
  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
  174. removal_bulk_remove_file(const char *filename, const char *filetype)
  175. {
  176. /* We need the postrm and list files for --purge. */
  177. if (strcmp(filetype, LISTFILE) == 0 ||
  178. strcmp(filetype, POSTRMFILE) == 0)
  179. return;
  180. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  181. if (unlink(filename))
  182. ohshite(_("unable to delete control info file `%.250s'"), filename);
  183. debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
  184. }
  185. static bool
  186. removal_bulk_file_is_shared(struct pkginfo *pkg, struct filenamenode *namenode)
  187. {
  188. struct filepackages_iterator *iter;
  189. struct pkginfo *otherpkg;
  190. bool shared = false;
  191. if (pkgset_installed_instances(pkg->set) <= 1)
  192. return false;
  193. iter = filepackages_iter_new(namenode);
  194. while ((otherpkg = filepackages_iter_next(iter))) {
  195. if (otherpkg == pkg)
  196. continue;
  197. if (otherpkg->set != pkg->set)
  198. continue;
  199. debug(dbg_eachfiledetail, "removal_bulk file shared with %s, skipping",
  200. pkg_name(otherpkg, pnaw_always));
  201. shared = true;
  202. break;
  203. }
  204. filepackages_iter_free(iter);
  205. return shared;
  206. }
  207. static void
  208. removal_bulk_remove_files(struct pkginfo *pkg)
  209. {
  210. int before;
  211. struct reversefilelistiter rlistit;
  212. struct fileinlist *leftover;
  213. struct filenamenode *namenode;
  214. static struct varbuf fnvb;
  215. struct stat stab;
  216. pkg_set_status(pkg, stat_halfinstalled);
  217. modstatdb_note(pkg);
  218. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  219. reversefilelist_init(&rlistit,pkg->clientdata->files);
  220. leftover = NULL;
  221. while ((namenode= reversefilelist_next(&rlistit))) {
  222. struct filenamenode *usenode;
  223. bool is_dir;
  224. debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
  225. namenode->name, namenode->flags);
  226. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  227. varbuf_reset(&fnvb);
  228. varbuf_add_str(&fnvb, instdir);
  229. varbuf_add_str(&fnvb, usenode->name);
  230. varbuf_end_str(&fnvb);
  231. before= fnvb.used;
  232. is_dir = stat(fnvb.buf, &stab) == 0 && S_ISDIR(stab.st_mode);
  233. /* A pkgset can share files between its instances that we
  234. * don't want to remove, we just want to forget them. This
  235. * applies to shared conffiles too. */
  236. if (!is_dir && removal_bulk_file_is_shared(pkg, namenode))
  237. continue;
  238. /* Non-shared conffiles are kept. */
  239. if (namenode->flags & fnnf_old_conff) {
  240. push_leftover(&leftover, namenode);
  241. continue;
  242. }
  243. if (is_dir) {
  244. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  245. /* Only delete a directory or a link to one if we're the only
  246. * package which uses it. Other files should only be listed
  247. * in this package (but we don't check). */
  248. if (dir_has_conffiles(namenode, pkg)) {
  249. push_leftover(&leftover,namenode);
  250. continue;
  251. }
  252. if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
  253. push_leftover(&leftover, namenode);
  254. continue;
  255. }
  256. if (dir_is_used_by_others(namenode, pkg))
  257. continue;
  258. }
  259. trig_file_activate(usenode, pkg);
  260. varbuf_trunc(&fnvb, before);
  261. varbuf_add_str(&fnvb, DPKGTEMPEXT);
  262. varbuf_end_str(&fnvb);
  263. debug(dbg_eachfiledetail, "removal_bulk cleaning temp '%s'", fnvb.buf);
  264. ensure_pathname_nonexisting(fnvb.buf);
  265. varbuf_trunc(&fnvb, before);
  266. varbuf_add_str(&fnvb, DPKGNEWEXT);
  267. varbuf_end_str(&fnvb);
  268. debug(dbg_eachfiledetail, "removal_bulk cleaning new '%s'", fnvb.buf);
  269. ensure_pathname_nonexisting(fnvb.buf);
  270. varbuf_trunc(&fnvb, before);
  271. varbuf_end_str(&fnvb);
  272. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  273. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  274. if (errno == ENOTEMPTY || errno == EEXIST) {
  275. debug(dbg_eachfiledetail,
  276. "removal_bulk '%s' was not empty, will try again later",
  277. fnvb.buf);
  278. push_leftover(&leftover,namenode);
  279. continue;
  280. } else if (errno == EBUSY || errno == EPERM) {
  281. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  282. "%s - directory may be a mount point?"),
  283. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  284. push_leftover(&leftover,namenode);
  285. continue;
  286. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  287. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  288. fnvb.buf);
  289. push_leftover(&leftover, namenode);
  290. continue;
  291. }
  292. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  293. debug(dbg_eachfiledetail, "removal_bulk unlinking '%s'", fnvb.buf);
  294. if (secure_unlink(fnvb.buf))
  295. ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
  296. }
  297. write_filelist_except(pkg, &pkg->installed, leftover, 0);
  298. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  299. "remove", NULL);
  300. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  301. trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
  302. trig_file_interests_save();
  303. debug(dbg_general, "removal_bulk cleaning info directory");
  304. pkg_infodb_foreach(pkg, &pkg->installed, removal_bulk_remove_file);
  305. dir_sync_path(pkg_infodb_get_dir());
  306. pkg_set_status(pkg, stat_configfiles);
  307. pkg->installed.essential = false;
  308. modstatdb_note(pkg);
  309. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  310. }
  311. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  312. struct reversefilelistiter rlistit;
  313. struct fileinlist *leftover;
  314. struct filenamenode *namenode;
  315. static struct varbuf fnvb;
  316. struct stat stab;
  317. /* We may have modified this previously. */
  318. ensure_packagefiles_available(pkg);
  319. modstatdb_note(pkg);
  320. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  321. reversefilelist_init(&rlistit,pkg->clientdata->files);
  322. leftover = NULL;
  323. while ((namenode= reversefilelist_next(&rlistit))) {
  324. struct filenamenode *usenode;
  325. debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
  326. namenode->name, namenode->flags);
  327. if (namenode->flags & fnnf_old_conff) {
  328. /* This can only happen if removal_bulk_remove_configfiles() got
  329. * interrupted half way. */
  330. debug(dbg_eachfiledetail, "removal_bulk expecting only left over dirs, "
  331. "ignoring conffile '%s'", namenode->name);
  332. continue;
  333. }
  334. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  335. varbuf_reset(&fnvb);
  336. varbuf_add_str(&fnvb, instdir);
  337. varbuf_add_str(&fnvb, usenode->name);
  338. varbuf_end_str(&fnvb);
  339. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  340. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  341. /* Only delete a directory or a link to one if we're the only
  342. * package which uses it. Other files should only be listed
  343. * in this package (but we don't check). */
  344. if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
  345. push_leftover(&leftover, namenode);
  346. continue;
  347. }
  348. if (dir_is_used_by_others(namenode, pkg))
  349. continue;
  350. }
  351. trig_file_activate(usenode, pkg);
  352. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  353. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  354. if (errno == ENOTEMPTY || errno == EEXIST) {
  355. warning(_("while removing %.250s, directory '%.250s' not empty so not removed"),
  356. pkg_name(pkg, pnaw_nonambig), namenode->name);
  357. push_leftover(&leftover,namenode);
  358. continue;
  359. } else if (errno == EBUSY || errno == EPERM) {
  360. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  361. "%s - directory may be a mount point?"),
  362. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  363. push_leftover(&leftover,namenode);
  364. continue;
  365. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  366. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  367. fnvb.buf);
  368. push_leftover(&leftover, namenode);
  369. continue;
  370. }
  371. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  372. if (lstat(fnvb.buf, &stab) == 0 && S_ISLNK(stab.st_mode)) {
  373. debug(dbg_eachfiledetail, "removal_bulk is a symlink to a directory");
  374. if (unlink(fnvb.buf))
  375. ohshite(_("cannot remove '%.250s'"), fnvb.buf);
  376. continue;
  377. }
  378. push_leftover(&leftover,namenode);
  379. continue;
  380. }
  381. write_filelist_except(pkg, &pkg->installed, leftover, 0);
  382. modstatdb_note(pkg);
  383. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  384. }
  385. static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
  386. static const char *const removeconffexts[] = { REMOVECONFFEXTS, NULL };
  387. int r, removevbbase;
  388. int conffnameused, conffbasenamelen;
  389. char *conffbasename;
  390. struct conffile *conff, **lconffp;
  391. struct fileinlist *searchfile;
  392. DIR *dsd;
  393. struct dirent *de;
  394. char *p;
  395. const char *const *ext;
  396. printf(_("Purging configuration files for %s ...\n"),
  397. pkg_name(pkg, pnaw_nonambig));
  398. log_action("purge", pkg, &pkg->installed);
  399. trig_activate_packageprocessing(pkg);
  400. /* We may have modified this above. */
  401. ensure_packagefiles_available(pkg);
  402. /* We're about to remove the configuration, so remove the note
  403. * about which version it was ... */
  404. dpkg_version_blank(&pkg->configversion);
  405. modstatdb_note(pkg);
  406. /* Remove from our list any conffiles that aren't ours any more or
  407. * are involved in diversions, except if we are the package doing the
  408. * diverting. */
  409. for (lconffp = &pkg->installed.conffiles; (conff = *lconffp) != NULL; ) {
  410. for (searchfile= pkg->clientdata->files;
  411. searchfile && strcmp(searchfile->namenode->name,conff->name);
  412. searchfile= searchfile->next);
  413. if (!searchfile) {
  414. debug(dbg_conff, "removal_bulk conffile not ours any more '%s'",
  415. conff->name);
  416. *lconffp= conff->next;
  417. } else if (searchfile->namenode->divert &&
  418. (searchfile->namenode->divert->camefrom ||
  419. (searchfile->namenode->divert->useinstead &&
  420. searchfile->namenode->divert->pkgset != pkg->set))) {
  421. debug(dbg_conff, "removal_bulk conffile '%s' ignored due to diversion",
  422. conff->name);
  423. *lconffp= conff->next;
  424. } else {
  425. debug(dbg_conffdetail, "removal_bulk set to new conffile '%s'",
  426. conff->name);
  427. conff->hash = NEWCONFFILEFLAG;
  428. lconffp= &conff->next;
  429. }
  430. }
  431. modstatdb_note(pkg);
  432. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  433. static struct varbuf fnvb, removevb;
  434. if (conff->obsolete) {
  435. debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
  436. conff->name);
  437. }
  438. varbuf_reset(&fnvb);
  439. r= conffderef(pkg, &fnvb, conff->name);
  440. debug(dbg_conffdetail, "removal_bulk conffile '%s' (= '%s')",
  441. conff->name, r == -1 ? "<r==-1>" : fnvb.buf);
  442. if (r == -1) continue;
  443. conffnameused = fnvb.used;
  444. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  445. ohshite(_("cannot remove old config file `%.250s' (= `%.250s')"),
  446. conff->name, fnvb.buf);
  447. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  448. *p = '\0';
  449. varbuf_reset(&removevb);
  450. varbuf_add_str(&removevb, fnvb.buf);
  451. varbuf_add_char(&removevb, '/');
  452. removevbbase= removevb.used;
  453. varbuf_end_str(&removevb);
  454. dsd= opendir(removevb.buf);
  455. if (!dsd) {
  456. int e=errno;
  457. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  458. fnvb.buf, strerror(e)); errno= e;
  459. if (errno == ENOENT || errno == ENOTDIR) continue;
  460. ohshite(_("cannot read config file dir `%.250s' (from `%.250s')"),
  461. fnvb.buf, conff->name);
  462. }
  463. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  464. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  465. *p= '/';
  466. conffbasenamelen= strlen(++p);
  467. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  468. while ((de = readdir(dsd)) != NULL) {
  469. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry='%s'"
  470. " conffbasename='%s' conffnameused=%d conffbasenamelen=%d",
  471. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  472. if (strncmp(de->d_name, conffbasename, conffbasenamelen) == 0) {
  473. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  474. for (ext= removeconffexts; *ext; ext++)
  475. if (strcmp(*ext, de->d_name + conffbasenamelen) == 0)
  476. goto yes_remove;
  477. p= de->d_name+conffbasenamelen;
  478. if (*p++ == '~') {
  479. while (*p && cisdigit(*p)) p++;
  480. if (*p == '~' && !*++p) goto yes_remove;
  481. }
  482. }
  483. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  484. if (de->d_name[0] == '#' &&
  485. strncmp(de->d_name + 1, conffbasename, conffbasenamelen) == 0 &&
  486. strcmp(de->d_name + 1 + conffbasenamelen, "#") == 0)
  487. goto yes_remove;
  488. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  489. continue;
  490. yes_remove:
  491. varbuf_trunc(&removevb, removevbbase);
  492. varbuf_add_str(&removevb, de->d_name);
  493. varbuf_end_str(&removevb);
  494. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing '%s'",
  495. removevb.buf);
  496. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  497. ohshite(_("cannot remove old backup config file `%.250s' (of `%.250s')"),
  498. removevb.buf, conff->name);
  499. }
  500. pop_cleanup(ehflag_normaltidy); /* closedir */
  501. }
  502. /* Remove the conffiles from the file list file. */
  503. write_filelist_except(pkg, &pkg->installed, pkg->clientdata->files,
  504. fnnf_old_conff);
  505. pkg->installed.conffiles = NULL;
  506. modstatdb_note(pkg);
  507. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  508. "purge", NULL);
  509. }
  510. /*
  511. * This is used both by deferred_remove() in this file, and at the end of
  512. * process_archive() in archives.c if it needs to finish removing a
  513. * conflicting package.
  514. */
  515. void removal_bulk(struct pkginfo *pkg) {
  516. bool foundpostrm;
  517. debug(dbg_general, "removal_bulk package %s", pkg_name(pkg, pnaw_always));
  518. if (pkg->status == stat_halfinstalled || pkg->status == stat_unpacked) {
  519. removal_bulk_remove_files(pkg);
  520. }
  521. foundpostrm = pkg_infodb_has_file(pkg, &pkg->installed, POSTRMFILE);
  522. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  523. if (!foundpostrm && !pkg->installed.conffiles) {
  524. /* If there are no config files and no postrm script then we
  525. * go straight into ‘purge’. */
  526. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  527. pkg_set_want(pkg, want_purge);
  528. dpkg_version_blank(&pkg->configversion);
  529. } else if (pkg->want == want_purge) {
  530. removal_bulk_remove_configfiles(pkg);
  531. }
  532. /* I.e., either of the two branches above. */
  533. if (pkg->want == want_purge) {
  534. const char *filename;
  535. /* Retry empty directories, and warn on any leftovers that aren't. */
  536. removal_bulk_remove_leftover_dirs(pkg);
  537. filename = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  538. debug(dbg_general, "removal_bulk purge done, removing list '%s'",
  539. filename);
  540. if (unlink(filename) && errno != ENOENT)
  541. ohshite(_("cannot remove old files list"));
  542. filename = pkg_infodb_get_file(pkg, &pkg->installed, POSTRMFILE);
  543. debug(dbg_general, "removal_bulk purge done, removing postrm '%s'",
  544. filename);
  545. if (unlink(filename) && errno != ENOENT)
  546. ohshite(_("can't remove old postrm script"));
  547. pkg_set_status(pkg, stat_notinstalled);
  548. pkg_set_want(pkg, want_unknown);
  549. /* This will mess up reverse links, but if we follow them
  550. * we won't go back because pkg->status is stat_notinstalled. */
  551. pkgbin_blank(&pkg->installed);
  552. }
  553. pkg_reset_eflags(pkg);
  554. modstatdb_note(pkg);
  555. debug(dbg_general, "removal done");
  556. }