cleanup.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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-2012 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 <ctype.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <utime.h>
  30. #include <fcntl.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <dpkg/i18n.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include <dpkg/pkg.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'"),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. maintscript_postinst(pkg, "abort-upgrade",
  105. versiondescribe(&pkg->available.version, vdew_nonambig),
  106. NULL);
  107. pkg_clear_eflags(pkg, eflag_reinstreq);
  108. post_postinst_tasks(pkg, stat_installed);
  109. cleanup_pkg_failed--;
  110. }
  111. /*
  112. * Also has conflictor in argv[1] and infavour in argv[2].
  113. * conflictor may be NULL if deconfigure was due to Breaks.
  114. */
  115. void ok_prermdeconfigure(int argc, void **argv) {
  116. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  117. if (cipaction->arg_int == act_install)
  118. enqueue_package(deconf);
  119. }
  120. /*
  121. * conflictor may be NULL.
  122. */
  123. void cu_prermdeconfigure(int argc, void **argv) {
  124. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  125. struct pkginfo *conflictor = (struct pkginfo *)argv[1];
  126. struct pkginfo *infavour= (struct pkginfo*)argv[2];
  127. if (conflictor) {
  128. maintscript_postinst(deconf, "abort-deconfigure",
  129. "in-favour",
  130. pkgbin_name(infavour, &infavour->available,
  131. pnaw_nonambig),
  132. versiondescribe(&infavour->available.version,
  133. vdew_nonambig),
  134. "removing",
  135. pkg_name(conflictor, pnaw_nonambig),
  136. versiondescribe(&conflictor->installed.version,
  137. vdew_nonambig),
  138. NULL);
  139. } else {
  140. maintscript_postinst(deconf, "abort-deconfigure",
  141. "in-favour",
  142. pkgbin_name(infavour, &infavour->available,
  143. pnaw_nonambig),
  144. versiondescribe(&infavour->available.version,
  145. vdew_nonambig),
  146. NULL);
  147. }
  148. post_postinst_tasks(deconf, stat_installed);
  149. }
  150. void cu_prerminfavour(int argc, void **argv) {
  151. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  152. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  153. if (cleanup_conflictor_failed++) return;
  154. maintscript_postinst(conflictor, "abort-remove",
  155. "in-favour",
  156. pkgbin_name(infavour, &infavour->available,
  157. pnaw_nonambig),
  158. versiondescribe(&infavour->available.version,
  159. vdew_nonambig),
  160. NULL);
  161. pkg_clear_eflags(conflictor, eflag_reinstreq);
  162. post_postinst_tasks(conflictor, stat_installed);
  163. cleanup_conflictor_failed--;
  164. }
  165. void cu_preinstverynew(int argc, void **argv) {
  166. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  167. char *cidir= (char*)argv[1];
  168. char *cidirrest= (char*)argv[2];
  169. if (cleanup_pkg_failed++) return;
  170. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  171. "abort-install", NULL);
  172. pkg_set_status(pkg, stat_notinstalled);
  173. pkg_clear_eflags(pkg, eflag_reinstreq);
  174. pkgbin_blank(&pkg->installed);
  175. modstatdb_note(pkg);
  176. cleanup_pkg_failed--;
  177. }
  178. void cu_preinstnew(int argc, void **argv) {
  179. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  180. char *cidir= (char*)argv[1];
  181. char *cidirrest= (char*)argv[2];
  182. if (cleanup_pkg_failed++) return;
  183. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  184. "abort-install",
  185. versiondescribe(&pkg->installed.version, vdew_nonambig),
  186. NULL);
  187. pkg_set_status(pkg, stat_configfiles);
  188. pkg_clear_eflags(pkg, eflag_reinstreq);
  189. modstatdb_note(pkg);
  190. cleanup_pkg_failed--;
  191. }
  192. void cu_preinstupgrade(int argc, void **argv) {
  193. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  194. char *cidir= (char*)argv[1];
  195. char *cidirrest= (char*)argv[2];
  196. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  197. if (cleanup_pkg_failed++) return;
  198. maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  199. "abort-upgrade",
  200. versiondescribe(&pkg->installed.version, vdew_nonambig),
  201. NULL);
  202. pkg_set_status(pkg, *oldstatusp);
  203. pkg_clear_eflags(pkg, eflag_reinstreq);
  204. modstatdb_note(pkg);
  205. cleanup_pkg_failed--;
  206. }
  207. void cu_postrmupgrade(int argc, void **argv) {
  208. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  209. if (cleanup_pkg_failed++) return;
  210. maintscript_installed(pkg, PREINSTFILE, "pre-installation",
  211. "abort-upgrade",
  212. versiondescribe(&pkg->available.version, vdew_nonambig),
  213. NULL);
  214. cleanup_pkg_failed--;
  215. }
  216. void cu_prermremove(int argc, void **argv) {
  217. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  218. enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
  219. if (cleanup_pkg_failed++) return;
  220. maintscript_postinst(pkg, "abort-remove", NULL);
  221. pkg_clear_eflags(pkg, eflag_reinstreq);
  222. post_postinst_tasks(pkg, *oldpkgstatus);
  223. cleanup_pkg_failed--;
  224. }