cleanup.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * 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 <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/pkg.h>
  37. #include <dpkg/options.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. * «pathname».dpkg-tmp exists - in this case we want to remove
  47. * «pathname» if it exists and replace it with «pathname».dpkg-tmp.
  48. * This undoes the backup operation.
  49. * «pathname».dpkg-tmp does not exist - «pathname» 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 «pathname».dpkg-new in
  53. * case that's still hanging around.
  54. *
  55. * For conffiles, we simply delete «pathname».dpkg-new. For these,
  56. * «pathname».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 filenamenode *namenode = argv[0];
  62. struct stat stab;
  63. cleanup_pkg_failed++; cleanup_conflictor_failed++;
  64. debug(dbg_eachfile, "cu_installnew '%s' flags=%o",
  65. namenode->name, namenode->flags);
  66. setupfnamevbs(namenode->name);
  67. if (!(namenode->flags & fnnf_new_conff) && !lstat(fnametmpvb.buf,&stab)) {
  68. /* OK, «pathname».dpkg-tmp exists. Remove «pathname» and
  69. * restore «pathname».dpkg-tmp ... */
  70. if (namenode->flags & fnnf_no_atomic_overwrite) {
  71. /* If we can't do an atomic overwrite we have to delete first any
  72. * link to the new version we may have created. */
  73. debug(dbg_eachfiledetail,"cu_installnew restoring nonatomic");
  74. if (secure_remove(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
  75. ohshite(_("unable to remove newly-installed version of `%.250s' to allow"
  76. " reinstallation of backup copy"),namenode->name);
  77. } else {
  78. debug(dbg_eachfiledetail,"cu_installnew restoring atomic");
  79. }
  80. /* Either we can do an atomic restore, or we've made room: */
  81. if (rename(fnametmpvb.buf,fnamevb.buf))
  82. ohshite(_("unable to restore backup version of `%.250s'"),namenode->name);
  83. /* If «pathname».dpkg-tmp was still a hard link to «pathname», then the
  84. * atomic rename did nothing, so we make sure to remove the backup. */
  85. else if (unlink(fnametmpvb.buf) && errno != ENOENT)
  86. ohshite(_("unable to remove backup copy of '%.250s'"), namenode->name);
  87. } else if (namenode->flags & fnnf_placed_on_disk) {
  88. debug(dbg_eachfiledetail,"cu_installnew removing new file");
  89. if (secure_remove(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
  90. ohshite(_("unable to remove newly-installed version of `%.250s'"),
  91. namenode->name);
  92. } else {
  93. debug(dbg_eachfiledetail,"cu_installnew not restoring");
  94. }
  95. /* Whatever, we delete «pathname».dpkg-new now, if it still exists. */
  96. if (secure_remove(fnamenewvb.buf) && errno != ENOENT && errno != ENOTDIR)
  97. ohshite(_("unable to remove newly-extracted version of `%.250s'"),namenode->name);
  98. cleanup_pkg_failed--; cleanup_conflictor_failed--;
  99. }
  100. void cu_prermupgrade(int argc, void **argv) {
  101. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  102. if (cleanup_pkg_failed++) return;
  103. maintscript_postinst(pkg, "abort-upgrade",
  104. versiondescribe(&pkg->available.version, vdew_nonambig),
  105. NULL);
  106. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  107. post_postinst_tasks(pkg, PKG_STAT_INSTALLED);
  108. cleanup_pkg_failed--;
  109. }
  110. /*
  111. * Also has conflictor in argv[1] and infavour in argv[2].
  112. * conflictor may be NULL if deconfigure was due to Breaks.
  113. */
  114. void ok_prermdeconfigure(int argc, void **argv) {
  115. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  116. if (cipaction->arg_int == act_install)
  117. enqueue_package(deconf);
  118. }
  119. /*
  120. * conflictor may be NULL.
  121. */
  122. void cu_prermdeconfigure(int argc, void **argv) {
  123. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  124. struct pkginfo *conflictor = (struct pkginfo *)argv[1];
  125. struct pkginfo *infavour= (struct pkginfo*)argv[2];
  126. if (conflictor) {
  127. maintscript_postinst(deconf, "abort-deconfigure",
  128. "in-favour",
  129. pkgbin_name(infavour, &infavour->available,
  130. pnaw_nonambig),
  131. versiondescribe(&infavour->available.version,
  132. vdew_nonambig),
  133. "removing",
  134. pkg_name(conflictor, pnaw_nonambig),
  135. versiondescribe(&conflictor->installed.version,
  136. vdew_nonambig),
  137. NULL);
  138. } else {
  139. maintscript_postinst(deconf, "abort-deconfigure",
  140. "in-favour",
  141. pkgbin_name(infavour, &infavour->available,
  142. pnaw_nonambig),
  143. versiondescribe(&infavour->available.version,
  144. vdew_nonambig),
  145. NULL);
  146. }
  147. post_postinst_tasks(deconf, PKG_STAT_INSTALLED);
  148. }
  149. void cu_prerminfavour(int argc, void **argv) {
  150. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  151. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  152. if (cleanup_conflictor_failed++) return;
  153. maintscript_postinst(conflictor, "abort-remove",
  154. "in-favour",
  155. pkgbin_name(infavour, &infavour->available,
  156. pnaw_nonambig),
  157. versiondescribe(&infavour->available.version,
  158. vdew_nonambig),
  159. NULL);
  160. pkg_clear_eflags(conflictor, PKG_EFLAG_REINSTREQ);
  161. post_postinst_tasks(conflictor, PKG_STAT_INSTALLED);
  162. cleanup_conflictor_failed--;
  163. }
  164. void cu_preinstverynew(int argc, void **argv) {
  165. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  166. char *cidir= (char*)argv[1];
  167. char *cidirrest= (char*)argv[2];
  168. if (cleanup_pkg_failed++) return;
  169. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  170. "abort-install", NULL);
  171. pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
  172. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  173. pkgbin_blank(&pkg->installed);
  174. modstatdb_note(pkg);
  175. cleanup_pkg_failed--;
  176. }
  177. void cu_preinstnew(int argc, void **argv) {
  178. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  179. char *cidir= (char*)argv[1];
  180. char *cidirrest= (char*)argv[2];
  181. if (cleanup_pkg_failed++) return;
  182. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  183. "abort-install",
  184. versiondescribe(&pkg->installed.version, vdew_nonambig),
  185. NULL);
  186. pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
  187. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  188. modstatdb_note(pkg);
  189. cleanup_pkg_failed--;
  190. }
  191. void cu_preinstupgrade(int argc, void **argv) {
  192. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  193. char *cidir= (char*)argv[1];
  194. char *cidirrest= (char*)argv[2];
  195. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  196. if (cleanup_pkg_failed++) return;
  197. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  198. "abort-upgrade",
  199. versiondescribe(&pkg->installed.version, vdew_nonambig),
  200. NULL);
  201. pkg_set_status(pkg, *oldstatusp);
  202. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  203. modstatdb_note(pkg);
  204. cleanup_pkg_failed--;
  205. }
  206. void cu_postrmupgrade(int argc, void **argv) {
  207. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  208. if (cleanup_pkg_failed++) return;
  209. maintscript_installed(pkg, PREINSTFILE, "pre-installation",
  210. "abort-upgrade",
  211. versiondescribe(&pkg->available.version, vdew_nonambig),
  212. NULL);
  213. cleanup_pkg_failed--;
  214. }
  215. void cu_prermremove(int argc, void **argv) {
  216. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  217. enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
  218. if (cleanup_pkg_failed++) return;
  219. maintscript_postinst(pkg, "abort-remove", NULL);
  220. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  221. post_postinst_tasks(pkg, *oldpkgstatus);
  222. cleanup_pkg_failed--;
  223. }