cleanup.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <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 <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/path.h>
  38. #include <dpkg/options.h>
  39. #include "filesdb.h"
  40. #include "main.h"
  41. #include "archives.h"
  42. int cleanup_pkg_failed=0, cleanup_conflictor_failed=0;
  43. /**
  44. * Something went wrong and we're undoing.
  45. *
  46. * We have the following possible situations for non-conffiles:
  47. * «pathname».dpkg-tmp exists - in this case we want to remove
  48. * «pathname» if it exists and replace it with «pathname».dpkg-tmp.
  49. * This undoes the backup operation.
  50. * «pathname».dpkg-tmp does not exist - «pathname» may be on the disk,
  51. * as a new file which didn't fail, remove it if it is.
  52. *
  53. * In both cases, we also make sure we delete «pathname».dpkg-new in
  54. * case that's still hanging around.
  55. *
  56. * For conffiles, we simply delete «pathname».dpkg-new. For these,
  57. * «pathname».dpkg-tmp shouldn't exist, as we don't make a backup
  58. * at this stage. Just to be on the safe side, though, we don't
  59. * look for it.
  60. */
  61. void cu_installnew(int argc, void **argv) {
  62. struct filenamenode *namenode = argv[0];
  63. struct stat stab;
  64. cleanup_pkg_failed++; cleanup_conflictor_failed++;
  65. debug(dbg_eachfile, "cu_installnew '%s' flags=%o",
  66. namenode->name, namenode->flags);
  67. setupfnamevbs(namenode->name);
  68. if (!(namenode->flags & fnnf_new_conff) && !lstat(fnametmpvb.buf,&stab)) {
  69. /* OK, «pathname».dpkg-tmp exists. Remove «pathname» and
  70. * restore «pathname».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 «pathname».dpkg-tmp was still a hard link to «pathname», then the
  85. * atomic 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 «pathname».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'"),
  99. namenode->name);
  100. cleanup_pkg_failed--; cleanup_conflictor_failed--;
  101. }
  102. void cu_prermupgrade(int argc, void **argv) {
  103. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  104. if (cleanup_pkg_failed++) return;
  105. maintscript_postinst(pkg, "abort-upgrade",
  106. versiondescribe(&pkg->available.version, vdew_nonambig),
  107. NULL);
  108. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  109. post_postinst_tasks(pkg, 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_int == act_install)
  119. enqueue_package(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. maintscript_postinst(deconf, "abort-deconfigure",
  130. "in-favour",
  131. pkgbin_name(infavour, &infavour->available,
  132. pnaw_nonambig),
  133. versiondescribe(&infavour->available.version,
  134. vdew_nonambig),
  135. "removing",
  136. pkg_name(conflictor, pnaw_nonambig),
  137. versiondescribe(&conflictor->installed.version,
  138. vdew_nonambig),
  139. NULL);
  140. } else {
  141. maintscript_postinst(deconf, "abort-deconfigure",
  142. "in-favour",
  143. pkgbin_name(infavour, &infavour->available,
  144. pnaw_nonambig),
  145. versiondescribe(&infavour->available.version,
  146. vdew_nonambig),
  147. NULL);
  148. }
  149. post_postinst_tasks(deconf, PKG_STAT_INSTALLED);
  150. }
  151. void cu_prerminfavour(int argc, void **argv) {
  152. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  153. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  154. if (cleanup_conflictor_failed++) return;
  155. maintscript_postinst(conflictor, "abort-remove",
  156. "in-favour",
  157. pkgbin_name(infavour, &infavour->available,
  158. pnaw_nonambig),
  159. versiondescribe(&infavour->available.version,
  160. vdew_nonambig),
  161. NULL);
  162. pkg_clear_eflags(conflictor, PKG_EFLAG_REINSTREQ);
  163. post_postinst_tasks(conflictor, PKG_STAT_INSTALLED);
  164. cleanup_conflictor_failed--;
  165. }
  166. void cu_preinstverynew(int argc, void **argv) {
  167. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  168. char *cidir= (char*)argv[1];
  169. char *cidirrest= (char*)argv[2];
  170. if (cleanup_pkg_failed++) return;
  171. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  172. "abort-install", NULL);
  173. pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
  174. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  175. pkgbin_blank(&pkg->installed);
  176. modstatdb_note(pkg);
  177. cleanup_pkg_failed--;
  178. }
  179. void cu_preinstnew(int argc, void **argv) {
  180. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  181. char *cidir= (char*)argv[1];
  182. char *cidirrest= (char*)argv[2];
  183. if (cleanup_pkg_failed++) return;
  184. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  185. "abort-install",
  186. versiondescribe(&pkg->installed.version, vdew_nonambig),
  187. versiondescribe(&pkg->available.version, vdew_nonambig),
  188. NULL);
  189. pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
  190. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  191. modstatdb_note(pkg);
  192. cleanup_pkg_failed--;
  193. }
  194. void cu_preinstupgrade(int argc, void **argv) {
  195. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  196. char *cidir= (char*)argv[1];
  197. char *cidirrest= (char*)argv[2];
  198. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  199. if (cleanup_pkg_failed++) return;
  200. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  201. "abort-upgrade",
  202. versiondescribe(&pkg->installed.version, vdew_nonambig),
  203. versiondescribe(&pkg->available.version, vdew_nonambig),
  204. NULL);
  205. pkg_set_status(pkg, *oldstatusp);
  206. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  207. modstatdb_note(pkg);
  208. cleanup_pkg_failed--;
  209. }
  210. void cu_postrmupgrade(int argc, void **argv) {
  211. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  212. if (cleanup_pkg_failed++) return;
  213. maintscript_installed(pkg, PREINSTFILE, "pre-installation",
  214. "abort-upgrade",
  215. versiondescribe(&pkg->available.version, vdew_nonambig),
  216. NULL);
  217. cleanup_pkg_failed--;
  218. }
  219. void cu_prermremove(int argc, void **argv) {
  220. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  221. enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
  222. if (cleanup_pkg_failed++) return;
  223. maintscript_postinst(pkg, "abort-remove", NULL);
  224. pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
  225. post_postinst_tasks(pkg, *oldpkgstatus);
  226. cleanup_pkg_failed--;
  227. }