LCOV - code coverage report
Current view: top level - src/main - remove.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 0 377 0.0 %
Date: 2022-12-03 00:40:01 Functions: 0 9 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 242 0.0 %

           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 :     varbuf_end_str(&raemsgs);
     155                 :          0 :     notice(_("dependency problems prevent removal of %s:\n%s"),
     156                 :            :             pkg_name(pkg, pnaw_nonambig), raemsgs.buf);
     157                 :          0 :     ohshit(_("dependency problems - not removing"));
     158         [ #  # ]:          0 :   } else if (raemsgs.used) {
     159                 :          0 :     varbuf_end_str(&raemsgs);
     160                 :          0 :     notice(_("%s: dependency problems, but removing anyway as you requested:\n%s"),
     161                 :            :             pkg_name(pkg, pnaw_nonambig), raemsgs.buf);
     162                 :            :   }
     163                 :          0 :   varbuf_destroy(&raemsgs);
     164                 :          0 :   sincenothing= 0;
     165                 :            : 
     166         [ #  # ]:          0 :   if (pkg->eflag & PKG_EFLAG_REINSTREQ)
     167                 :          0 :     forcibleerr(FORCE_REMOVE_REINSTREQ,
     168                 :          0 :                 _("package is in a very bad inconsistent state; you should\n"
     169                 :            :                   " reinstall it before attempting a removal"));
     170                 :            : 
     171                 :          0 :   ensure_allinstfiles_available();
     172                 :          0 :   fsys_hash_init();
     173                 :            : 
     174         [ #  # ]:          0 :   if (f_noact) {
     175                 :          0 :     printf(_("Would remove or purge %s (%s) ...\n"),
     176                 :            :            pkg_name(pkg, pnaw_nonambig),
     177                 :          0 :            versiondescribe(&pkg->installed.version, vdew_nonambig));
     178                 :          0 :     pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
     179                 :          0 :     pkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
     180                 :          0 :     return;
     181                 :            :   }
     182                 :            : 
     183                 :          0 :   pkg_conffiles_mark_old(pkg);
     184                 :            : 
     185                 :            :   /* Only print and log removal action once. This avoids duplication when
     186                 :            :    * using --remove and --purge in sequence. */
     187         [ #  # ]:          0 :   if (pkg->status > PKG_STAT_CONFIGFILES) {
     188                 :          0 :     printf(_("Removing %s (%s) ...\n"), pkg_name(pkg, pnaw_nonambig),
     189                 :          0 :            versiondescribe(&pkg->installed.version, vdew_nonambig));
     190                 :          0 :     log_action("remove", pkg, &pkg->installed);
     191                 :            :   }
     192                 :            : 
     193                 :          0 :   trig_activate_packageprocessing(pkg);
     194         [ #  # ]:          0 :   if (pkg->status >= PKG_STAT_HALFCONFIGURED) {
     195                 :            :     static enum pkgstatus oldpkgstatus;
     196                 :            : 
     197                 :          0 :     oldpkgstatus= pkg->status;
     198                 :          0 :     pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
     199                 :          0 :     modstatdb_note(pkg);
     200                 :          0 :     push_cleanup(cu_prermremove, ~ehflag_normaltidy, 2,
     201                 :            :                  (void *)pkg, (void *)&oldpkgstatus);
     202                 :          0 :     maintscript_installed(pkg, PRERMFILE, "pre-removal", "remove", NULL);
     203                 :            : 
     204                 :            :     /* Will turn into ‘half-installed’ soon ... */
     205                 :          0 :     pkg_set_status(pkg, PKG_STAT_UNPACKED);
     206                 :            :   }
     207                 :            : 
     208                 :          0 :   removal_bulk(pkg);
     209                 :            : }
     210                 :            : 
     211                 :            : static void
     212                 :          0 : push_leftover(struct fsys_namenode_list **leftoverp,
     213                 :            :               struct fsys_namenode *namenode)
     214                 :            : {
     215                 :            :   struct fsys_namenode_list *newentry;
     216                 :            : 
     217                 :          0 :   newentry = nfmalloc(sizeof(*newentry));
     218                 :          0 :   newentry->next= *leftoverp;
     219                 :          0 :   newentry->namenode= namenode;
     220                 :          0 :   *leftoverp= newentry;
     221                 :          0 : }
     222                 :            : 
     223                 :            : static void
     224                 :          0 : removal_bulk_remove_file(const char *filename, const char *filetype)
     225                 :            : {
     226                 :            :   /* We need the postrm and list files for --purge. */
     227         [ #  # ]:          0 :   if (strcmp(filetype, LISTFILE) == 0 ||
     228         [ #  # ]:          0 :       strcmp(filetype, POSTRMFILE) == 0)
     229                 :          0 :     return;
     230                 :            : 
     231                 :          0 :   debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
     232                 :            : 
     233         [ #  # ]:          0 :   if (unlink(filename))
     234                 :          0 :     ohshite(_("unable to delete control info file '%.250s'"), filename);
     235                 :            : 
     236                 :          0 :   debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
     237                 :            : }
     238                 :            : 
     239                 :            : static bool
     240                 :          0 : removal_bulk_file_is_shared(struct pkginfo *pkg, struct fsys_namenode *namenode)
     241                 :            : {
     242                 :            :   struct fsys_node_pkgs_iter *iter;
     243                 :            :   struct pkginfo *otherpkg;
     244                 :          0 :   bool shared = false;
     245                 :            : 
     246         [ #  # ]:          0 :   if (pkgset_installed_instances(pkg->set) <= 1)
     247                 :          0 :     return false;
     248                 :            : 
     249                 :          0 :   iter = fsys_node_pkgs_iter_new(namenode);
     250         [ #  # ]:          0 :   while ((otherpkg = fsys_node_pkgs_iter_next(iter))) {
     251         [ #  # ]:          0 :     if (otherpkg == pkg)
     252                 :          0 :       continue;
     253         [ #  # ]:          0 :     if (otherpkg->set != pkg->set)
     254                 :          0 :       continue;
     255                 :            : 
     256                 :          0 :     debug(dbg_eachfiledetail, "removal_bulk file shared with %s, skipping",
     257                 :            :           pkg_name(otherpkg, pnaw_always));
     258                 :          0 :     shared = true;
     259                 :          0 :     break;
     260                 :            :   }
     261                 :          0 :   fsys_node_pkgs_iter_free(iter);
     262                 :            : 
     263                 :          0 :   return shared;
     264                 :            : }
     265                 :            : 
     266                 :            : static void
     267                 :          0 : removal_bulk_remove_files(struct pkginfo *pkg)
     268                 :            : {
     269                 :            :   struct fsys_hash_rev_iter rev_iter;
     270                 :            :   struct fsys_namenode_list *leftover;
     271                 :            :   struct fsys_namenode *namenode;
     272                 :            :   static struct varbuf fnvb;
     273                 :            :   struct varbuf_state fnvb_state;
     274                 :            :   struct stat stab;
     275                 :            : 
     276                 :          0 :     pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
     277                 :          0 :     modstatdb_note(pkg);
     278                 :          0 :     push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
     279                 :            : 
     280                 :          0 :     fsys_hash_rev_iter_init(&rev_iter, pkg->files);
     281                 :          0 :     leftover = NULL;
     282         [ #  # ]:          0 :     while ((namenode = fsys_hash_rev_iter_next(&rev_iter))) {
     283                 :            :       struct fsys_namenode *usenode;
     284                 :            :       bool is_dir;
     285                 :            : 
     286                 :          0 :       debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
     287                 :          0 :             namenode->name, namenode->flags);
     288                 :            : 
     289                 :          0 :       usenode = namenodetouse(namenode, pkg, &pkg->installed);
     290                 :            : 
     291                 :          0 :       varbuf_reset(&fnvb);
     292                 :          0 :       varbuf_add_str(&fnvb, dpkg_fsys_get_dir());
     293                 :          0 :       varbuf_add_str(&fnvb, usenode->name);
     294                 :          0 :       varbuf_end_str(&fnvb);
     295                 :          0 :       varbuf_snapshot(&fnvb, &fnvb_state);
     296                 :            : 
     297   [ #  #  #  # ]:          0 :       is_dir = stat(fnvb.buf, &stab) == 0 && S_ISDIR(stab.st_mode);
     298                 :            : 
     299                 :            :       /* A pkgset can share files between its instances that we
     300                 :            :        * don't want to remove, we just want to forget them. This
     301                 :            :        * applies to shared conffiles too. */
     302   [ #  #  #  # ]:          0 :       if (!is_dir && removal_bulk_file_is_shared(pkg, namenode))
     303                 :          0 :         continue;
     304                 :            : 
     305                 :            :       /* Non-shared conffiles are kept. */
     306         [ #  # ]:          0 :       if (namenode->flags & FNNF_OLD_CONFF) {
     307                 :          0 :         push_leftover(&leftover, namenode);
     308                 :          0 :         continue;
     309                 :            :       }
     310                 :            : 
     311         [ #  # ]:          0 :       if (is_dir) {
     312                 :          0 :         debug(dbg_eachfiledetail, "removal_bulk is a directory");
     313                 :            :         /* Only delete a directory or a link to one if we're the only
     314                 :            :          * package which uses it. Other files should only be listed
     315                 :            :          * in this package (but we don't check). */
     316         [ #  # ]:          0 :         if (dir_has_conffiles(namenode, pkg)) {
     317                 :          0 :           push_leftover(&leftover,namenode);
     318                 :          0 :           continue;
     319                 :            :         }
     320         [ #  # ]:          0 :         if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
     321                 :          0 :           push_leftover(&leftover, namenode);
     322                 :          0 :           continue;
     323                 :            :         }
     324         [ #  # ]:          0 :         if (dir_is_used_by_others(namenode, pkg))
     325                 :          0 :           continue;
     326                 :            : 
     327         [ #  # ]:          0 :         if (strcmp(usenode->name, "/.") == 0) {
     328                 :          0 :           debug(dbg_eachfiledetail,
     329                 :            :                 "removal_bulk '%s' root directory, cannot remove", fnvb.buf);
     330                 :          0 :           push_leftover(&leftover, namenode);
     331                 :          0 :           continue;
     332                 :            :         }
     333                 :            :       }
     334                 :            : 
     335                 :          0 :       trig_path_activate(usenode, pkg);
     336                 :            : 
     337                 :          0 :       varbuf_rollback(&fnvb_state);
     338                 :          0 :       varbuf_add_str(&fnvb, DPKGTEMPEXT);
     339                 :          0 :       varbuf_end_str(&fnvb);
     340                 :          0 :       debug(dbg_eachfiledetail, "removal_bulk cleaning temp '%s'", fnvb.buf);
     341                 :          0 :       path_remove_tree(fnvb.buf);
     342                 :            : 
     343                 :          0 :       varbuf_rollback(&fnvb_state);
     344                 :          0 :       varbuf_add_str(&fnvb, DPKGNEWEXT);
     345                 :          0 :       varbuf_end_str(&fnvb);
     346                 :          0 :       debug(dbg_eachfiledetail, "removal_bulk cleaning new '%s'", fnvb.buf);
     347                 :          0 :       path_remove_tree(fnvb.buf);
     348                 :            : 
     349                 :          0 :       varbuf_rollback(&fnvb_state);
     350                 :          0 :       varbuf_end_str(&fnvb);
     351                 :            : 
     352                 :          0 :       debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
     353   [ #  #  #  #  :          0 :       if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
                   #  # ]
     354   [ #  #  #  # ]:          0 :       if (errno == ENOTEMPTY || errno == EEXIST) {
     355                 :          0 :         debug(dbg_eachfiledetail,
     356                 :            :               "removal_bulk '%s' was not empty, will try again later",
     357                 :            :               fnvb.buf);
     358                 :          0 :         push_leftover(&leftover,namenode);
     359                 :          0 :         continue;
     360   [ #  #  #  # ]:          0 :       } else if (errno == EBUSY || errno == EPERM) {
     361                 :          0 :         warning(_("while removing %.250s, unable to remove directory '%.250s': "
     362                 :            :                   "%s - directory may be a mount point?"),
     363                 :          0 :                 pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
     364                 :          0 :         push_leftover(&leftover,namenode);
     365                 :          0 :         continue;
     366                 :            :       }
     367         [ #  # ]:          0 :       if (errno != ENOTDIR)
     368                 :          0 :         ohshite(_("cannot remove '%.250s'"), fnvb.buf);
     369                 :          0 :       debug(dbg_eachfiledetail, "removal_bulk unlinking '%s'", fnvb.buf);
     370         [ #  # ]:          0 :       if (secure_unlink(fnvb.buf))
     371                 :          0 :         ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
     372                 :            :     }
     373                 :          0 :     write_filelist_except(pkg, &pkg->installed, leftover, 0);
     374                 :          0 :     maintscript_installed(pkg, POSTRMFILE, "post-removal", "remove", NULL);
     375                 :            : 
     376                 :          0 :     trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
     377                 :            :                   trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
     378                 :          0 :     trig_file_interests_save();
     379                 :            : 
     380                 :          0 :     debug(dbg_general, "removal_bulk cleaning info directory");
     381                 :          0 :     pkg_infodb_foreach(pkg, &pkg->installed, removal_bulk_remove_file);
     382                 :          0 :     dir_sync_path(pkg_infodb_get_dir());
     383                 :            : 
     384                 :          0 :     pkg_set_status(pkg, PKG_STAT_CONFIGFILES);
     385                 :          0 :     pkg->installed.essential = false;
     386                 :          0 :     pkg->installed.is_protected = false;
     387                 :          0 :     modstatdb_note(pkg);
     388                 :          0 :     push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
     389                 :          0 : }
     390                 :            : 
     391                 :          0 : static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
     392                 :            :   struct fsys_hash_rev_iter rev_iter;
     393                 :            :   struct fsys_namenode_list *leftover;
     394                 :            :   struct fsys_namenode *namenode;
     395                 :            :   static struct varbuf fnvb;
     396                 :            :   struct stat stab;
     397                 :            : 
     398                 :            :   /* We may have modified this previously. */
     399                 :          0 :   ensure_packagefiles_available(pkg);
     400                 :            : 
     401                 :          0 :   modstatdb_note(pkg);
     402                 :          0 :   push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
     403                 :            : 
     404                 :          0 :   fsys_hash_rev_iter_init(&rev_iter, pkg->files);
     405                 :          0 :   leftover = NULL;
     406         [ #  # ]:          0 :   while ((namenode = fsys_hash_rev_iter_next(&rev_iter))) {
     407                 :            :     struct fsys_namenode *usenode;
     408                 :            : 
     409                 :          0 :     debug(dbg_eachfile, "removal_bulk '%s' flags=%o",
     410                 :          0 :           namenode->name, namenode->flags);
     411         [ #  # ]:          0 :     if (namenode->flags & FNNF_OLD_CONFF) {
     412                 :            :       /* This can only happen if removal_bulk_remove_configfiles() got
     413                 :            :        * interrupted half way. */
     414                 :          0 :       debug(dbg_eachfiledetail, "removal_bulk expecting only left over dirs, "
     415                 :            :                                 "ignoring conffile '%s'", namenode->name);
     416                 :          0 :       continue;
     417                 :            :     }
     418                 :            : 
     419                 :          0 :     usenode = namenodetouse(namenode, pkg, &pkg->installed);
     420                 :            : 
     421                 :          0 :     varbuf_reset(&fnvb);
     422                 :          0 :     varbuf_add_str(&fnvb, dpkg_fsys_get_dir());
     423                 :          0 :     varbuf_add_str(&fnvb, usenode->name);
     424                 :          0 :     varbuf_end_str(&fnvb);
     425                 :            : 
     426   [ #  #  #  # ]:          0 :     if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
     427                 :          0 :       debug(dbg_eachfiledetail, "removal_bulk is a directory");
     428                 :            :       /* Only delete a directory or a link to one if we're the only
     429                 :            :        * package which uses it. Other files should only be listed
     430                 :            :        * in this package (but we don't check). */
     431         [ #  # ]:          0 :       if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
     432                 :          0 :         push_leftover(&leftover, namenode);
     433                 :          0 :         continue;
     434                 :            :       }
     435         [ #  # ]:          0 :       if (dir_is_used_by_others(namenode, pkg))
     436                 :          0 :         continue;
     437                 :            : 
     438         [ #  # ]:          0 :       if (strcmp(usenode->name, "/.") == 0) {
     439                 :          0 :         debug(dbg_eachfiledetail,
     440                 :            :               "removal_bulk '%s' root directory, cannot remove", fnvb.buf);
     441                 :          0 :         push_leftover(&leftover, namenode);
     442                 :          0 :         continue;
     443                 :            :       }
     444                 :            :     }
     445                 :            : 
     446                 :          0 :     trig_path_activate(usenode, pkg);
     447                 :            : 
     448                 :          0 :     debug(dbg_eachfiledetail, "removal_bulk removing '%s'", fnvb.buf);
     449   [ #  #  #  #  :          0 :     if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
                   #  # ]
     450   [ #  #  #  # ]:          0 :     if (errno == ENOTEMPTY || errno == EEXIST) {
     451                 :          0 :       warning(_("while removing %.250s, directory '%.250s' not empty so not removed"),
     452                 :            :               pkg_name(pkg, pnaw_nonambig), namenode->name);
     453                 :          0 :       push_leftover(&leftover,namenode);
     454                 :          0 :       continue;
     455   [ #  #  #  # ]:          0 :     } else if (errno == EBUSY || errno == EPERM) {
     456                 :          0 :       warning(_("while removing %.250s, unable to remove directory '%.250s': "
     457                 :            :                 "%s - directory may be a mount point?"),
     458                 :          0 :               pkg_name(pkg, pnaw_nonambig), namenode->name, strerror(errno));
     459                 :          0 :       push_leftover(&leftover,namenode);
     460                 :          0 :       continue;
     461                 :            :     }
     462         [ #  # ]:          0 :     if (errno != ENOTDIR)
     463                 :          0 :       ohshite(_("cannot remove '%.250s'"), fnvb.buf);
     464                 :            : 
     465   [ #  #  #  # ]:          0 :     if (lstat(fnvb.buf, &stab) == 0 && S_ISLNK(stab.st_mode)) {
     466                 :          0 :       debug(dbg_eachfiledetail, "removal_bulk is a symlink to a directory");
     467                 :            : 
     468         [ #  # ]:          0 :       if (unlink(fnvb.buf))
     469                 :          0 :         ohshite(_("cannot remove '%.250s'"), fnvb.buf);
     470                 :            : 
     471                 :          0 :       continue;
     472                 :            :     }
     473                 :            : 
     474                 :          0 :     push_leftover(&leftover,namenode);
     475                 :            :   }
     476                 :          0 :   write_filelist_except(pkg, &pkg->installed, leftover, 0);
     477                 :            : 
     478                 :          0 :   modstatdb_note(pkg);
     479                 :          0 :   push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
     480                 :          0 : }
     481                 :            : 
     482                 :          0 : static void removal_bulk_remove_configfiles(struct pkginfo *pkg) {
     483                 :            :   static const char *const removeconffexts[] = { REMOVECONFFEXTS, NULL };
     484                 :            :   int rc;
     485                 :            :   int conffnameused, conffbasenamelen;
     486                 :            :   char *conffbasename;
     487                 :            :   struct conffile *conff, **lconffp;
     488                 :            :   struct fsys_namenode_list *searchfile;
     489                 :            :   DIR *dsd;
     490                 :            :   struct dirent *de;
     491                 :            :   char *p;
     492                 :            :   const char *const *ext;
     493                 :            : 
     494                 :          0 :     printf(_("Purging configuration files for %s (%s) ...\n"),
     495                 :            :            pkg_name(pkg, pnaw_nonambig),
     496                 :          0 :            versiondescribe(&pkg->installed.version, vdew_nonambig));
     497                 :          0 :     log_action("purge", pkg, &pkg->installed);
     498                 :          0 :     trig_activate_packageprocessing(pkg);
     499                 :            : 
     500                 :            :     /* We may have modified this above. */
     501                 :          0 :     ensure_packagefiles_available(pkg);
     502                 :            : 
     503                 :            :     /* We're about to remove the configuration, so remove the note
     504                 :            :      * about which version it was ... */
     505                 :          0 :     dpkg_version_blank(&pkg->configversion);
     506                 :          0 :     modstatdb_note(pkg);
     507                 :            : 
     508                 :            :     /* Remove from our list any conffiles that aren't ours any more or
     509                 :            :      * are involved in diversions, except if we are the package doing the
     510                 :            :      * diverting. */
     511         [ #  # ]:          0 :     for (lconffp = &pkg->installed.conffiles; (conff = *lconffp) != NULL; ) {
     512                 :          0 :       for (searchfile = pkg->files;
     513   [ #  #  #  # ]:          0 :            searchfile && strcmp(searchfile->namenode->name,conff->name);
     514                 :          0 :            searchfile= searchfile->next);
     515         [ #  # ]:          0 :       if (!searchfile) {
     516                 :          0 :         debug(dbg_conff, "removal_bulk conffile not ours any more '%s'",
     517                 :            :               conff->name);
     518                 :          0 :         *lconffp= conff->next;
     519         [ #  # ]:          0 :       } else if (searchfile->namenode->divert &&
     520         [ #  # ]:          0 :                  (searchfile->namenode->divert->camefrom ||
     521         [ #  # ]:          0 :                   (searchfile->namenode->divert->useinstead &&
     522         [ #  # ]:          0 :                    searchfile->namenode->divert->pkgset != pkg->set))) {
     523                 :          0 :         debug(dbg_conff, "removal_bulk conffile '%s' ignored due to diversion",
     524                 :            :               conff->name);
     525                 :          0 :         *lconffp= conff->next;
     526                 :            :       } else {
     527                 :          0 :         debug(dbg_conffdetail, "removal_bulk set to new conffile '%s'",
     528                 :            :               conff->name);
     529                 :          0 :         conff->hash = NEWCONFFILEFLAG;
     530                 :          0 :         lconffp= &conff->next;
     531                 :            :       }
     532                 :            :     }
     533                 :          0 :     modstatdb_note(pkg);
     534                 :            : 
     535         [ #  # ]:          0 :     for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
     536                 :            :       struct fsys_namenode *namenode, *usenode;
     537                 :            :     static struct varbuf fnvb, removevb;
     538                 :            :       struct varbuf_state removevb_state;
     539                 :            : 
     540         [ #  # ]:          0 :       if (conff->obsolete) {
     541                 :          0 :         debug(dbg_conffdetail, "removal_bulk conffile obsolete %s",
     542                 :            :               conff->name);
     543                 :            :       }
     544                 :          0 :       varbuf_reset(&fnvb);
     545                 :          0 :       rc = conffderef(pkg, &fnvb, conff->name);
     546         [ #  # ]:          0 :       debug(dbg_conffdetail, "removal_bulk conffile '%s' (= '%s')",
     547                 :            :             conff->name, rc == -1 ? "<rc == -1>" : fnvb.buf);
     548         [ #  # ]:          0 :       if (rc == -1)
     549                 :          0 :         continue;
     550                 :            : 
     551                 :          0 :       namenode = fsys_hash_find_node(conff->name, 0);
     552                 :          0 :       usenode = namenodetouse(namenode, pkg, &pkg->installed);
     553                 :            : 
     554                 :          0 :       trig_path_activate(usenode, pkg);
     555                 :            : 
     556                 :          0 :       conffnameused = fnvb.used;
     557   [ #  #  #  #  :          0 :       if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
                   #  # ]
     558                 :          0 :         ohshite(_("cannot remove old config file '%.250s' (= '%.250s')"),
     559                 :            :                 conff->name, fnvb.buf);
     560         [ #  # ]:          0 :       p= strrchr(fnvb.buf,'/'); if (!p) continue;
     561                 :          0 :       *p = '\0';
     562                 :          0 :       varbuf_reset(&removevb);
     563                 :          0 :       varbuf_add_dir(&removevb, fnvb.buf);
     564                 :          0 :       varbuf_end_str(&removevb);
     565                 :          0 :       varbuf_snapshot(&removevb, &removevb_state);
     566                 :            : 
     567                 :          0 :       dsd= opendir(removevb.buf);
     568         [ #  # ]:          0 :       if (!dsd) {
     569                 :          0 :         int e=errno;
     570                 :          0 :         debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
     571                 :          0 :               fnvb.buf, strerror(e)); errno= e;
     572   [ #  #  #  # ]:          0 :         if (errno == ENOENT || errno == ENOTDIR) continue;
     573                 :          0 :         ohshite(_("cannot read config file directory '%.250s' (from '%.250s')"),
     574                 :            :                 fnvb.buf, conff->name);
     575                 :            :       }
     576                 :          0 :       debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
     577                 :          0 :       push_cleanup(cu_closedir, ~0, 1, (void *)dsd);
     578                 :          0 :       *p= '/';
     579                 :          0 :       conffbasenamelen= strlen(++p);
     580                 :          0 :       conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
     581         [ #  # ]:          0 :       while ((de = readdir(dsd)) != NULL) {
     582                 :          0 :         debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry='%s'"
     583                 :            :               " conffbasename='%s' conffnameused=%d conffbasenamelen=%d",
     584                 :          0 :               de->d_name, conffbasename, conffnameused, conffbasenamelen);
     585         [ #  # ]:          0 :         if (strncmp(de->d_name, conffbasename, conffbasenamelen) == 0) {
     586                 :          0 :           debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
     587         [ #  # ]:          0 :           for (ext= removeconffexts; *ext; ext++)
     588         [ #  # ]:          0 :             if (strcmp(*ext, de->d_name + conffbasenamelen) == 0)
     589                 :          0 :               goto yes_remove;
     590                 :          0 :           p= de->d_name+conffbasenamelen;
     591         [ #  # ]:          0 :           if (*p++ == '~') {
     592   [ #  #  #  # ]:          0 :             while (*p && c_isdigit(*p))
     593                 :          0 :               p++;
     594   [ #  #  #  # ]:          0 :             if (*p == '~' && !*++p) goto yes_remove;
     595                 :            :           }
     596                 :            :         }
     597                 :          0 :         debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
     598         [ #  # ]:          0 :         if (de->d_name[0] == '#' &&
     599         [ #  # ]:          0 :             strncmp(de->d_name + 1, conffbasename, conffbasenamelen) == 0 &&
     600         [ #  # ]:          0 :             strcmp(de->d_name + 1 + conffbasenamelen, "#") == 0)
     601                 :          0 :           goto yes_remove;
     602                 :          0 :         debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
     603                 :          0 :         continue;
     604                 :          0 :       yes_remove:
     605                 :          0 :         varbuf_rollback(&removevb_state);
     606                 :          0 :         varbuf_add_str(&removevb, de->d_name);
     607                 :          0 :         varbuf_end_str(&removevb);
     608                 :          0 :         debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing '%s'",
     609                 :            :               removevb.buf);
     610   [ #  #  #  #  :          0 :         if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
                   #  # ]
     611                 :          0 :           ohshite(_("cannot remove old backup config file '%.250s' (of '%.250s')"),
     612                 :            :                   removevb.buf, conff->name);
     613                 :            :       }
     614                 :          0 :       pop_cleanup(ehflag_normaltidy); /* closedir */
     615                 :            :     }
     616                 :            : 
     617                 :            :     /* Remove the conffiles from the file list file. */
     618                 :          0 :     write_filelist_except(pkg, &pkg->installed, pkg->files,
     619                 :            :                           FNNF_OLD_CONFF);
     620                 :            : 
     621                 :          0 :     pkg->installed.conffiles = NULL;
     622                 :          0 :     modstatdb_note(pkg);
     623                 :            : 
     624                 :          0 :     maintscript_installed(pkg, POSTRMFILE, "post-removal", "purge", NULL);
     625                 :          0 : }
     626                 :            : 
     627                 :            : /*
     628                 :            :  * This is used both by deferred_remove() in this file, and at the end of
     629                 :            :  * process_archive() in archives.c if it needs to finish removing a
     630                 :            :  * conflicting package.
     631                 :            :  */
     632                 :          0 : void removal_bulk(struct pkginfo *pkg) {
     633                 :            :   bool foundpostrm;
     634                 :            : 
     635                 :          0 :   debug(dbg_general, "removal_bulk package %s", pkg_name(pkg, pnaw_always));
     636                 :            : 
     637         [ #  # ]:          0 :   if (pkg->status == PKG_STAT_HALFINSTALLED ||
     638         [ #  # ]:          0 :       pkg->status == PKG_STAT_UNPACKED) {
     639                 :          0 :     removal_bulk_remove_files(pkg);
     640                 :            :   }
     641                 :            : 
     642                 :          0 :   foundpostrm = pkg_infodb_has_file(pkg, &pkg->installed, POSTRMFILE);
     643                 :            : 
     644                 :          0 :   debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
     645                 :            : 
     646   [ #  #  #  # ]:          0 :   if (!foundpostrm && !pkg->installed.conffiles) {
     647                 :            :     /* If there are no config files and no postrm script then we
     648                 :            :      * go straight into ‘purge’.  */
     649                 :          0 :     debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
     650                 :            : 
     651                 :          0 :     pkg_set_want(pkg, PKG_WANT_PURGE);
     652                 :          0 :     dpkg_version_blank(&pkg->configversion);
     653         [ #  # ]:          0 :   } else if (pkg->want == PKG_WANT_PURGE) {
     654                 :            : 
     655                 :          0 :     removal_bulk_remove_configfiles(pkg);
     656                 :            : 
     657                 :            :   }
     658                 :            : 
     659                 :            :   /* I.e., either of the two branches above. */
     660         [ #  # ]:          0 :   if (pkg->want == PKG_WANT_PURGE) {
     661                 :            :     const char *filename;
     662                 :            : 
     663                 :            :     /* Retry empty directories, and warn on any leftovers that aren't. */
     664                 :          0 :     removal_bulk_remove_leftover_dirs(pkg);
     665                 :            : 
     666                 :          0 :     filename = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
     667                 :          0 :     debug(dbg_general, "removal_bulk purge done, removing list '%s'",
     668                 :            :           filename);
     669   [ #  #  #  # ]:          0 :     if (unlink(filename) && errno != ENOENT)
     670                 :          0 :       ohshite(_("cannot remove old files list"));
     671                 :            : 
     672                 :          0 :     filename = pkg_infodb_get_file(pkg, &pkg->installed, POSTRMFILE);
     673                 :          0 :     debug(dbg_general, "removal_bulk purge done, removing postrm '%s'",
     674                 :            :           filename);
     675   [ #  #  #  # ]:          0 :     if (unlink(filename) && errno != ENOENT)
     676                 :          0 :       ohshite(_("can't remove old postrm script"));
     677                 :            : 
     678                 :          0 :     pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
     679                 :          0 :     pkg_set_want(pkg, PKG_WANT_UNKNOWN);
     680                 :            : 
     681                 :            :     /* This will mess up reverse links, but if we follow them
     682                 :            :      * we won't go back because pkg->status is PKG_STAT_NOTINSTALLED. */
     683                 :          0 :     pkgbin_blank(&pkg->installed);
     684                 :            :   }
     685                 :            : 
     686                 :          0 :   pkg_reset_eflags(pkg);
     687                 :          0 :   modstatdb_note(pkg);
     688                 :            : 
     689                 :          0 :   debug(dbg_general, "removal done");
     690                 :          0 : }

Generated by: LCOV version 1.16