cleanup.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_installed(pkg,POSTINSTFILE,"post-installation",
  99. "abort-upgrade",
  100. versiondescribe(&pkg->available.version,
  101. vdew_nonambig),
  102. (char*)0);
  103. pkg->status= stat_installed;
  104. pkg->eflag &= ~eflagf_reinstreq;
  105. modstatdb_note(pkg);
  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. if (cipaction->arg == act_install)
  112. add_to_queue(deconf);
  113. }
  114. void cu_prermdeconfigure(int argc, void **argv) {
  115. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  116. struct pkginfo *conflictor= (struct pkginfo*)argv[1];
  117. struct pkginfo *infavour= (struct pkginfo*)argv[2];
  118. maintainer_script_installed(deconf,POSTINSTFILE,"post-installation",
  119. "abort-deconfigure", "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. (char*)0);
  126. deconf->status= stat_installed;
  127. modstatdb_note(deconf);
  128. }
  129. void cu_prerminfavour(int argc, void **argv) {
  130. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  131. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  132. if (cleanup_conflictor_failed++) return;
  133. maintainer_script_installed(conflictor,POSTINSTFILE,"post-installation",
  134. "abort-remove", "in-favour", infavour->name,
  135. versiondescribe(&infavour->available.version,
  136. vdew_nonambig),
  137. (char*)0);
  138. conflictor->status= stat_installed;
  139. conflictor->eflag &= ~eflagf_reinstreq;
  140. modstatdb_note(conflictor);
  141. cleanup_conflictor_failed--;
  142. }
  143. void cu_preinstverynew(int argc, void **argv) {
  144. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  145. char *cidir= (char*)argv[1];
  146. char *cidirrest= (char*)argv[2];
  147. if (cleanup_pkg_failed++) return;
  148. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  149. "abort-install",(char*)0);
  150. pkg->status= stat_notinstalled;
  151. pkg->eflag &= ~eflagf_reinstreq;
  152. blankpackageperfile(&pkg->installed);
  153. modstatdb_note(pkg);
  154. cleanup_pkg_failed--;
  155. }
  156. void cu_preinstnew(int argc, void **argv) {
  157. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  158. char *cidir= (char*)argv[1];
  159. char *cidirrest= (char*)argv[2];
  160. if (cleanup_pkg_failed++) return;
  161. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  162. "abort-install", versiondescribe(&pkg->installed.version,
  163. vdew_nonambig),
  164. (char*)0);
  165. pkg->status= stat_configfiles;
  166. pkg->eflag &= ~eflagf_reinstreq;
  167. modstatdb_note(pkg);
  168. cleanup_pkg_failed--;
  169. }
  170. void cu_preinstupgrade(int argc, void **argv) {
  171. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  172. char *cidir= (char*)argv[1];
  173. char *cidirrest= (char*)argv[2];
  174. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  175. if (cleanup_pkg_failed++) return;
  176. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  177. "abort-upgrade",
  178. versiondescribe(&pkg->installed.version,
  179. vdew_nonambig),
  180. (char*)0);
  181. pkg->status= *oldstatusp;
  182. pkg->eflag &= ~eflagf_reinstreq;
  183. modstatdb_note(pkg);
  184. cleanup_pkg_failed--;
  185. }
  186. void cu_postrmupgrade(int argc, void **argv) {
  187. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  188. if (cleanup_pkg_failed++) return;
  189. maintainer_script_installed(pkg,PREINSTFILE,"pre-installation",
  190. "abort-upgrade", versiondescribe(&pkg->available.version,
  191. vdew_nonambig),
  192. (char*)0);
  193. cleanup_pkg_failed--;
  194. }
  195. void cu_prermremove(int argc, void **argv) {
  196. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  197. if (cleanup_pkg_failed++) return;
  198. maintainer_script_installed(pkg,POSTINSTFILE,"post-installation",
  199. "abort-remove", (char*)0);
  200. pkg->status= stat_installed;
  201. pkg->eflag &= ~eflagf_reinstreq;
  202. modstatdb_note(pkg);
  203. cleanup_pkg_failed--;
  204. }