LCOV - code coverage report
Current view: top level - src/query - main.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 16 390 4.1 %
Date: 2022-12-03 00:40:01 Functions: 2 26 7.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 192 0.5 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * dpkg-query - program for query the dpkg database
       3                 :            :  * querycmd.c - status enquiry and listing options
       4                 :            :  *
       5                 :            :  * Copyright © 1995,1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
       6                 :            :  * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
       7                 :            :  * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
       8                 :            :  * Copyright © 2011 Linaro Limited
       9                 :            :  * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
      10                 :            :  *
      11                 :            :  * This is free software; you can redistribute it and/or modify
      12                 :            :  * it under the terms of the GNU General Public License as published by
      13                 :            :  * the Free Software Foundation; either version 2 of the License, or
      14                 :            :  * (at your option) any later version.
      15                 :            :  *
      16                 :            :  * This is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU General Public License for more details.
      20                 :            :  *
      21                 :            :  * You should have received a copy of the GNU General Public License
      22                 :            :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <config.h>
      26                 :            : #include <compat.h>
      27                 :            : 
      28                 :            : #include <sys/types.h>
      29                 :            : #include <sys/stat.h>
      30                 :            : 
      31                 :            : #if HAVE_LOCALE_H
      32                 :            : #include <locale.h>
      33                 :            : #endif
      34                 :            : #include <errno.h>
      35                 :            : #include <limits.h>
      36                 :            : #include <string.h>
      37                 :            : #include <fcntl.h>
      38                 :            : #include <dirent.h>
      39                 :            : #include <fnmatch.h>
      40                 :            : #include <unistd.h>
      41                 :            : #include <stdlib.h>
      42                 :            : #include <stdio.h>
      43                 :            : 
      44                 :            : #include <dpkg/i18n.h>
      45                 :            : #include <dpkg/dpkg.h>
      46                 :            : #include <dpkg/dpkg-db.h>
      47                 :            : #include <dpkg/debug.h>
      48                 :            : #include <dpkg/pkg-array.h>
      49                 :            : #include <dpkg/pkg-spec.h>
      50                 :            : #include <dpkg/pkg-format.h>
      51                 :            : #include <dpkg/pkg-show.h>
      52                 :            : #include <dpkg/string.h>
      53                 :            : #include <dpkg/path.h>
      54                 :            : #include <dpkg/file.h>
      55                 :            : #include <dpkg/pager.h>
      56                 :            : #include <dpkg/options.h>
      57                 :            : #include <dpkg/db-ctrl.h>
      58                 :            : #include <dpkg/db-fsys.h>
      59                 :            : 
      60                 :            : #include "actions.h"
      61                 :            : 
      62                 :            : static const char *showformat = "${binary:Package}\t${Version}\n";
      63                 :            : 
      64                 :            : static int opt_loadavail = 0;
      65                 :            : 
      66                 :            : static int
      67                 :          0 : pkg_array_match_patterns(struct pkg_array *array,
      68                 :            :                          pkg_array_visitor_func *pkg_visitor, void *pkg_data,
      69                 :            :                          const char *const *argv)
      70                 :            : {
      71                 :            :   int argc, i, ip, *found;
      72                 :          0 :   int rc = 0;
      73                 :            :   struct pkg_spec *ps;
      74                 :            : 
      75         [ #  # ]:          0 :   for (argc = 0; argv[argc]; argc++);
      76                 :          0 :   found = m_calloc(argc, sizeof(int));
      77                 :            : 
      78                 :          0 :   ps = m_malloc(sizeof(*ps) * argc);
      79         [ #  # ]:          0 :   for (ip = 0; ip < argc; ip++) {
      80                 :          0 :     pkg_spec_init(&ps[ip], PKG_SPEC_PATTERNS | PKG_SPEC_ARCH_WILDCARD);
      81                 :          0 :     pkg_spec_parse(&ps[ip], argv[ip]);
      82                 :            :   }
      83                 :            : 
      84         [ #  # ]:          0 :   for (i = 0; i < array->n_pkgs; i++) {
      85                 :            :     struct pkginfo *pkg;
      86                 :          0 :     bool pkg_found = false;
      87                 :            : 
      88                 :          0 :     pkg = array->pkgs[i];
      89         [ #  # ]:          0 :     for (ip = 0; ip < argc; ip++) {
      90         [ #  # ]:          0 :       if (pkg_spec_match_pkg(&ps[ip], pkg, &pkg->installed)) {
      91                 :          0 :         pkg_found = true;
      92                 :          0 :         found[ip]++;
      93                 :            :       }
      94                 :            :     }
      95         [ #  # ]:          0 :     if (!pkg_found)
      96                 :          0 :       array->pkgs[i] = NULL;
      97                 :            :   }
      98                 :            : 
      99                 :          0 :   pkg_array_foreach(array, pkg_visitor, pkg_data);
     100                 :            : 
     101         [ #  # ]:          0 :   for (ip = 0; ip < argc; ip++) {
     102         [ #  # ]:          0 :     if (!found[ip]) {
     103                 :          0 :       notice(_("no packages found matching %s"), argv[ip]);
     104                 :          0 :       rc++;
     105                 :            :     }
     106                 :          0 :     pkg_spec_destroy(&ps[ip]);
     107                 :            :   }
     108                 :            : 
     109                 :          0 :   free(ps);
     110                 :          0 :   free(found);
     111                 :            : 
     112                 :          0 :   return rc;
     113                 :            : }
     114                 :            : 
     115                 :            : struct list_format {
     116                 :            :   bool head;
     117                 :            :   int nw;
     118                 :            :   int vw;
     119                 :            :   int aw;
     120                 :            :   int dw;
     121                 :            : };
     122                 :            : 
     123                 :            : static void
     124                 :          0 : list_format_init(struct list_format *fmt, struct pkg_array *array)
     125                 :            : {
     126                 :            :   int i;
     127                 :            : 
     128         [ #  # ]:          0 :   if (fmt->nw != 0)
     129                 :          0 :     return;
     130                 :            : 
     131                 :          0 :   fmt->nw = 14;
     132                 :          0 :   fmt->vw = 12;
     133                 :          0 :   fmt->aw = 12;
     134                 :          0 :   fmt->dw = 33;
     135                 :            : 
     136         [ #  # ]:          0 :   for (i = 0; i < array->n_pkgs; i++) {
     137                 :            :     int plen, vlen, alen, dlen;
     138                 :            : 
     139         [ #  # ]:          0 :     if (array->pkgs[i] == NULL)
     140                 :          0 :       continue;
     141                 :            : 
     142                 :          0 :     plen = str_width(pkg_name(array->pkgs[i], pnaw_nonambig));
     143                 :          0 :     vlen = str_width(versiondescribe(&array->pkgs[i]->installed.version,
     144                 :            :                                      vdew_nonambig));
     145                 :          0 :     alen = str_width(dpkg_arch_describe(array->pkgs[i]->installed.arch));
     146                 :          0 :     pkg_synopsis(array->pkgs[i], &dlen);
     147                 :            : 
     148         [ #  # ]:          0 :     if (plen > fmt->nw)
     149                 :          0 :       fmt->nw = plen;
     150         [ #  # ]:          0 :     if (vlen > fmt->vw)
     151                 :          0 :       fmt->vw = vlen;
     152         [ #  # ]:          0 :     if (alen > fmt->aw)
     153                 :          0 :       fmt->aw = alen;
     154         [ #  # ]:          0 :     if (dlen > fmt->dw)
     155                 :          0 :       fmt->dw = dlen;
     156                 :            :   }
     157                 :            : }
     158                 :            : 
     159                 :            : static void
     160                 :          0 : list_format_print(struct list_format *fmt,
     161                 :            :                   int c_want, int c_status, int c_eflag,
     162                 :            :                   const char *name, const char *version, const char *arch,
     163                 :            :                   const char *desc, int desc_len)
     164                 :            : {
     165                 :            :   struct str_crop_info ns, vs, as, ds;
     166                 :            : 
     167                 :          0 :   str_gen_crop(name, fmt->nw, &ns);
     168                 :          0 :   str_gen_crop(version, fmt->vw, &vs);
     169                 :          0 :   str_gen_crop(arch, fmt->aw, &as);
     170                 :          0 :   str_gen_crop(desc, desc_len, &ds);
     171                 :            : 
     172                 :          0 :   printf("%c%c%c %-*.*s %-*.*s %-*.*s %.*s\n", c_want, c_status, c_eflag,
     173                 :            :          ns.max_bytes, ns.str_bytes, name,
     174                 :            :          vs.max_bytes, vs.str_bytes, version,
     175                 :            :          as.max_bytes, as.str_bytes, arch,
     176                 :            :          ds.str_bytes, desc);
     177                 :          0 : }
     178                 :            : 
     179                 :            : static void
     180                 :          0 : list_format_print_header(struct list_format *fmt)
     181                 :            : {
     182                 :            :   int l;
     183                 :            : 
     184         [ #  # ]:          0 :   if (fmt->head)
     185                 :          0 :     return;
     186                 :            : 
     187                 :            :   /* TRANSLATORS: This is the header that appears on 'dpkg-query -l'. The
     188                 :            :    * string should remain under 80 characters. The uppercase letters in
     189                 :            :    * the state values denote the abbreviated letter that will appear on
     190                 :            :    * the first three columns, which should ideally match the English one
     191                 :            :    * (e.g. Remove → supRimeix), see dpkg-query(1) for further details. The
     192                 :            :    * translated message can use additional lines if needed. */
     193                 :          0 :   fputs(_("\
     194                 :            : Desired=Unknown/Install/Remove/Purge/Hold\n\
     195                 :            : | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend\n\
     196                 :            : |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)\n"), stdout);
     197                 :          0 :   list_format_print(fmt, '|', '|', '/', _("Name"), _("Version"),
     198                 :          0 :                     _("Architecture"), _("Description"), fmt->dw);
     199                 :            : 
     200                 :            :   /* Status */
     201                 :          0 :   printf("+++-");
     202                 :            : 
     203                 :            :  /* Package name. */
     204         [ #  # ]:          0 :   for (l = 0; l < fmt->nw; l++)
     205                 :          0 :     printf("=");
     206                 :          0 :   printf("-");
     207                 :            : 
     208                 :            :   /* Version. */
     209         [ #  # ]:          0 :   for (l = 0; l < fmt->vw; l++)
     210                 :          0 :     printf("=");
     211                 :          0 :   printf("-");
     212                 :            : 
     213                 :            :   /* Architecture. */
     214         [ #  # ]:          0 :   for (l = 0; l < fmt->aw; l++)
     215                 :          0 :     printf("=");
     216                 :          0 :   printf("-");
     217                 :            : 
     218                 :            :   /* Description. */
     219         [ #  # ]:          0 :   for (l = 0; l < fmt->dw; l++)
     220                 :          0 :     printf("=");
     221                 :          0 :   printf("\n");
     222                 :            : 
     223                 :          0 :   fmt->head = true;
     224                 :            : }
     225                 :            : 
     226                 :            : static void
     227                 :          0 : pkg_array_list_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
     228                 :            : {
     229                 :          0 :   struct list_format *fmt = pkg_data;
     230                 :            :   int l;
     231                 :            :   const char *pdesc;
     232                 :            : 
     233                 :          0 :   list_format_init(fmt, array);
     234                 :          0 :   list_format_print_header(fmt);
     235                 :            : 
     236                 :          0 :   pdesc = pkg_synopsis(pkg, &l);
     237                 :          0 :   l = min(l, fmt->dw);
     238                 :            : 
     239                 :          0 :   list_format_print(fmt,
     240                 :            :                     pkg_abbrev_want(pkg),
     241                 :            :                     pkg_abbrev_status(pkg),
     242                 :            :                     pkg_abbrev_eflag(pkg),
     243                 :            :                     pkg_name(pkg, pnaw_nonambig),
     244                 :          0 :                     versiondescribe(&pkg->installed.version, vdew_nonambig),
     245                 :            :                     dpkg_arch_describe(pkg->installed.arch),
     246                 :            :                     pdesc, l);
     247                 :          0 : }
     248                 :            : 
     249                 :            : static int
     250                 :          0 : listpackages(const char *const *argv)
     251                 :            : {
     252                 :            :   struct pkg_array array;
     253                 :            :   struct pkginfo *pkg;
     254                 :            :   int i;
     255                 :          0 :   int rc = 0;
     256                 :            :   struct list_format fmt;
     257                 :            :   struct pager *pager;
     258                 :            : 
     259         [ #  # ]:          0 :   if (!opt_loadavail)
     260                 :          0 :     modstatdb_open(msdbrw_readonly);
     261                 :            :   else
     262                 :          0 :     modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
     263                 :            : 
     264                 :          0 :   pkg_array_init_from_hash(&array);
     265                 :          0 :   pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
     266                 :            : 
     267                 :          0 :   memset(&fmt, 0, sizeof(fmt));
     268                 :            : 
     269                 :          0 :   pager = pager_spawn(_("showing package list on pager"));
     270                 :            : 
     271         [ #  # ]:          0 :   if (!*argv) {
     272         [ #  # ]:          0 :     for (i = 0; i < array.n_pkgs; i++) {
     273                 :          0 :       pkg = array.pkgs[i];
     274         [ #  # ]:          0 :       if (pkg->status == PKG_STAT_NOTINSTALLED)
     275                 :          0 :         array.pkgs[i] = NULL;
     276                 :            :     }
     277                 :            : 
     278                 :          0 :     pkg_array_foreach(&array, pkg_array_list_item, &fmt);
     279                 :            :   } else {
     280                 :          0 :     rc = pkg_array_match_patterns(&array, pkg_array_list_item, &fmt, argv);
     281                 :            :   }
     282                 :            : 
     283                 :          0 :   m_output(stdout, _("<standard output>"));
     284                 :          0 :   m_output(stderr, _("<standard error>"));
     285                 :            : 
     286                 :          0 :   pager_reap(pager);
     287                 :            : 
     288                 :          0 :   pkg_array_destroy(&array);
     289                 :          0 :   modstatdb_shutdown();
     290                 :            : 
     291                 :          0 :   return rc;
     292                 :            : }
     293                 :            : 
     294                 :            : static int
     295                 :          0 : searchoutput(struct fsys_namenode *namenode)
     296                 :            : {
     297                 :            :   struct fsys_node_pkgs_iter *iter;
     298                 :            :   struct pkginfo *pkg_owner;
     299                 :            :   int found;
     300                 :            : 
     301         [ #  # ]:          0 :   if (namenode->divert) {
     302                 :          0 :     const char *name_from = namenode->divert->camefrom ?
     303         [ #  # ]:          0 :                             namenode->divert->camefrom->name : namenode->name;
     304                 :          0 :     const char *name_to = namenode->divert->useinstead ?
     305         [ #  # ]:          0 :                           namenode->divert->useinstead->name : namenode->name;
     306                 :            : 
     307         [ #  # ]:          0 :     if (namenode->divert->pkgset) {
     308                 :          0 :       printf(_("diversion by %s from: %s\n"),
     309                 :          0 :              namenode->divert->pkgset->name, name_from);
     310                 :          0 :       printf(_("diversion by %s to: %s\n"),
     311                 :          0 :              namenode->divert->pkgset->name, name_to);
     312                 :            :     } else {
     313                 :          0 :       printf(_("local diversion from: %s\n"), name_from);
     314                 :          0 :       printf(_("local diversion to: %s\n"), name_to);
     315                 :            :     }
     316                 :            :   }
     317                 :          0 :   found= 0;
     318                 :            : 
     319                 :          0 :   iter = fsys_node_pkgs_iter_new(namenode);
     320         [ #  # ]:          0 :   while ((pkg_owner = fsys_node_pkgs_iter_next(iter))) {
     321         [ #  # ]:          0 :     if (found)
     322                 :          0 :       fputs(", ", stdout);
     323                 :          0 :     fputs(pkg_name(pkg_owner, pnaw_nonambig), stdout);
     324                 :          0 :     found++;
     325                 :            :   }
     326                 :          0 :   fsys_node_pkgs_iter_free(iter);
     327                 :            : 
     328         [ #  # ]:          0 :   if (found) printf(": %s\n",namenode->name);
     329                 :          0 :   return found + (namenode->divert ? 1 : 0);
     330                 :            : }
     331                 :            : 
     332                 :            : static int
     333                 :          0 : searchfiles(const char *const *argv)
     334                 :            : {
     335                 :            :   struct fsys_namenode *namenode;
     336                 :            :   struct fsys_hash_iter *iter;
     337                 :            :   const char *thisarg;
     338                 :            :   int found;
     339                 :          0 :   int failures = 0;
     340                 :          0 :   struct varbuf path = VARBUF_INIT;
     341                 :            :   static struct varbuf vb;
     342                 :            : 
     343         [ #  # ]:          0 :   if (!*argv)
     344                 :          0 :     badusage(_("--search needs at least one file name pattern argument"));
     345                 :            : 
     346                 :          0 :   modstatdb_open(msdbrw_readonly);
     347                 :          0 :   ensure_allinstfiles_available_quiet();
     348                 :          0 :   ensure_diversions();
     349                 :            : 
     350         [ #  # ]:          0 :   while ((thisarg = *argv++) != NULL) {
     351                 :          0 :     found= 0;
     352                 :            : 
     353         [ #  # ]:          0 :     if (!strchr("*[?/",*thisarg)) {
     354                 :          0 :       varbuf_reset(&vb);
     355                 :          0 :       varbuf_add_char(&vb, '*');
     356                 :          0 :       varbuf_add_str(&vb, thisarg);
     357                 :          0 :       varbuf_add_char(&vb, '*');
     358                 :          0 :       varbuf_end_str(&vb);
     359                 :          0 :       thisarg= vb.buf;
     360                 :            :     }
     361         [ #  # ]:          0 :     if (!strpbrk(thisarg, "*[?\\")) {
     362                 :            :       /* Trim trailing ‘/’ and ‘/.’ from the argument if it is not
     363                 :            :        * a pattern, just a pathname. */
     364                 :          0 :       varbuf_reset(&path);
     365                 :          0 :       varbuf_add_str(&path, thisarg);
     366                 :          0 :       varbuf_end_str(&path);
     367                 :          0 :       varbuf_trunc(&path, path_trim_slash_slashdot(path.buf));
     368                 :            : 
     369                 :          0 :       namenode = fsys_hash_find_node(path.buf, 0);
     370                 :          0 :       found += searchoutput(namenode);
     371                 :            :     } else {
     372                 :          0 :       iter = fsys_hash_iter_new();
     373         [ #  # ]:          0 :       while ((namenode = fsys_hash_iter_next(iter)) != NULL) {
     374         [ #  # ]:          0 :         if (fnmatch(thisarg,namenode->name,0)) continue;
     375                 :          0 :         found+= searchoutput(namenode);
     376                 :            :       }
     377                 :          0 :       fsys_hash_iter_free(iter);
     378                 :            :     }
     379         [ #  # ]:          0 :     if (!found) {
     380                 :          0 :       notice(_("no path found matching pattern %s"), thisarg);
     381                 :          0 :       failures++;
     382                 :          0 :       m_output(stderr, _("<standard error>"));
     383                 :            :     } else {
     384                 :          0 :       m_output(stdout, _("<standard output>"));
     385                 :            :     }
     386                 :            :   }
     387                 :          0 :   modstatdb_shutdown();
     388                 :            : 
     389                 :          0 :   varbuf_destroy(&path);
     390                 :            : 
     391                 :          0 :   return failures;
     392                 :            : }
     393                 :            : 
     394                 :            : static int
     395                 :          0 : print_status(const char *const *argv)
     396                 :            : {
     397                 :            :   const char *thisarg;
     398                 :            :   struct pkginfo *pkg;
     399                 :          0 :   int failures = 0;
     400                 :            : 
     401                 :          0 :   modstatdb_open(msdbrw_readonly);
     402                 :            : 
     403         [ #  # ]:          0 :   if (!*argv) {
     404                 :          0 :     writedb_records(stdout, _("<standard output>"), 0);
     405                 :            :   } else {
     406         [ #  # ]:          0 :     while ((thisarg = *argv++) != NULL) {
     407                 :          0 :       pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
     408                 :            : 
     409         [ #  # ]:          0 :       if (pkg->status == PKG_STAT_NOTINSTALLED &&
     410   [ #  #  #  # ]:          0 :           pkg->priority == PKG_PRIO_UNKNOWN &&
     411                 :          0 :           str_is_unset(pkg->section) &&
     412         [ #  # ]:          0 :           !pkg->archives &&
     413         [ #  # ]:          0 :           pkg->want == PKG_WANT_UNKNOWN &&
     414         [ #  # ]:          0 :           !pkg_is_informative(pkg, &pkg->installed)) {
     415                 :          0 :         notice(_("package '%s' is not installed and no information is available"),
     416                 :            :                pkg_name(pkg, pnaw_nonambig));
     417                 :          0 :         failures++;
     418                 :            :       } else {
     419                 :          0 :         writerecord(stdout, _("<standard output>"), pkg, &pkg->installed);
     420                 :            :       }
     421                 :            : 
     422         [ #  # ]:          0 :       if (*argv != NULL)
     423                 :          0 :         putchar('\n');
     424                 :            :     }
     425                 :            :   }
     426                 :            : 
     427                 :          0 :   m_output(stdout, _("<standard output>"));
     428         [ #  # ]:          0 :   if (failures) {
     429                 :          0 :     fputs(_("Use dpkg --info (= dpkg-deb --info) to examine archive files.\n"),
     430                 :            :           stderr);
     431                 :          0 :     m_output(stderr, _("<standard error>"));
     432                 :            :   }
     433                 :            : 
     434                 :          0 :   modstatdb_shutdown();
     435                 :            : 
     436                 :          0 :   return failures;
     437                 :            : }
     438                 :            : 
     439                 :            : static int
     440                 :          0 : print_avail(const char *const *argv)
     441                 :            : {
     442                 :            :   const char *thisarg;
     443                 :            :   struct pkginfo *pkg;
     444                 :          0 :   int failures = 0;
     445                 :            : 
     446                 :          0 :   modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
     447                 :            : 
     448         [ #  # ]:          0 :   if (!*argv) {
     449                 :          0 :     writedb_records(stdout, _("<standard output>"), wdb_dump_available);
     450                 :            :   } else {
     451         [ #  # ]:          0 :     while ((thisarg = *argv++) != NULL) {
     452                 :          0 :       pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
     453                 :            : 
     454         [ #  # ]:          0 :       if (!pkg_is_informative(pkg, &pkg->available)) {
     455                 :          0 :         notice(_("package '%s' is not available"),
     456                 :            :                pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
     457                 :          0 :         failures++;
     458                 :            :       } else {
     459                 :          0 :         writerecord(stdout, _("<standard output>"), pkg, &pkg->available);
     460                 :            :       }
     461                 :            : 
     462         [ #  # ]:          0 :       if (*argv != NULL)
     463                 :          0 :         putchar('\n');
     464                 :            :     }
     465                 :            :   }
     466                 :            : 
     467                 :          0 :   m_output(stdout, _("<standard output>"));
     468         [ #  # ]:          0 :   if (failures)
     469                 :          0 :     m_output(stderr, _("<standard error>"));
     470                 :            : 
     471                 :          0 :   modstatdb_shutdown();
     472                 :            : 
     473                 :          0 :   return failures;
     474                 :            : }
     475                 :            : 
     476                 :            : static int
     477                 :          0 : list_files(const char *const *argv)
     478                 :            : {
     479                 :            :   const char *thisarg;
     480                 :            :   struct fsys_namenode_list *file;
     481                 :            :   struct pkginfo *pkg;
     482                 :            :   struct fsys_namenode *namenode;
     483                 :          0 :   int failures = 0;
     484                 :            : 
     485         [ #  # ]:          0 :   if (!*argv)
     486                 :          0 :     badusage(_("--%s needs at least one package name argument"), cipaction->olong);
     487                 :            : 
     488                 :          0 :   modstatdb_open(msdbrw_readonly);
     489                 :            : 
     490         [ #  # ]:          0 :   while ((thisarg = *argv++) != NULL) {
     491                 :          0 :     pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
     492                 :            : 
     493         [ #  # ]:          0 :     switch (pkg->status) {
     494                 :          0 :     case PKG_STAT_NOTINSTALLED:
     495                 :          0 :       notice(_("package '%s' is not installed"),
     496                 :            :              pkg_name(pkg, pnaw_nonambig));
     497                 :          0 :       failures++;
     498                 :          0 :       break;
     499                 :          0 :     default:
     500                 :          0 :       ensure_packagefiles_available(pkg);
     501                 :          0 :       ensure_diversions();
     502                 :          0 :       file = pkg->files;
     503         [ #  # ]:          0 :       if (!file) {
     504                 :          0 :         printf(_("Package '%s' does not contain any files (!)\n"),
     505                 :            :                pkg_name(pkg, pnaw_nonambig));
     506                 :            :       } else {
     507         [ #  # ]:          0 :         while (file) {
     508                 :          0 :           namenode = file->namenode;
     509                 :          0 :           puts(namenode->name);
     510   [ #  #  #  # ]:          0 :           if (namenode->divert && !namenode->divert->camefrom) {
     511         [ #  # ]:          0 :             if (!namenode->divert->pkgset)
     512                 :          0 :               printf(_("locally diverted to: %s\n"),
     513                 :          0 :                      namenode->divert->useinstead->name);
     514         [ #  # ]:          0 :             else if (pkg->set == namenode->divert->pkgset)
     515                 :          0 :               printf(_("package diverts others to: %s\n"),
     516                 :          0 :                      namenode->divert->useinstead->name);
     517                 :            :             else
     518                 :          0 :               printf(_("diverted by %s to: %s\n"),
     519                 :          0 :                      namenode->divert->pkgset->name,
     520                 :          0 :                      namenode->divert->useinstead->name);
     521                 :            :           }
     522                 :          0 :           file = file->next;
     523                 :            :         }
     524                 :            :       }
     525                 :          0 :       break;
     526                 :            :     }
     527                 :            : 
     528         [ #  # ]:          0 :     if (*argv != NULL)
     529                 :          0 :       putchar('\n');
     530                 :            :   }
     531                 :            : 
     532                 :          0 :   m_output(stdout, _("<standard output>"));
     533         [ #  # ]:          0 :   if (failures) {
     534                 :          0 :     fputs(_("Use dpkg --contents (= dpkg-deb --contents) to list archive files contents.\n"),
     535                 :            :              stderr);
     536                 :          0 :     m_output(stderr, _("<standard error>"));
     537                 :            :   }
     538                 :            : 
     539                 :          0 :   modstatdb_shutdown();
     540                 :            : 
     541                 :          0 :   return failures;
     542                 :            : }
     543                 :            : 
     544                 :            : static void
     545                 :          0 : pkg_array_load_db_fsys(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
     546                 :            : {
     547                 :          0 :   ensure_packagefiles_available(pkg);
     548                 :          0 : }
     549                 :            : 
     550                 :            : static void
     551                 :          0 : pkg_array_show_item(struct pkg_array *array, struct pkginfo *pkg, void *pkg_data)
     552                 :            : {
     553                 :          0 :   struct pkg_format_node *fmt = pkg_data;
     554                 :            : 
     555                 :          0 :   pkg_format_show(fmt, pkg, &pkg->installed);
     556                 :          0 : }
     557                 :            : 
     558                 :            : static int
     559                 :          0 : showpackages(const char *const *argv)
     560                 :            : {
     561                 :            :   struct dpkg_error err;
     562                 :            :   struct pkg_array array;
     563                 :            :   struct pkginfo *pkg;
     564                 :            :   struct pkg_format_node *fmt;
     565                 :            :   bool fmt_needs_db_fsys;
     566                 :            :   int i;
     567                 :          0 :   int rc = 0;
     568                 :            : 
     569                 :          0 :   fmt = pkg_format_parse(showformat, &err);
     570         [ #  # ]:          0 :   if (!fmt) {
     571                 :          0 :     notice(_("error in show format: %s"), err.str);
     572                 :          0 :     dpkg_error_destroy(&err);
     573                 :          0 :     rc++;
     574                 :          0 :     return rc;
     575                 :            :   }
     576                 :            : 
     577                 :          0 :   fmt_needs_db_fsys = pkg_format_needs_db_fsys(fmt);
     578                 :            : 
     579         [ #  # ]:          0 :   if (!opt_loadavail)
     580                 :          0 :     modstatdb_open(msdbrw_readonly);
     581                 :            :   else
     582                 :          0 :     modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
     583                 :            : 
     584                 :          0 :   pkg_array_init_from_hash(&array);
     585                 :          0 :   pkg_array_sort(&array, pkg_sorter_by_nonambig_name_arch);
     586                 :            : 
     587         [ #  # ]:          0 :   if (!*argv) {
     588         [ #  # ]:          0 :     if (fmt_needs_db_fsys)
     589                 :          0 :       ensure_allinstfiles_available_quiet();
     590         [ #  # ]:          0 :     for (i = 0; i < array.n_pkgs; i++) {
     591                 :          0 :       pkg = array.pkgs[i];
     592         [ #  # ]:          0 :       if (pkg->status == PKG_STAT_NOTINSTALLED)
     593                 :          0 :         continue;
     594                 :          0 :       pkg_format_show(fmt, pkg, &pkg->installed);
     595                 :            :     }
     596                 :            :   } else {
     597         [ #  # ]:          0 :     if (fmt_needs_db_fsys)
     598                 :          0 :       pkg_array_foreach(&array, pkg_array_load_db_fsys, NULL);
     599                 :          0 :     rc = pkg_array_match_patterns(&array, pkg_array_show_item, fmt, argv);
     600                 :            :   }
     601                 :            : 
     602                 :          0 :   m_output(stdout, _("<standard output>"));
     603                 :          0 :   m_output(stderr, _("<standard error>"));
     604                 :            : 
     605                 :          0 :   pkg_array_destroy(&array);
     606                 :          0 :   pkg_format_free(fmt);
     607                 :          0 :   modstatdb_shutdown();
     608                 :            : 
     609                 :          0 :   return rc;
     610                 :            : }
     611                 :            : 
     612                 :            : static bool
     613                 :          0 : pkg_infodb_is_internal(const char *filetype)
     614                 :            : {
     615                 :            :   /* Do not expose internal database files. */
     616         [ #  # ]:          0 :   if (strcmp(filetype, LISTFILE) == 0 ||
     617         [ #  # ]:          0 :       strcmp(filetype, CONFFILESFILE) == 0)
     618                 :          0 :     return true;
     619                 :            : 
     620         [ #  # ]:          0 :   if (strlen(filetype) > MAXCONTROLFILENAME)
     621                 :          0 :     return true;
     622                 :            : 
     623                 :          0 :   return false;
     624                 :            : }
     625                 :            : 
     626                 :            : static void
     627                 :          0 : pkg_infodb_check_filetype(const char *filetype)
     628                 :            : {
     629                 :            :   const char *c;
     630                 :            : 
     631                 :            :   /* Validate control file name for sanity. */
     632         [ #  # ]:          0 :   for (c = "/."; *c; c++)
     633         [ #  # ]:          0 :     if (strchr(filetype, *c))
     634                 :          0 :       badusage(_("control file contains %c"), *c);
     635                 :          0 : }
     636                 :            : 
     637                 :            : static void
     638                 :          0 : pkg_infodb_print_filename(const char *filename, const char *filetype)
     639                 :            : {
     640         [ #  # ]:          0 :   if (pkg_infodb_is_internal(filetype))
     641                 :          0 :     return;
     642                 :            : 
     643                 :          0 :   printf("%s\n", filename);
     644                 :            : }
     645                 :            : 
     646                 :            : static void
     647                 :          0 : pkg_infodb_print_filetype(const char *filename, const char *filetype)
     648                 :            : {
     649         [ #  # ]:          0 :   if (pkg_infodb_is_internal(filetype))
     650                 :          0 :     return;
     651                 :            : 
     652                 :          0 :   printf("%s\n", filetype);
     653                 :            : }
     654                 :            : 
     655                 :            : static void
     656                 :          0 : control_path_file(struct pkginfo *pkg, const char *control_file)
     657                 :            : {
     658                 :            :   const char *control_pathname;
     659                 :            :   struct stat st;
     660                 :            : 
     661                 :          0 :   control_pathname = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
     662         [ #  # ]:          0 :   if (stat(control_pathname, &st) < 0)
     663                 :          0 :     return;
     664         [ #  # ]:          0 :   if (!S_ISREG(st.st_mode))
     665                 :          0 :     return;
     666                 :            : 
     667                 :          0 :   pkg_infodb_print_filename(control_pathname, control_file);
     668                 :            : }
     669                 :            : 
     670                 :            : static int
     671                 :          0 : control_path(const char *const *argv)
     672                 :            : {
     673                 :            :   struct pkginfo *pkg;
     674                 :            :   const char *pkgname;
     675                 :            :   const char *control_file;
     676                 :            : 
     677                 :          0 :   pkgname = *argv++;
     678         [ #  # ]:          0 :   if (!pkgname)
     679                 :          0 :     badusage(_("--%s needs at least one package name argument"),
     680                 :          0 :              cipaction->olong);
     681                 :            : 
     682                 :          0 :   control_file = *argv++;
     683   [ #  #  #  # ]:          0 :   if (control_file && *argv)
     684                 :          0 :     badusage(_("--%s takes at most two arguments"), cipaction->olong);
     685                 :            : 
     686         [ #  # ]:          0 :   if (control_file)
     687                 :          0 :     pkg_infodb_check_filetype(control_file);
     688                 :            : 
     689                 :          0 :   modstatdb_open(msdbrw_readonly);
     690                 :            : 
     691                 :          0 :   pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
     692         [ #  # ]:          0 :   if (pkg->status == PKG_STAT_NOTINSTALLED)
     693                 :          0 :     ohshit(_("package '%s' is not installed"),
     694                 :            :            pkg_name(pkg, pnaw_nonambig));
     695                 :            : 
     696         [ #  # ]:          0 :   if (control_file)
     697                 :          0 :     control_path_file(pkg, control_file);
     698                 :            :   else
     699                 :          0 :     pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filename);
     700                 :            : 
     701                 :          0 :   modstatdb_shutdown();
     702                 :            : 
     703                 :          0 :   return 0;
     704                 :            : }
     705                 :            : 
     706                 :            : static int
     707                 :          0 : control_list(const char *const *argv)
     708                 :            : {
     709                 :            :   struct pkginfo *pkg;
     710                 :            :   const char *pkgname;
     711                 :            : 
     712                 :          0 :   pkgname = *argv++;
     713   [ #  #  #  # ]:          0 :   if (!pkgname || *argv)
     714                 :          0 :     badusage(_("--%s takes one package name argument"), cipaction->olong);
     715                 :            : 
     716                 :          0 :   modstatdb_open(msdbrw_readonly);
     717                 :            : 
     718                 :          0 :   pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
     719         [ #  # ]:          0 :   if (pkg->status == PKG_STAT_NOTINSTALLED)
     720                 :          0 :     ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
     721                 :            : 
     722                 :          0 :   pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_print_filetype);
     723                 :            : 
     724                 :          0 :   modstatdb_shutdown();
     725                 :            : 
     726                 :          0 :   return 0;
     727                 :            : }
     728                 :            : 
     729                 :            : static int
     730                 :          0 : control_show(const char *const *argv)
     731                 :            : {
     732                 :            :   struct pkginfo *pkg;
     733                 :            :   const char *pkgname;
     734                 :            :   const char *filename;
     735                 :            :   const char *control_file;
     736                 :            : 
     737                 :          0 :   pkgname = *argv++;
     738   [ #  #  #  # ]:          0 :   if (!pkgname || !*argv)
     739                 :          0 :     badusage(_("--%s takes exactly two arguments"),
     740                 :          0 :              cipaction->olong);
     741                 :            : 
     742                 :          0 :   control_file = *argv++;
     743   [ #  #  #  # ]:          0 :   if (!control_file || *argv)
     744                 :          0 :     badusage(_("--%s takes exactly two arguments"), cipaction->olong);
     745                 :            : 
     746                 :          0 :   pkg_infodb_check_filetype(control_file);
     747                 :            : 
     748                 :          0 :   modstatdb_open(msdbrw_readonly);
     749                 :            : 
     750                 :          0 :   pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
     751         [ #  # ]:          0 :   if (pkg->status == PKG_STAT_NOTINSTALLED)
     752                 :          0 :     ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
     753                 :            : 
     754         [ #  # ]:          0 :   if (pkg_infodb_has_file(pkg, &pkg->installed, control_file))
     755                 :          0 :     filename = pkg_infodb_get_file(pkg, &pkg->installed, control_file);
     756                 :            :   else
     757                 :          0 :     ohshit(_("control file '%s' does not exist"), control_file);
     758                 :            : 
     759                 :          0 :   modstatdb_shutdown();
     760                 :            : 
     761                 :          0 :   file_show(filename);
     762                 :            : 
     763                 :          0 :   return 0;
     764                 :            : }
     765                 :            : 
     766                 :            : static void
     767                 :          0 : set_no_pager(const struct cmdinfo *ci, const char *value)
     768                 :            : {
     769                 :          0 :   pager_enable(false);
     770                 :          0 : }
     771                 :            : 
     772                 :            : static int
     773                 :         19 : printversion(const char *const *argv)
     774                 :            : {
     775                 :         19 :   printf(_("Debian %s package management program query tool version %s.\n"),
     776                 :            :          DPKGQUERY, PACKAGE_RELEASE);
     777                 :         19 :   printf(_(
     778                 :            : "This is free software; see the GNU General Public License version 2 or\n"
     779                 :            : "later for copying conditions. There is NO warranty.\n"));
     780                 :            : 
     781                 :         19 :   m_output(stdout, _("<standard output>"));
     782                 :            : 
     783                 :         19 :   return 0;
     784                 :            : }
     785                 :            : 
     786                 :            : static int
     787                 :          0 : usage(const char *const *argv)
     788                 :            : {
     789                 :          0 :   printf(_(
     790                 :            : "Usage: %s [<option>...] <command>\n"
     791                 :            : "\n"), DPKGQUERY);
     792                 :            : 
     793                 :          0 :   printf(_(
     794                 :            : "Commands:\n"
     795                 :            : "  -s, --status [<package>...]      Display package status details.\n"
     796                 :            : "  -p, --print-avail [<package>...] Display available version details.\n"
     797                 :            : "  -L, --listfiles <package>...     List files 'owned' by package(s).\n"
     798                 :            : "  -l, --list [<pattern>...]        List packages concisely.\n"
     799                 :            : "  -W, --show [<pattern>...]        Show information on package(s).\n"
     800                 :            : "  -S, --search <pattern>...        Find package(s) owning file(s).\n"
     801                 :            : "      --control-list <package>     Print the package control file list.\n"
     802                 :            : "      --control-show <package> <file>\n"
     803                 :            : "                                   Show the package control file.\n"
     804                 :            : "  -c, --control-path <package> [<file>]\n"
     805                 :            : "                                   Print path for package control file.\n"
     806                 :            : "\n"));
     807                 :            : 
     808                 :          0 :   printf(_(
     809                 :            : "  -?, --help                       Show this help message.\n"
     810                 :            : "      --version                    Show the version.\n"
     811                 :            : "\n"));
     812                 :            : 
     813                 :          0 :   printf(_(
     814                 :            : "Options:\n"
     815                 :            : "  --admindir=<directory>           Use <directory> instead of %s.\n"
     816                 :            : "  --root=<directory>               Use <directory> instead of %s.\n"
     817                 :            : "  --load-avail                     Use available file on --show and --list.\n"
     818                 :            : "  --no-pager                       Disables the use of any pager.\n"
     819                 :            : "  -f|--showformat=<format>         Use alternative format for --show.\n"
     820                 :            : "\n"), ADMINDIR, "/");
     821                 :            : 
     822                 :          0 :   printf(_(
     823                 :            : "Format syntax:\n"
     824                 :            : "  A format is a string that will be output for each package. The format\n"
     825                 :            : "  can include the standard escape sequences \\n (newline), \\r (carriage\n"
     826                 :            : "  return) or \\\\ (plain backslash). Package information can be included\n"
     827                 :            : "  by inserting variable references to package fields using the ${var[;width]}\n"
     828                 :            : "  syntax. Fields will be right-aligned unless the width is negative in which\n"
     829                 :            : "  case left alignment will be used.\n"));
     830                 :            : 
     831                 :          0 :   m_output(stdout, _("<standard output>"));
     832                 :            : 
     833                 :          0 :   return 0;
     834                 :            : }
     835                 :            : 
     836                 :            : static const char printforhelp[] = N_(
     837                 :            : "Use --help for help about querying packages.");
     838                 :            : 
     839                 :            : /* This table has both the action entries in it and the normal options.
     840                 :            :  * The action entries are made with the ACTION macro, as they all
     841                 :            :  * have a very similar structure. */
     842                 :            : static const struct cmdinfo cmdinfos[]= {
     843                 :            :   ACTION( "listfiles",                      'L', act_listfiles,     list_files      ),
     844                 :            :   ACTION( "status",                         's', act_status,        print_status    ),
     845                 :            :   ACTION( "print-avail",                    'p', act_printavail,    print_avail     ),
     846                 :            :   ACTION( "list",                           'l', act_listpackages,  listpackages    ),
     847                 :            :   ACTION( "search",                         'S', act_searchfiles,   searchfiles     ),
     848                 :            :   ACTION( "show",                           'W', act_listpackages,  showpackages    ),
     849                 :            :   ACTION( "control-path",                   'c', act_controlpath,   control_path    ),
     850                 :            :   ACTION( "control-list",                    0,  act_controllist,   control_list    ),
     851                 :            :   ACTION( "control-show",                    0,  act_controlshow,   control_show    ),
     852                 :            :   ACTION( "help",                           '?', act_help,          usage           ),
     853                 :            :   ACTION( "version",                         0,  act_version,       printversion    ),
     854                 :            : 
     855                 :            :   { "admindir",   0,   1, NULL, NULL,        set_admindir, 0 },
     856                 :            :   { "root",       0,   1, NULL, NULL,        set_root, 0   },
     857                 :            :   { "load-avail", 0,   0, &opt_loadavail, NULL, NULL, 1    },
     858                 :            :   { "showformat", 'f', 1, NULL, &showformat, NULL          },
     859                 :            :   { "no-pager",   0,   0, NULL, NULL,        set_no_pager  },
     860                 :            :   {  NULL,        0,   0, NULL, NULL,        NULL          }
     861                 :            : };
     862                 :            : 
     863                 :         19 : int main(int argc, const char *const *argv) {
     864                 :            :   int ret;
     865                 :            : 
     866                 :         19 :   dpkg_set_report_piped_mode(_IOFBF);
     867                 :         19 :   dpkg_locales_init(PACKAGE);
     868                 :         19 :   dpkg_program_init("dpkg-query");
     869                 :         19 :   dpkg_options_parse(&argv, cmdinfos, printforhelp);
     870                 :            : 
     871                 :         19 :   debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());
     872                 :            : 
     873         [ -  + ]:         19 :   if (!cipaction) badusage(_("need an action option"));
     874                 :            : 
     875                 :         19 :   ret = cipaction->action(argv);
     876                 :            : 
     877                 :         19 :   dpkg_program_done();
     878                 :         19 :   dpkg_locales_done();
     879                 :            : 
     880                 :         19 :   return !!ret;
     881                 :            : }

Generated by: LCOV version 1.16