cleanup.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <dpkg-i18n.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <utime.h>
  30. #include <assert.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <fcntl.h>
  34. #include <sys/stat.h>
  35. #include <sys/types.h>
  36. #include <dpkg.h>
  37. #include <dpkg-db.h>
  38. #include <tarfn.h>
  39. #include <myopt.h>
  40. #include "filesdb.h"
  41. #include "main.h"
  42. #include "archives.h"
  43. int cleanup_pkg_failed=0, cleanup_conflictor_failed=0;
  44. void cu_installnew(int argc, void **argv) {
  45. /* Something went wrong and we're undoing.
  46. * We have the following possible situations for non-conffiles:
  47. * <foo>.dpkg-tmp exists - in this case we want to remove
  48. * <foo> if it exists and replace it with <foo>.dpkg-tmp.
  49. * This undoes the backup operation.
  50. * <foo>.dpkg-tmp does not exist - <foo> may be on the disk,
  51. * as a new file which didn't fail, remove it if it is.
  52. * In both cases, we also make sure we delete <foo>.dpkg-new in
  53. * case that's still hanging around.
  54. * For conffiles, we simply delete <foo>.dpkg-new. For these,
  55. * <foo>.dpkg-tmp shouldn't exist, as we don't make a backup
  56. * at this stage. Just to be on the safe side, though, we don't
  57. * look for it.
  58. */
  59. struct fileinlist *nifd= (struct fileinlist*)argv[0];
  60. struct filenamenode *namenode;
  61. struct stat stab;
  62. cleanup_pkg_failed++; cleanup_conflictor_failed++;
  63. namenode= nifd->namenode;
  64. debug(dbg_eachfile,"cu_installnew `%s' flags=%o",namenode->name,namenode->flags);
  65. setupfnamevbs(namenode->name);
  66. if (!(namenode->flags & fnnf_new_conff) && !lstat(fnametmpvb.buf,&stab)) {
  67. /* OK, <foo>.dpkg-tmp exists. Remove <foo> and
  68. * restore <foo>.dpkg-tmp ...
  69. */
  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. */
  74. debug(dbg_eachfiledetail,"cu_installnew restoring nonatomic");
  75. if (unlinkorrmdir(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. } else if (namenode->flags & fnnf_placed_on_disk) {
  85. debug(dbg_eachfiledetail,"cu_installnew removing new file");
  86. if (unlinkorrmdir(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
  87. ohshite(_("unable to remove newly-installed version of `%.250s'"),
  88. namenode->name);
  89. } else {
  90. debug(dbg_eachfiledetail,"cu_installnew not restoring");
  91. }
  92. /* Whatever, we delete <foo>.dpkg-new now, if it still exists. */
  93. if (unlinkorrmdir(fnamenewvb.buf) && errno != ENOENT && errno != ENOTDIR)
  94. ohshite(_("unable to remove newly-extracted version of `%.250s'"),namenode->name);
  95. cleanup_pkg_failed--; cleanup_conflictor_failed--;
  96. }
  97. void cu_prermupgrade(int argc, void **argv) {
  98. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  99. if (cleanup_pkg_failed++) return;
  100. maintainer_script_postinst(pkg, "abort-upgrade",
  101. versiondescribe(&pkg->available.version,
  102. vdew_nonambig),
  103. NULL);
  104. pkg->eflag &= ~eflagf_reinstreq;
  105. post_postinst_tasks(pkg, stat_installed);
  106. cleanup_pkg_failed--;
  107. }
  108. void ok_prermdeconfigure(int argc, void **argv) {
  109. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  110. /* also has conflictor in argv[1] and infavour in argv[2].
  111. * conflictor may be 0 if deconfigure was due to Breaks */
  112. if (cipaction->arg == act_install)
  113. add_to_queue(deconf);
  114. }
  115. void cu_prermdeconfigure(int argc, void **argv) {
  116. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  117. struct pkginfo *conflictor= (struct pkginfo*)argv[1]; /* may be 0 */
  118. struct pkginfo *infavour= (struct pkginfo*)argv[2];
  119. if (conflictor) {
  120. maintainer_script_postinst(deconf, "abort-deconfigure",
  121. "in-favour", infavour->name,
  122. versiondescribe(&infavour->available.version,
  123. vdew_nonambig),
  124. "removing", conflictor->name,
  125. versiondescribe(&conflictor->installed.version,
  126. vdew_nonambig),
  127. NULL);
  128. } else {
  129. maintainer_script_postinst(deconf, "abort-deconfigure",
  130. "in-favour", infavour->name,
  131. versiondescribe(&infavour->available.version,
  132. vdew_nonambig),
  133. NULL);
  134. }
  135. post_postinst_tasks(deconf, stat_installed);
  136. }
  137. void cu_prerminfavour(int argc, void **argv) {
  138. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  139. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  140. if (cleanup_conflictor_failed++) return;
  141. maintainer_script_postinst(conflictor, "abort-remove",
  142. "in-favour", infavour->name,
  143. versiondescribe(&infavour->available.version,
  144. vdew_nonambig),
  145. NULL);
  146. conflictor->eflag &= ~eflagf_reinstreq;
  147. post_postinst_tasks(conflictor, stat_installed);
  148. cleanup_conflictor_failed--;
  149. }
  150. void cu_preinstverynew(int argc, void **argv) {
  151. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  152. char *cidir= (char*)argv[1];
  153. char *cidirrest= (char*)argv[2];
  154. if (cleanup_pkg_failed++) return;
  155. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  156. "abort-install", NULL);
  157. pkg->status= stat_notinstalled;
  158. pkg->eflag &= ~eflagf_reinstreq;
  159. blankpackageperfile(&pkg->installed);
  160. modstatdb_note(pkg);
  161. cleanup_pkg_failed--;
  162. }
  163. void cu_preinstnew(int argc, void **argv) {
  164. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  165. char *cidir= (char*)argv[1];
  166. char *cidirrest= (char*)argv[2];
  167. if (cleanup_pkg_failed++) return;
  168. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  169. "abort-install", versiondescribe(&pkg->installed.version,
  170. vdew_nonambig),
  171. NULL);
  172. pkg->status= stat_configfiles;
  173. pkg->eflag &= ~eflagf_reinstreq;
  174. modstatdb_note(pkg);
  175. cleanup_pkg_failed--;
  176. }
  177. void cu_preinstupgrade(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. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  182. if (cleanup_pkg_failed++) return;
  183. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  184. "abort-upgrade",
  185. versiondescribe(&pkg->installed.version,
  186. vdew_nonambig),
  187. NULL);
  188. pkg->status= *oldstatusp;
  189. pkg->eflag &= ~eflagf_reinstreq;
  190. modstatdb_note(pkg);
  191. cleanup_pkg_failed--;
  192. }
  193. void cu_postrmupgrade(int argc, void **argv) {
  194. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  195. if (cleanup_pkg_failed++) return;
  196. maintainer_script_installed(pkg,PREINSTFILE,"pre-installation",
  197. "abort-upgrade", versiondescribe(&pkg->available.version,
  198. vdew_nonambig),
  199. NULL);
  200. cleanup_pkg_failed--;
  201. }
  202. void cu_prermremove(int argc, void **argv) {
  203. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  204. enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
  205. if (cleanup_pkg_failed++) return;
  206. maintainer_script_postinst(pkg, "abort-remove", NULL);
  207. pkg->eflag &= ~eflagf_reinstreq;
  208. post_postinst_tasks(pkg, *oldpkgstatus);
  209. cleanup_pkg_failed--;
  210. }