remove.c 23 KB

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