remove.c 22 KB

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