remove.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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-2015 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/path.h>
  38. #include <dpkg/dir.h>
  39. #include <dpkg/options.h>
  40. #include <dpkg/triglib.h>
  41. #include "infodb.h"
  42. #include "filesdb.h"
  43. #include "main.h"
  44. /*
  45. * pkgdepcheck may be a virtual pkg.
  46. */
  47. static void checkforremoval(struct pkginfo *pkgtoremove,
  48. struct pkgset *pkgdepcheck,
  49. enum dep_check *rokp, struct varbuf *raemsgs)
  50. {
  51. struct deppossi *possi;
  52. struct pkginfo *depender;
  53. enum dep_check ok;
  54. int before;
  55. for (possi = pkgdepcheck->depended.installed; possi; possi = possi->rev_next) {
  56. if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
  57. depender= possi->up->up;
  58. debug(dbg_depcon, "checking depending package '%s'",
  59. pkg_name(depender, pnaw_always));
  60. if (depender->status < PKG_STAT_UNPACKED)
  61. continue;
  62. if (ignore_depends(depender)) {
  63. debug(dbg_depcon, "ignoring depending package '%s'",
  64. pkg_name(depender, pnaw_always));
  65. continue;
  66. }
  67. if (dependtry > 1) { if (findbreakcycle(pkgtoremove)) sincenothing= 0; }
  68. before= raemsgs->used;
  69. ok= dependencies_ok(depender,pkgtoremove,raemsgs);
  70. if (ok == DEP_CHECK_HALT &&
  71. depender->clientdata->istobe == PKG_ISTOBE_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 != PKG_WANT_UNKNOWN) {
  86. if (cipaction->arg_int == act_purge)
  87. pkg_set_want(pkg, PKG_WANT_PURGE);
  88. else
  89. pkg_set_want(pkg, PKG_WANT_DEINSTALL);
  90. if (!f_noact)
  91. modstatdb_note(pkg);
  92. }
  93. if (pkg->status == PKG_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 = PKG_ISTOBE_NORMAL;
  98. return;
  99. } else if (!f_pending &&
  100. pkg->status == PKG_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 = PKG_ISTOBE_NORMAL;
  107. return;
  108. }
  109. if (pkg->installed.essential && pkg->status != PKG_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 = PKG_ISTOBE_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 & PKG_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, PKG_STAT_NOTINSTALLED);
  150. pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  151. return;
  152. }
  153. pkg_conffiles_mark_old(pkg);
  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 >= PKG_STAT_HALFCONFIGURED) {
  159. static enum pkgstatus oldpkgstatus;
  160. oldpkgstatus= pkg->status;
  161. pkg_set_status(pkg, 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, 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, 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. if (strcmp(usenode->name, "/.") == 0) {
  265. debug(dbg_eachfiledetail,
  266. "removal_bulk '%s' root directory, cannot remove", fnvb.buf);
  267. push_leftover(&leftover, namenode);
  268. continue;
  269. }
  270. }
  271. trig_path_activate(usenode, pkg);
  272. varbuf_trunc(&fnvb, before);
  273. varbuf_add_str(&fnvb, DPKGTEMPEXT);
  274. varbuf_end_str(&fnvb);
  275. debug(dbg_eachfiledetail, "removal_bulk cleaning temp '%s'", fnvb.buf);
  276. path_remove_tree(fnvb.buf);
  277. varbuf_trunc(&fnvb, before);
  278. varbuf_add_str(&fnvb, DPKGNEWEXT);
  279. varbuf_end_str(&fnvb);
  280. debug(dbg_eachfiledetail, "removal_bulk cleaning new '%s'", fnvb.buf);
  281. path_remove_tree(fnvb.buf);
  282. varbuf_trunc(&fnvb, before);
  283. varbuf_end_str(&fnvb);
  284. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  285. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  286. if (errno == ENOTEMPTY || errno == EEXIST) {
  287. debug(dbg_eachfiledetail,
  288. "removal_bulk '%s' was not empty, will try again later",
  289. fnvb.buf);
  290. push_leftover(&leftover,namenode);
  291. continue;
  292. } else if (errno == EBUSY || errno == EPERM) {
  293. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  294. "%s - directory may be a mount point?"),
  295. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  296. push_leftover(&leftover,namenode);
  297. continue;
  298. }
  299. if (errno != ENOTDIR)
  300. ohshite(_("cannot remove '%.250s'"), fnvb.buf);
  301. debug(dbg_eachfiledetail, "removal_bulk unlinking '%s'", fnvb.buf);
  302. if (secure_unlink(fnvb.buf))
  303. ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
  304. }
  305. write_filelist_except(pkg, &pkg->installed, leftover, 0);
  306. maintscript_installed(pkg, POSTRMFILE, "post-removal", "remove", NULL);
  307. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  308. trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
  309. trig_file_interests_save();
  310. debug(dbg_general, "removal_bulk cleaning info directory");
  311. pkg_infodb_foreach(pkg, &pkg->installed, removal_bulk_remove_file);
  312. dir_sync_path(pkg_infodb_get_dir());
  313. pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
  314. pkg->installed.essential = false;
  315. modstatdb_note(pkg);
  316. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  317. }
  318. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  319. struct reversefilelistiter rlistit;
  320. struct fileinlist *leftover;
  321. struct filenamenode *namenode;
  322. static struct varbuf fnvb;
  323. struct stat stab;
  324. /* We may have modified this previously. */
  325. ensure_packagefiles_available(pkg);
  326. modstatdb_note(pkg);
  327. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  328. reversefilelist_init(&rlistit,pkg->clientdata->files);
  329. leftover = NULL;
  330. while ((namenode= reversefilelist_next(&rlistit))) {
  331. struct filenamenode *usenode;
  332. debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
  333. namenode->name, namenode->flags);
  334. if (namenode->flags & fnnf_old_conff) {
  335. /* This can only happen if removal_bulk_remove_configfiles() got
  336. * interrupted half way. */
  337. debug(dbg_eachfiledetail, "removal_bulk expecting only left over dirs, "
  338. "ignoring conffile '%s'", namenode->name);
  339. continue;
  340. }
  341. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  342. varbuf_reset(&fnvb);
  343. varbuf_add_str(&fnvb, instdir);
  344. varbuf_add_str(&fnvb, usenode->name);
  345. varbuf_end_str(&fnvb);
  346. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  347. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  348. /* Only delete a directory or a link to one if we're the only
  349. * package which uses it. Other files should only be listed
  350. * in this package (but we don't check). */
  351. if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
  352. push_leftover(&leftover, namenode);
  353. continue;
  354. }
  355. if (dir_is_used_by_others(namenode, pkg))
  356. continue;
  357. if (strcmp(usenode->name, "/.") == 0) {
  358. debug(dbg_eachfiledetail,
  359. "removal_bulk '%s' root directory, cannot remove", fnvb.buf);
  360. push_leftover(&leftover, namenode);
  361. continue;
  362. }
  363. }
  364. trig_path_activate(usenode, pkg);
  365. debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
  366. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  367. if (errno == ENOTEMPTY || errno == EEXIST) {
  368. warning(_("while removing %.250s, directory '%.250s' not empty so not removed"),
  369. pkg_name(pkg, pnaw_nonambig), namenode->name);
  370. push_leftover(&leftover,namenode);
  371. continue;
  372. } else if (errno == EBUSY || errno == EPERM) {
  373. warning(_("while removing %.250s, unable to remove directory '%.250s': "
  374. "%s - directory may be a mount point?"),
  375. pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
  376. push_leftover(&leftover,namenode);
  377. continue;
  378. }
  379. if (errno != ENOTDIR)
  380. ohshite(_("cannot remove '%.250s'"), fnvb.buf);
  381. if (lstat(fnvb.buf, &stab) == 0 && S_ISLNK(stab.st_mode)) {
  382. debug(dbg_eachfiledetail, "removal_bulk is a symlink to a directory");
  383. if (unlink(fnvb.buf))
  384. ohshite(_("cannot remove '%.250s'"), fnvb.buf);
  385. continue;
  386. }
  387. push_leftover(&leftover,namenode);
  388. continue;
  389. }
  390. write_filelist_except(pkg, &pkg->installed, leftover, 0);
  391. modstatdb_note(pkg);
  392. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  393. }
  394. static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
  395. static const char *const removeconffexts[] = { REMOVECONFFEXTS, NULL };
  396. int rc, removevbbase;
  397. int conffnameused, conffbasenamelen;
  398. char *conffbasename;
  399. struct conffile *conff, **lconffp;
  400. struct fileinlist *searchfile;
  401. DIR *dsd;
  402. struct dirent *de;
  403. char *p;
  404. const char *const *ext;
  405. printf(_("Purging configuration files for %s (%s) ...\n"),
  406. pkg_name(pkg, pnaw_nonambig),
  407. versiondescribe(&pkg->installed.version, vdew_nonambig));
  408. log_action("purge", pkg, &pkg->installed);
  409. trig_activate_packageprocessing(pkg);
  410. /* We may have modified this above. */
  411. ensure_packagefiles_available(pkg);
  412. /* We're about to remove the configuration, so remove the note
  413. * about which version it was ... */
  414. dpkg_version_blank(&pkg->configversion);
  415. modstatdb_note(pkg);
  416. /* Remove from our list any conffiles that aren't ours any more or
  417. * are involved in diversions, except if we are the package doing the
  418. * diverting. */
  419. for (lconffp = &pkg->installed.conffiles; (conff = *lconffp) != NULL; ) {
  420. for (searchfile= pkg->clientdata->files;
  421. searchfile && strcmp(searchfile->namenode->name,conff->name);
  422. searchfile= searchfile->next);
  423. if (!searchfile) {
  424. debug(dbg_conff, "removal_bulk conffile not ours any more '%s'",
  425. conff->name);
  426. *lconffp= conff->next;
  427. } else if (searchfile->namenode->divert &&
  428. (searchfile->namenode->divert->camefrom ||
  429. (searchfile->namenode->divert->useinstead &&
  430. searchfile->namenode->divert->pkgset != pkg->set))) {
  431. debug(dbg_conff, "removal_bulk conffile '%s' ignored due to diversion",
  432. conff->name);
  433. *lconffp= conff->next;
  434. } else {
  435. debug(dbg_conffdetail, "removal_bulk set to new conffile '%s'",
  436. conff->name);
  437. conff->hash = NEWCONFFILEFLAG;
  438. lconffp= &conff->next;
  439. }
  440. }
  441. modstatdb_note(pkg);
  442. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  443. static struct varbuf fnvb, removevb;
  444. if (conff->obsolete) {
  445. debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
  446. conff->name);
  447. }
  448. varbuf_reset(&fnvb);
  449. rc = conffderef(pkg, &fnvb, conff->name);
  450. debug(dbg_conffdetail, "removal_bulk conffile '%s' (= '%s')",
  451. conff->name, rc == -1 ? "<rc == -1>" : fnvb.buf);
  452. if (rc == -1)
  453. continue;
  454. conffnameused = fnvb.used;
  455. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  456. ohshite(_("cannot remove old config file '%.250s' (= '%.250s')"),
  457. conff->name, fnvb.buf);
  458. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  459. *p = '\0';
  460. varbuf_reset(&removevb);
  461. varbuf_add_str(&removevb, fnvb.buf);
  462. varbuf_add_char(&removevb, '/');
  463. removevbbase= removevb.used;
  464. varbuf_end_str(&removevb);
  465. dsd= opendir(removevb.buf);
  466. if (!dsd) {
  467. int e=errno;
  468. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  469. fnvb.buf, strerror(e)); errno= e;
  470. if (errno == ENOENT || errno == ENOTDIR) continue;
  471. ohshite(_("cannot read config file directory '%.250s' (from '%.250s')"),
  472. fnvb.buf, conff->name);
  473. }
  474. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  475. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  476. *p= '/';
  477. conffbasenamelen= strlen(++p);
  478. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  479. while ((de = readdir(dsd)) != NULL) {
  480. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry='%s'"
  481. " conffbasename='%s' conffnameused=%d conffbasenamelen=%d",
  482. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  483. if (strncmp(de->d_name, conffbasename, conffbasenamelen) == 0) {
  484. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  485. for (ext= removeconffexts; *ext; ext++)
  486. if (strcmp(*ext, de->d_name + conffbasenamelen) == 0)
  487. goto yes_remove;
  488. p= de->d_name+conffbasenamelen;
  489. if (*p++ == '~') {
  490. while (*p && c_isdigit(*p))
  491. p++;
  492. if (*p == '~' && !*++p) goto yes_remove;
  493. }
  494. }
  495. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  496. if (de->d_name[0] == '#' &&
  497. strncmp(de->d_name + 1, conffbasename, conffbasenamelen) == 0 &&
  498. strcmp(de->d_name + 1 + conffbasenamelen, "#") == 0)
  499. goto yes_remove;
  500. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  501. continue;
  502. yes_remove:
  503. varbuf_trunc(&removevb, removevbbase);
  504. varbuf_add_str(&removevb, de->d_name);
  505. varbuf_end_str(&removevb);
  506. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing '%s'",
  507. removevb.buf);
  508. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  509. ohshite(_("cannot remove old backup config file '%.250s' (of '%.250s')"),
  510. removevb.buf, conff->name);
  511. }
  512. pop_cleanup(ehflag_normaltidy); /* closedir */
  513. }
  514. /* Remove the conffiles from the file list file. */
  515. write_filelist_except(pkg, &pkg->installed, pkg->clientdata->files,
  516. fnnf_old_conff);
  517. pkg->installed.conffiles = NULL;
  518. modstatdb_note(pkg);
  519. maintscript_installed(pkg, POSTRMFILE, "post-removal", "purge", NULL);
  520. }
  521. /*
  522. * This is used both by deferred_remove() in this file, and at the end of
  523. * process_archive() in archives.c if it needs to finish removing a
  524. * conflicting package.
  525. */
  526. void removal_bulk(struct pkginfo *pkg) {
  527. bool foundpostrm;
  528. debug(dbg_general, "removal_bulk package %s", pkg_name(pkg, pnaw_always));
  529. if (pkg->status == PKG_STAT_HALFINSTALLED ||
  530. pkg->status == PKG_STAT_UNPACKED) {
  531. removal_bulk_remove_files(pkg);
  532. }
  533. foundpostrm = pkg_infodb_has_file(pkg, &pkg->installed, POSTRMFILE);
  534. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  535. if (!foundpostrm && !pkg->installed.conffiles) {
  536. /* If there are no config files and no postrm script then we
  537. * go straight into ‘purge’. */
  538. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  539. pkg_set_want(pkg, PKG_WANT_PURGE);
  540. dpkg_version_blank(&pkg->configversion);
  541. } else if (pkg->want == PKG_WANT_PURGE) {
  542. removal_bulk_remove_configfiles(pkg);
  543. }
  544. /* I.e., either of the two branches above. */
  545. if (pkg->want == PKG_WANT_PURGE) {
  546. const char *filename;
  547. /* Retry empty directories, and warn on any leftovers that aren't. */
  548. removal_bulk_remove_leftover_dirs(pkg);
  549. filename = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  550. debug(dbg_general, "removal_bulk purge done, removing list '%s'",
  551. filename);
  552. if (unlink(filename) && errno != ENOENT)
  553. ohshite(_("cannot remove old files list"));
  554. filename = pkg_infodb_get_file(pkg, &pkg->installed, POSTRMFILE);
  555. debug(dbg_general, "removal_bulk purge done, removing postrm '%s'",
  556. filename);
  557. if (unlink(filename) && errno != ENOENT)
  558. ohshite(_("can't remove old postrm script"));
  559. pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
  560. pkg_set_want(pkg, PKG_WANT_UNKNOWN);
  561. /* This will mess up reverse links, but if we follow them
  562. * we won't go back because pkg->status is PKG_STAT_NOTINSTALLED. */
  563. pkgbin_blank(&pkg->installed);
  564. }
  565. pkg_reset_eflags(pkg);
  566. modstatdb_note(pkg);
  567. debug(dbg_general, "removal done");
  568. }