cleanup.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <iwj10@cus.cam.ac.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 <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <utime.h>
  27. #include <assert.h>
  28. #include <time.h>
  29. #include <ctype.h>
  30. #include <fcntl.h>
  31. #include <sys/stat.h>
  32. #include <sys/types.h>
  33. #include <config.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. We also make sure
  48. * we delete <foo>.dpkg-new in case that's still hanging around.
  49. * <foo>.dpkg-tmp does not exist - in this case we haven't
  50. * got as far as creating it (or there wasn't an old version).
  51. * In this case we just delete <foo>.dpkg-new if it exists,
  52. * as it may be a half-extracted thing.
  53. * For conffiles, we simply delete <foo>.dpkg-new. For these,
  54. * <foo>.dpkg-tmp shouldn't exist, as we don't make a backup
  55. * at this stage. Just to be on the safe side, though, we don't
  56. * look for it.
  57. */
  58. struct fileinlist *nifd= (struct fileinlist*)argv[0];
  59. struct filenamenode *namenode;
  60. struct stat stab;
  61. cleanup_pkg_failed++; cleanup_conflictor_failed++;
  62. namenode= nifd->namenode;
  63. debug(dbg_eachfile,"cu_installnew `%s' flags=%o",namenode->name,namenode->flags);
  64. setupfnamevbs(namenode->name);
  65. if (!(namenode->flags & fnnf_new_conff) && !lstat(fnametmpvb.buf,&stab)) {
  66. /* OK, <foo>.dpkg-tmp exists. Remove <foo> and
  67. * restore <foo>.dpkg-tmp ...
  68. */
  69. if (namenode->flags & fnnf_no_atomic_overwrite) {
  70. /* If we can't do an atomic overwrite we have to delete first any
  71. * link to the new version we may have created.
  72. */
  73. debug(dbg_eachfiledetail,"cu_installnew restoring nonatomic");
  74. if (unlinkorrmdir(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. } else {
  84. debug(dbg_eachfiledetail,"cu_installnew not restoring");
  85. }
  86. /* Whatever, we delete <foo>.dpkg-new now, if it still exists. */
  87. if (unlinkorrmdir(fnamenewvb.buf) && errno != ENOENT && errno != ENOTDIR)
  88. ohshite(_("unable to remove newly-extracted version of `%.250s'"),namenode->name);
  89. cleanup_pkg_failed--; cleanup_conflictor_failed--;
  90. }
  91. void cu_prermupgrade(int argc, void **argv) {
  92. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  93. if (cleanup_pkg_failed++) return;
  94. maintainer_script_installed(pkg,POSTINSTFILE,"post-installation",
  95. "abort-upgrade",
  96. versiondescribe(&pkg->available.version,
  97. vdew_nonambig),
  98. (char*)0);
  99. pkg->status= stat_installed;
  100. pkg->eflag &= ~eflagf_reinstreq;
  101. modstatdb_note(pkg);
  102. cleanup_pkg_failed--;
  103. }
  104. void ok_prermdeconfigure(int argc, void **argv) {
  105. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  106. /* also has conflictor in argv[1] and infavour in argv[2] */
  107. if (cipaction->arg == act_install)
  108. add_to_queue(deconf);
  109. }
  110. void cu_prermdeconfigure(int argc, void **argv) {
  111. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  112. struct pkginfo *conflictor= (struct pkginfo*)argv[1];
  113. struct pkginfo *infavour= (struct pkginfo*)argv[2];
  114. maintainer_script_installed(deconf,POSTINSTFILE,"post-installation",
  115. "abort-deconfigure", "in-favour", infavour->name,
  116. versiondescribe(&infavour->available.version,
  117. vdew_nonambig),
  118. "removing", conflictor->name,
  119. versiondescribe(&conflictor->installed.version,
  120. vdew_nonambig),
  121. (char*)0);
  122. deconf->status= stat_installed;
  123. modstatdb_note(deconf);
  124. }
  125. void cu_prerminfavour(int argc, void **argv) {
  126. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  127. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  128. if (cleanup_conflictor_failed++) return;
  129. maintainer_script_installed(conflictor,POSTINSTFILE,"post-installation",
  130. "abort-remove", "in-favour", infavour->name,
  131. versiondescribe(&infavour->available.version,
  132. vdew_nonambig),
  133. (char*)0);
  134. conflictor->status= stat_installed;
  135. conflictor->eflag &= ~eflagf_reinstreq;
  136. modstatdb_note(conflictor);
  137. cleanup_conflictor_failed--;
  138. }
  139. void cu_preinstverynew(int argc, void **argv) {
  140. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  141. char *cidir= (char*)argv[1];
  142. char *cidirrest= (char*)argv[2];
  143. if (cleanup_pkg_failed++) return;
  144. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  145. "abort-install",(char*)0);
  146. pkg->status= stat_notinstalled;
  147. pkg->eflag &= ~eflagf_reinstreq;
  148. modstatdb_note(pkg);
  149. cleanup_pkg_failed--;
  150. }
  151. void cu_preinstnew(int argc, void **argv) {
  152. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  153. char *cidir= (char*)argv[1];
  154. char *cidirrest= (char*)argv[2];
  155. if (cleanup_pkg_failed++) return;
  156. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  157. "abort-install", versiondescribe(&pkg->installed.version,
  158. vdew_nonambig),
  159. (char*)0);
  160. pkg->status= stat_configfiles;
  161. pkg->eflag &= ~eflagf_reinstreq;
  162. modstatdb_note(pkg);
  163. cleanup_pkg_failed--;
  164. }
  165. void cu_preinstupgrade(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. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  170. if (cleanup_pkg_failed++) return;
  171. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  172. "abort-upgrade",
  173. versiondescribe(&pkg->installed.version,
  174. vdew_nonambig),
  175. (char*)0);
  176. pkg->status= *oldstatusp;
  177. pkg->eflag &= ~eflagf_reinstreq;
  178. modstatdb_note(pkg);
  179. cleanup_pkg_failed--;
  180. }
  181. void cu_postrmupgrade(int argc, void **argv) {
  182. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  183. if (cleanup_pkg_failed++) return;
  184. maintainer_script_installed(pkg,PREINSTFILE,"pre-installation",
  185. "abort-upgrade", versiondescribe(&pkg->available.version,
  186. vdew_nonambig),
  187. (char*)0);
  188. cleanup_pkg_failed--;
  189. }
  190. void cu_prermremove(int argc, void **argv) {
  191. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  192. if (cleanup_pkg_failed++) return;
  193. maintainer_script_installed(pkg,POSTINSTFILE,"post-installation",
  194. "abort-remove", (char*)0);
  195. pkg->status= stat_installed;
  196. pkg->eflag &= ~eflagf_reinstreq;
  197. modstatdb_note(pkg);
  198. cleanup_pkg_failed--;
  199. }