Branch data Line data Source code
1 : : /*
2 : : * dpkg - main program for package management
3 : : * remove.c - functionality for removing packages
4 : : *
5 : : * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 : : * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
7 : : *
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.
12 : : *
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.
17 : : *
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/>.
20 : : */
21 : :
22 : : #include <config.h>
23 : : #include <compat.h>
24 : :
25 : : #include <sys/types.h>
26 : : #include <sys/stat.h>
27 : :
28 : : #include <errno.h>
29 : : #include <string.h>
30 : : #include <fcntl.h>
31 : : #include <dirent.h>
32 : : #include <unistd.h>
33 : : #include <stdlib.h>
34 : : #include <stdio.h>
35 : :
36 : : #include <dpkg/i18n.h>
37 : : #include <dpkg/c-ctype.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/dir.h>
43 : : #include <dpkg/options.h>
44 : : #include <dpkg/triglib.h>
45 : : #include <dpkg/db-ctrl.h>
46 : : #include <dpkg/db-fsys.h>
47 : :
48 : : #include "main.h"
49 : :
50 : : /*
51 : : * pkgdepcheck may be a virtual pkg.
52 : : */
53 : 0 : static void checkforremoval(struct pkginfo *pkgtoremove,
54 : : struct pkgset *pkgdepcheck,
55 : : enum dep_check *rokp, struct varbuf *raemsgs)
56 : : {
57 : : struct deppossi *possi;
58 : : struct pkginfo *depender;
59 : : enum dep_check ok;
60 : : struct varbuf_state raemsgs_state;
61 : :
62 [ # # ]: 0 : for (possi = pkgdepcheck->depended.installed; possi; possi = possi->rev_next) {
63 [ # # # # ]: 0 : if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
64 : 0 : depender= possi->up->up;
65 : 0 : debug(dbg_depcon, "checking depending package '%s'",
66 : : pkg_name(depender, pnaw_always));
67 [ # # ]: 0 : if (depender->status < PKG_STAT_UNPACKED)
68 : 0 : continue;
69 [ # # ]: 0 : if (ignore_depends(depender)) {
70 : 0 : debug(dbg_depcon, "ignoring depending package '%s'",
71 : : pkg_name(depender, pnaw_always));
72 : 0 : continue;
73 : : }
74 [ # # ]: 0 : if (dependtry >= DEPEND_TRY_CYCLES) {
75 [ # # ]: 0 : if (findbreakcycle(pkgtoremove))
76 : 0 : sincenothing = 0;
77 : : }
78 : 0 : varbuf_snapshot(raemsgs, &raemsgs_state);
79 : 0 : ok= dependencies_ok(depender,pkgtoremove,raemsgs);
80 [ # # ]: 0 : if (ok == DEP_CHECK_HALT &&
81 [ # # ]: 0 : depender->clientdata &&
82 [ # # ]: 0 : depender->clientdata->istobe == PKG_ISTOBE_REMOVE)
83 : 0 : ok = DEP_CHECK_DEFER;
84 [ # # ]: 0 : if (ok == DEP_CHECK_DEFER)
85 : : /* Don't burble about reasons for deferral. */
86 : 0 : varbuf_rollback(&raemsgs_state);
87 [ # # ]: 0 : if (ok < *rokp) *rokp= ok;
88 : : }
89 : 0 : }
90 : :
91 : 0 : void deferred_remove(struct pkginfo *pkg) {
92 : 0 : struct varbuf raemsgs = VARBUF_INIT;
93 : : struct dependency *dep;
94 : : enum dep_check rok;
95 : :
96 : 0 : debug(dbg_general, "deferred_remove package %s",
97 : : pkg_name(pkg, pnaw_always));
98 : :
99 [ # # # # ]: 0 : if (!f_pending && pkg->want != PKG_WANT_UNKNOWN) {
100 [ # # ]: 0 : if (cipaction->arg_int == act_purge)
101 : 0 : pkg_set_want(pkg, PKG_WANT_PURGE);
102 : : else
103 : 0 : pkg_set_want(pkg, PKG_WANT_DEINSTALL);
104 : :
105 [ # # ]: 0 : if (!f_noact)
106 : 0 : modstatdb_note(pkg);
107 : : }
108 : :
109 : 0 : ensure_package_clientdata(pkg);
110 : :
111 [ # # ]: 0 : if (pkg->status == PKG_STAT_NOTINSTALLED) {
112 : 0 : sincenothing = 0;
113 : 0 : warning(_("ignoring request to remove %.250s which isn't installed"),
114 : : pkg_name(pkg, pnaw_nonambig));
115 : 0 : pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
116 : 0 : return;
117 [ # # ]: 0 : } else if (!f_pending &&
118 [ # # ]: 0 : pkg->status == PKG_STAT_CONFIGFILES &&
119 [ # # ]: 0 : cipaction->arg_int != act_purge) {
120 : 0 : sincenothing = 0;
121 : 0 : warning(_("ignoring request to remove %.250s, only the config\n"
122 : : " files of which are on the system; use --purge to remove them too"),
123 : : pkg_name(pkg, pnaw_nonambig));
124 : 0 : pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
125 : 0 : return;
126 : : }
127 : :
128 [ # # ]: 0 : if (pkg->status != PKG_STAT_CONFIGFILES) {
129 [ # # ]: 0 : if (pkg->installed.essential)
130 : 0 : forcibleerr(FORCE_REMOVE_ESSENTIAL,
131 : 0 : _("this is an essential package; it should not be removed"));
132 [ # # ]: 0 : if (pkg->installed.is_protected)
133 : 0 : forcibleerr(FORCE_REMOVE_PROTECTED,
134 : 0 : _("this is a protected package; it should not be removed"));
135 : : }
136 : :
137 : 0 : debug(dbg_general, "checking dependencies for remove '%s'",
138 : : pkg_name(pkg, pnaw_always));
139 : 0 : rok = DEP_CHECK_OK;
140 : 0 : checkforremoval(pkg, pkg->set, &rok, &raemsgs);
141 [ # # ]: 0 : for (dep= pkg->installed.depends; dep; dep= dep->next) {
142 [ # # ]: 0 : if (dep->type != dep_provides) continue;
143 : 0 : debug(dbg_depcon, "checking virtual package '%s'", dep->list->ed->name);
144 : 0 : checkforremoval(pkg, dep->list->ed, &rok, &raemsgs);
145 : : }
146 : :
147 [ # # ]: 0 : if (rok == DEP_CHECK_DEFER) {
148 : 0 : varbuf_destroy(&raemsgs);
149 : 0 : pkg->clientdata->istobe = PKG_ISTOBE_REMOVE;
150 : 0 : enqueue_package(pkg);
151 : 0 : return;
152 [ # # ]: 0 : } else if (rok == DEP_CHECK_HALT) {
153 : 0 : sincenothing= 0;
154 : 0 : notice(_("dependency problems prevent removal of %s:\n%s"),
155 : : pkg_name(pkg, pnaw_nonambig), varbuf_str(&raemsgs));
156 : 0 : ohshit(_("dependency problems - not removing"));
157 [ # # ]: 0 : } else if (raemsgs.used) {
158 : 0 : notice(_("%s: dependency problems, but removing anyway as you requested:\n%s"),
159 : : pkg_name(pkg, pnaw_nonambig), varbuf_str(&raemsgs));
160 : : }
161 : 0 : varbuf_destroy(&raemsgs);
162 : 0 : sincenothing= 0;
163 : :
164 [ # # ]: 0 : if (pkg->eflag & PKG_EFLAG_REINSTREQ)
165 : 0 : forcibleerr(FORCE_REMOVE_REINSTREQ,
166 : 0 : _("package is in a very bad inconsistent state; you should\n"
167 : : " reinstall it before attempting a removal"));
168 : :
169 : 0 : ensure_allinstfiles_available();
170 : 0 : fsys_hash_init();
171 : :
172 [ # # ]: 0 : if (f_noact) {
173 : 0 : printf(_("Would remove or purge %s (%s) ...\n"),
174 : : pkg_name(pkg, pnaw_nonambig),
175 : 0 : versiondescribe(&pkg->installed.version, vdew_nonambig));
176 : 0 : pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
177 : 0 : pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
178 : 0 : return;
179 : : }
180 : :
181 : 0 : pkg_conffiles_mark_old(pkg);
182 : :
183 : : /* Only print and log removal action once. This avoids duplication when
184 : : * using --remove and --purge in sequence. */
185 [ # # ]: 0 : if (pkg->status > PKG_STAT_CONFIGFILES) {
186 : 0 : printf(_("Removing %s (%s) ...\n"), pkg_name(pkg, pnaw_nonambig),
187 : 0 : versiondescribe(&pkg->installed.version, vdew_nonambig));
188 : 0 : log_action("remove", pkg, &pkg->installed);
189 : : }
190 : :
191 : 0 : trig_activate_packageprocessing(pkg);
192 [ # # ]: 0 : if (pkg->status >= PKG_STAT_HALFCONFIGURED) {
193 : : static enum pkgstatus oldpkgstatus;
194 : :
195 : 0 : oldpkgstatus= pkg->status;
196 : 0 : pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
197 : 0 : modstatdb_note(pkg);
198 : 0 : push_cleanup(cu_prermremove, ~ehflag_normaltidy, 2,
199 : : (void *)pkg, (void *)&oldpkgstatus);
200 : 0 : maintscript_installed(pkg, PRERMFILE, "pre-removal", "remove", NULL);
201 : :
202 : : /* Will turn into ‘half-installed’ soon ... */
203 : 0 : pkg_set_status(pkg, PKG_STAT_UNPACKED);
204 : : }
205 : :
206 : 0 : removal_bulk(pkg);
207 : : }
208 : :
209 : : static void
210 : 0 : push_leftover(struct fsys_namenode_list **leftoverp,
211 : : struct fsys_namenode *namenode)
212 : : {
213 : : struct fsys_namenode_list *newentry;
214 : :
215 : 0 : newentry = nfmalloc(sizeof(*newentry));
216 : 0 : newentry->next= *leftoverp;
217 : 0 : newentry->namenode= namenode;
218 : 0 : *leftoverp= newentry;
219 : 0 : }
220 : :
221 : : static void
222 : 0 : removal_bulk_remove_file(const char *filename, const char *filetype)
223 : : {
224 : : /* We need the postrm and list files for --purge. */
225 [ # # ]: 0 : if (strcmp(filetype, LISTFILE) == 0 ||
226 [ # # ]: 0 : strcmp(filetype, POSTRMFILE) == 0)
227 : 0 : return;
228 : :
229 : 0 : debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
230 : :
231 [ # # ]: 0 : if (unlink(filename))
232 : 0 : ohshite(_("unable to delete control info file '%.250s'"), filename);
233 : :
234 : 0 : debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
235 : : }
236 : :
237 : : static bool
238 : 0 : removal_bulk_file_is_shared(struct pkginfo *pkg, struct fsys_namenode *namenode)
239 : : {
240 : : struct fsys_node_pkgs_iter *iter;
241 : : struct pkginfo *otherpkg;
242 : 0 : bool shared = false;
243 : :
244 [ # # ]: 0 : if (pkgset_installed_instances(pkg->set) <= 1)
245 : 0 : return false;
246 : :
247 : 0 : iter = fsys_node_pkgs_iter_new(namenode);
248 [ # # ]: 0 : while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
249 [ # # ]: 0 : if (otherpkg == pkg)
250 : 0 : continue;
251 [ # # ]: 0 : if (otherpkg->set != pkg->set)
252 : 0 : continue;
253 : :
254 : 0 : debug(dbg_eachfiledetail, "removal_bulk file shared with %s, skipping",
255 : : pkg_name(otherpkg, pnaw_always));
256 : 0 : shared = true;
257 : 0 : break;
258 : : }
259 : 0 : fsys_node_pkgs_iter_free(iter);
260 : :
261 : 0 : return shared;
262 : : }
263 : :
264 : : static void
265 : 0 : removal_bulk_remove_files(struct pkginfo *pkg)
266 : : {
267 : : struct fsys_hash_rev_iter rev_iter;
268 : : struct fsys_namenode_list *leftover;
269 : : struct fsys_namenode *namenode;
270 : : static struct varbuf fnvb;
271 : : struct varbuf_state fnvb_state;
272 : : struct stat stab;
273 : :
274 : 0 : pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
275 : 0 : modstatdb_note(pkg);
276 : 0 : push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
277 : :
278 : 0 : fsys_hash_rev_iter_init(&rev_iter, pkg->files);
279 : 0 : leftover = NULL;
280 [ # # ]: 0 : while ((namenode = fsys_hash_rev_iter_next(&rev_iter))) {
281 : : struct fsys_namenode *usenode;
282 : : bool is_dir;
283 : :
284 : 0 : debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
285 : 0 : namenode->name, namenode->flags);
286 : :
287 : 0 : usenode = namenodetouse(namenode, pkg, &pkg->installed);
288 : :
289 : 0 : varbuf_set_str(&fnvb, dpkg_fsys_get_dir());
290 : 0 : varbuf_add_str(&fnvb, usenode->name);
291 : 0 : varbuf_snapshot(&fnvb, &fnvb_state);
292 : :
293 [ # # # # ]: 0 : is_dir = stat(fnvb.buf, &stab) == 0 && S_ISDIR(stab.st_mode);
294 : :
295 : : /* A pkgset can share files between its instances that we
296 : : * don't want to remove, we just want to forget them. This
297 : : * applies to shared conffiles too. */
298 [ # # # # ]: 0 : if (!is_dir && removal_bulk_file_is_shared(pkg, namenode))
299 : 0 : continue;
300 : :
301 : : /* Non-shared conffiles are kept. */
302 [ # # ]: 0 : if (namenode->flags & FNNF_OLD_CONFF) {
303 : 0 : push_leftover(&leftover, namenode);
304 : 0 : continue;
305 : : }
306 : :
307 [ # # ]: 0 : if (is_dir) {
308 : 0 : debug(dbg_eachfiledetail, "removal_bulk is a directory");
309 : : /* Only delete a directory or a link to one if we're the only
310 : : * package which uses it. Other files should only be listed
311 : : * in this package (but we don't check). */
312 [ # # ]: 0 : if (dir_has_conffiles(namenode, pkg)) {
313 : 0 : push_leftover(&leftover,namenode);
314 : 0 : continue;
315 : : }
316 [ # # ]: 0 : if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
317 : 0 : push_leftover(&leftover, namenode);
318 : 0 : continue;
319 : : }
320 [ # # ]: 0 : if (dir_is_used_by_others(namenode, pkg))
321 : 0 : continue;
322 : :
323 [ # # ]: 0 : if (strcmp(usenode->name, "/.") == 0) {
324 : 0 : debug(dbg_eachfiledetail,
325 : : "removal_bulk '%s' root directory, cannot remove", fnvb.buf);
326 : 0 : push_leftover(&leftover, namenode);
327 : 0 : continue;
328 : : }
329 : : }
330 : :
331 : 0 : trig_path_activate(usenode, pkg);
332 : :
333 : 0 : varbuf_rollback(&fnvb_state);
334 : 0 : varbuf_add_str(&fnvb, DPKGTEMPEXT);
335 : 0 : debug(dbg_eachfiledetail, "removal_bulk cleaning temp '%s'", fnvb.buf);
336 : 0 : path_remove_tree(fnvb.buf);
337 : :
338 : 0 : varbuf_rollback(&fnvb_state);
339 : 0 : varbuf_add_str(&fnvb, DPKGNEWEXT);
340 : 0 : debug(dbg_eachfiledetail, "removal_bulk cleaning new '%s'", fnvb.buf);
341 : 0 : path_remove_tree(fnvb.buf);
342 : :
343 : 0 : varbuf_rollback(&fnvb_state);
344 : :
345 : 0 : debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
346 [ # # # # : 0 : if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
# # ]
347 [ # # # # ]: 0 : if (errno == ENOTEMPTY || errno == EEXIST) {
348 : 0 : debug(dbg_eachfiledetail,
349 : : "removal_bulk '%s' was not empty, will try again later",
350 : : fnvb.buf);
351 : 0 : push_leftover(&leftover,namenode);
352 : 0 : continue;
353 [ # # # # ]: 0 : } else if (errno == EBUSY || errno == EPERM) {
354 : 0 : warning(_("while removing %.250s, unable to remove directory '%.250s': "
355 : : "%s - directory may be a mount point?"),
356 : 0 : pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
357 : 0 : push_leftover(&leftover,namenode);
358 : 0 : continue;
359 : : }
360 [ # # ]: 0 : if (errno != ENOTDIR)
361 : 0 : ohshite(_("cannot remove '%.250s'"), fnvb.buf);
362 : 0 : debug(dbg_eachfiledetail, "removal_bulk unlinking '%s'", fnvb.buf);
363 [ # # ]: 0 : if (secure_unlink(fnvb.buf))
364 : 0 : ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
365 : : }
366 : 0 : write_filelist_except(pkg, &pkg->installed, leftover, 0);
367 : 0 : maintscript_installed(pkg, POSTRMFILE, "post-removal", "remove", NULL);
368 : :
369 : 0 : trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
370 : : trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
371 : 0 : trig_file_interests_save();
372 : :
373 : 0 : debug(dbg_general, "removal_bulk cleaning info directory");
374 : 0 : pkg_infodb_foreach(pkg, &pkg->installed, removal_bulk_remove_file);
375 : 0 : dir_sync_path(pkg_infodb_get_dir());
376 : :
377 : 0 : pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
378 : 0 : pkg->installed.essential = false;
379 : 0 : pkg->installed.is_protected = false;
380 : 0 : modstatdb_note(pkg);
381 : 0 : push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
382 : 0 : }
383 : :
384 : 0 : static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
385 : : struct fsys_hash_rev_iter rev_iter;
386 : : struct fsys_namenode_list *leftover;
387 : : struct fsys_namenode *namenode;
388 : : static struct varbuf fnvb;
389 : : struct stat stab;
390 : :
391 : : /* We may have modified this previously. */
392 : 0 : ensure_packagefiles_available(pkg);
393 : :
394 : 0 : modstatdb_note(pkg);
395 : 0 : push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
396 : :
397 : 0 : fsys_hash_rev_iter_init(&rev_iter, pkg->files);
398 : 0 : leftover = NULL;
399 [ # # ]: 0 : while ((namenode = fsys_hash_rev_iter_next(&rev_iter))) {
400 : : struct fsys_namenode *usenode;
401 : :
402 : 0 : debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
403 : 0 : namenode->name, namenode->flags);
404 [ # # ]: 0 : if (namenode->flags & FNNF_OLD_CONFF) {
405 : : /* This can only happen if removal_bulk_remove_configfiles() got
406 : : * interrupted half way. */
407 : 0 : debug(dbg_eachfiledetail, "removal_bulk expecting only left over dirs, "
408 : : "ignoring conffile '%s'", namenode->name);
409 : 0 : continue;
410 : : }
411 : :
412 : 0 : usenode = namenodetouse(namenode, pkg, &pkg->installed);
413 : :
414 : 0 : varbuf_set_str(&fnvb, dpkg_fsys_get_dir());
415 : 0 : varbuf_add_str(&fnvb, usenode->name);
416 : :
417 [ # # # # ]: 0 : if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
418 : 0 : debug(dbg_eachfiledetail, "removal_bulk is a directory");
419 : : /* Only delete a directory or a link to one if we're the only
420 : : * package which uses it. Other files should only be listed
421 : : * in this package (but we don't check). */
422 [ # # ]: 0 : if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
423 : 0 : push_leftover(&leftover, namenode);
424 : 0 : continue;
425 : : }
426 [ # # ]: 0 : if (dir_is_used_by_others(namenode, pkg))
427 : 0 : continue;
428 : :
429 [ # # ]: 0 : if (strcmp(usenode->name, "/.") == 0) {
430 : 0 : debug(dbg_eachfiledetail,
431 : : "removal_bulk '%s' root directory, cannot remove", fnvb.buf);
432 : 0 : push_leftover(&leftover, namenode);
433 : 0 : continue;
434 : : }
435 : : }
436 : :
437 : 0 : trig_path_activate(usenode, pkg);
438 : :
439 : 0 : debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
440 [ # # # # : 0 : if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
# # ]
441 [ # # # # ]: 0 : if (errno == ENOTEMPTY || errno == EEXIST) {
442 : 0 : warning(_("while removing %.250s, directory '%.250s' not empty so not removed"),
443 : : pkg_name(pkg, pnaw_nonambig), namenode->name);
444 : 0 : push_leftover(&leftover,namenode);
445 : 0 : continue;
446 [ # # # # ]: 0 : } else if (errno == EBUSY || errno == EPERM) {
447 : 0 : warning(_("while removing %.250s, unable to remove directory '%.250s': "
448 : : "%s - directory may be a mount point?"),
449 : 0 : pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
450 : 0 : push_leftover(&leftover,namenode);
451 : 0 : continue;
452 : : }
453 [ # # ]: 0 : if (errno != ENOTDIR)
454 : 0 : ohshite(_("cannot remove '%.250s'"), fnvb.buf);
455 : :
456 [ # # # # ]: 0 : if (lstat(fnvb.buf, &stab) == 0 && S_ISLNK(stab.st_mode)) {
457 : 0 : debug(dbg_eachfiledetail, "removal_bulk is a symlink to a directory");
458 : :
459 [ # # ]: 0 : if (unlink(fnvb.buf))
460 : 0 : ohshite(_("cannot remove '%.250s'"), fnvb.buf);
461 : :
462 : 0 : continue;
463 : : }
464 : :
465 : 0 : push_leftover(&leftover,namenode);
466 : : }
467 : 0 : write_filelist_except(pkg, &pkg->installed, leftover, 0);
468 : :
469 : 0 : modstatdb_note(pkg);
470 : 0 : push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
471 : 0 : }
472 : :
473 : 0 : static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
474 : : static const char *const removeconffexts[] = { REMOVECONFFEXTS, NULL };
475 : : int rc;
476 : : int conffnameused, conffbasenamelen;
477 : : char *conffbasename;
478 : : struct conffile *conff, **lconffp;
479 : : struct fsys_namenode_list *searchfile;
480 : : DIR *dsd;
481 : : struct dirent *de;
482 : : char *p;
483 : : const char *const *ext;
484 : :
485 : 0 : printf(_("Purging configuration files for %s (%s) ...\n"),
486 : : pkg_name(pkg, pnaw_nonambig),
487 : 0 : versiondescribe(&pkg->installed.version, vdew_nonambig));
488 : 0 : log_action("purge", pkg, &pkg->installed);
489 : 0 : trig_activate_packageprocessing(pkg);
490 : :
491 : : /* We may have modified this above. */
492 : 0 : ensure_packagefiles_available(pkg);
493 : :
494 : : /* We're about to remove the configuration, so remove the note
495 : : * about which version it was ... */
496 : 0 : dpkg_version_blank(&pkg->configversion);
497 : 0 : modstatdb_note(pkg);
498 : :
499 : : /* Remove from our list any conffiles that aren't ours any more or
500 : : * are involved in diversions, except if we are the package doing the
501 : : * diverting. */
502 [ # # ]: 0 : for (lconffp = &pkg->installed.conffiles; (conff = *lconffp) != NULL; ) {
503 : 0 : for (searchfile = pkg->files;
504 [ # # # # ]: 0 : searchfile && strcmp(searchfile->namenode->name,conff->name);
505 : 0 : searchfile= searchfile->next);
506 [ # # ]: 0 : if (!searchfile) {
507 : 0 : debug(dbg_conff, "removal_bulk conffile not ours any more '%s'",
508 : : conff->name);
509 : 0 : *lconffp= conff->next;
510 [ # # ]: 0 : } else if (searchfile->namenode->divert &&
511 [ # # ]: 0 : (searchfile->namenode->divert->camefrom ||
512 [ # # ]: 0 : (searchfile->namenode->divert->useinstead &&
513 [ # # ]: 0 : searchfile->namenode->divert->pkgset != pkg->set))) {
514 : 0 : debug(dbg_conff, "removal_bulk conffile '%s' ignored due to diversion",
515 : : conff->name);
516 : 0 : *lconffp= conff->next;
517 : : } else {
518 : 0 : debug(dbg_conffdetail, "removal_bulk set to new conffile '%s'",
519 : : conff->name);
520 : 0 : conff->hash = NEWCONFFILEFLAG;
521 : 0 : lconffp= &conff->next;
522 : : }
523 : : }
524 : 0 : modstatdb_note(pkg);
525 : :
526 [ # # ]: 0 : for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
527 : : struct fsys_namenode *namenode, *usenode;
528 : : static struct varbuf fnvb, removevb;
529 : : struct varbuf_state removevb_state;
530 : :
531 [ # # ]: 0 : if (conff->flags & CONFFILE_OBSOLETE) {
532 : 0 : debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
533 : : conff->name);
534 : : }
535 : 0 : varbuf_reset(&fnvb);
536 : 0 : rc = conffderef(pkg, &fnvb, conff->name);
537 [ # # ]: 0 : if (rc < 0) {
538 : 0 : debug(dbg_conffdetail, "removal_bulk conffile '%s' (rc < %d)",
539 : : conff->name, rc);
540 : 0 : continue;
541 : : } else {
542 : 0 : debug(dbg_conffdetail, "removal_bulk conffile '%s' (deref '%s')",
543 : : conff->name, fnvb.buf);
544 : : }
545 : :
546 : 0 : namenode = fsys_hash_find_node(conff->name, FHFF_NONE);
547 : 0 : usenode = namenodetouse(namenode, pkg, &pkg->installed);
548 : :
549 : 0 : trig_path_activate(usenode, pkg);
550 : :
551 : 0 : conffnameused = fnvb.used;
552 [ # # # # : 0 : if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
# # ]
553 : 0 : ohshite(_("cannot remove old config file '%.250s' (= '%.250s')"),
554 : : conff->name, fnvb.buf);
555 [ # # ]: 0 : p= strrchr(fnvb.buf,'/'); if (!p) continue;
556 : 0 : *p = '\0';
557 : 0 : varbuf_reset(&removevb);
558 : 0 : varbuf_add_dir(&removevb, fnvb.buf);
559 : 0 : varbuf_snapshot(&removevb, &removevb_state);
560 : :
561 : 0 : dsd= opendir(removevb.buf);
562 [ # # ]: 0 : if (!dsd) {
563 : 0 : int e=errno;
564 : 0 : debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
565 : 0 : fnvb.buf, strerror(e)); errno= e;
566 [ # # # # ]: 0 : if (errno == ENOENT || errno == ENOTDIR) continue;
567 : 0 : ohshite(_("cannot read config file directory '%.250s' (from '%.250s')"),
568 : : fnvb.buf, conff->name);
569 : : }
570 : 0 : debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
571 : 0 : push_cleanup(cu_closedir, ~0, 1, (void *)dsd);
572 : 0 : *p= '/';
573 : 0 : conffbasenamelen= strlen(++p);
574 : 0 : conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
575 [ # # ]: 0 : while ((de = readdir(dsd)) != NULL) {
576 : 0 : debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry='%s'"
577 : : " conffbasename='%s' conffnameused=%d conffbasenamelen=%d",
578 : 0 : de->d_name, conffbasename, conffnameused, conffbasenamelen);
579 [ # # ]: 0 : if (strncmp(de->d_name, conffbasename, conffbasenamelen) == 0) {
580 : 0 : debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
581 [ # # ]: 0 : for (ext= removeconffexts; *ext; ext++)
582 [ # # ]: 0 : if (strcmp(*ext, de->d_name + conffbasenamelen) == 0)
583 : 0 : goto yes_remove;
584 : 0 : p= de->d_name+conffbasenamelen;
585 [ # # ]: 0 : if (*p++ == '~') {
586 [ # # # # ]: 0 : while (*p && c_isdigit(*p))
587 : 0 : p++;
588 [ # # # # ]: 0 : if (*p == '~' && !*++p) goto yes_remove;
589 : : }
590 : : }
591 : 0 : debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
592 [ # # ]: 0 : if (de->d_name[0] == '#' &&
593 [ # # ]: 0 : strncmp(de->d_name + 1, conffbasename, conffbasenamelen) == 0 &&
594 [ # # ]: 0 : strcmp(de->d_name + 1 + conffbasenamelen, "#") == 0)
595 : 0 : goto yes_remove;
596 : 0 : debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
597 : 0 : continue;
598 : 0 : yes_remove:
599 : 0 : varbuf_rollback(&removevb_state);
600 : 0 : varbuf_add_str(&removevb, de->d_name);
601 : 0 : debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing '%s'",
602 : : removevb.buf);
603 [ # # # # : 0 : if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
# # ]
604 : 0 : ohshite(_("cannot remove old backup config file '%.250s' (of '%.250s')"),
605 : : removevb.buf, conff->name);
606 : : }
607 : 0 : pop_cleanup(ehflag_normaltidy); /* closedir */
608 : : }
609 : :
610 : : /* Remove the conffiles from the file list file. */
611 : 0 : write_filelist_except(pkg, &pkg->installed, pkg->files,
612 : : FNNF_OLD_CONFF);
613 : :
614 : 0 : pkg->installed.conffiles = NULL;
615 : 0 : modstatdb_note(pkg);
616 : :
617 : 0 : maintscript_installed(pkg, POSTRMFILE, "post-removal", "purge", NULL);
618 : 0 : }
619 : :
620 : : /*
621 : : * This is used both by deferred_remove() in this file, and at the end of
622 : : * process_archive() in archives.c if it needs to finish removing a
623 : : * conflicting package.
624 : : */
625 : 0 : void removal_bulk(struct pkginfo *pkg) {
626 : : bool foundpostrm;
627 : :
628 : 0 : debug(dbg_general, "removal_bulk package %s", pkg_name(pkg, pnaw_always));
629 : :
630 [ # # ]: 0 : if (pkg->status == PKG_STAT_HALFINSTALLED ||
631 [ # # ]: 0 : pkg->status == PKG_STAT_UNPACKED) {
632 : 0 : removal_bulk_remove_files(pkg);
633 : : }
634 : :
635 : 0 : foundpostrm = pkg_infodb_has_file(pkg, &pkg->installed, POSTRMFILE);
636 : :
637 : 0 : debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
638 : :
639 [ # # # # ]: 0 : if (!foundpostrm && !pkg->installed.conffiles) {
640 : : /* If there are no config files and no postrm script then we
641 : : * go straight into ‘purge’. */
642 : 0 : debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
643 : :
644 : 0 : pkg_set_want(pkg, PKG_WANT_PURGE);
645 : 0 : dpkg_version_blank(&pkg->configversion);
646 [ # # ]: 0 : } else if (pkg->want == PKG_WANT_PURGE) {
647 : 0 : removal_bulk_remove_configfiles(pkg);
648 : : }
649 : :
650 : : /* I.e., either of the two branches above. */
651 [ # # ]: 0 : if (pkg->want == PKG_WANT_PURGE) {
652 : : const char *filename;
653 : :
654 : : /* Retry empty directories, and warn on any leftovers that aren't. */
655 : 0 : removal_bulk_remove_leftover_dirs(pkg);
656 : :
657 : 0 : filename = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
658 : 0 : debug(dbg_general, "removal_bulk purge done, removing list '%s'",
659 : : filename);
660 [ # # # # ]: 0 : if (unlink(filename) && errno != ENOENT)
661 : 0 : ohshite(_("cannot remove old files list"));
662 : :
663 : 0 : filename = pkg_infodb_get_file(pkg, &pkg->installed, POSTRMFILE);
664 : 0 : debug(dbg_general, "removal_bulk purge done, removing postrm '%s'",
665 : : filename);
666 [ # # # # ]: 0 : if (unlink(filename) && errno != ENOENT)
667 : 0 : ohshite(_("can't remove old postrm script"));
668 : :
669 : 0 : pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
670 : 0 : pkg_set_want(pkg, PKG_WANT_UNKNOWN);
671 : :
672 : : /* This will mess up reverse links, but if we follow them
673 : : * we won't go back because pkg->status is PKG_STAT_NOTINSTALLED. */
674 : 0 : pkgbin_blank(&pkg->installed);
675 : : }
676 : :
677 : 0 : pkg_reset_eflags(pkg);
678 : 0 : modstatdb_note(pkg);
679 : :
680 : 0 : debug(dbg_general, "removal done");
681 : 0 : }
|