cleanup.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. NULL);
  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. * 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_installed(deconf, POSTINSTFILE, "post-installation",
  121. "abort-deconfigure",
  122. "in-favour", infavour->name,
  123. versiondescribe(&infavour->available.version,
  124. vdew_nonambig),
  125. "removing", conflictor->name,
  126. versiondescribe(&conflictor->installed.version,
  127. vdew_nonambig),
  128. NULL);
  129. } else {
  130. maintainer_script_installed(deconf, POSTINSTFILE, "post-installation",
  131. "abort-deconfigure",
  132. "in-favour", infavour->name,
  133. versiondescribe(&infavour->available.version,
  134. vdew_nonambig),
  135. NULL);
  136. }
  137. deconf->status= stat_installed;
  138. modstatdb_note(deconf);
  139. }
  140. void cu_prerminfavour(int argc, void **argv) {
  141. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  142. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  143. if (cleanup_conflictor_failed++) return;
  144. maintainer_script_installed(conflictor,POSTINSTFILE,"post-installation",
  145. "abort-remove", "in-favour", infavour->name,
  146. versiondescribe(&infavour->available.version,
  147. vdew_nonambig),
  148. NULL);
  149. conflictor->status= stat_installed;
  150. conflictor->eflag &= ~eflagf_reinstreq;
  151. modstatdb_note(conflictor);
  152. cleanup_conflictor_failed--;
  153. }
  154. void cu_preinstverynew(int argc, void **argv) {
  155. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  156. char *cidir= (char*)argv[1];
  157. char *cidirrest= (char*)argv[2];
  158. if (cleanup_pkg_failed++) return;
  159. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  160. "abort-install", NULL);
  161. pkg->status= stat_notinstalled;
  162. pkg->eflag &= ~eflagf_reinstreq;
  163. blankpackageperfile(&pkg->installed);
  164. modstatdb_note(pkg);
  165. cleanup_pkg_failed--;
  166. }
  167. void cu_preinstnew(int argc, void **argv) {
  168. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  169. char *cidir= (char*)argv[1];
  170. char *cidirrest= (char*)argv[2];
  171. if (cleanup_pkg_failed++) return;
  172. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  173. "abort-install", versiondescribe(&pkg->installed.version,
  174. vdew_nonambig),
  175. NULL);
  176. pkg->status= stat_configfiles;
  177. pkg->eflag &= ~eflagf_reinstreq;
  178. modstatdb_note(pkg);
  179. cleanup_pkg_failed--;
  180. }
  181. void cu_preinstupgrade(int argc, void **argv) {
  182. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  183. char *cidir= (char*)argv[1];
  184. char *cidirrest= (char*)argv[2];
  185. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  186. if (cleanup_pkg_failed++) return;
  187. maintainer_script_new(pkg->name, POSTRMFILE,"post-removal",cidir,cidirrest,
  188. "abort-upgrade",
  189. versiondescribe(&pkg->installed.version,
  190. vdew_nonambig),
  191. NULL);
  192. pkg->status= *oldstatusp;
  193. pkg->eflag &= ~eflagf_reinstreq;
  194. modstatdb_note(pkg);
  195. cleanup_pkg_failed--;
  196. }
  197. void cu_postrmupgrade(int argc, void **argv) {
  198. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  199. if (cleanup_pkg_failed++) return;
  200. maintainer_script_installed(pkg,PREINSTFILE,"pre-installation",
  201. "abort-upgrade", versiondescribe(&pkg->available.version,
  202. vdew_nonambig),
  203. NULL);
  204. cleanup_pkg_failed--;
  205. }
  206. void cu_prermremove(int argc, void **argv) {
  207. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  208. enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
  209. if (cleanup_pkg_failed++) return;
  210. maintainer_script_installed(pkg,POSTINSTFILE,"post-installation",
  211. "abort-remove", NULL);
  212. pkg->status= *oldpkgstatus;
  213. pkg->eflag &= ~eflagf_reinstreq;
  214. modstatdb_note(pkg);
  215. cleanup_pkg_failed--;
  216. }