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/>.
25 #include <sys/types.h>
37 #include <dpkg/i18n.h>
38 #include <dpkg/dpkg.h>
39 #include <dpkg/dpkg-db.h>
41 #include <dpkg/path.h>
42 #include <dpkg/options.h>
48 int cleanup_pkg_failed=0, cleanup_conflictor_failed=0;
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
68 void cu_installnew(int argc, void **argv) {
69 struct filenamenode *namenode = argv[0];
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);
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'"),
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'"),
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),
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.
131 void ok_prermdeconfigure(int argc, void **argv) {
132 struct pkginfo *deconf= (struct pkginfo*)argv[0];
134 if (cipaction->arg_int == act_install)
135 enqueue_package(deconf);
139 * conflictor may be NULL.
141 void cu_prermdeconfigure(int argc, void **argv) {
142 struct pkginfo *deconf= (struct pkginfo*)argv[0];
143 struct pkginfo *conflictor = (struct pkginfo *)argv[1];
144 struct pkginfo *infavour= (struct pkginfo*)argv[2];
147 maintscript_postinst(deconf, "abort-deconfigure",
149 pkgbin_name(infavour, &infavour->available,
151 versiondescribe(&infavour->available.version,
154 pkg_name(conflictor, pnaw_nonambig),
155 versiondescribe(&conflictor->installed.version,
159 maintscript_postinst(deconf, "abort-deconfigure",
161 pkgbin_name(infavour, &infavour->available,
163 versiondescribe(&infavour->available.version,
168 post_postinst_tasks(deconf, PKG_STAT_INSTALLED);
171 void cu_prerminfavour(int argc, void **argv) {
172 struct pkginfo *conflictor= (struct pkginfo*)argv[0];
173 struct pkginfo *infavour= (struct pkginfo*)argv[1];
175 if (cleanup_conflictor_failed++) return;
176 maintscript_postinst(conflictor, "abort-remove",
178 pkgbin_name(infavour, &infavour->available,
180 versiondescribe(&infavour->available.version,
183 pkg_clear_eflags(conflictor, PKG_EFLAG_REINSTREQ);
184 post_postinst_tasks(conflictor, PKG_STAT_INSTALLED);
185 cleanup_conflictor_failed--;
188 void cu_preinstverynew(int argc, void **argv) {
189 struct pkginfo *pkg= (struct pkginfo*)argv[0];
190 char *cidir= (char*)argv[1];
191 char *cidirrest= (char*)argv[2];
193 if (cleanup_pkg_failed++) return;
194 maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
195 "abort-install", NULL);
196 pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
197 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
198 pkgbin_blank(&pkg->installed);
200 cleanup_pkg_failed--;
203 void cu_preinstnew(int argc, void **argv) {
204 struct pkginfo *pkg= (struct pkginfo*)argv[0];
205 char *cidir= (char*)argv[1];
206 char *cidirrest= (char*)argv[2];
208 if (cleanup_pkg_failed++) return;
209 maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
211 versiondescribe(&pkg->installed.version, vdew_nonambig),
212 versiondescribe(&pkg->available.version, vdew_nonambig),
214 pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
215 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
217 cleanup_pkg_failed--;
220 void cu_preinstupgrade(int argc, void **argv) {
221 struct pkginfo *pkg= (struct pkginfo*)argv[0];
222 char *cidir= (char*)argv[1];
223 char *cidirrest= (char*)argv[2];
224 enum pkgstatus *oldstatusp= (enum pkgstatus*)argv[3];
226 if (cleanup_pkg_failed++) return;
227 maintscript_new(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
229 versiondescribe(&pkg->installed.version, vdew_nonambig),
230 versiondescribe(&pkg->available.version, vdew_nonambig),
232 pkg_set_status(pkg, *oldstatusp);
233 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
235 cleanup_pkg_failed--;
238 void cu_postrmupgrade(int argc, void **argv) {
239 struct pkginfo *pkg= (struct pkginfo*)argv[0];
241 if (cleanup_pkg_failed++) return;
242 maintscript_installed(pkg, PREINSTFILE, "pre-installation",
244 versiondescribe(&pkg->available.version, vdew_nonambig),
246 cleanup_pkg_failed--;
249 void cu_prermremove(int argc, void **argv) {
250 struct pkginfo *pkg= (struct pkginfo*)argv[0];
251 enum pkgstatus *oldpkgstatus= (enum pkgstatus*)argv[1];
253 if (cleanup_pkg_failed++) return;
254 maintscript_postinst(pkg, "abort-remove", NULL);
255 pkg_clear_eflags(pkg, PKG_EFLAG_REINSTREQ);
256 post_postinst_tasks(pkg, *oldpkgstatus);
257 cleanup_pkg_failed--;