cleanup.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <time.h>
  31. #include <ctype.h>
  32. #include <fcntl.h>
  33. #include <sys/stat.h>
  34. #include <sys/types.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include <dpkg/tarfn.h>
  38. #include <dpkg/myopt.h>
  39. #include "filesdb.h"
  40. #include "main.h"
  41. #include "archives.h"
  42. int cleanup_pkg_failed=0, cleanup_conflictor_failed=0;
  43. void cu_installnew(int argc, void **argv) {
  44. /* Something went wrong and we're undoing.
  45. * We have the following possible situations for non-conffiles:
  46. * <foo>.dpkg-tmp exists - in this case we want to remove
  47. * <foo> if it exists and replace it with <foo>.dpkg-tmp.
  48. * This undoes the backup operation.
  49. * <foo>.dpkg-tmp does not exist - <foo> may be on the disk,
  50. * as a new file which didn't fail, remove it if it is.
  51. * In both cases, we also make sure we delete <foo>.dpkg-new in
  52. * case that's still hanging around.
  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 if (namenode->flags & fnnf_placed_on_disk) {
  84. debug(dbg_eachfiledetail,"cu_installnew removing new file");
  85. if (unlinkorrmdir(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
  86. ohshite(_("unable to remove newly-installed version of `%.250s'"),
  87. namenode->name);
  88. } else {
  89. debug(dbg_eachfiledetail,"cu_installnew not restoring");
  90. }
  91. /* Whatever, we delete <foo>.dpkg-new now, if it still exists. */
  92. if (unlinkorrmdir(fnamenewvb.buf) && errno != ENOENT && errno != ENOTDIR)
  93. ohshite(_("unable to remove newly-extracted version of `%.250s'"),namenode->name);
  94. cleanup_pkg_failed--; cleanup_conflictor_failed--;
  95. }
  96. void cu_prermupgrade(int argc, void **argv) {
  97. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  98. if (cleanup_pkg_failed++) return;
  99. maintainer_script_postinst(pkg, "abort-upgrade",
  100. versiondescribe(&pkg->available.version,
  101. vdew_nonambig),
  102. NULL);
  103. pkg->eflag &= ~eflag_reinstreq;
  104. post_postinst_tasks(pkg, stat_installed);
  105. cleanup_pkg_failed--;
  106. }
  107. void ok_prermdeconfigure(int argc, void **argv) {
  108. struct pkginfo *deconf= (struct pkginfo*)argv[0];
  109. /* also has conflictor in argv[1] and infavour in argv[2].
  110. * conflictor may be 0 if deconfigure was due to Breaks */
  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]; /* may be 0 */
  117. struct pkginfo *infavour= (struct pkginfo*)argv[2];
  118. if (conflictor) {
  119. maintainer_script_postinst(deconf, "abort-deconfigure",
  120. "in-favour", infavour->name,
  121. versiondescribe(&infavour->available.version,
  122. vdew_nonambig),
  123. "removing", conflictor->name,
  124. versiondescribe(&conflictor->installed.version,
  125. vdew_nonambig),
  126. NULL);
  127. } else {
  128. maintainer_script_postinst(deconf, "abort-deconfigure",
  129. "in-favour", infavour->name,
  130. versiondescribe(&infavour->available.version,
  131. vdew_nonambig),
  132. NULL);
  133. }
  134. post_postinst_tasks(deconf, stat_installed);
  135. }
  136. void cu_prerminfavour(int argc, void **argv) {
  137. struct pkginfo *conflictor= (struct pkginfo*)argv[0];
  138. struct pkginfo *infavour= (struct pkginfo*)argv[1];
  139. if (cleanup_conflictor_failed++) return;
  140. maintainer_script_postinst(conflictor, "abort-remove",
  141. "in-favour", infavour->name,
  142. versiondescribe(&infavour->available.version,
  143. vdew_nonambig),
  144. NULL);
  145. conflictor->eflag &= ~eflag_reinstreq;
  146. post_postinst_tasks(conflictor, stat_installed);
  147. cleanup_conflictor_failed--;
  148. }
  149. void cu_preinstverynew(int argc, void **argv) {
  150. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  151. char *cidir= (char*)argv[1];
  152. char *cidirrest= (char*)argv[2];
  153. if (cleanup_pkg_failed++) return;
  154. maintainer_script_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  155. "abort-install", NULL);
  156. pkg->status= stat_notinstalled;
  157. pkg->eflag &= ~eflag_reinstreq;
  158. blankpackageperfile(&pkg->installed);
  159. modstatdb_note(pkg);
  160. cleanup_pkg_failed--;
  161. }
  162. void cu_preinstnew(int argc, void **argv) {
  163. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  164. char *cidir= (char*)argv[1];
  165. char *cidirrest= (char*)argv[2];
  166. if (cleanup_pkg_failed++) return;
  167. maintainer_script_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  168. "abort-install", versiondescribe(&pkg->installed.version,
  169. vdew_nonambig),
  170. NULL);
  171. pkg->status= stat_configfiles;
  172. pkg->eflag &= ~eflag_reinstreq;
  173. modstatdb_note(pkg);
  174. cleanup_pkg_failed--;
  175. }
  176. void cu_preinstupgrade(int argc, void **argv) {
  177. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  178. char *cidir= (char*)argv[1];
  179. char *cidirrest= (char*)argv[2];
  180. enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
  181. if (cleanup_pkg_failed++) return;
  182. maintainer_script_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  183. "abort-upgrade",
  184. versiondescribe(&pkg->installed.version,
  185. vdew_nonambig),
  186. NULL);
  187. pkg->status= *oldstatusp;
  188. pkg->eflag &= ~eflag_reinstreq;
  189. modstatdb_note(pkg);
  190. cleanup_pkg_failed--;
  191. }
  192. void cu_postrmupgrade(int argc, void **argv) {
  193. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  194. if (cleanup_pkg_failed++) return;
  195. maintainer_script_installed(pkg,PREINSTFILE,"pre-installation",
  196. "abort-upgrade", versiondescribe(&pkg->available.version,
  197. vdew_nonambig),
  198. NULL);
  199. cleanup_pkg_failed--;
  200. }
  201. void cu_prermremove(int argc, void **argv) {
  202. struct pkginfo *pkg= (struct pkginfo*)argv[0];
  203. enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
  204. if (cleanup_pkg_failed++) return;
  205. maintainer_script_postinst(pkg, "abort-remove", NULL);
  206. pkg->eflag &= ~eflag_reinstreq;
  207. post_postinst_tasks(pkg, *oldpkgstatus);
  208. cleanup_pkg_failed--;
  209. }