remove.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* Change: separate removal_bulk handling of halfinstalled or unpacked pkgs
  2. * (ie, remove the real files in the .deb) into its own function.
  3. * Note that "installed" state is converted to unpacked by
  4. * deferred_remove() or halfinstalled by process_archive())
  5. *
  6. * Change: separate purging of configfiles and running of postrm to its
  7. * own function.
  8. *
  9. * Change: retry removing directories after postrm purge is finished, to
  10. * handle directories that had conffiles or config files and so
  11. * forth in them. also only warn about non-empty directories at
  12. * that point.
  13. */
  14. /*
  15. * dpkg - main program for package management
  16. * remove.c - functionality for removing packages
  17. *
  18. * Copyright (C) 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  19. *
  20. * This is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2,
  23. * or (at your option) any later version.
  24. *
  25. * This is distributed in the hope that it will be useful, but
  26. * WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public
  31. * License along with dpkg; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. */
  34. #include <config.h>
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <fcntl.h>
  41. #include <sys/stat.h>
  42. #include <sys/types.h>
  43. #include <dirent.h>
  44. #include <ctype.h>
  45. #include <unistd.h>
  46. #include <string.h>
  47. #include <assert.h>
  48. #include <dpkg.h>
  49. #include <dpkg-db.h>
  50. #include <myopt.h>
  51. #include "filesdb.h"
  52. #include "main.h"
  53. static void checkforremoval(struct pkginfo *pkgtoremove,
  54. struct pkginfo *pkgdepcheck, /* may be virtual pkg */
  55. int *rokp, struct varbuf *raemsgs) {
  56. struct deppossi *possi;
  57. struct pkginfo *depender;
  58. int before, ok;
  59. for (possi= pkgdepcheck->installed.depended; possi; possi= possi->nextrev) {
  60. if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
  61. depender= possi->up->up;
  62. debug(dbg_depcon,"checking depending package `%s'",depender->name);
  63. if (depender->status != stat_installed) continue;
  64. if (ignore_depends(depender)) {
  65. debug(dbg_depcon,"ignoring depending package `%s'\n",depender->name);
  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 == 0 && depender->clientdata->istobe == itb_remove) ok= 1;
  72. if (ok == 1) raemsgs->used= before; /* Don't burble about reasons for deferral */
  73. if (ok < *rokp) *rokp= ok;
  74. }
  75. }
  76. void deferred_remove(struct pkginfo *pkg) {
  77. struct varbuf raemsgs;
  78. int rok;
  79. struct dependency *dep;
  80. debug(dbg_general,"deferred_remove package %s",pkg->name);
  81. if (pkg->status == stat_notinstalled) {
  82. fprintf(stderr,
  83. _("dpkg - warning: ignoring request to remove %.250s which isn't installed.\n"),
  84. pkg->name);
  85. pkg->clientdata->istobe= itb_normal;
  86. return;
  87. } else if (!f_pending &&
  88. pkg->status == stat_configfiles &&
  89. cipaction->arg != act_purge) {
  90. fprintf(stderr,
  91. _("dpkg - warning: ignoring request to remove %.250s, only the config\n"
  92. " files of which are on the system. Use --purge to remove them too.\n"),
  93. pkg->name);
  94. pkg->clientdata->istobe= itb_normal;
  95. return;
  96. }
  97. assert(pkg->installed.valid);
  98. if (pkg->installed.essential && pkg->status != stat_configfiles)
  99. forcibleerr(fc_removeessential, _("This is an essential package -"
  100. " it should not be removed."));
  101. if (!f_pending)
  102. pkg->want= (cipaction->arg == act_purge) ? want_purge : want_deinstall;
  103. if (!f_noact) modstatdb_note(pkg);
  104. debug(dbg_general,"checking dependencies for remove `%s'",pkg->name);
  105. varbufinit(&raemsgs);
  106. rok= 2;
  107. checkforremoval(pkg,pkg,&rok,&raemsgs);
  108. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  109. if (dep->type != dep_provides) continue;
  110. debug(dbg_depcon,"checking virtual package `%s'",dep->list->ed->name);
  111. checkforremoval(pkg,dep->list->ed,&rok,&raemsgs);
  112. }
  113. if (rok == 1) {
  114. varbuffree(&raemsgs);
  115. pkg->clientdata->istobe= itb_remove;
  116. add_to_queue(pkg);
  117. return;
  118. } else if (rok == 0) {
  119. sincenothing= 0;
  120. varbufaddc(&raemsgs,0);
  121. fprintf(stderr,
  122. _("dpkg: dependency problems prevent removal of %s:\n%s"),
  123. pkg->name, raemsgs.buf);
  124. ohshit(_("dependency problems - not removing"));
  125. } else if (raemsgs.used) {
  126. varbufaddc(&raemsgs,0);
  127. fprintf(stderr,
  128. _("dpkg: %s: dependency problems, but removing anyway as you request:\n%s"),
  129. pkg->name, raemsgs.buf);
  130. }
  131. varbuffree(&raemsgs);
  132. sincenothing= 0;
  133. if (pkg->eflag & eflagf_reinstreq)
  134. forcibleerr(fc_removereinstreq,
  135. _("Package is in a very bad inconsistent state - you should\n"
  136. " reinstall it before attempting a removal."));
  137. ensure_allinstfiles_available();
  138. filesdbinit();
  139. if (f_noact) {
  140. printf(_("Would remove or purge %s ...\n"),pkg->name);
  141. pkg->status= stat_notinstalled;
  142. pkg->clientdata->istobe= itb_normal;
  143. return;
  144. }
  145. oldconffsetflags(pkg->installed.conffiles);
  146. printf(_("Removing %s ...\n"),pkg->name);
  147. log_action("remove", pkg);
  148. if (pkg->status == stat_halfconfigured || pkg->status == stat_installed) {
  149. static enum pkgstatus oldpkgstatus;
  150. oldpkgstatus= pkg->status;
  151. pkg->status= stat_halfconfigured;
  152. modstatdb_note(pkg);
  153. push_cleanup(cu_prermremove, ~ehflag_normaltidy, 0, 0, 2,
  154. (void *)pkg, (void *)&oldpkgstatus);
  155. maintainer_script_installed(pkg, PRERMFILE, "pre-removal",
  156. "remove", NULL);
  157. pkg->status= stat_unpacked; /* Will turn into halfinstalled soon ... */
  158. }
  159. removal_bulk(pkg);
  160. }
  161. static void push_leftover(struct fileinlist **leftoverp,
  162. struct filenamenode *namenode) {
  163. struct fileinlist *newentry;
  164. newentry= nfmalloc(sizeof(struct fileinlist));
  165. newentry->next= *leftoverp;
  166. newentry->namenode= namenode;
  167. *leftoverp= newentry;
  168. }
  169. static void removal_bulk_remove_files(
  170. struct pkginfo *pkg,
  171. int *out_foundpostrm)
  172. {
  173. int before;
  174. int infodirbaseused;
  175. struct reversefilelistiter rlistit;
  176. struct fileinlist *leftover;
  177. struct filenamenode *namenode;
  178. static struct varbuf fnvb;
  179. DIR *dsd;
  180. struct dirent *de;
  181. struct stat stab;
  182. pkg->status= stat_halfinstalled;
  183. modstatdb_note(pkg);
  184. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  185. reversefilelist_init(&rlistit,pkg->clientdata->files);
  186. leftover= 0;
  187. while ((namenode= reversefilelist_next(&rlistit))) {
  188. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  189. namenode->name, namenode->flags);
  190. if (namenode->flags & fnnf_old_conff) {
  191. push_leftover(&leftover,namenode);
  192. continue;
  193. }
  194. varbufreset(&fnvb);
  195. varbufaddstr(&fnvb,instdir);
  196. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  197. before= fnvb.used;
  198. varbufaddstr(&fnvb,DPKGTEMPEXT);
  199. varbufaddc(&fnvb,0);
  200. debug(dbg_eachfiledetail, "removal_bulk cleaning temp `%s'", fnvb.buf);
  201. ensure_pathname_nonexisting(fnvb.buf);
  202. fnvb.used= before;
  203. varbufaddstr(&fnvb,DPKGNEWEXT);
  204. varbufaddc(&fnvb,0);
  205. debug(dbg_eachfiledetail, "removal_bulk cleaning new `%s'", fnvb.buf);
  206. ensure_pathname_nonexisting(fnvb.buf);
  207. fnvb.used= before;
  208. varbufaddc(&fnvb,0);
  209. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  210. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  211. /* Only delete a directory or a link to one if we're the only
  212. * package which uses it. Other files should only be listed
  213. * in this package (but we don't check).
  214. */
  215. if (hasdirectoryconffiles(namenode,pkg)) {
  216. push_leftover(&leftover,namenode);
  217. continue;
  218. }
  219. if (isdirectoryinuse(namenode,pkg)) continue;
  220. }
  221. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  222. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  223. if (errno == ENOTEMPTY) {
  224. debug(dbg_eachfiledetail, "removal_bulk `%s' was not empty, will try again later",
  225. fnvb.buf);
  226. push_leftover(&leftover,namenode);
  227. continue;
  228. } else if (errno == EBUSY || errno == EPERM) {
  229. fprintf(stderr, _("dpkg - warning: while removing %.250s,"
  230. " unable to remove directory `%.250s':"
  231. " %s - directory may be a mount point ?\n"),
  232. pkg->name, namenode->name, strerror(errno));
  233. push_leftover(&leftover,namenode);
  234. continue;
  235. }
  236. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  237. debug(dbg_eachfiledetail, "removal_bulk unlinking `%s'", fnvb.buf);
  238. {
  239. /*
  240. * If file to remove is a device or s[gu]id, change its mode
  241. * so that a malicious user cannot use it even if it's linked
  242. * to another file
  243. */
  244. struct stat stat_buf;
  245. if (lstat(fnvb.buf,&stat_buf)==0) {
  246. if (S_ISCHR(stat_buf.st_mode) || S_ISBLK(stat_buf.st_mode)) {
  247. chmod(fnvb.buf,0);
  248. }
  249. if (stat_buf.st_mode & (S_ISUID|S_ISGID)) {
  250. chmod(fnvb.buf,stat_buf.st_mode & ~(S_ISUID|S_ISGID));
  251. }
  252. }
  253. }
  254. if (unlink(fnvb.buf)) ohshite(_("cannot remove file `%.250s'"),fnvb.buf);
  255. }
  256. write_filelist_except(pkg,leftover,0);
  257. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  258. "remove", NULL);
  259. varbufreset(&fnvb);
  260. varbufaddstr(&fnvb,admindir);
  261. varbufaddstr(&fnvb,"/" INFODIR);
  262. infodirbaseused= fnvb.used;
  263. varbufaddc(&fnvb,0);
  264. dsd= opendir(fnvb.buf); if (!dsd) ohshite(_("cannot read info directory"));
  265. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  266. *out_foundpostrm= 0;
  267. debug(dbg_general, "removal_bulk cleaning info directory");
  268. while ((de= readdir(dsd)) != 0) {
  269. char *p;
  270. debug(dbg_veryverbose, "removal_bulk info file `%s'", de->d_name);
  271. if (de->d_name[0] == '.') continue;
  272. p= strrchr(de->d_name,'.'); if (!p) continue;
  273. if (strlen(pkg->name) != (size_t)(p-de->d_name) ||
  274. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  275. debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
  276. /* We need the postrm and list files for --purge. */
  277. if (!strcmp(p+1,LISTFILE)) continue;
  278. if (!strcmp(p+1,POSTRMFILE)) { *out_foundpostrm=1; continue; }
  279. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  280. fnvb.used= infodirbaseused;
  281. varbufaddstr(&fnvb,de->d_name);
  282. varbufaddc(&fnvb,0);
  283. if (unlink(fnvb.buf))
  284. ohshite(_("unable to delete control info file `%.250s'"),fnvb.buf);
  285. debug(dbg_scripts, "removal_bulk info unlinked %s",fnvb.buf);
  286. }
  287. pop_cleanup(ehflag_normaltidy); /* closedir */
  288. pkg->status= stat_configfiles;
  289. pkg->installed.essential= 0;
  290. modstatdb_note(pkg);
  291. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  292. }
  293. static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
  294. struct reversefilelistiter rlistit;
  295. struct fileinlist *leftover;
  296. struct filenamenode *namenode;
  297. static struct varbuf fnvb;
  298. struct stat stab;
  299. modstatdb_note(pkg);
  300. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  301. reversefilelist_init(&rlistit,pkg->clientdata->files);
  302. leftover= 0;
  303. while ((namenode= reversefilelist_next(&rlistit))) {
  304. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  305. namenode->name, namenode->flags);
  306. if (namenode->flags & fnnf_old_conff) {
  307. push_leftover(&leftover,namenode);
  308. continue;
  309. }
  310. varbufreset(&fnvb);
  311. varbufaddstr(&fnvb,instdir);
  312. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  313. varbufaddc(&fnvb,0);
  314. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  315. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  316. /* Only delete a directory or a link to one if we're the only
  317. * package which uses it. Other files should only be listed
  318. * in this package (but we don't check).
  319. */
  320. if (hasdirectoryconffiles(namenode,pkg)) {
  321. push_leftover(&leftover,namenode);
  322. continue;
  323. }
  324. if (isdirectoryinuse(namenode,pkg)) continue;
  325. }
  326. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  327. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  328. if (errno == ENOTEMPTY) {
  329. fprintf(stderr,
  330. _("dpkg - warning: while removing %.250s, directory `%.250s' not empty "
  331. "so not removed.\n"),
  332. pkg->name, namenode->name);
  333. push_leftover(&leftover,namenode);
  334. continue;
  335. } else if (errno == EBUSY || errno == EPERM) {
  336. fprintf(stderr, _("dpkg - warning: while removing %.250s,"
  337. " unable to remove directory `%.250s':"
  338. " %s - directory may be a mount point ?\n"),
  339. pkg->name, namenode->name, strerror(errno));
  340. push_leftover(&leftover,namenode);
  341. continue;
  342. }
  343. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  344. push_leftover(&leftover,namenode);
  345. continue;
  346. }
  347. write_filelist_except(pkg,leftover,0);
  348. modstatdb_note(pkg);
  349. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  350. }
  351. static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
  352. static const char *const removeconffexts[]= { REMOVECONFFEXTS, 0 };
  353. int r, removevbbase;
  354. int conffnameused, conffbasenamelen;
  355. char *conffbasename;
  356. struct conffile *conff, **lconffp;
  357. struct fileinlist *searchfile;
  358. DIR *dsd;
  359. struct dirent *de;
  360. char *p;
  361. const char *const *ext;
  362. printf(_("Purging configuration files for %s ...\n"),pkg->name);
  363. log_action("purge", pkg);
  364. ensure_packagefiles_available(pkg); /* We may have modified this above. */
  365. /* We're about to remove the configuration, so remove the note
  366. * about which version it was ...
  367. */
  368. blankversion(&pkg->configversion);
  369. modstatdb_note(pkg);
  370. /* Remove from our list any conffiles that aren't ours any more or
  371. * are involved in diversions, except if we are the package doing the
  372. * diverting.
  373. */
  374. for (lconffp= &pkg->installed.conffiles; (conff= *lconffp) != 0; ) {
  375. for (searchfile= pkg->clientdata->files;
  376. searchfile && strcmp(searchfile->namenode->name,conff->name);
  377. searchfile= searchfile->next);
  378. if (!searchfile) {
  379. debug(dbg_conff,"removal_bulk conffile not ours any more `%s'",conff->name);
  380. *lconffp= conff->next;
  381. } else if (searchfile->namenode->divert &&
  382. (searchfile->namenode->divert->camefrom ||
  383. (searchfile->namenode->divert->useinstead &&
  384. searchfile->namenode->divert->pkg != pkg))) {
  385. debug(dbg_conff,"removal_bulk conffile `%s' ignored due to diversion\n",
  386. conff->name);
  387. *lconffp= conff->next;
  388. } else {
  389. debug(dbg_conffdetail,"removal_bulk set to new conffile `%s'",conff->name);
  390. conff->hash= NEWCONFFILEFLAG; /* yes, cast away const */
  391. lconffp= &conff->next;
  392. }
  393. }
  394. modstatdb_note(pkg);
  395. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  396. static struct varbuf fnvb, removevb;
  397. if (conff->obsolete) {
  398. debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
  399. conff->name);
  400. continue;
  401. }
  402. varbufreset(&fnvb);
  403. r= conffderef(pkg, &fnvb, conff->name);
  404. debug(dbg_conffdetail, "removal_bulk conffile `%s' (= `%s')",
  405. conff->name, r == -1 ? "<r==-1>" : fnvb.buf);
  406. if (r == -1) continue;
  407. conffnameused= fnvb.used-1;
  408. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  409. ohshite(_("cannot remove old config file `%.250s' (= `%.250s')"),
  410. conff->name, fnvb.buf);
  411. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  412. *p= 0;
  413. varbufreset(&removevb);
  414. varbufaddstr(&removevb,fnvb.buf);
  415. varbufaddc(&removevb,'/');
  416. removevbbase= removevb.used;
  417. varbufaddc(&removevb,0);
  418. dsd= opendir(removevb.buf);
  419. if (!dsd) {
  420. int e=errno;
  421. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  422. fnvb.buf, strerror(e)); errno= e;
  423. if (errno == ENOENT || errno == ENOTDIR) continue;
  424. ohshite(_("cannot read config file dir `%.250s' (from `%.250s')"),
  425. fnvb.buf, conff->name);
  426. }
  427. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  428. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  429. *p= '/';
  430. conffbasenamelen= strlen(++p);
  431. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  432. while ((de= readdir(dsd)) != 0) {
  433. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry=`%s'"
  434. " conffbasename=`%s' conffnameused=%d conffbasenamelen=%d",
  435. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  436. if (!strncmp(de->d_name,conffbasename,conffbasenamelen)) {
  437. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  438. for (ext= removeconffexts; *ext; ext++)
  439. if (!strcmp(*ext,de->d_name+conffbasenamelen)) goto yes_remove;
  440. p= de->d_name+conffbasenamelen;
  441. if (*p++ == '~') {
  442. while (*p && cisdigit(*p)) p++;
  443. if (*p == '~' && !*++p) goto yes_remove;
  444. }
  445. }
  446. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  447. if (de->d_name[0] == '#' &&
  448. !strncmp(de->d_name+1,conffbasename,conffbasenamelen) &&
  449. !strcmp(de->d_name+1+conffbasenamelen,"#"))
  450. goto yes_remove;
  451. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  452. continue;
  453. yes_remove:
  454. removevb.used= removevbbase;
  455. varbufaddstr(&removevb,de->d_name); varbufaddc(&removevb,0);
  456. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing `%s'",
  457. removevb.buf);
  458. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  459. ohshite(_("cannot remove old backup config file `%.250s' (of `%.250s')"),
  460. removevb.buf, conff->name);
  461. }
  462. pop_cleanup(ehflag_normaltidy); /* closedir */
  463. }
  464. pkg->installed.conffiles= 0;
  465. modstatdb_note(pkg);
  466. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  467. "purge", NULL);
  468. }
  469. void removal_bulk(struct pkginfo *pkg) {
  470. /* This is used both by deferred_remove in this file, and at
  471. * the end of process_archive in archives.c if it needs to finish
  472. * removing a conflicting package.
  473. */
  474. int pkgnameused;
  475. int foundpostrm= 0;
  476. const char *postrmfilename;
  477. debug(dbg_general,"removal_bulk package %s",pkg->name);
  478. if (pkg->status == stat_halfinstalled || pkg->status == stat_unpacked) {
  479. removal_bulk_remove_files(pkg, &foundpostrm);
  480. } else {
  481. struct stat stab;
  482. postrmfilename= pkgadminfile(pkg,POSTRMFILE);
  483. if (!lstat(postrmfilename,&stab)) foundpostrm= 1;
  484. else if (errno == ENOENT) foundpostrm= 0;
  485. else ohshite(_("unable to check existence of `%.250s'"),postrmfilename);
  486. }
  487. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  488. if (!foundpostrm && !pkg->installed.conffiles) {
  489. /* If there are no config files and no postrm script then we
  490. * go straight into `purge'.
  491. */
  492. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  493. pkg->want= want_purge;
  494. } else if (pkg->want == want_purge) {
  495. removal_bulk_remove_configfiles(pkg);
  496. }
  497. if (pkg->want == want_purge) {
  498. /* ie, either of the two branches above. */
  499. static struct varbuf fnvb;
  500. /* retry empty directories, and warn on any leftovers that aren't */
  501. removal_bulk_remove_leftover_dirs(pkg);
  502. varbufreset(&fnvb);
  503. varbufaddstr(&fnvb,admindir);
  504. varbufaddstr(&fnvb,"/" INFODIR);
  505. varbufaddstr(&fnvb,pkg->name);
  506. pkgnameused= fnvb.used;
  507. varbufaddstr(&fnvb,"." LISTFILE);
  508. varbufaddc(&fnvb,0);
  509. debug(dbg_general, "removal_bulk purge done, removing list `%s'",fnvb.buf);
  510. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("cannot remove old files list"));
  511. fnvb.used= pkgnameused;
  512. varbufaddstr(&fnvb,"." POSTRMFILE);
  513. varbufaddc(&fnvb,0);
  514. debug(dbg_general, "removal_bulk purge done, removing postrm `%s'",fnvb.buf);
  515. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("can't remove old postrm script"));
  516. pkg->status= stat_notinstalled;
  517. /* This will mess up reverse links, but if we follow them
  518. * we won't go back because pkg->status is stat_notinstalled.
  519. */
  520. pkg->installed.depends= 0;
  521. pkg->installed.essential= 0;
  522. pkg->installed.description= pkg->installed.maintainer= 0;
  523. pkg->installed.source= pkg->installed.installedsize= 0;
  524. pkg->installed.origin= pkg->installed.bugs= 0;
  525. pkg->installed.architecture= 0;
  526. blankversion(&pkg->installed.version);
  527. pkg->installed.arbs= 0;
  528. }
  529. pkg->eflag= eflagv_ok;
  530. modstatdb_note(pkg);
  531. debug(dbg_general, "removal done");
  532. }