This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch main
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=7b55e3c006d8d91c658f95e1748d8b50bc7f0210

commit 7b55e3c006d8d91c658f95e1748d8b50bc7f0210
Author: Guillem Jover <[email protected]>
AuthorDate: Fri Feb 11 22:16:12 2022 +0100

    libdpkg: Refactor command actions for compression
    
    Setup the «struct command» from the fd_fd_filter() caller, so that we
    can use its API to add variable amount of arguments. This cleans up
    conditional option handling, and will make it easier to extend them
    further.
---
 lib/dpkg/compress.c | 114 +++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 81 insertions(+), 33 deletions(-)

diff --git a/lib/dpkg/compress.c b/lib/dpkg/compress.c
index 93e8e012b..264eeb018 100644
--- a/lib/dpkg/compress.c
+++ b/lib/dpkg/compress.c
@@ -56,12 +56,9 @@
     !defined(WITH_LIBBZ2)
 #include <dpkg/subproc.h>
 
-static void DPKG_ATTR_SENTINEL
-fd_fd_filter(int fd_in, int fd_out, const char *desc, const char *delenv[],
-             const char *file, ...)
+static void
+fd_fd_filter(struct command *cmd, int fd_in, int fd_out, const char *delenv[])
 {
-       va_list args;
-       struct command cmd;
        pid_t pid;
        int i;
 
@@ -79,15 +76,30 @@ fd_fd_filter(int fd_in, int fd_out, const char *desc, const 
char *delenv[],
                for (i = 0; delenv[i]; i++)
                        unsetenv(delenv[i]);
 
-               command_init(&cmd, file, desc);
-               command_add_arg(&cmd, file);
-               va_start(args, file);
-               command_add_argv(&cmd, args);
-               va_end(args);
-
-               command_exec(&cmd);
+               command_exec(cmd);
        }
-       subproc_reap(pid, desc, 0);
+       subproc_reap(pid, cmd->name, 0);
+}
+
+static void
+command_compress_init(struct command *cmd, const char *name, const char *desc,
+                      int level)
+{
+       static char combuf[6];
+
+       command_init(cmd, name, desc);
+       command_add_arg(cmd, name);
+
+       snprintf(combuf, sizeof(combuf), "-c%d", level);
+       command_add_arg(cmd, combuf);
+}
+
+static void
+command_decompress_init(struct command *cmd, const char *name, const char 
*desc)
+{
+       command_init(cmd, name, desc);
+       command_add_arg(cmd, name);
+       command_add_arg(cmd, "-dc");
 }
 #endif
 
@@ -275,17 +287,27 @@ static void
 decompress_gzip(struct compress_params *params, int fd_in, int fd_out,
                 const char *desc)
 {
-       fd_fd_filter(fd_in, fd_out, desc, env_gzip, GZIP, "-dc", NULL);
+       struct command cmd;
+
+       command_decompress_init(&cmd, GZIP, desc);
+
+       fd_fd_filter(&cmd, fd_in, fd_out, env_gzip);
+
+       command_destroy(&cmd);
 }
 
 static void
 compress_gzip(struct compress_params *params, int fd_in, int fd_out,
               const char *desc)
 {
-       char combuf[6];
+       struct command cmd;
+
+       command_compress_init(&cmd, GZIP, desc, params->level);
+       command_add_arg(&cmd, "-n");
 
-       snprintf(combuf, sizeof(combuf), "-c%d", params->level);
-       fd_fd_filter(fd_in, fd_out, desc, env_gzip, GZIP, "-n", combuf, NULL);
+       fd_fd_filter(&cmd, fd_in, fd_out, env_gzip);
+
+       command_destroy(&cmd);
 }
 #endif
 
@@ -417,17 +439,26 @@ static void
 decompress_bzip2(struct compress_params *params, int fd_in, int fd_out,
                  const char *desc)
 {
-       fd_fd_filter(fd_in, fd_out, desc, env_bzip2, BZIP2, "-dc", NULL);
+       struct command cmd;
+
+       command_decompress_init(&cmd, BZIP2, desc);
+
+       fd_fd_filter(&cmd, fd_in, fd_out, env_bzip2);
+
+       command_destroy(&cmd);
 }
 
 static void
 compress_bzip2(struct compress_params *params, int fd_in, int fd_out,
                const char *desc)
 {
-       char combuf[6];
+       struct command cmd;
+
+       command_compress_init(&cmd, BZIP2, desc, params->level);
+
+       fd_fd_filter(&cmd, fd_in, fd_out, env_bzip2);
 
-       snprintf(combuf, sizeof(combuf), "-c%d", params->level);
-       fd_fd_filter(fd_in, fd_out, desc, env_bzip2, BZIP2, combuf, NULL);
+       command_destroy(&cmd);
 }
 #endif
 
@@ -793,23 +824,29 @@ static void
 decompress_xz(struct compress_params *params, int fd_in, int fd_out,
               const char *desc)
 {
-       fd_fd_filter(fd_in, fd_out, desc, env_xz, XZ, "-dc", NULL);
+       struct command cmd;
+
+       command_decompress_init(&cmd, XZ, desc);
+
+       fd_fd_filter(&cmd, fd_in, fd_out, env_xz);
+
+       command_destroy(&cmd);
 }
 
 static void
 compress_xz(struct compress_params *params, int fd_in, int fd_out,
             const char *desc)
 {
-       char combuf[6];
-       const char *strategy;
+       struct command cmd;
+
+       command_compress_init(&cmd, XZ, desc, params->level);
 
        if (params->strategy == COMPRESSOR_STRATEGY_EXTREME)
-               strategy = "-e";
-       else
-               strategy = NULL;
+               command_add_arg(&cmd, "-e");
+
+       fd_fd_filter(&cmd, fd_in, fd_out, env_xz);
 
-       snprintf(combuf, sizeof(combuf), "-c%d", params->level);
-       fd_fd_filter(fd_in, fd_out, desc, env_xz, XZ, combuf, strategy, NULL);
+       command_destroy(&cmd);
 }
 #endif
 
@@ -894,17 +931,28 @@ static void
 decompress_lzma(struct compress_params *params, int fd_in, int fd_out,
                 const char *desc)
 {
-       fd_fd_filter(fd_in, fd_out, desc, env_xz, XZ, "-dc", "--format=lzma", 
NULL);
+       struct command cmd;
+
+       command_decompress_init(&cmd, XZ, desc);
+       command_add_arg(&cmd, "--format=lzma");
+
+       fd_fd_filter(&cmd, fd_in, fd_out, env_xz);
+
+       command_destroy(&cmd);
 }
 
 static void
 compress_lzma(struct compress_params *params, int fd_in, int fd_out,
               const char *desc)
 {
-       char combuf[6];
+       struct command cmd;
+
+       command_compress_init(&cmd, XZ, desc, params->level);
+       command_add_arg(&cmd, "--format=lzma");
+
+       fd_fd_filter(&cmd, fd_in, fd_out, env_xz);
 
-       snprintf(combuf, sizeof(combuf), "-c%d", params->level);
-       fd_fd_filter(fd_in, fd_out, desc, env_xz, XZ, combuf, "--format=lzma", 
NULL);
+       command_destroy(&cmd);
 }
 #endif
 

-- 
Dpkg.Org's dpkg

Reply via email to