remove.c 23 KB

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