cleanup.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * dpkg - main program for package management
  3. * cleanup.c - cleanup functions, used when we need to unwind
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <errno.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include <utime.h>
  29. #include <fcntl.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/tarfn.h>
  37. #include <dpkg/myopt.h>
  38. #include "filesdb.h"
  39. #include "main.h"
  40. #include "archives.h"
  41. int cleanup_pkg_failed=0, cleanup_conflictor_failed=0;
  42. /**
  43. * Something went wrong and we're undoing.
  44. *
  45. * We have the following possible situations for non-conffiles:
  46. * <foo>.dpkg-tmp exists - in this case we want to remove
  47. * <foo> if it exists and replace it with <foo>.dpkg-tmp.
  48. * This undoes the backup operation.
  49. * <foo>.dpkg-tmp does not exist - <foo> may be on the disk,
  50. * as a new file which didn't fail, remove it if it is.
  51. *
  52. * In both cases, we also make sure we delete <foo>.dpkg-new in
  53. * case that's still hanging around.
  54. *
  55. * For conffiles, we simply delete <foo>.dpkg-new. For these,
  56. * <foo>.dpkg-tmp shouldn't exist, as we don't make a backup
  57. * at this stage. Just to be on the safe side, though, we don't
  58. * look for it.
  59. */
  60. void cu_installnew(int argc, void **argv) {
  61. struct fileinlist *nifd= (struct fileinlist*)argv[0];
  62. struct filenamenode *namenode;
  63. struct stat stab;
  64. cleanup_pkg_failed++; cleanup_conflictor_failed++;
  65. namenode= nifd->namenode;
  66. debug(dbg_eachfile,"cu_installnew `%s' flags=%o",namenode->name,namenode->flags);
  67. setupfnamevbs(namenode->name);
  68. if (!(namenode->flags & fnnf_new_conff) && !lstat(fnametmpvb.buf,&stab)) {
  69. /* OK, <foo>.dpkg-tmp exists. Remove <foo> and
  70. * restore <foo>.dpkg-tmp ... */
  71. if (namenode->flags & fnnf_no_atomic_overwrite) {
  72. /* If we can't do an atomic overwrite we have to delete first any
  73. * link to the new version we may have created. */
  74. debug(dbg_eachfiledetail,"cu_installnew restoring nonatomic");
  75. if (secure_remove(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
  76. ohshite(_("unable to remove newly-installed version of `%.250s' to allow"
  77. " reinstallation of backup copy"),namenode->name);
  78. } else {
  79. debug(dbg_eachfiledetail,"cu_installnew restoring atomic");
  80. }
  81. /* Either we can do an atomic restore, or we've made room: */
  82. if (rename(fnametmpvb.buf,fnamevb.buf))
  83. ohshite(_("unable to restore backup version of `%.250s'"),namenode->name);
  84. /* If <foo>.dpkg-tmp was still a hard link to <foo>, then the atomic
  85. * rename did nothing, so we make sure to remove the backup. */
  86. else if (unlink(fnametmpvb.buf) && errno != ENOENT)
  87. ohshite(_("unable to remove backup copy of '%.250s'"), namenode->name);
  88. } else if (namenode->flags & fnnf_placed_on_disk) {
  89. debug(dbg_eachfiledetail,"cu_installnew removing new file");
  90. if (secure_remove(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
  91. ohshite(_("unable to remove newly-installed version of `%.250s'"),
  92. namenode->name);
  93. } else {
  94. debug(dbg_eachfiledetail,"cu_installnew not restoring");
  95. }
  96. /* Whatever, we delete <foo>.dpkg-new now, if it still exists. */
  97. if (secure_remove(fnamenewvb.buf) && errno != ENOENT && errno != ENOTDIR)
  98. ohshite(_("unable to remove newly-extracted version of `%.250s'"),namenode->name);
  99. cleanup_pkg_failed--; cleanup_conflictor_failed--;
  100. }
  101. void cu_prermupgrade(int argc, void **argv) {
  102. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  103. if (cleanup_pkg_failed++) return;
  104. maintainer_script_postinst(pkg, "abort-upgrade",
  105. versiondescribe(&pkg->available.version,
  106. vdew_nonambig),
  107. NULL);
  108. pkg->eflag &= ~eflag_reinstreq;
  109. post_postinst_tasks(pkg, stat_installed);
  110. cleanup_pkg_failed--;
  111. }
  112. /*
  113. * Also has conflictor in argv[1] and infavour in argv[2].
  114. * conflictor may be NULL if deconfigure was due to Breaks.
  115. */
  116. void ok_prermdeconfigure(int argc, void **argv) {
  117. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  118. if (cipaction->arg == act_install)
  119. add_to_queue(deconf);
  120. }
  121. /*
  122. * conflictor may be NULL.
  123. */
  124. void cu_prermdeconfigure(int argc, void **argv) {
  125. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  126. struct pkginfo *conflictor = (struct pkginfo *)argv[1];
  127. struct pkginfo *infavour= (struct pkginfo*)argv[2];
  128. if (conflictor) {
  129. maintainer_script_postinst(deconf, "abort-deconfigure",
  130. "in-favour", infavour->name,
  131. versiondescribe(&infavour->available.version,
  132. vdew_nonambig),
  133. "removing", conflictor->name,
  134. versiondescribe(&conflictor->installed.version,
  135. vdew_nonambig),
  136. NULL);
  137. } else {
  138. maintainer_script_postinst(deconf, "abort-deconfigure",
  139. "in-favour", infavour->name,
  140. versiondescribe(&infavour->available.version,
  141. vdew_nonambig),
  142. NULL);
  143. }
  144. post_postinst_tasks(deconf, stat_installed);
  145. }
  146. void cu_prerminfavour(int argc, void **argv) {
  147. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  148. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  149. if (cleanup_conflictor_failed++) return;
  150. maintainer_script_postinst(conflictor, "abort-remove",
  151. "in-favour", infavour->name,
  152. versiondescribe(&infavour->available.version,
  153. vdew_nonambig),
  154. NULL);
  155. conflictor->eflag &= ~eflag_reinstreq;
  156. post_postinst_tasks(conflictor, stat_installed);
  157. cleanup_conflictor_failed--;
  158. }
  159. void cu_preinstverynew(int argc, void **argv) {
  160. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  161. char *cidir= (char*)argv[1];
  162. char *cidirrest= (char*)argv[2];
  163. if (cleanup_pkg_failed++) return;
  164. maintainer_script_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  165. "abort-install", NULL);
  166. pkg->status= stat_notinstalled;
  167. pkg->eflag &= ~eflag_reinstreq;
  168. pkg_perfile_blank(&pkg->installed);
  169. modstatdb_note(pkg);
  170. cleanup_pkg_failed--;
  171. }
  172. void cu_preinstnew(int argc, void **argv) {
  173. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  174. char *cidir= (char*)argv[1];
  175. char *cidirrest= (char*)argv[2];
  176. if (cleanup_pkg_failed++) return;
  177. maintainer_script_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  178. "abort-install", versiondescribe(&pkg->installed.version,
  179. vdew_nonambig),
  180. NULL);
  181. pkg->status= stat_configfiles;
  182. pkg->eflag &= ~eflag_reinstreq;
  183. modstatdb_note(pkg);
  184. cleanup_pkg_failed--;
  185. }
  186. void cu_preinstupgrade(int argc, void **argv) {
  187. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  188. char *cidir= (char*)argv[1];
  189. char *cidirrest= (char*)argv[2];
  190. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  191. if (cleanup_pkg_failed++) return;
  192. maintainer_script_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  193. "abort-upgrade",
  194. versiondescribe(&pkg->installed.version,
  195. vdew_nonambig),
  196. NULL);
  197. pkg->status= *oldstatusp;
  198. pkg->eflag &= ~eflag_reinstreq;
  199. modstatdb_note(pkg);
  200. cleanup_pkg_failed--;
  201. }
  202. void cu_postrmupgrade(int argc, void **argv) {
  203. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  204. if (cleanup_pkg_failed++) return;
  205. maintainer_script_installed(pkg,PREINSTFILE,"pre-installation",
  206. "abort-upgrade", versiondescribe(&pkg->available.version,
  207. vdew_nonambig),
  208. NULL);
  209. cleanup_pkg_failed--;
  210. }
  211. void cu_prermremove(int argc, void **argv) {
  212. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  213. enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
  214. if (cleanup_pkg_failed++) return;
  215. maintainer_script_postinst(pkg, "abort-remove", NULL);
  216. pkg->eflag &= ~eflag_reinstreq;
  217. post_postinst_tasks(pkg, *oldpkgstatus);
  218. cleanup_pkg_failed--;
  219. }