cleanup.c 8.7 KB

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