remove.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 == PKG_STAT_INSTALLED ||
  60. depender->status == PKG_STAT_TRIGGERSPENDING ||
  61. depender->status == PKG_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 &&
  72. depender->clientdata->istobe == PKG_ISTOBE_REMOVE)
  73. ok = DEP_CHECK_DEFER;
  74. if (ok == DEP_CHECK_DEFER)
  75. /* Don't burble about reasons for deferral. */
  76. varbuf_trunc(raemsgs, before);
  77. if (ok < *rokp) *rokp= ok;
  78. }
  79. }
  80. void deferred_remove(struct pkginfo *pkg) {
  81. struct varbuf raemsgs = VARBUF_INIT;
  82. struct dependency *dep;
  83. enum dep_check rok;
  84. debug(dbg_general, "deferred_remove package %s",
  85. pkg_name(pkg, pnaw_always));
  86. if (!f_pending && pkg->want != PKG_WANT_UNKNOWN) {
  87. if (cipaction->arg_int == act_purge)
  88. pkg_set_want(pkg, PKG_WANT_PURGE);
  89. else
  90. pkg_set_want(pkg, PKG_WANT_DEINSTALL);
  91. if (!f_noact)
  92. modstatdb_note(pkg);
  93. }
  94. if (pkg->status == PKG_STAT_NOTINSTALLED) {
  95. sincenothing = 0;
  96. warning(_("ignoring request to remove %.250s which isn't installed"),
  97. pkg_name(pkg, pnaw_nonambig));
  98. pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  99. return;
  100. } else if (!f_pending &&
  101. pkg->status == PKG_STAT_CONFIGFILES &&
  102. cipaction->arg_int != act_purge) {
  103. sincenothing = 0;
  104. warning(_("ignoring request to remove %.250s, only the config\n"
  105. " files of which are on the system; use --purge to remove them too"),
  106. pkg_name(pkg, pnaw_nonambig));
  107. pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  108. return;
  109. }
  110. if (pkg->installed.essential && pkg->status != PKG_STAT_CONFIGFILES)
  111. forcibleerr(fc_removeessential,
  112. _("this is an essential package; it should not be removed"));
  113. debug(dbg_general, "checking dependencies for remove '%s'",
  114. pkg_name(pkg, pnaw_always));
  115. rok = DEP_CHECK_OK;
  116. checkforremoval(pkg, pkg->set, &rok, &raemsgs);
  117. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  118. if (dep->type != dep_provides) continue;
  119. debug(dbg_depcon, "checking virtual package '%s'", dep->list->ed->name);
  120. checkforremoval(pkg, dep->list->ed, &rok, &raemsgs);
  121. }
  122. if (rok == DEP_CHECK_DEFER) {
  123. varbuf_destroy(&raemsgs);
  124. pkg->clientdata->istobe = PKG_ISTOBE_REMOVE;
  125. enqueue_package(pkg);
  126. return;
  127. } else if (rok == DEP_CHECK_HALT) {
  128. sincenothing= 0;
  129. varbuf_end_str(&raemsgs);
  130. notice(_("dependency problems prevent removal of %s:\n%s"),
  131. pkg_name(pkg, pnaw_nonambig), raemsgs.buf);
  132. ohshit(_("dependency problems - not removing"));
  133. } else if (raemsgs.used) {
  134. varbuf_end_str(&raemsgs);
  135. notice(_("%s: dependency problems, but removing anyway as you requested:\n%s"),
  136. pkg_name(pkg, pnaw_nonambig), raemsgs.buf);
  137. }
  138. varbuf_destroy(&raemsgs);
  139. sincenothing= 0;
  140. if (pkg->eflag & PKG_EFLAG_REINSTREQ)
  141. forcibleerr(fc_removereinstreq,
  142. _("package is in a very bad inconsistent state; you should\n"
  143. " reinstall it before attempting a removal"));
  144. ensure_allinstfiles_available();
  145. filesdbinit();
  146. if (f_noact) {
  147. printf(_("Would remove or purge %s (%s) ...\n"),
  148. pkg_name(pkg, pnaw_nonambig),
  149. versiondescribe(&pkg->installed.version, vdew_nonambig));
  150. pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
  151. pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  152. return;
  153. }
  154. oldconffsetflags(pkg->installed.conffiles);
  155. printf(_("Removing %s (%s) ...\n"), pkg_name(pkg, pnaw_nonambig),
  156. versiondescribe(&pkg->installed.version, vdew_nonambig));
  157. log_action("remove", pkg, &pkg->installed);
  158. trig_activate_packageprocessing(pkg);
  159. if (pkg->status >= PKG_STAT_HALFCONFIGURED) {
  160. static enum pkgstatus oldpkgstatus;
  161. oldpkgstatus= pkg->status;
  162. pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
  163. modstatdb_note(pkg);
  164. push_cleanup(cu_prermremove, ~ehflag_normaltidy, NULL, 0, 2,
  165. (void *)pkg, (void *)&oldpkgstatus);
  166. maintscript_installed(pkg, PRERMFILE, "pre-removal", "remove", NULL);
  167. /* Will turn into ‘half-installed’ soon ... */
  168. pkg_set_status(pkg, PKG_STAT_UNPACKED);
  169. }
  170. removal_bulk(pkg);
  171. }
  172. static void push_leftover(struct fileinlist **leftoverp,
  173. struct filenamenode *namenode) {
  174. struct fileinlist *newentry;
  175. newentry= nfmalloc(sizeof(struct fileinlist));
  176. newentry->next= *leftoverp;
  177. newentry->namenode= namenode;
  178. *leftoverp= newentry;
  179. }
  180. static void
  181. removal_bulk_remove_file(const char *filename, const char *filetype)
  182. {
  183. /* We need the postrm and list files for --purge. */
  184. if (strcmp(filetype, LISTFILE) == 0 ||
  185. strcmp(filetype, POSTRMFILE) == 0)
  186. return;
  187. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  188. if (unlink(filename))
  189. ohshite(_("unable to delete control info file `%.250s'"), filename);
  190. debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
  191. }
  192. static bool
  193. removal_bulk_file_is_shared(struct pkginfo *pkg, struct filenamenode *namenode)
  194. {
  195. struct filepackages_iterator *iter;
  196. struct pkginfo *otherpkg;
  197. bool shared = false;
  198. if (pkgset_installed_instances(pkg->set) <= 1)
  199. return false;
  200. iter = filepackages_iter_new(namenode);
  201. while ((otherpkg = filepackages_iter_next(iter))) {
  202. if (otherpkg == pkg)
  203. continue;
  204. if (otherpkg->set != pkg->set)
  205. continue;
  206. debug(dbg_eachfiledetail, "removal_bulk file shared with %s, skipping",
  207. pkg_name(otherpkg, pnaw_always));
  208. shared = true;
  209. break;
  210. }
  211. filepackages_iter_free(iter);
  212. return shared;
  213. }
  214. static void
  215. removal_bulk_remove_files(struct pkginfo *pkg)
  216. {
  217. int before;
  218. struct reversefilelistiter rlistit;
  219. struct fileinlist *leftover;
  220. struct filenamenode *namenode;
  221. static struct varbuf fnvb;
  222. struct stat stab;
  223. pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
  224. modstatdb_note(pkg);
  225. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  226. reversefilelist_init(&rlistit,pkg->clientdata->files);
  227. leftover = NULL;
  228. while ((namenode= reversefilelist_next(&rlistit))) {
  229. struct filenamenode *usenode;
  230. bool is_dir;
  231. debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
  232. namenode->name, namenode->flags);
  233. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  234. varbuf_reset(&fnvb);
  235. varbuf_add_str(&fnvb, instdir);
  236. varbuf_add_str(&fnvb, usenode->name);
  237. varbuf_end_str(&fnvb);
  238. before= fnvb.used;
  239. is_dir = stat(fnvb.buf, &stab) == 0 && S_ISDIR(stab.st_mode);
  240. /* A pkgset can share files between its instances that we
  241. * don't want to remove, we just want to forget them. This
  242. * applies to shared conffiles too. */
  243. if (!is_dir && removal_bulk_file_is_shared(pkg, namenode))
  244. continue;
  245. /* Non-shared conffiles are kept. */
  246. if (namenode->flags & fnnf_old_conff) {
  247. push_leftover(&leftover, namenode);
  248. continue;
  249. }
  250. if (is_dir) {
  251. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  252. /* Only delete a directory or a link to one if we're the only
  253. * package which uses it. Other files should only be listed
  254. * in this package (but we don't check). */
  255. if (dir_has_conffiles(namenode, pkg)) {
  256. push_leftover(&leftover,namenode);
  257. continue;
  258. }
  259. if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
  260. push_leftover(&leftover, namenode);
  261. continue;
  262. }
  263. if (dir_is_used_by_others(namenode, pkg))
  264. continue;
  265. }
  266. trig_path_activate(usenode, pkg);
  267. varbuf_trunc(&fnvb, before);
  268. varbuf_add_str(&fnvb, DPKGTEMPEXT);
  269. varbuf_end_str(&fnvb);
  270. debug(dbg_eachfiledetail, "removal_bulk cleaning temp '%s'", fnvb.buf);
  271. ensure_pathname_nonexisting(fnvb.buf);
  272. varbuf_trunc(&fnvb, before);
  273. varbuf_add_str(&fnvb, DPKGNEWEXT);
  274. varbuf_end_str(&fnvb);
  275. debug(dbg_eachfiledetail, "removal_bulk cleaning new '%s'", fnvb.buf);
  276. ensure_pathname_nonexisting(fnvb.buf);
  277. varbuf_trunc(&fnvb, before);
  278. varbuf_end_str(&fnvb);
  279. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  280. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  281. if (errno == ENOTEMPTY || errno == EEXIST) {
  282. debug(dbg_eachfiledetail,
  283. "removal_bulk '%s' was not empty, will try again later",
  284. fnvb.buf);
  285. push_leftover(&leftover,namenode);
  286. continue;
  287. } else if (errno == EBUSY || errno == EPERM) {
  288. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  289. "%s - directory may be a mount point?"),
  290. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  291. push_leftover(&leftover,namenode);
  292. continue;
  293. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  294. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  295. fnvb.buf);
  296. push_leftover(&leftover, namenode);
  297. continue;
  298. }
  299. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  300. debug(dbg_eachfiledetail, "removal_bulk unlinking '%s'", fnvb.buf);
  301. if (secure_unlink(fnvb.buf))
  302. ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
  303. }
  304. write_filelist_except(pkg, &pkg->installed, leftover, 0);
  305. maintscript_installed(pkg, POSTRMFILE, "post-removal", "remove", NULL);
  306. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  307. trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
  308. trig_file_interests_save();
  309. debug(dbg_general, "removal_bulk cleaning info directory");
  310. pkg_infodb_foreach(pkg, &pkg->installed, removal_bulk_remove_file);
  311. dir_sync_path(pkg_infodb_get_dir());
  312. pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
  313. pkg->installed.essential = false;
  314. modstatdb_note(pkg);
  315. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  316. }
  317. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  318. struct reversefilelistiter rlistit;
  319. struct fileinlist *leftover;
  320. struct filenamenode *namenode;
  321. static struct varbuf fnvb;
  322. struct stat stab;
  323. /* We may have modified this previously. */
  324. ensure_packagefiles_available(pkg);
  325. modstatdb_note(pkg);
  326. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  327. reversefilelist_init(&rlistit,pkg->clientdata->files);
  328. leftover = NULL;
  329. while ((namenode= reversefilelist_next(&rlistit))) {
  330. struct filenamenode *usenode;
  331. debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
  332. namenode->name, namenode->flags);
  333. if (namenode->flags & fnnf_old_conff) {
  334. /* This can only happen if removal_bulk_remove_configfiles() got
  335. * interrupted half way. */
  336. debug(dbg_eachfiledetail, "removal_bulk expecting only left over dirs, "
  337. "ignoring conffile '%s'", namenode->name);
  338. continue;
  339. }
  340. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  341. varbuf_reset(&fnvb);
  342. varbuf_add_str(&fnvb, instdir);
  343. varbuf_add_str(&fnvb, usenode->name);
  344. varbuf_end_str(&fnvb);
  345. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  346. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  347. /* Only delete a directory or a link to one if we're the only
  348. * package which uses it. Other files should only be listed
  349. * in this package (but we don't check). */
  350. if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
  351. push_leftover(&leftover, namenode);
  352. continue;
  353. }
  354. if (dir_is_used_by_others(namenode, pkg))
  355. continue;
  356. }
  357. trig_path_activate(usenode, pkg);
  358. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  359. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  360. if (errno == ENOTEMPTY || errno == EEXIST) {
  361. warning(_("while removing %.250s, directory '%.250s' not empty so not removed"),
  362. pkg_name(pkg, pnaw_nonambig), namenode->name);
  363. push_leftover(&leftover,namenode);
  364. continue;
  365. } else if (errno == EBUSY || errno == EPERM) {
  366. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  367. "%s - directory may be a mount point?"),
  368. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  369. push_leftover(&leftover,namenode);
  370. continue;
  371. } else if (errno == EINVAL && strcmp(usenode->name, "/.") == 0) {
  372. debug(dbg_eachfiledetail, "removal_bulk '%s' root directory, cannot remove",
  373. fnvb.buf);
  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 && cisdigit(*p)) p++;
  488. if (*p == '~' && !*++p) goto yes_remove;
  489. }
  490. }
  491. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  492. if (de->d_name[0] == '#' &&
  493. strncmp(de->d_name + 1, conffbasename, conffbasenamelen) == 0 &&
  494. strcmp(de->d_name + 1 + conffbasenamelen, "#") == 0)
  495. goto yes_remove;
  496. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  497. continue;
  498. yes_remove:
  499. varbuf_trunc(&removevb, removevbbase);
  500. varbuf_add_str(&removevb, de->d_name);
  501. varbuf_end_str(&removevb);
  502. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing '%s'",
  503. removevb.buf);
  504. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  505. ohshite(_("cannot remove old backup config file `%.250s' (of `%.250s')"),
  506. removevb.buf, conff->name);
  507. }
  508. pop_cleanup(ehflag_normaltidy); /* closedir */
  509. }
  510. /* Remove the conffiles from the file list file. */
  511. write_filelist_except(pkg, &pkg->installed, pkg->clientdata->files,
  512. fnnf_old_conff);
  513. pkg->installed.conffiles = NULL;
  514. modstatdb_note(pkg);
  515. maintscript_installed(pkg, POSTRMFILE, "post-removal", "purge", NULL);
  516. }
  517. /*
  518. * This is used both by deferred_remove() in this file, and at the end of
  519. * process_archive() in archives.c if it needs to finish removing a
  520. * conflicting package.
  521. */
  522. void removal_bulk(struct pkginfo *pkg) {
  523. bool foundpostrm;
  524. debug(dbg_general, "removal_bulk package %s", pkg_name(pkg, pnaw_always));
  525. if (pkg->status == PKG_STAT_HALFINSTALLED ||
  526. pkg->status == PKG_STAT_UNPACKED) {
  527. removal_bulk_remove_files(pkg);
  528. }
  529. foundpostrm = pkg_infodb_has_file(pkg, &pkg->installed, POSTRMFILE);
  530. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  531. if (!foundpostrm && !pkg->installed.conffiles) {
  532. /* If there are no config files and no postrm script then we
  533. * go straight into ‘purge’. */
  534. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  535. pkg_set_want(pkg, PKG_WANT_PURGE);
  536. dpkg_version_blank(&pkg->configversion);
  537. } else if (pkg->want == PKG_WANT_PURGE) {
  538. removal_bulk_remove_configfiles(pkg);
  539. }
  540. /* I.e., either of the two branches above. */
  541. if (pkg->want == PKG_WANT_PURGE) {
  542. const char *filename;
  543. /* Retry empty directories, and warn on any leftovers that aren't. */
  544. removal_bulk_remove_leftover_dirs(pkg);
  545. filename = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  546. debug(dbg_general, "removal_bulk purge done, removing list '%s'",
  547. filename);
  548. if (unlink(filename) && errno != ENOENT)
  549. ohshite(_("cannot remove old files list"));
  550. filename = pkg_infodb_get_file(pkg, &pkg->installed, POSTRMFILE);
  551. debug(dbg_general, "removal_bulk purge done, removing postrm '%s'",
  552. filename);
  553. if (unlink(filename) && errno != ENOENT)
  554. ohshite(_("can't remove old postrm script"));
  555. pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
  556. pkg_set_want(pkg, PKG_WANT_UNKNOWN);
  557. /* This will mess up reverse links, but if we follow them
  558. * we won't go back because pkg->status is PKG_STAT_NOTINSTALLED. */
  559. pkgbin_blank(&pkg->installed);
  560. }
  561. pkg_reset_eflags(pkg);
  562. modstatdb_note(pkg);
  563. debug(dbg_general, "removal done");
  564. }