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>
43 #include <dpkg/db-fsys.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 fsys_namenode
*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 or multi-arch
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
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];
149 maintscript_postinst(deconf
, "abort-deconfigure",
151 pkgbin_name(infavour
, &infavour
->available
,
153 versiondescribe(&infavour
->available
.version
,
156 pkg_name(conflictor
, pnaw_nonambig
),
157 versiondescribe(&conflictor
->installed
.version
,
161 maintscript_postinst(deconf
, "abort-deconfigure",
163 pkgbin_name(infavour
, &infavour
->available
,
165 versiondescribe(&infavour
->available
.version
,
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",
180 pkgbin_name(infavour
, &infavour
->available
,
182 versiondescribe(&infavour
->available
.version
,
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
);
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
,
213 versiondescribe(&pkg
->installed
.version
, vdew_nonambig
),
214 versiondescribe(&pkg
->available
.version
, vdew_nonambig
),
216 pkg_set_status(pkg
, PKG_STAT_CONFIGFILES
);
217 pkg_clear_eflags(pkg
, PKG_EFLAG_REINSTREQ
);
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
,
231 versiondescribe(&pkg
->installed
.version
, vdew_nonambig
),
232 versiondescribe(&pkg
->available
.version
, vdew_nonambig
),
234 pkg_set_status(pkg
, *oldstatusp
);
235 pkg_clear_eflags(pkg
, PKG_EFLAG_REINSTREQ
);
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",
246 versiondescribe(&pkg
->available
.version
, vdew_nonambig
),
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
--;