LCOV - code coverage report
Current view: top level - src/deb - main.c (source / functions) Coverage Total Hit
Test: dpkg 1.22.7-3-g89f48 C code coverage Lines: 61.4 % 88 54
Test Date: 2024-07-17 02:53:43 Functions: 54.5 % 11 6
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 41.1 % 56 23

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * dpkg-deb - construction and deconstruction of *.deb archives
       3                 :             :  * main.c - main program
       4                 :             :  *
       5                 :             :  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
       6                 :             :  * Copyright © 2006-2014 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/wait.h>
      27                 :             : 
      28                 :             : #include <limits.h>
      29                 :             : #ifdef HAVE_LOCALE_H
      30                 :             : #include <locale.h>
      31                 :             : #endif
      32                 :             : #include <errno.h>
      33                 :             : #include <string.h>
      34                 :             : #include <dirent.h>
      35                 :             : #include <unistd.h>
      36                 :             : #include <stdlib.h>
      37                 :             : #include <stdio.h>
      38                 :             : 
      39                 :             : #include <dpkg/macros.h>
      40                 :             : #include <dpkg/i18n.h>
      41                 :             : #include <dpkg/dpkg.h>
      42                 :             : #include <dpkg/dpkg-db.h>
      43                 :             : #include <dpkg/compress.h>
      44                 :             : #include <dpkg/options.h>
      45                 :             : 
      46                 :             : #include "dpkg-deb.h"
      47                 :             : 
      48                 :             : const char *opt_showformat = "${Package}\t${Version}\n";
      49                 :             : 
      50                 :             : static int
      51                 :           2 : printversion(const char *const *argv)
      52                 :             : {
      53                 :           2 :   printf(_("Debian '%s' package archive backend version %s.\n"),
      54                 :             :          BACKEND, PACKAGE_RELEASE);
      55                 :           2 :   printf(_(
      56                 :             : "This is free software; see the GNU General Public License version 2 or\n"
      57                 :             : "later for copying conditions. There is NO warranty.\n"));
      58                 :             : 
      59                 :           2 :   m_output(stdout, _("<standard output>"));
      60                 :             : 
      61                 :           2 :   return 0;
      62                 :             : }
      63                 :             : 
      64                 :             : static int
      65                 :           1 : usage(const char *const *argv)
      66                 :             : {
      67                 :           1 :   printf(_(
      68                 :             : "Usage: %s [<option>...] <command>\n"
      69                 :             : "\n"), BACKEND);
      70                 :             : 
      71                 :           1 :   printf(_(
      72                 :             : "Commands:\n"
      73                 :             : "  -b|--build <directory> [<deb>]   Build an archive.\n"
      74                 :             : "  -c|--contents <deb>              List contents.\n"
      75                 :             : "  -I|--info <deb> [<cfile>...]     Show info to stdout.\n"
      76                 :             : "  -W|--show <deb>                  Show information on package(s)\n"
      77                 :             : "  -f|--field <deb> [<cfield>...]   Show field(s) to stdout.\n"
      78                 :             : "  -e|--control <deb> [<directory>] Extract control info.\n"
      79                 :             : "  -x|--extract <deb> <directory>   Extract files.\n"
      80                 :             : "  -X|--vextract <deb> <directory>  Extract & list files.\n"
      81                 :             : "  -R|--raw-extract <deb> <directory>\n"
      82                 :             : "                                   Extract control info and files.\n"
      83                 :             : "  --ctrl-tarfile <deb>             Output control tarfile.\n"
      84                 :             : "  --fsys-tarfile <deb>             Output filesystem tarfile.\n"
      85                 :             : "\n"));
      86                 :             : 
      87                 :           1 :   printf(_(
      88                 :             : "  -?, --help                       Show this help message.\n"
      89                 :             : "      --version                    Show the version.\n"
      90                 :             : "\n"));
      91                 :             : 
      92                 :           1 :   printf(_(
      93                 :             : "<deb> is the filename of a Debian format archive.\n"
      94                 :             : "<cfile> is the name of an administrative file component.\n"
      95                 :             : "<cfield> is the name of a field in the main 'control' file.\n"
      96                 :             : "\n"));
      97                 :             : 
      98                 :           1 :   printf(_(
      99                 :             : "Options:\n"
     100                 :             : "  -v, --verbose                    Enable verbose output.\n"
     101                 :             : "  -D, --debug                      Enable debugging output.\n"
     102                 :             : "      --showformat=<format>        Use alternative format for --show.\n"
     103                 :             : "      --deb-format=<format>        Select archive format.\n"
     104                 :             : "                                     Allowed values: 0.939000, 2.0 (default).\n"
     105                 :             : "      --nocheck                    Suppress control file check (build bad\n"
     106                 :             : "                                     packages).\n"
     107                 :             : "      --root-owner-group           Forces the owner and groups to root.\n"
     108                 :             : "      --threads-max=<threads>      Use at most <threads> with compressor.\n"
     109                 :             : "      --[no-]uniform-compression   Use the compression params on all members.\n"
     110                 :             : "  -z#                              Set the compression level when building.\n"
     111                 :             : "  -Z<type>                         Set the compression type used when building.\n"
     112                 :             : "                                     Allowed types: gzip, xz, zstd, none.\n"
     113                 :             : "  -S<strategy>                     Set the compression strategy when building.\n"
     114                 :             : "                                     Allowed values: none; extreme (xz);\n"
     115                 :             : "                                     filtered, huffman, rle, fixed (gzip).\n"
     116                 :             : "\n"));
     117                 :             : 
     118                 :           1 :   printf(_(
     119                 :             : "Format syntax:\n"
     120                 :             : "  A format is a string that will be output for each package. The format\n"
     121                 :             : "  can include the standard escape sequences \\n (newline), \\r (carriage\n"
     122                 :             : "  return) or \\\\ (plain backslash). Package information can be included\n"
     123                 :             : "  by inserting variable references to package fields using the ${var[;width]}\n"
     124                 :             : "  syntax. Fields will be right-aligned unless the width is negative in which\n"
     125                 :             : "  case left alignment will be used.\n"));
     126                 :             : 
     127                 :           1 :   printf(_(
     128                 :             : "\n"
     129                 :             : "Use 'dpkg' to install and remove packages from your system, or\n"
     130                 :             : "'apt' or 'aptitude' for user-friendly package management. Packages\n"
     131                 :             : "unpacked using 'dpkg-deb --extract' will be incorrectly installed !\n"));
     132                 :             : 
     133                 :           1 :   m_output(stdout, _("<standard output>"));
     134                 :             : 
     135                 :           1 :   return 0;
     136                 :             : }
     137                 :             : 
     138                 :             : static const char printforhelp[] =
     139                 :             :   N_("Type dpkg-deb --help for help about manipulating *.deb files;\n"
     140                 :             :      "Type dpkg --help for help about installing and deinstalling packages.");
     141                 :             : 
     142                 :             : int opt_debug = 0;
     143                 :             : int opt_nocheck = 0;
     144                 :             : int opt_verbose = 0;
     145                 :             : int opt_root_owner_group = 0;
     146                 :             : int opt_uniform_compression = 1;
     147                 :             : 
     148                 :             : struct deb_version deb_format = DEB_VERSION(2, 0);
     149                 :             : 
     150                 :             : static void
     151                 :           1 : set_deb_format(const struct cmdinfo *cip, const char *value)
     152                 :             : {
     153                 :             :   const char *err;
     154                 :             : 
     155                 :           1 :   err = deb_version_parse(&deb_format, value);
     156         [ -  + ]:           1 :   if (err)
     157                 :           0 :     badusage(_("invalid deb format version: %s"), err);
     158                 :             : 
     159   [ -  +  -  - ]:           1 :   if ((deb_format.major == 2 && deb_format.minor == 0) ||
     160   [ +  -  +  - ]:           1 :       (deb_format.major == 0 && deb_format.minor == 939000))
     161                 :           1 :     return;
     162                 :             :   else
     163                 :           0 :     badusage(_("unknown deb format version: %s"), value);
     164                 :             : }
     165                 :             : 
     166                 :             : struct compress_params compress_params_deb0 = {
     167                 :             :   .type = COMPRESSOR_TYPE_GZIP,
     168                 :             :   .strategy = COMPRESSOR_STRATEGY_NONE,
     169                 :             :   .level = -1,
     170                 :             :   .threads_max = -1,
     171                 :             : };
     172                 :             : 
     173                 :             : struct compress_params compress_params = {
     174                 :             :   .type = DEB_DEFAULT_COMPRESSOR,
     175                 :             :   .strategy = COMPRESSOR_STRATEGY_NONE,
     176                 :             :   .level = -1,
     177                 :             :   .threads_max = -1,
     178                 :             : };
     179                 :             : 
     180                 :             : static long
     181                 :           0 : parse_compress_level(const char *str)
     182                 :             : {
     183                 :             :   long value;
     184                 :             :   char *end;
     185                 :             : 
     186                 :           0 :   errno = 0;
     187                 :           0 :   value = strtol(str, &end, 10);
     188   [ #  #  #  #  :           0 :   if (str == end || *end != '\0' || errno != 0)
                   #  # ]
     189                 :           0 :     return 0;
     190                 :             : 
     191                 :           0 :   return value;
     192                 :             : }
     193                 :             : 
     194                 :             : static void
     195                 :           0 : set_compress_level(const struct cmdinfo *cip, const char *value)
     196                 :             : {
     197                 :           0 :   compress_params.level = dpkg_options_parse_arg_int(cip, value);
     198                 :           0 : }
     199                 :             : 
     200                 :             : static void
     201                 :           0 : set_compress_strategy(const struct cmdinfo *cip, const char *value)
     202                 :             : {
     203                 :           0 :   compress_params.strategy = compressor_get_strategy(value);
     204         [ #  # ]:           0 :   if (compress_params.strategy == COMPRESSOR_STRATEGY_UNKNOWN)
     205                 :           0 :     badusage(_("unknown compression strategy '%s'!"), value);
     206                 :           0 : }
     207                 :             : 
     208                 :             : static enum compressor_type
     209                 :          10 : parse_compress_type(const char *value)
     210                 :             : {
     211                 :             :   enum compressor_type type;
     212                 :             : 
     213                 :          10 :   type = compressor_find_by_name(value);
     214         [ -  + ]:          10 :   if (type == COMPRESSOR_TYPE_UNKNOWN)
     215                 :           0 :     badusage(_("unknown compression type '%s'!"), value);
     216         [ -  + ]:          10 :   if (type == COMPRESSOR_TYPE_LZMA)
     217                 :           0 :     badusage(_("obsolete compression type '%s'; use xz instead"), value);
     218         [ -  + ]:          10 :   if (type == COMPRESSOR_TYPE_BZIP2)
     219                 :           0 :     badusage(_("obsolete compression type '%s'; use xz or gzip instead"), value);
     220                 :             : 
     221                 :          10 :   return type;
     222                 :             : }
     223                 :             : 
     224                 :             : static void
     225                 :          10 : set_compress_type(const struct cmdinfo *cip, const char *value)
     226                 :             : {
     227                 :          10 :   compress_params.type = parse_compress_type(value);
     228                 :          10 : }
     229                 :             : 
     230                 :             : static long
     231                 :           0 : parse_threads_max(const char *str)
     232                 :             : {
     233                 :             :   long value;
     234                 :             :   char *end;
     235                 :             : 
     236                 :           0 :   errno = 0;
     237                 :           0 :   value = strtol(str, &end, 10);
     238   [ #  #  #  #  :           0 :   if (str == end || *end != '\0' || errno != 0)
                   #  # ]
     239                 :           0 :     return 0;
     240                 :             : 
     241                 :           0 :   return value;
     242                 :             : }
     243                 :             : 
     244                 :             : static void
     245                 :           0 : set_threads_max(const struct cmdinfo *cip, const char *value)
     246                 :             : {
     247                 :           0 :   compress_params.threads_max = dpkg_options_parse_arg_int(cip, value);
     248                 :           0 : }
     249                 :             : 
     250                 :             : static const struct cmdinfo cmdinfos[]= {
     251                 :             :   ACTION("build",         'b', 0, do_build),
     252                 :             :   ACTION("contents",      'c', 0, do_contents),
     253                 :             :   ACTION("control",       'e', 0, do_control),
     254                 :             :   ACTION("info",          'I', 0, do_info),
     255                 :             :   ACTION("field",         'f', 0, do_field),
     256                 :             :   ACTION("extract",       'x', 0, do_extract),
     257                 :             :   ACTION("vextract",      'X', 0, do_vextract),
     258                 :             :   ACTION("raw-extract",   'R', 0, do_raw_extract),
     259                 :             :   ACTION("ctrl-tarfile",  0,   0, do_ctrltarfile),
     260                 :             :   ACTION("fsys-tarfile",  0,   0, do_fsystarfile),
     261                 :             :   ACTION("show",          'W', 0, do_showinfo),
     262                 :             :   ACTION("help",          '?', 0, usage),
     263                 :             :   ACTION("version",       0,   0, printversion),
     264                 :             : 
     265                 :             :   { "deb-format",    0,   1, NULL,           NULL,         set_deb_format   },
     266                 :             :   { "debug",         'D', 0, &opt_debug,     NULL,         NULL,          1 },
     267                 :             :   { "verbose",       'v', 0, &opt_verbose,   NULL,         NULL,          1 },
     268                 :             :   { "nocheck",       0,   0, &opt_nocheck,   NULL,         NULL,          1 },
     269                 :             :   { "root-owner-group",    0, 0, &opt_root_owner_group,    NULL, NULL,    1 },
     270                 :             :   { "threads-max",   0,   1, NULL,           NULL,         set_threads_max  },
     271                 :             :   { "uniform-compression", 0, 0, &opt_uniform_compression, NULL, NULL,    1 },
     272                 :             :   { "no-uniform-compression", 0, 0, &opt_uniform_compression, NULL, NULL, 0 },
     273                 :             :   { NULL,            'z', 1, NULL,           NULL,         set_compress_level },
     274                 :             :   { NULL,            'Z', 1, NULL,           NULL,         set_compress_type  },
     275                 :             :   { NULL,            'S', 1, NULL,           NULL,         set_compress_strategy },
     276                 :             :   { "showformat",    0,   1, NULL,           &opt_showformat,  NULL         },
     277                 :             :   {  NULL,           0,   0, NULL,           NULL,         NULL             }
     278                 :             : };
     279                 :             : 
     280                 :          78 : int main(int argc, const char *const *argv) {
     281                 :             :   struct dpkg_error err;
     282                 :             :   char *env;
     283                 :             :   int ret;
     284                 :             : 
     285                 :          78 :   dpkg_locales_init(PACKAGE);
     286                 :          78 :   dpkg_program_init(BACKEND);
     287                 :             :   /* XXX: Integrate this into options initialization/parsing. */
     288                 :          78 :   env = getenv("DPKG_DEB_THREADS_MAX");
     289         [ -  + ]:          78 :   if (str_is_set(env))
     290                 :           0 :     compress_params.threads_max = parse_threads_max(env);
     291                 :          78 :   env = getenv("DPKG_DEB_COMPRESSOR_TYPE");
     292         [ -  + ]:          78 :   if (str_is_set(env))
     293                 :           0 :     compress_params.type = parse_compress_type(env);
     294                 :          78 :   env = getenv("DPKG_DEB_COMPRESSOR_LEVEL");
     295         [ -  + ]:          78 :   if (str_is_set(env))
     296                 :           0 :     compress_params.level = parse_compress_level(env);
     297                 :          78 :   dpkg_options_parse(&argv, cmdinfos, printforhelp);
     298                 :             : 
     299         [ -  + ]:          78 :   if (!cipaction) badusage(_("need an action option"));
     300                 :             : 
     301   [ -  +  -  - ]:          78 :   if (!opt_uniform_compression && deb_format.major == 0)
     302                 :           0 :     badusage(_("unsupported deb format '%d.%d' with non-uniform compression"),
     303                 :             :              deb_format.major, deb_format.minor);
     304                 :             : 
     305         [ +  + ]:          78 :   if (deb_format.major == 0)
     306                 :           1 :     compress_params = compress_params_deb0;
     307                 :             : 
     308         [ -  + ]:          78 :   if (!compressor_check_params(&compress_params, &err))
     309                 :           0 :     badusage(_("invalid compressor parameters: %s"), err.str);
     310                 :             : 
     311         [ +  - ]:          78 :   if (opt_uniform_compression &&
     312         [ +  + ]:          78 :       (compress_params.type != COMPRESSOR_TYPE_NONE &&
     313         [ +  + ]:          71 :        compress_params.type != COMPRESSOR_TYPE_GZIP &&
     314         [ +  + ]:          69 :        compress_params.type != COMPRESSOR_TYPE_ZSTD &&
     315         [ -  + ]:          68 :        compress_params.type != COMPRESSOR_TYPE_XZ))
     316                 :           0 :     badusage(_("unsupported compression type '%s' with uniform compression"),
     317                 :             :              compressor_get_name(compress_params.type));
     318                 :             : 
     319                 :          78 :   ret = cipaction->action(argv);
     320                 :             : 
     321                 :          57 :   dpkg_program_done();
     322                 :          57 :   dpkg_locales_done();
     323                 :             : 
     324                 :          57 :   return ret;
     325                 :             : }
        

Generated by: LCOV version 2.0-1