LCOV - code coverage report
Current view: top level - src/deb - main.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 50 86 58.1 %
Date: 2022-12-03 00:40:01 Functions: 6 11 54.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18 52 34.6 %

           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                 :            : #if 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 *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, 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 debugflag = 0;
     143                 :            : int nocheckflag = 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 = {
     167                 :            :   .type = DPKG_DEB_DEFAULT_COMPRESSOR,
     168                 :            :   .strategy = COMPRESSOR_STRATEGY_NONE,
     169                 :            :   .level = -1,
     170                 :            :   .threads_max = -1,
     171                 :            : };
     172                 :            : 
     173                 :            : static long
     174                 :          0 : parse_compress_level(const char *str)
     175                 :            : {
     176                 :            :   long value;
     177                 :            :   char *end;
     178                 :            : 
     179                 :          0 :   errno = 0;
     180                 :          0 :   value = strtol(str, &end, 10);
     181   [ #  #  #  #  :          0 :   if (str == end || *end != '\0' || errno != 0)
                   #  # ]
     182                 :          0 :     return 0;
     183                 :            : 
     184                 :          0 :   return value;
     185                 :            : }
     186                 :            : 
     187                 :            : static void
     188                 :          0 : set_compress_level(const struct cmdinfo *cip, const char *value)
     189                 :            : {
     190                 :            :   long level;
     191                 :            : 
     192                 :          0 :   level = dpkg_options_parse_arg_int(cip, value);
     193   [ #  #  #  # ]:          0 :   if (level < 0 || level > 9)
     194                 :          0 :     badusage(_("invalid compression level for -%c: %ld"), cip->oshort, level);
     195                 :            : 
     196                 :          0 :   compress_params.level = level;
     197                 :          0 : }
     198                 :            : 
     199                 :            : static void
     200                 :          0 : set_compress_strategy(const struct cmdinfo *cip, const char *value)
     201                 :            : {
     202                 :          0 :   compress_params.strategy = compressor_get_strategy(value);
     203         [ #  # ]:          0 :   if (compress_params.strategy == COMPRESSOR_STRATEGY_UNKNOWN)
     204                 :          0 :     badusage(_("unknown compression strategy '%s'!"), value);
     205                 :          0 : }
     206                 :            : 
     207                 :            : static enum compressor_type
     208                 :          3 : parse_compress_type(const char *value)
     209                 :            : {
     210                 :            :   enum compressor_type type;
     211                 :            : 
     212                 :          3 :   type = compressor_find_by_name(value);
     213         [ -  + ]:          3 :   if (type == COMPRESSOR_TYPE_UNKNOWN)
     214                 :          0 :     badusage(_("unknown compression type '%s'!"), value);
     215         [ -  + ]:          3 :   if (type == COMPRESSOR_TYPE_LZMA)
     216                 :          0 :     badusage(_("obsolete compression type '%s'; use xz instead"), value);
     217         [ -  + ]:          3 :   if (type == COMPRESSOR_TYPE_BZIP2)
     218                 :          0 :     badusage(_("obsolete compression type '%s'; use xz or gzip instead"), value);
     219                 :            : 
     220                 :          3 :   return type;
     221                 :            : }
     222                 :            : 
     223                 :            : static void
     224                 :          3 : set_compress_type(const struct cmdinfo *cip, const char *value)
     225                 :            : {
     226                 :          3 :   compress_params.type = parse_compress_type(value);
     227                 :          3 : }
     228                 :            : 
     229                 :            : static long
     230                 :          0 : parse_threads_max(const char *str)
     231                 :            : {
     232                 :            :   long value;
     233                 :            :   char *end;
     234                 :            : 
     235                 :          0 :   errno = 0;
     236                 :          0 :   value = strtol(str, &end, 10);
     237   [ #  #  #  #  :          0 :   if (str == end || *end != '\0' || errno != 0)
                   #  # ]
     238                 :          0 :     return 0;
     239                 :            : 
     240                 :          0 :   return value;
     241                 :            : }
     242                 :            : 
     243                 :            : static void
     244                 :          0 : set_threads_max(const struct cmdinfo *cip, const char *value)
     245                 :            : {
     246                 :          0 :   compress_params.threads_max = dpkg_options_parse_arg_int(cip, value);
     247                 :          0 : }
     248                 :            : 
     249                 :            : static const struct cmdinfo cmdinfos[]= {
     250                 :            :   ACTION("build",         'b', 0, do_build),
     251                 :            :   ACTION("contents",      'c', 0, do_contents),
     252                 :            :   ACTION("control",       'e', 0, do_control),
     253                 :            :   ACTION("info",          'I', 0, do_info),
     254                 :            :   ACTION("field",         'f', 0, do_field),
     255                 :            :   ACTION("extract",       'x', 0, do_extract),
     256                 :            :   ACTION("vextract",      'X', 0, do_vextract),
     257                 :            :   ACTION("raw-extract",   'R', 0, do_raw_extract),
     258                 :            :   ACTION("ctrl-tarfile",  0,   0, do_ctrltarfile),
     259                 :            :   ACTION("fsys-tarfile",  0,   0, do_fsystarfile),
     260                 :            :   ACTION("show",          'W', 0, do_showinfo),
     261                 :            :   ACTION("help",          '?', 0, usage),
     262                 :            :   ACTION("version",       0,   0, printversion),
     263                 :            : 
     264                 :            :   { "deb-format",    0,   1, NULL,           NULL,         set_deb_format   },
     265                 :            :   { "debug",         'D', 0, &debugflag,     NULL,         NULL,          1 },
     266                 :            :   { "verbose",       'v', 0, &opt_verbose,   NULL,         NULL,          1 },
     267                 :            :   { "nocheck",       0,   0, &nocheckflag,   NULL,         NULL,          1 },
     268                 :            :   { "root-owner-group",    0, 0, &opt_root_owner_group,    NULL, NULL,    1 },
     269                 :            :   { "threads-max",   0,   1, NULL,           NULL,         set_threads_max  },
     270                 :            :   { "uniform-compression", 0, 0, &opt_uniform_compression, NULL, NULL,    1 },
     271                 :            :   { "no-uniform-compression", 0, 0, &opt_uniform_compression, NULL, NULL, 0 },
     272                 :            :   { NULL,            'z', 1, NULL,           NULL,         set_compress_level },
     273                 :            :   { NULL,            'Z', 1, NULL,           NULL,         set_compress_type  },
     274                 :            :   { NULL,            'S', 1, NULL,           NULL,         set_compress_strategy },
     275                 :            :   { "showformat",    0,   1, NULL,           &showformat,  NULL             },
     276                 :            :   {  NULL,           0,   0, NULL,           NULL,         NULL             }
     277                 :            : };
     278                 :            : 
     279                 :         59 : int main(int argc, const char *const *argv) {
     280                 :            :   struct dpkg_error err;
     281                 :            :   char *env;
     282                 :            :   int ret;
     283                 :            : 
     284                 :         59 :   dpkg_locales_init(PACKAGE);
     285                 :         59 :   dpkg_program_init(BACKEND);
     286                 :            :   /* XXX: Integrate this into options initialization/parsing. */
     287                 :         59 :   env = getenv("DPKG_DEB_THREADS_MAX");
     288         [ -  + ]:         59 :   if (str_is_set(env))
     289                 :          0 :     compress_params.threads_max = parse_threads_max(env);
     290                 :         59 :   env = getenv("DPKG_DEB_COMPRESSOR_TYPE");
     291         [ -  + ]:         59 :   if (str_is_set(env))
     292                 :          0 :     compress_params.type = parse_compress_type(env);
     293                 :         59 :   env = getenv("DPKG_DEB_COMPRESSOR_LEVEL");
     294         [ -  + ]:         59 :   if (str_is_set(env))
     295                 :          0 :     compress_params.level = parse_compress_level(env);
     296                 :         59 :   dpkg_options_parse(&argv, cmdinfos, printforhelp);
     297                 :            : 
     298         [ -  + ]:         59 :   if (!cipaction) badusage(_("need an action option"));
     299                 :            : 
     300         [ -  + ]:         59 :   if (!compressor_check_params(&compress_params, &err))
     301                 :          0 :     badusage(_("invalid compressor parameters: %s"), err.str);
     302                 :            : 
     303         [ +  - ]:         59 :   if (opt_uniform_compression &&
     304         [ +  + ]:         59 :       (compress_params.type != COMPRESSOR_TYPE_NONE &&
     305         [ +  + ]:         57 :        compress_params.type != COMPRESSOR_TYPE_GZIP &&
     306         [ -  + ]:         56 :        compress_params.type != COMPRESSOR_TYPE_XZ))
     307                 :          0 :     badusage(_("unsupported compression type '%s' with uniform compression"),
     308                 :            :              compressor_get_name(compress_params.type));
     309                 :            : 
     310                 :         59 :   ret = cipaction->action(argv);
     311                 :            : 
     312                 :         38 :   dpkg_program_done();
     313                 :         38 :   dpkg_locales_done();
     314                 :            : 
     315                 :         38 :   return ret;
     316                 :            : }

Generated by: LCOV version 1.16