remove.c 23 KB

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