libdpkg: Deindent an else clause
[dpkg.git] / src / main / cleanup.c
blob0f7d6ff925c8be572b23005e6c083cb8123a4b13
1 /*
2 * dpkg - main program for package management
3 * cleanup.c - cleanup functions, used when we need to unwind
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <time.h>
31 #include <utime.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdio.h>
37 #include <dpkg/i18n.h>
38 #include <dpkg/dpkg.h>
39 #include <dpkg/dpkg-db.h>
40 #include <dpkg/pkg.h>
41 #include <dpkg/path.h>
42 #include <dpkg/options.h>
43 #include <dpkg/db-fsys.h>
45 #include "main.h"
46 #include "archives.h"
48 int cleanup_pkg_failed=0, cleanup_conflictor_failed=0;
50 /**
51 * Something went wrong and we're undoing.
53 * We have the following possible situations for non-conffiles:
54 * «pathname».dpkg-tmp exists - in this case we want to remove
55 * «pathname» if it exists and replace it with «pathname».dpkg-tmp.
56 * This undoes the backup operation.
57 * «pathname».dpkg-tmp does not exist - «pathname» may be on the disk,
58 * as a new file which didn't fail, remove it if it is.
60 * In both cases, we also make sure we delete «pathname».dpkg-new in
61 * case that's still hanging around.
63 * For conffiles, we simply delete «pathname».dpkg-new. For these,
64 * «pathname».dpkg-tmp shouldn't exist, as we don't make a backup
65 * at this stage. Just to be on the safe side, though, we don't
66 * look for it.
68 void cu_installnew(int argc, void **argv) {
69 struct fsys_namenode *namenode = argv[0];
70 struct stat stab;
72 cleanup_pkg_failed++; cleanup_conflictor_failed++;
74 debug(dbg_eachfile, "cu_installnew '%s' flags=%o",
75 namenode->name, namenode->flags);
77 setupfnamevbs(namenode->name);
79 if (!(namenode->flags & FNNF_NEW_CONFF) && !lstat(fnametmpvb.buf,&stab)) {
80 /* OK, «pathname».dpkg-tmp exists. Remove «pathname» and
81 * restore «pathname».dpkg-tmp ... */
82 if (namenode->flags & FNNF_NO_ATOMIC_OVERWRITE) {
83 /* If we can't do an atomic overwrite we have to delete first any
84 * link to the new version we may have created. */
85 debug(dbg_eachfiledetail,"cu_installnew restoring nonatomic");
86 if (secure_remove(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
87 ohshite(_("unable to remove newly-installed version of '%.250s' to allow"
88 " reinstallation of backup copy"),namenode->name);
89 } else {
90 debug(dbg_eachfiledetail,"cu_installnew restoring atomic");
92 /* Either we can do an atomic restore, or we've made room: */
93 if (rename(fnametmpvb.buf,fnamevb.buf))
94 ohshite(_("unable to restore backup version of '%.250s'"), namenode->name);
95 /* If «pathname».dpkg-tmp was still a hard link to «pathname», then the
96 * atomic rename did nothing, so we make sure to remove the backup. */
97 else if (unlink(fnametmpvb.buf) && errno != ENOENT)
98 ohshite(_("unable to remove backup copy of '%.250s'"), namenode->name);
99 } else if (namenode->flags & FNNF_PLACED_ON_DISK) {
100 debug(dbg_eachfiledetail,"cu_installnew removing new file");
101 if (secure_remove(fnamevb.buf) && errno != ENOENT && errno != ENOTDIR)
102 ohshite(_("unable to remove newly-installed version of '%.250s'"),
103 namenode->name);
104 } else {
105 debug(dbg_eachfiledetail,"cu_installnew not restoring");
107 /* Whatever, we delete «pathname».dpkg-new now, if it still exists. */
108 if (secure_remove(fnamenewvb.buf) && errno != ENOENT && errno != ENOTDIR)
109 ohshite(_("unable to remove newly-extracted version of '%.250s'"),
110 namenode->name);
112 cleanup_pkg_failed--; cleanup_conflictor_failed--;
115 void cu_prermupgrade(int argc, void **argv) {
116 struct pkginfo *pkg= (struct pkginfo*)argv[0];
118 if (cleanup_pkg_failed++) return;
119 maintscript_postinst(pkg, "abort-upgrade",
120 versiondescribe(&pkg->available.version, vdew_nonambig),
121 NULL);
122 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
123 post_postinst_tasks(pkg, PKG_STAT_INSTALLED);
124 cleanup_pkg_failed--;
128 * Also has conflictor in argv[1] and infavour in argv[2].
129 * conflictor may be NULL if deconfigure was due to Breaks or multi-arch
130 * instance sync.
132 void ok_prermdeconfigure(int argc, void **argv) {
133 struct pkginfo *deconf= (struct pkginfo*)argv[0];
135 if (cipaction->arg_int == act_install)
136 enqueue_package(deconf);
140 * conflictor may be NULL if deconfigure was due to Breaks or multi-arch
141 * instance sync.
143 void cu_prermdeconfigure(int argc, void **argv) {
144 struct pkginfo *deconf= (struct pkginfo*)argv[0];
145 struct pkginfo *conflictor = (struct pkginfo *)argv[1];
146 struct pkginfo *infavour= (struct pkginfo*)argv[2];
148 if (conflictor) {
149 maintscript_postinst(deconf, "abort-deconfigure",
150 "in-favour",
151 pkgbin_name(infavour, &infavour->available,
152 pnaw_nonambig),
153 versiondescribe(&infavour->available.version,
154 vdew_nonambig),
155 "removing",
156 pkg_name(conflictor, pnaw_nonambig),
157 versiondescribe(&conflictor->installed.version,
158 vdew_nonambig),
159 NULL);
160 } else {
161 maintscript_postinst(deconf, "abort-deconfigure",
162 "in-favour",
163 pkgbin_name(infavour, &infavour->available,
164 pnaw_nonambig),
165 versiondescribe(&infavour->available.version,
166 vdew_nonambig),
167 NULL);
170 post_postinst_tasks(deconf, PKG_STAT_INSTALLED);
173 void cu_prerminfavour(int argc, void **argv) {
174 struct pkginfo *conflictor= (struct pkginfo*)argv[0];
175 struct pkginfo *infavour= (struct pkginfo*)argv[1];
177 if (cleanup_conflictor_failed++) return;
178 maintscript_postinst(conflictor, "abort-remove",
179 "in-favour",
180 pkgbin_name(infavour, &infavour->available,
181 pnaw_nonambig),
182 versiondescribe(&infavour->available.version,
183 vdew_nonambig),
184 NULL);
185 pkg_clear_eflags(conflictor, PKG_EFLAG_REINSTREQ);
186 post_postinst_tasks(conflictor, PKG_STAT_INSTALLED);
187 cleanup_conflictor_failed--;
190 void cu_preinstverynew(int argc, void **argv) {
191 struct pkginfo *pkg= (struct pkginfo*)argv[0];
192 char *cidir= (char*)argv[1];
193 char *cidirrest= (char*)argv[2];
195 if (cleanup_pkg_failed++) return;
196 maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
197 "abort-install", NULL);
198 pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
199 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
200 pkgbin_blank(&pkg->installed);
201 modstatdb_note(pkg);
202 cleanup_pkg_failed--;
205 void cu_preinstnew(int argc, void **argv) {
206 struct pkginfo *pkg= (struct pkginfo*)argv[0];
207 char *cidir= (char*)argv[1];
208 char *cidirrest= (char*)argv[2];
210 if (cleanup_pkg_failed++) return;
211 maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
212 "abort-install",
213 versiondescribe(&pkg->installed.version, vdew_nonambig),
214 versiondescribe(&pkg->available.version, vdew_nonambig),
215 NULL);
216 pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
217 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
218 modstatdb_note(pkg);
219 cleanup_pkg_failed--;
222 void cu_preinstupgrade(int argc, void **argv) {
223 struct pkginfo *pkg= (struct pkginfo*)argv[0];
224 char *cidir= (char*)argv[1];
225 char *cidirrest= (char*)argv[2];
226 enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
228 if (cleanup_pkg_failed++) return;
229 maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
230 "abort-upgrade",
231 versiondescribe(&pkg->installed.version, vdew_nonambig),
232 versiondescribe(&pkg->available.version, vdew_nonambig),
233 NULL);
234 pkg_set_status(pkg, *oldstatusp);
235 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
236 modstatdb_note(pkg);
237 cleanup_pkg_failed--;
240 void cu_postrmupgrade(int argc, void **argv) {
241 struct pkginfo *pkg= (struct pkginfo*)argv[0];
243 if (cleanup_pkg_failed++) return;
244 maintscript_installed(pkg, PREINSTFILE, "pre-installation",
245 "abort-upgrade",
246 versiondescribe(&pkg->available.version, vdew_nonambig),
247 NULL);
248 cleanup_pkg_failed--;
251 void cu_prermremove(int argc, void **argv) {
252 struct pkginfo *pkg= (struct pkginfo*)argv[0];
253 const enum pkgstatus *oldpkgstatus = (enum pkgstatus *)argv[1];
255 if (cleanup_pkg_failed++) return;
256 maintscript_postinst(pkg, "abort-remove", NULL);
257 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
258 post_postinst_tasks(pkg, *oldpkgstatus);
259 cleanup_pkg_failed--;